mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-22 17:22:19 +01:00
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
Fill all with catering always fetched and filled today, so picking 24.10 on 23.10 reported "No catering delivery today." FillAllFlow.tap now takes the picked day: fetch, slot schedule, occupancy and dedup all resolve against it, and logDishes stamps each dish at its slot time on that day. - A plan only confirms onto the day it was proposed for; changing the date (or back to today) disarms it. - A later-day fill leaves today's delivery queue and prefill alone and returns the form to today, as a manual "Log for later" does. - Only today's fetch records the single-day "already fetched" marker; recording a later date would skip that day's automatic fetch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VwYgigocPkPWa73QZRtU38
251 lines
7.3 KiB
Dart
251 lines
7.3 KiB
Dart
/// The two-tap state machine behind "Fill all with catering".
|
|
///
|
|
/// The guarantee under test is that *no write happens on the first tap* and
|
|
/// that the confirm re-reads the world: occupancy at write time, and the
|
|
/// calendar day. Each test asserts the log's contents, not a return value.
|
|
library;
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:diet_guard_app/services/kuchnia_fill.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_fill_flow.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_import.dart';
|
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'kuchnia_fill_support.dart';
|
|
|
|
/// Counts and records fetches; the result is swappable per test.
|
|
class _Fetcher {
|
|
_Fetcher(this.result);
|
|
|
|
KuchniaRefresh result;
|
|
final List<DateTime> days = [];
|
|
Completer<void>? gate;
|
|
|
|
Future<KuchniaRefresh> call(DateTime day) async {
|
|
days.add(day);
|
|
if (gate != null) await gate!.future;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
late LogStorageService storage;
|
|
late _Fetcher fetcher;
|
|
late DateTime clock;
|
|
late Set<int> occupied;
|
|
late int occupancyReads;
|
|
Completer<void>? occupancyGate;
|
|
late FillAllFlow flow;
|
|
|
|
setUp(() {
|
|
storage = freshStorage();
|
|
fetcher = _Fetcher(KuchniaRefresh(dishes: dishes(4)));
|
|
clock = DateTime(2026, 8, 23, 10);
|
|
occupied = {};
|
|
occupancyReads = 0;
|
|
occupancyGate = null;
|
|
flow = FillAllFlow(
|
|
fetch: fetcher.call,
|
|
now: () => clock,
|
|
slots: (_) => defaultSlots,
|
|
occupiedSlots: (_) async {
|
|
occupancyReads++;
|
|
if (occupancyGate != null) await occupancyGate!.future;
|
|
return {...occupied};
|
|
},
|
|
storage: storage,
|
|
);
|
|
});
|
|
|
|
tearDown(LogStorageService.resetForTesting);
|
|
|
|
test('the first tap proposes and writes nothing', () async {
|
|
final outcome = await flow.tap();
|
|
|
|
expect(outcome!.written, isEmpty);
|
|
expect(outcome.message, startsWith('Will log 4'));
|
|
expect(flow.armedCount, 4);
|
|
expect(await storage.todayEntries(), isEmpty);
|
|
expect(fetcher.days, [DateTime(2026, 8, 23)]);
|
|
});
|
|
|
|
test('the second tap logs the plan with the catering source', () async {
|
|
await flow.tap();
|
|
final outcome = await flow.tap();
|
|
|
|
expect(outcome!.message, 'Logged 4 catering dishes.');
|
|
expect(await loggedToday(storage), ['D1@480', 'D2@720', 'D3@960', 'D4@1200']);
|
|
expect(
|
|
(await storage.todayEntries()).map((e) => e.source),
|
|
everyElement(kuchniaSource),
|
|
);
|
|
expect(flow.armedCount, 0);
|
|
expect(fetcher.days, hasLength(1), reason: 'confirm must not refetch');
|
|
});
|
|
|
|
test(
|
|
'confirm re-checks occupancy: a meal logged between taps stays',
|
|
() async {
|
|
await flow.tap();
|
|
expect(flow.armedCount, 4);
|
|
// Logged on the PC (or here) after the proposal was shown.
|
|
await storage.logMeal(
|
|
'my own lunch',
|
|
dishNutrition(dish('x', 1)),
|
|
slotMinute: 720,
|
|
);
|
|
occupied = {720};
|
|
|
|
final outcome = await flow.tap();
|
|
|
|
expect(outcome!.message, 'Logged 3 catering dishes.');
|
|
expect(await loggedToday(storage), [
|
|
'my own lunch@720',
|
|
'D1@480',
|
|
'D3@960',
|
|
'D4@1200',
|
|
]);
|
|
expect(occupancyReads, 2, reason: 'once to propose, once to confirm');
|
|
},
|
|
);
|
|
|
|
test('a confirm into a now-full day writes nothing', () async {
|
|
await flow.tap();
|
|
occupied = {480, 720, 960, 1200};
|
|
|
|
final outcome = await flow.tap();
|
|
|
|
expect(outcome!.message, nothingToFillMessage);
|
|
expect(outcome.written, isEmpty);
|
|
expect(await storage.todayEntries(), isEmpty);
|
|
expect(flow.armedCount, 0);
|
|
});
|
|
|
|
test(
|
|
'5 dishes on 4 slots: both dishes of an empty slot are logged',
|
|
() async {
|
|
fetcher.result = KuchniaRefresh(dishes: dishes(5));
|
|
await flow.tap();
|
|
await flow.tap();
|
|
|
|
expect(await loggedToday(storage), [
|
|
'D1@480',
|
|
'D2@480',
|
|
'D3@720',
|
|
'D4@960',
|
|
'D5@1200',
|
|
]);
|
|
},
|
|
);
|
|
|
|
group('midnight', () {
|
|
test('a plan from yesterday is refetched, not confirmed', () async {
|
|
clock = DateTime(2026, 8, 23, 23, 59);
|
|
await flow.tap();
|
|
expect(flow.armedCount, 4);
|
|
|
|
clock = DateTime(2026, 8, 24, 0, 1);
|
|
final outcome = await flow.tap();
|
|
|
|
expect(fetcher.days, [DateTime(2026, 8, 23), DateTime(2026, 8, 24)]);
|
|
expect(outcome!.written, isEmpty);
|
|
expect(outcome.message, startsWith('Will log 4'));
|
|
expect(await storage.todayEntries(), isEmpty);
|
|
expect(flow.armedCount, 4, reason: 're-armed for the new day');
|
|
});
|
|
|
|
test('the day is pinned at tap time, not when the fetch lands', () async {
|
|
clock = DateTime(2026, 8, 23, 23, 59);
|
|
fetcher.gate = Completer<void>();
|
|
final pending = flow.tap();
|
|
clock = DateTime(2026, 8, 24, 0, 1);
|
|
fetcher.gate!.complete();
|
|
await pending;
|
|
|
|
// That menu was fetched for the 23rd: a tap on the 24th must refetch
|
|
// rather than confirm it.
|
|
fetcher.gate = null;
|
|
final outcome = await flow.tap();
|
|
|
|
expect(fetcher.days, [DateTime(2026, 8, 23), DateTime(2026, 8, 24)]);
|
|
expect(outcome!.written, isEmpty);
|
|
expect(await storage.todayEntries(), isEmpty);
|
|
});
|
|
});
|
|
|
|
group('nothing to arm', () {
|
|
test('a fetch error surfaces its reason', () async {
|
|
fetcher.result = const KuchniaRefresh(reason: 'Login failed (401).');
|
|
final outcome = await flow.tap();
|
|
|
|
expect(outcome!.message, 'Login failed (401).');
|
|
expect(flow.armedCount, 0);
|
|
});
|
|
|
|
test('no delivery says so', () async {
|
|
fetcher.result = const KuchniaRefresh();
|
|
final outcome = await flow.tap();
|
|
|
|
expect(outcome!.message, 'No catering delivery today.');
|
|
expect(flow.armedCount, 0);
|
|
});
|
|
|
|
test('all slots full arms nothing', () async {
|
|
occupied = {480, 720, 960, 1200};
|
|
final outcome = await flow.tap();
|
|
|
|
expect(outcome!.message, nothingToFillMessage);
|
|
expect(flow.armedCount, 0);
|
|
// The next tap fetches again instead of "confirming" an empty plan.
|
|
await flow.tap();
|
|
expect(fetcher.days, hasLength(2));
|
|
expect(await storage.todayEntries(), isEmpty);
|
|
});
|
|
});
|
|
|
|
group('busy', () {
|
|
test('a tap while the fetch is pending is ignored', () async {
|
|
fetcher.gate = Completer<void>();
|
|
final first = flow.tap();
|
|
expect(flow.busy, isTrue);
|
|
|
|
expect(await flow.tap(), isNull);
|
|
expect(fetcher.days, hasLength(1));
|
|
|
|
fetcher.gate!.complete();
|
|
final outcome = await first;
|
|
expect(flow.busy, isFalse);
|
|
expect(outcome!.message, startsWith('Will log 4'));
|
|
expect(await storage.todayEntries(), isEmpty);
|
|
});
|
|
|
|
test('a second tap during the confirm cannot log twice', () async {
|
|
await flow.tap();
|
|
occupancyGate = Completer<void>();
|
|
final confirm = flow.tap();
|
|
expect(flow.busy, isTrue);
|
|
|
|
expect(await flow.tap(), isNull);
|
|
|
|
occupancyGate!.complete();
|
|
await confirm;
|
|
expect(flow.busy, isFalse);
|
|
expect(await loggedToday(storage), hasLength(4));
|
|
});
|
|
});
|
|
|
|
test('cancel disarms; the next tap fetches again', () async {
|
|
await flow.tap();
|
|
flow.cancel();
|
|
|
|
expect(flow.armedCount, 0);
|
|
final outcome = await flow.tap();
|
|
|
|
expect(fetcher.days, hasLength(2));
|
|
expect(outcome!.written, isEmpty);
|
|
expect(await storage.todayEntries(), isEmpty);
|
|
});
|
|
}
|