mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-29 15:43:35 +01:00
Compare commits
No commits in common. "9bfc14684e26688ee25414529e95b14c807f8e8a" and "c1ad2e24063e7ebd3cecf6017b8676b840d6f1d7" have entirely different histories.
9bfc14684e
...
c1ad2e2406
121
NEXT_SESSION.md
Normal file
121
NEXT_SESSION.md
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
# Next session: fix the phone's catering prefill, add the one-tap button
|
||||||
|
|
||||||
|
Paste the prompt below into a fresh session. Delete this file once it's done.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Prompt
|
||||||
|
|
||||||
|
Two bugs in the phone-side Kuchnia Wikinga import that shipped on 2026-08-23
|
||||||
|
(commits `24be05b9c38`..`d9b3d57900f`). Read
|
||||||
|
`docs/kuchnia-wikinga-phone.md` and `docs/kuchnia-wikinga.md` first.
|
||||||
|
|
||||||
|
State: `main` is clean and pushed. Phone (Pixel 6a, `23181JEGR08034`) has the
|
||||||
|
build with both bugs installed. 1040 Python tests at 100% branch coverage,
|
||||||
|
695 Dart tests — all green, which is exactly the problem: **both bugs pass the
|
||||||
|
entire suite.**
|
||||||
|
|
||||||
|
### Bug 1 — a prefilled dish logs ~2.6x its real calories (data corruption, fix first)
|
||||||
|
|
||||||
|
Reproduced on the user's real phone on 2026-08-23:
|
||||||
|
|
||||||
|
| | |
|
||||||
|
|---|---|
|
||||||
|
| bank record | `472.0 kcal`, `25.89 P`, `52.95 C`, `16.62 F`, `258.0 g` |
|
||||||
|
| what got logged | `1217.8 kcal`, `66.8 P`, `136.6 C`, `42.9 F`, `258.0 g` |
|
||||||
|
| ratio | `1217.8 / 472.0` = **2.58** = `258 g / 100` |
|
||||||
|
|
||||||
|
Entry: `Pieczona owsianka daktylowo-czekoladowa, sos gruszkowy, orzechy włoskie`
|
||||||
|
at `2026-08-23T08:00:09+02:00`, `source: "kuchnia wikinga"`, slot 8.
|
||||||
|
|
||||||
|
**Root cause** (confirmed, don't re-derive): `fillControllersFromDish` in
|
||||||
|
`app/lib/screens/log_meal_kuchnia_mixin.dart` sets `desc`, the four macros and
|
||||||
|
`macros.grams` — but **never clears `macros.perGrams`**. `nutritionForPortion`
|
||||||
|
(`app/lib/models/nutrition.dart:87`) does
|
||||||
|
`referenceGrams = perGrams > 0 ? perGrams : ateGrams`, so a stale `100` left in
|
||||||
|
the per-grams field from a previous food-bank pick makes it treat the dish's
|
||||||
|
*per-portion* macros as *per-100 g* and rescale them by `258/100`.
|
||||||
|
|
||||||
|
The PC does not have this bug because `_gatelock_delivery.py:154` calls
|
||||||
|
`self._clear_inputs()` before filling. The Dart mirror dropped that step.
|
||||||
|
|
||||||
|
Required:
|
||||||
|
|
||||||
|
- Clear **every** macro field (including `perGrams`) before filling, the same
|
||||||
|
way the PC does. Prefer making `fillControllersFromDish` unable to leave a
|
||||||
|
stale field rather than adding a `perGrams.clear()` line that the next field
|
||||||
|
can forget again.
|
||||||
|
- A test that would have caught it: pre-seed `perGrams` with `100`, prefill a
|
||||||
|
dish, and assert the *computed* `Nutrition` equals the dish's own macros —
|
||||||
|
not just that the text fields look right. The current
|
||||||
|
`log_meal_kuchnia_mixin_test.dart` asserts controller **text** and passes
|
||||||
|
with this bug present; that is why it shipped.
|
||||||
|
- Check the same class of bug in the settings-button path and anywhere else a
|
||||||
|
dish reaches the form.
|
||||||
|
|
||||||
|
**Also fix the already-corrupted entry.** One real log entry is wrong by
|
||||||
|
~746 kcal on 2026-08-23. It is synced, so correcting it locally and letting the
|
||||||
|
merge carry the fix is the right move — but confirm with the user before
|
||||||
|
editing their food log, and do not hand-edit `food_log.json` while the app or
|
||||||
|
a sync tick might write it.
|
||||||
|
|
||||||
|
### Bug 2 — the phone has no one-tap "add today's delivery" button
|
||||||
|
|
||||||
|
The PC's lock screen has a "🍱 Today's delivery" button that walks the whole
|
||||||
|
delivery dish by dish (`_gatelock_delivery.py`). On the phone the queue exists
|
||||||
|
and works (`KuchniaQueueService`, `LogMealKuchniaMixin`), but the **only** ways
|
||||||
|
to trigger it are the log screen's `initState` and the Settings "Fetch today's
|
||||||
|
delivery" button — there is no visible control on the log screen itself.
|
||||||
|
|
||||||
|
So when the user is standing in the kitchen, there is nothing to tap.
|
||||||
|
|
||||||
|
Required:
|
||||||
|
|
||||||
|
- A visible control on the log screen that loads today's delivery and prefills
|
||||||
|
the first dish, matching the PC's affordance.
|
||||||
|
- It must show the queue state — the PC's `"(N more to go)"`. The mixin already
|
||||||
|
exposes `queueStatusLine` and `dishesStillQueued`.
|
||||||
|
- Respect the guard split documented in `docs/kuchnia-wikinga.md`'s Triggers
|
||||||
|
table: an **explicit** user tap goes and looks (unguarded, like the CLI and
|
||||||
|
the settings button); the automatic `initState` load stays guarded by
|
||||||
|
`KuchniaQueueService.refreshOnce`.
|
||||||
|
- `app/lib/screens/log_meal_screen.dart` is at **exactly 249/250 lines**. The
|
||||||
|
button will not fit there — put it in a widget of its own, as
|
||||||
|
`settings_kuchnia.dart` does.
|
||||||
|
|
||||||
|
### Constraints
|
||||||
|
|
||||||
|
- 250 lines per file (`file-length` hook). `AGENTS.md` is at exactly 250.
|
||||||
|
- 100% Python branch coverage; `flutter test` green.
|
||||||
|
- `pre-commit run --files <changed>` before finishing. The repo bans `noqa`
|
||||||
|
outright — there is a `no-noqa` hook, so a suppression is not an escape
|
||||||
|
hatch.
|
||||||
|
- Any new Python state path must patch `conftest._isolate_state` in the *same*
|
||||||
|
edit, or the suite writes to live user data.
|
||||||
|
- Never log a delivery unattended — a delivered meal is not an eaten meal.
|
||||||
|
- Don't break the cross-language parity gate (`tests/fixtures/kuchnia_day.json`,
|
||||||
|
asserted by both suites). If you change `dishFieldValues` or the banked
|
||||||
|
record, regenerate with `scripts/build_kuchnia_fixture.py` and **re-read the
|
||||||
|
diff** — the script blesses whatever Python currently does.
|
||||||
|
|
||||||
|
### Verify on the phone, not just in tests
|
||||||
|
|
||||||
|
Mobile is the primary platform. Deploy with
|
||||||
|
`~/.claude/scripts/phone_deploy.sh ~/diet-guard/app --release`. Never uninstall
|
||||||
|
or `pm clear`.
|
||||||
|
|
||||||
|
**The phone is the user's daily driver — do not leave the app mid-flow, and do
|
||||||
|
not grab focus on their desktop.**
|
||||||
|
|
||||||
|
Verifying bug 1 needs an actual log: prefill a delivered dish, log it, and read
|
||||||
|
the entry back out of `food_log.json` after a sync. The kcal must match the
|
||||||
|
dish's own value, not a multiple of it. A green widget test is what let this
|
||||||
|
ship in the first place.
|
||||||
|
|
||||||
|
### Done
|
||||||
|
|
||||||
|
- A dish prefilled from the delivery logs its own macros exactly, with a stale
|
||||||
|
`perGrams` present, proven by reading the written entry back.
|
||||||
|
- The corrupted 2026-08-23 owsianka entry reads 472 kcal.
|
||||||
|
- One tap on the log screen loads the delivery and walks it dish by dish, with
|
||||||
|
the remaining count visible — observed on the phone.
|
||||||
@ -41,10 +41,6 @@ class _EditEntryScreenState extends State<EditEntryScreen> {
|
|||||||
super.initState();
|
super.initState();
|
||||||
final e = widget.entry;
|
final e = widget.entry;
|
||||||
_descController = TextEditingController(text: e.desc);
|
_descController = TextEditingController(text: e.desc);
|
||||||
// Same clear-before-fill discipline as `fillControllersFromDish`: the
|
|
||||||
// entry's macros describe the whole eaten portion, so any reference
|
|
||||||
// weight left in `perGrams` would rescale them on save.
|
|
||||||
_macros.clear();
|
|
||||||
_macros.kcal.text = e.kcal.toStringAsFixed(0);
|
_macros.kcal.text = e.kcal.toStringAsFixed(0);
|
||||||
_macros.protein.text = e.proteinG.toStringAsFixed(0);
|
_macros.protein.text = e.proteinG.toStringAsFixed(0);
|
||||||
_macros.carbs.text = e.carbsG.toStringAsFixed(0);
|
_macros.carbs.text = e.carbsG.toStringAsFixed(0);
|
||||||
@ -89,10 +85,12 @@ class _EditEntryScreenState extends State<EditEntryScreen> {
|
|||||||
|
|
||||||
void _onSuggestionSelected(FoodSuggestion suggestion) {
|
void _onSuggestionSelected(FoodSuggestion suggestion) {
|
||||||
_descController.text = suggestion.name;
|
_descController.text = suggestion.name;
|
||||||
// Routed through `fillFrom` rather than six hand-written assignments, so
|
_macros.kcal.text = suggestion.nutrition.kcal.toStringAsFixed(0);
|
||||||
// this path and the log screen's cannot drift and neither can forget a
|
_macros.protein.text = suggestion.nutrition.proteinG.toStringAsFixed(0);
|
||||||
// field. It clears first, so nothing survives from the previous pick.
|
_macros.carbs.text = suggestion.nutrition.carbsG.toStringAsFixed(0);
|
||||||
_macros.fillFrom(suggestion.nutrition);
|
_macros.fat.text = suggestion.nutrition.fatG.toStringAsFixed(0);
|
||||||
|
_macros.perGrams.text = suggestion.nutrition.grams.toStringAsFixed(0);
|
||||||
|
_macros.grams.text = suggestion.nutrition.grams.toStringAsFixed(0);
|
||||||
setState(() {
|
setState(() {
|
||||||
_source = 'food bank';
|
_source = 'food bank';
|
||||||
_suggestions = const [];
|
_suggestions = const [];
|
||||||
|
|||||||
@ -20,7 +20,6 @@ library;
|
|||||||
|
|
||||||
import 'package:diet_guard_app/models/kuchnia_dish.dart';
|
import 'package:diet_guard_app/models/kuchnia_dish.dart';
|
||||||
import 'package:diet_guard_app/models/slot.dart';
|
import 'package:diet_guard_app/models/slot.dart';
|
||||||
import 'package:diet_guard_app/services/kuchnia_import.dart';
|
|
||||||
import 'package:diet_guard_app/services/kuchnia_queue.dart';
|
import 'package:diet_guard_app/services/kuchnia_queue.dart';
|
||||||
import 'package:diet_guard_app/services/kuchnia_spread.dart';
|
import 'package:diet_guard_app/services/kuchnia_spread.dart';
|
||||||
import 'package:diet_guard_app/services/log_storage_service.dart';
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
||||||
@ -32,24 +31,11 @@ import 'package:flutter/material.dart';
|
|||||||
///
|
///
|
||||||
/// Free function rather than a mixin method so the numeric formatting is
|
/// Free function rather than a mixin method so the numeric formatting is
|
||||||
/// testable without pumping a widget.
|
/// testable without pumping a widget.
|
||||||
///
|
|
||||||
/// Clears every macro field first, mirroring `_gatelock_delivery.py`'s
|
|
||||||
/// `self._clear_inputs()`. Dropping that step is what corrupted a real log
|
|
||||||
/// entry on 2026-08-23: a dish carries *per-portion* macros and leaves
|
|
||||||
/// [MacroControllers.perGrams] untouched, so a `100` left behind by an
|
|
||||||
/// earlier food-bank pick made `nutritionForPortion` read them as *per-100g*
|
|
||||||
/// and scale them by `grams / 100` -- 472 kcal logged as 1217.8.
|
|
||||||
///
|
|
||||||
/// The clear is a single call covering all six controllers rather than a
|
|
||||||
/// `perGrams.clear()` line here, so a seventh field added later cannot be
|
|
||||||
/// forgotten at this call site; `MacroControllers.clear()` is enumerated by
|
|
||||||
/// its own test.
|
|
||||||
void fillControllersFromDish(
|
void fillControllersFromDish(
|
||||||
KuchniaDish dish,
|
KuchniaDish dish,
|
||||||
TextEditingController desc,
|
TextEditingController desc,
|
||||||
MacroControllers macros,
|
MacroControllers macros,
|
||||||
) {
|
) {
|
||||||
macros.clear();
|
|
||||||
desc.text = dish.name;
|
desc.text = dish.name;
|
||||||
final (grams, values) = dishFieldValues(dish);
|
final (grams, values) = dishFieldValues(dish);
|
||||||
macros.kcal.text = values.$1;
|
macros.kcal.text = values.$1;
|
||||||
@ -125,72 +111,6 @@ mixin LogMealKuchniaMixin<T extends StatefulWidget> on State<T> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads today's delivery on an **explicit** user tap, unguarded.
|
|
||||||
///
|
|
||||||
/// Deliberately [refreshDelivery], not [KuchniaQueueService.refreshOnce]:
|
|
||||||
/// per `docs/kuchnia-wikinga.md`'s Triggers table, a user who asks gets a
|
|
||||||
/// look, the same as the CLI and the settings button. The guard exists to
|
|
||||||
/// rate-limit *automatic* triggers, and applying it here would make the
|
|
||||||
/// button silently do nothing for the rest of the day after the startup
|
|
||||||
/// load — the failure that has nothing to tap in the kitchen.
|
|
||||||
///
|
|
||||||
/// Still skips dishes already logged today (a dish logged on the PC must
|
|
||||||
/// not be re-offered) and still records the fetch, so a successful tap
|
|
||||||
/// also spares the automatic path a second walk.
|
|
||||||
///
|
|
||||||
/// Returns a line to show the user, or null when a dish was prefilled and
|
|
||||||
/// the form speaks for itself.
|
|
||||||
Future<String?> loadDeliveryOnTap() async {
|
|
||||||
if (!KuchniaQueueService.isInitialized) return 'Catering is not ready yet.';
|
|
||||||
final logged = await LogStorageService.instance.todayEntries();
|
|
||||||
if (!mounted) return null;
|
|
||||||
final result = await refreshDelivery(DateTime.now());
|
|
||||||
if (!mounted) return null;
|
|
||||||
if (!result.ok) return result.reason;
|
|
||||||
if (result.dishes.isEmpty) return 'No delivery today.';
|
|
||||||
await KuchniaQueueService.instance.recordFetched(DateTime.now());
|
|
||||||
if (!mounted) return null;
|
|
||||||
final ordered = dishesInSlotOrder(
|
|
||||||
result.dishes,
|
|
||||||
daySlots(MealScheduleService.current),
|
|
||||||
);
|
|
||||||
KuchniaQueueService.instance.offer(
|
|
||||||
ordered,
|
|
||||||
alreadyLogged: {
|
|
||||||
for (final entry in logged) entry.desc.trim().toLowerCase(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (KuchniaQueueService.next == null) {
|
|
||||||
return 'All delivered dishes are already logged.';
|
|
||||||
}
|
|
||||||
prefillNextDish();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether an explicit delivery fetch is in flight.
|
|
||||||
///
|
|
||||||
/// Only guards against a double tap opening two logins at once -- it is not
|
|
||||||
/// the daily guard, which an explicit tap deliberately bypasses.
|
|
||||||
bool deliveryBusy = false;
|
|
||||||
|
|
||||||
/// Runs [loadDeliveryOnTap] and shows its outcome as a SnackBar.
|
|
||||||
///
|
|
||||||
/// Lives here rather than on the screen because `log_meal_screen.dart` is at
|
|
||||||
/// the 250-line cap, and this is the mixin's own behaviour anyway.
|
|
||||||
Future<void> loadDeliveryAndReport() async {
|
|
||||||
setState(() => deliveryBusy = true);
|
|
||||||
String? message;
|
|
||||||
try {
|
|
||||||
message = await loadDeliveryOnTap();
|
|
||||||
} finally {
|
|
||||||
if (mounted) setState(() => deliveryBusy = false);
|
|
||||||
}
|
|
||||||
if (!mounted || message == null) return;
|
|
||||||
ScaffoldMessenger.of(
|
|
||||||
context,
|
|
||||||
).showSnackBar(SnackBar(content: Text(message)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Prefills the form with the next queued dish, if there is one.
|
/// Prefills the form with the next queued dish, if there is one.
|
||||||
///
|
///
|
||||||
/// Safe to call when the queue is empty: it simply clears the offer.
|
/// Safe to call when the queue is empty: it simply clears the offer.
|
||||||
|
|||||||
@ -18,7 +18,6 @@ import 'package:diet_guard_app/services/log_storage_service.dart';
|
|||||||
import 'package:diet_guard_app/services/meal_schedule_service.dart';
|
import 'package:diet_guard_app/services/meal_schedule_service.dart';
|
||||||
import 'package:diet_guard_app/ui/theme.dart';
|
import 'package:diet_guard_app/ui/theme.dart';
|
||||||
import 'package:diet_guard_app/widgets/autocomplete_suggestion_list.dart';
|
import 'package:diet_guard_app/widgets/autocomplete_suggestion_list.dart';
|
||||||
import 'package:diet_guard_app/widgets/log_meal_actions_row.dart';
|
|
||||||
import 'package:diet_guard_app/widgets/macro_input_row.dart';
|
import 'package:diet_guard_app/widgets/macro_input_row.dart';
|
||||||
import 'package:diet_guard_app/widgets/slot_selector_row.dart';
|
import 'package:diet_guard_app/widgets/slot_selector_row.dart';
|
||||||
import 'package:diet_guard_app/widgets/sync_health_banner.dart';
|
import 'package:diet_guard_app/widgets/sync_health_banner.dart';
|
||||||
@ -179,6 +178,7 @@ class _LogMealScreenState extends State<LogMealScreen>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -218,11 +218,17 @@ class _LogMealScreenState extends State<LogMealScreen>
|
|||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
MacroInputRow(controllers: _macros, compact: true),
|
MacroInputRow(controllers: _macros, compact: true),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
LogMealActionsRow(
|
Row(
|
||||||
onLoadDelivery: loadDeliveryAndReport,
|
children: [
|
||||||
onLog: _onLogMeal,
|
const Spacer(),
|
||||||
deliveryBusy: deliveryBusy,
|
Tooltip(
|
||||||
dishesQueued: dishesStillQueued,
|
message: 'Log meal',
|
||||||
|
child: FilledButton(
|
||||||
|
onPressed: _onLogMeal,
|
||||||
|
child: const Icon(Icons.check_circle),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
// Mutually exclusive: _status carries a validation complaint,
|
// Mutually exclusive: _status carries a validation complaint,
|
||||||
// _progress the post-log summary. A successful log clears one
|
// _progress the post-log summary. A successful log clears one
|
||||||
|
|||||||
@ -1,95 +0,0 @@
|
|||||||
/// The log form's action row: load today's delivery, then log the meal.
|
|
||||||
///
|
|
||||||
/// Its own file because `log_meal_screen.dart` sits at the repo's 250-line
|
|
||||||
/// ceiling; lifting the whole row rather than just the delivery button keeps
|
|
||||||
/// both controls' layout in one place and returns budget to the screen.
|
|
||||||
library;
|
|
||||||
|
|
||||||
import 'package:diet_guard_app/services/kuchnia_client.dart';
|
|
||||||
import 'package:diet_guard_app/ui/theme.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
/// A delivery button beside the log button.
|
|
||||||
///
|
|
||||||
/// The delivery control mirrors the PC lock screen's "🍱 Today's delivery":
|
|
||||||
/// standing in the kitchen, there must be something to tap. It stays visible
|
|
||||||
/// and enabled even when there is nothing to load — the outcome arrives as a
|
|
||||||
/// message rather than as a disabled button, because a disabled control gives
|
|
||||||
/// no way to retry after a late delivery and a hidden one reads as broken.
|
|
||||||
class LogMealActionsRow extends StatelessWidget {
|
|
||||||
/// Creates the action row.
|
|
||||||
const LogMealActionsRow({
|
|
||||||
required this.onLoadDelivery,
|
|
||||||
required this.onLog,
|
|
||||||
this.deliveryBusy = false,
|
|
||||||
this.dishesQueued = 0,
|
|
||||||
this.canFetchDelivery,
|
|
||||||
super.key,
|
|
||||||
});
|
|
||||||
|
|
||||||
/// Loads today's delivery and prefills the first/next dish.
|
|
||||||
final Future<void> Function() onLoadDelivery;
|
|
||||||
|
|
||||||
/// Writes the current form as a log entry.
|
|
||||||
final VoidCallback onLog;
|
|
||||||
|
|
||||||
/// Whether a delivery fetch is in flight; disables the button meanwhile so
|
|
||||||
/// a second tap cannot start a concurrent login against the caterer.
|
|
||||||
final bool deliveryBusy;
|
|
||||||
|
|
||||||
/// How many delivered dishes are still queued, shown as the PC's
|
|
||||||
/// "(N more to go)" so the user knows the walk is not finished.
|
|
||||||
final int dishesQueued;
|
|
||||||
|
|
||||||
/// Whether the platform can fetch at all. Defaults to
|
|
||||||
/// [kuchniaFetchSupported] — false on web, where the desktop build runs and
|
|
||||||
/// the caterer's panel blocks browser requests, so the control is hidden
|
|
||||||
/// rather than shipped dead.
|
|
||||||
final bool? canFetchDelivery;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final canFetch = canFetchDelivery ?? kuchniaFetchSupported;
|
|
||||||
return Row(
|
|
||||||
children: [
|
|
||||||
// Flexible, not Spacer-and-hope: `OutlinedButton.icon` lays its label
|
|
||||||
// out unbounded, so on a narrow phone the queue suffix renders wider
|
|
||||||
// than the row and is clipped without ever throwing an overflow. The
|
|
||||||
// Flexible caps the width and the FittedBox scales the text into it.
|
|
||||||
if (canFetch)
|
|
||||||
Expanded(
|
|
||||||
child: Tooltip(
|
|
||||||
message: "Load today's catering delivery",
|
|
||||||
child: OutlinedButton.icon(
|
|
||||||
onPressed: deliveryBusy ? null : onLoadDelivery,
|
|
||||||
icon: const Icon(Icons.restaurant),
|
|
||||||
label: FittedBox(
|
|
||||||
fit: BoxFit.scaleDown,
|
|
||||||
child: Text(_label, maxLines: 1),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
// No Spacer: it is a flex child too, so it would split the free space
|
|
||||||
// with the Flexible above and re-create the clipping this guards
|
|
||||||
// against. The Flexible takes the slack and pushes the log button
|
|
||||||
// right on its own.
|
|
||||||
if (!canFetch) const Spacer(),
|
|
||||||
const SizedBox(width: AppSpacing.sm),
|
|
||||||
Tooltip(
|
|
||||||
message: 'Log meal',
|
|
||||||
child: FilledButton(
|
|
||||||
onPressed: onLog,
|
|
||||||
child: const Icon(Icons.check_circle),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
String get _label {
|
|
||||||
if (deliveryBusy) return 'Loading…';
|
|
||||||
if (dishesQueued > 0) return "Today's delivery ($dishesQueued more to go)";
|
|
||||||
return "Today's delivery";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -43,12 +43,7 @@ class MacroControllers {
|
|||||||
///
|
///
|
||||||
/// Both [perGrams] and [grams] are set to the suggestion's own weight, so
|
/// Both [perGrams] and [grams] are set to the suggestion's own weight, so
|
||||||
/// the row starts out describing exactly the portion that was banked.
|
/// the row starts out describing exactly the portion that was banked.
|
||||||
///
|
|
||||||
/// Clears first so no field can survive from a previous pick. This one
|
|
||||||
/// assignment set happens to cover all six today, but the clear is what
|
|
||||||
/// makes that non-load-bearing -- see [clear].
|
|
||||||
void fillFrom(Nutrition nutrition) {
|
void fillFrom(Nutrition nutrition) {
|
||||||
clear();
|
|
||||||
kcal.text = nutrition.kcal.toStringAsFixed(0);
|
kcal.text = nutrition.kcal.toStringAsFixed(0);
|
||||||
protein.text = nutrition.proteinG.toStringAsFixed(0);
|
protein.text = nutrition.proteinG.toStringAsFixed(0);
|
||||||
carbs.text = nutrition.carbsG.toStringAsFixed(0);
|
carbs.text = nutrition.carbsG.toStringAsFixed(0);
|
||||||
|
|||||||
@ -2,7 +2,7 @@ name: diet_guard_app
|
|||||||
description: "Companion phone app for diet_guard: log meals on the go."
|
description: "Companion phone app for diet_guard: log meals on the go."
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
version: 1.0.69+1
|
version: 1.0.67+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.12.2
|
sdk: ^3.12.2
|
||||||
|
|||||||
@ -147,51 +147,4 @@ void main() {
|
|||||||
expect(find.text('1200'), findsOneWidget);
|
expect(find.text('1200'), findsOneWidget);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('a stale per-grams does not rescale an edited entry on save', (
|
|
||||||
tester,
|
|
||||||
) async {
|
|
||||||
// The edit screen reaches the same `nutritionForPortion` as the log form,
|
|
||||||
// so it is exposed to the identical corruption: pick a suggestion (which
|
|
||||||
// sets `perGrams`), retype the macros as a whole portion, and the save
|
|
||||||
// would scale them by grams/perGrams. Asserting the *written entry* is
|
|
||||||
// what catches it -- the fields on screen look right either way.
|
|
||||||
await tester.runAsync(() async {
|
|
||||||
await FoodBankService.instance.addManualEntry(
|
|
||||||
const FoodBankRecord(
|
|
||||||
desc: 'pizza obok pracy',
|
|
||||||
kcal: 1200,
|
|
||||||
proteinG: 52,
|
|
||||||
carbsG: 145,
|
|
||||||
fatG: 42,
|
|
||||||
grams: 400,
|
|
||||||
count: 1,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
await LogStorageService.instance.writeLog({
|
|
||||||
'2026-06-22': [_entry],
|
|
||||||
});
|
|
||||||
|
|
||||||
await tester.pumpWidget(
|
|
||||||
MaterialApp(home: EditEntryScreen(entry: _entry)),
|
|
||||||
);
|
|
||||||
await settle(tester);
|
|
||||||
|
|
||||||
await tester.tap(find.text('pizza obok pracy').last);
|
|
||||||
await settle(tester);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Save'));
|
|
||||||
await settle(tester);
|
|
||||||
|
|
||||||
final saved = (await LogStorageService.instance.readLog())['2026-06-22']!
|
|
||||||
.first;
|
|
||||||
expect(
|
|
||||||
saved.kcal,
|
|
||||||
1200,
|
|
||||||
reason: 'the banked portion must save as itself, not rescaled by '
|
|
||||||
'its own reference weight.',
|
|
||||||
);
|
|
||||||
expect(saved.grams, 400);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,36 +51,12 @@ class _HostState extends State<_Host> with LogMealKuchniaMixin<_Host> {
|
|||||||
final TextEditingController desc = TextEditingController();
|
final TextEditingController desc = TextEditingController();
|
||||||
final MacroControllers macros = MacroControllers();
|
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
|
@override
|
||||||
TextEditingController get descController => desc;
|
TextEditingController get descController => desc;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MacroControllers get macroControllers => macros;
|
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,
|
/// Stands in for the screen's submit: the entry write is irrelevant here,
|
||||||
/// the queue advance is what is under test.
|
/// the queue advance is what is under test.
|
||||||
void submit() {
|
void submit() {
|
||||||
|
|||||||
@ -1,183 +0,0 @@
|
|||||||
/// 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');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@ -1,123 +0,0 @@
|
|||||||
/// The log form's action row, and the delivery button the phone lacked.
|
|
||||||
///
|
|
||||||
/// The PC lock screen has had a "🍱 Today's delivery" button since the import
|
|
||||||
/// shipped; the phone had the queue but no visible control, so standing in
|
|
||||||
/// the kitchen there was nothing to tap. These tests pin the affordance and
|
|
||||||
/// the two ways it silently fails: a clipped label, and a dead control on the
|
|
||||||
/// web build.
|
|
||||||
library;
|
|
||||||
|
|
||||||
import 'package:diet_guard_app/widgets/log_meal_actions_row.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
Future<void> pump(
|
|
||||||
WidgetTester tester, {
|
|
||||||
int queued = 0,
|
|
||||||
bool busy = false,
|
|
||||||
bool canFetch = true,
|
|
||||||
Future<void> Function()? onLoad,
|
|
||||||
VoidCallback? onLog,
|
|
||||||
double width = 360,
|
|
||||||
}) async {
|
|
||||||
await tester.pumpWidget(
|
|
||||||
MaterialApp(
|
|
||||||
home: Scaffold(
|
|
||||||
body: SizedBox(
|
|
||||||
width: width,
|
|
||||||
child: LogMealActionsRow(
|
|
||||||
onLoadDelivery: onLoad ?? () async {},
|
|
||||||
onLog: onLog ?? () {},
|
|
||||||
deliveryBusy: busy,
|
|
||||||
dishesQueued: queued,
|
|
||||||
canFetchDelivery: canFetch,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
testWidgets('the delivery button is there to tap', (tester) async {
|
|
||||||
var taps = 0;
|
|
||||||
await pump(tester, onLoad: () async => taps++);
|
|
||||||
|
|
||||||
await tester.tap(find.byIcon(Icons.restaurant));
|
|
||||||
await tester.pump();
|
|
||||||
|
|
||||||
expect(taps, 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('the queue count is visible, as the PC shows it', (tester) async {
|
|
||||||
await pump(tester, queued: 4);
|
|
||||||
expect(find.text("Today's delivery (4 more to go)"), findsOneWidget);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('the long label is scaled, not silently clipped', (tester) async {
|
|
||||||
// A Row clips an oversized child without throwing, so "no overflow
|
|
||||||
// exception" proves nothing. Measure the rendered text instead.
|
|
||||||
await pump(tester, queued: 4, width: 300);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
|
|
||||||
// `getSize` on the Text reports its *unscaled* intrinsic width, because
|
|
||||||
// FittedBox scales via a transform rather than by resizing its child.
|
|
||||||
// The painted extent is what the user sees, so measure the rect the
|
|
||||||
// FittedBox actually occupies.
|
|
||||||
final painted = tester.getRect(
|
|
||||||
find.ancestor(
|
|
||||||
of: find.text("Today's delivery (4 more to go)"),
|
|
||||||
matching: find.byType(FittedBox),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
final row = tester.getRect(find.byType(LogMealActionsRow));
|
|
||||||
expect(
|
|
||||||
painted.width,
|
|
||||||
lessThanOrEqualTo(row.width),
|
|
||||||
reason: 'the label must fit the row it is rendered in',
|
|
||||||
);
|
|
||||||
expect(painted.right, lessThanOrEqualTo(row.right + 0.5));
|
|
||||||
expect(tester.takeException(), isNull);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('a fetch in flight disables the button', (tester) async {
|
|
||||||
await pump(tester, busy: true);
|
|
||||||
|
|
||||||
final button = tester.widget<OutlinedButton>(
|
|
||||||
find.byType(OutlinedButton),
|
|
||||||
);
|
|
||||||
expect(button.onPressed, isNull);
|
|
||||||
expect(find.text('Loading…'), findsOneWidget);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('the button stays visible with nothing queued', (tester) async {
|
|
||||||
// Never hidden or disabled on an empty queue: the outcome arrives as a
|
|
||||||
// SnackBar so a late delivery can still be retried.
|
|
||||||
await pump(tester);
|
|
||||||
|
|
||||||
expect(find.text("Today's delivery"), findsOneWidget);
|
|
||||||
final button = tester.widget<OutlinedButton>(
|
|
||||||
find.byType(OutlinedButton),
|
|
||||||
);
|
|
||||||
expect(button.onPressed, isNotNull);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('web gets no delivery button at all', (tester) async {
|
|
||||||
// The desktop target *is* the web build, and the caterer's panel blocks
|
|
||||||
// browser requests -- an unguarded button would ship a dead control.
|
|
||||||
await pump(tester, canFetch: false);
|
|
||||||
|
|
||||||
expect(find.byIcon(Icons.restaurant), findsNothing);
|
|
||||||
expect(find.byIcon(Icons.check_circle), findsOneWidget);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('the log button still logs', (tester) async {
|
|
||||||
var logged = 0;
|
|
||||||
await pump(tester, onLog: () => logged++);
|
|
||||||
|
|
||||||
await tester.tap(find.byIcon(Icons.check_circle));
|
|
||||||
await tester.pump();
|
|
||||||
|
|
||||||
expect(logged, 1);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user