diet-guard/app/test/screens/log_meal_prefill_nutrition_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

184 lines
5.6 KiB
Dart

/// What a prefilled dish actually *logs*, as distinct from what it displays.
///
/// Split from `log_meal_kuchnia_mixin_test.dart` (250-line cap) along the
/// seam the 2026-08-23 corruption exposed: that file asserts the queue's
/// mechanics and the controllers' text, and every one of its tests passed
/// while a delivered dish logged 2.58x its real calories. The bug lived in
/// the *computed* `Nutrition`, which nothing there looked at.
///
/// So these tests assert the computed value, never the field text.
library;
import 'package:diet_guard_app/models/kuchnia_dish.dart';
import 'package:diet_guard_app/screens/log_meal_actions.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('a stale per-grams does not rescale the prefilled dish', (
tester,
) async {
// The 2026-08-23 corruption: a food-bank pick leaves `perGrams` at 100,
// the dish prefills over it without clearing, and `nutritionForPortion`
// reads the dish's *per-portion* macros as *per-100g* and multiplies them
// by 300/100. Asserting controller text passes throughout that bug --
// only the computed Nutrition sees it.
await pumpHost(tester);
host.macros.perGrams.text = '100';
KuchniaQueueService.instance.offer([_dish('Pancakes', 1)]);
host.prefillNextDish();
await tester.pump();
final nutrition = nutritionFromControllers(host.macros, 'kuchnia wikinga');
expect(
nutrition.kcal,
400,
reason: 'the dish must log its own calories. 1200 here is the stale '
'per-grams rescaling a per-portion dish by 300/100.',
);
expect(nutrition.proteinG, 25);
expect(nutrition.carbsG, 45.5);
expect(nutrition.fatG, 12);
expect(nutrition.grams, 300);
});
testWidgets('clear() empties every macro controller', (tester) async {
// Enumerated deliberately: the bug was a *forgotten* field, so a new
// seventh controller must fail this until it is cleared too.
await pumpHost(tester);
final controllers = <String, TextEditingController>{
'kcal': host.macros.kcal,
'protein': host.macros.protein,
'carbs': host.macros.carbs,
'fat': host.macros.fat,
'perGrams': host.macros.perGrams,
'grams': host.macros.grams,
};
for (final c in controllers.values) {
c.text = '99';
}
host.macros.clear();
controllers.forEach((name, c) {
expect(c.text, isEmpty, reason: '$name survived clear()');
});
});
testWidgets('the prefill stamps catering even though clearing demotes it', (
tester,
) async {
// Ordering is load-bearing and invisible: `clear()` fires the macro
// listener, which demotes a food-bank pick to 'manual'. `onDishPrefilled`
// runs after and must have the last word, or a delivered dish logs as a
// manual entry and the queue never advances.
await pumpHost(tester);
host.source = 'food bank';
host.macros.perGrams.text = '100';
KuchniaQueueService.instance.offer([_dish('Pancakes', 1)]);
host.prefillNextDish();
await tester.pump();
expect(host.source, 'catering');
});
}