diet-guard/app/test/screens/log_meal_kuchnia_mixin_test.dart
Krzysztof kuhy Rudnicki c897279c8b fix: clear every macro field before a prefill, add the phone's delivery button
`fillControllersFromDish` set the dish's name, four macros and `grams` but
left `perGrams` untouched. A `100` left there by an earlier food-bank pick
made `nutritionForPortion` read the dish's per-portion macros as per-100g and
rescale them: one real entry on 2026-08-23 logged 472 kcal as 1217.8 (258/100).

The PC never had this -- `_gatelock_delivery.py` calls `_clear_inputs()` first
-- so the fix is that same discipline, as one `MacroControllers.clear()` call
rather than a `perGrams.clear()` line a seventh field could forget. `fillFrom`
now clears too, and `edit_entry_screen` routes through it instead of six
hand-written assignments; its `initState` fill had the same shape.

The old tests asserted controller *text* and passed throughout the bug, so the
new ones assert the computed `Nutrition` -- verified failing (400 -> 1200.0)
before the fix. `clear()` gets an enumerated test so a new field fails until
it is cleared too.

Also adds the log screen's "Today's delivery" button, matching the PC lock
screen's affordance: explicit taps are unguarded per the Triggers table, still
skip dishes already logged, and show the queue count. The whole action row
moved to its own widget, which put `log_meal_screen.dart` back under the cap.
The label is a FittedBox in an Expanded -- measured, since a Row clips an
oversized label without ever throwing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX
2026-08-23 09:34:47 +02:00

212 lines
6.3 KiB
Dart

/// The delivered-dish queue as the log form actually drives it.
///
/// `kuchnia_queue_test.dart` covers the queue's own state machine. This file
/// covers the part that state machine exists for and that a queue-only test
/// cannot see: after a submit, are the **form controllers** already holding
/// the next dish?
///
/// That distinction is the whole point. On the PC this regressed once --
/// `_prefill_next_dish` briefly had exactly one caller, so the queue was
/// correct and the form was not. Every dish after the first stayed behind
/// another tap and the "(N more to go)" line became a dead letter. A test
/// asserting "the dish was offered" passes throughout that bug; one asserting
/// the controller contents after a submit does not.
///
/// Driven through a minimal host rather than the real `LogMealScreen`: that
/// screen's `initState` fans out into sync, notifications and the food bank,
/// and pumping it to quiescence in a widget test does not terminate. The
/// mixin is the unit that owns this behaviour, so it is the unit under test.
library;
import 'package:diet_guard_app/models/kuchnia_dish.dart';
import 'package:diet_guard_app/screens/log_meal_kuchnia_mixin.dart';
import 'package:diet_guard_app/services/document_store.dart';
import 'package:diet_guard_app/services/kuchnia_queue.dart';
import 'package:diet_guard_app/widgets/macro_input_row.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
/// In-memory store, so these tests need no plugin channel.
class _MemoryStore implements DocumentStore {
final Map<String, String> documents = {};
@override
Future<String?> read(String name) async => documents[name];
@override
Future<void> write(String name, String contents) async {
documents[name] = contents;
}
}
/// The smallest widget that uses the mixin the way the log screen does.
class _Host extends StatefulWidget {
const _Host();
@override
State<_Host> createState() => _HostState();
}
class _HostState extends State<_Host> with LogMealKuchniaMixin<_Host> {
final TextEditingController desc = TextEditingController();
final MacroControllers macros = MacroControllers();
@override
void initState() {
super.initState();
for (final c in [
macros.kcal,
macros.protein,
macros.carbs,
macros.fat,
macros.perGrams,
macros.grams,
]) {
c.addListener(() {
if (source == 'food bank') source = 'manual';
});
}
}
@override
TextEditingController get descController => desc;
@override
MacroControllers get macroControllers => macros;
/// Mirrors the screen: a macro edit demotes a food-bank pick to manual,
/// and a prefilled dish stamps catering.
String source = 'manual';
@override
void onDishPrefilled() => source = 'catering';
/// Stands in for the screen's submit: the entry write is irrelevant here,
/// the queue advance is what is under test.
void submit() {
final logged = desc.text;
desc.clear();
macros.clear();
advanceQueueAfterLog(logged);
}
@override
Widget build(BuildContext context) =>
Text(queueStatusLine ?? '', textDirection: TextDirection.ltr);
}
KuchniaDish _dish(String name, int priority) => KuchniaDish(
name: name,
kcal: 400,
proteinG: 25,
carbsG: 45.5,
fatG: 12,
grams: 300,
priority: priority,
slotLabel: 'Obiad',
);
void main() {
late _HostState host;
Future<void> pumpHost(WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: _Host()));
host = tester.state(find.byType(_Host));
}
setUp(() async {
await KuchniaQueueService.initForTesting(_MemoryStore());
});
tearDown(KuchniaQueueService.resetForTesting);
testWidgets('after a submit the form already holds the next dish', (
tester,
) async {
await pumpHost(tester);
KuchniaQueueService.instance.offer([
_dish('Pancakes', 1),
_dish('Kaszotto', 2),
]);
host.prefillNextDish();
await tester.pump();
expect(host.desc.text, 'Pancakes');
host.submit();
await tester.pump();
expect(
host.desc.text,
'Kaszotto',
reason: 'the form must already hold dish 2. Empty here means the queue '
'is right and the form is not -- the dead-letter regression where '
'every dish after the first waits behind another tap.',
);
expect(KuchniaQueueService.remaining, 1);
});
testWidgets('the macros come with the dish, not just the name', (
tester,
) async {
await pumpHost(tester);
KuchniaQueueService.instance.offer([_dish('Pancakes', 1)]);
host.prefillNextDish();
await tester.pump();
// Whole numbers print without a trailing `.0`, matching Python's `:g`.
expect(host.macros.kcal.text, '400');
expect(host.macros.protein.text, '25');
expect(host.macros.carbs.text, '45.5');
expect(host.macros.fat.text, '12');
expect(host.macros.grams.text, '300');
});
testWidgets('the leftover dish is named rather than dropped', (tester) async {
// Five delivered meals against four default slots leaves one queued when
// the last slot is logged. It is already banked, so say so.
await pumpHost(tester);
KuchniaQueueService.instance.offer([_dish('A', 1), _dish('B', 2)]);
host.prefillNextDish();
await tester.pump();
host.submit();
await tester.pump();
expect(find.text('1 more delivered dish to log.'), findsOneWidget);
});
testWidgets('editing the description does not consume the offer', (
tester,
) async {
// What the user logged was not the delivered dish, so marking it eaten
// would lose a real delivered meal silently.
await pumpHost(tester);
KuchniaQueueService.instance.offer([_dish('Pancakes', 1)]);
host.prefillNextDish();
await tester.pump();
host.desc.text = 'Something else';
host.submit();
await tester.pump();
expect(KuchniaQueueService.remaining, 1);
});
testWidgets('an empty queue clears the offer instead of throwing', (
tester,
) async {
await pumpHost(tester);
host.prefillNextDish();
await tester.pump();
expect(host.offeredDish, isNull);
expect(host.queueStatusLine, isNull);
});
testWidgets('submitting with nothing offered is a no-op', (tester) async {
await pumpHost(tester);
host.submit();
await tester.pump();
expect(KuchniaQueueService.remaining, 0);
});
}