diet-guard/app/lib/models/kuchnia_dish.dart
Krzysztof kuhy Rudnicki 24be05b9c3 feat: mirror the catering parser and slot spread in Dart, gated by one fixture
The phone is getting its own Kuchnia Wikinga importer so the feature works
with the PC switched off. That turns a risk `_kuchnia_spread.py` called
"theoretical" into a live one: two implementations must now agree exactly on
which dishes are dropped, which slot each lands on, and the JSON a banked
record encodes to. Disagreement means each device re-adds what the other
dropped, and since `add_manual_entry` restamps `t` unconditionally, the whole
curated bank republishes to every peer on every refresh. A slot mismatch is
worse -- a checkpoint one device offers and the other does not can never be
satisfied.

So the port lands with its gate rather than after it. One shared fixture,
`tests/fixtures/kuchnia_day.json`, is asserted by both `pytest` and
`flutter test`: same input, same expected dishes, drops, slots, bank keys and
records. Two suites written independently from the same prose is not a gate.

The fixture is five real dishes (a scrubbed 2026-08-22 capture) plus synthetic
meals that each pin one drop or fallback path -- a per-100 g mix-up, an absurd
portion, a stringly-typed number, a non-dict entry, a missing `nutrition`, a
missing `mealPriority`, and a twin-dish pair. Without those, "the same meals
are dropped" is a vacuous assertion against an all-happy-path capture. Both
halves were mutation-checked: flipping an expected slot, drop count or record
value fails each suite, and flipping `435.0` to `435` fails the byte-identity
test while the value comparison still passes, which is why both exist.

Three traps the port had to survive, each caught before it shipped:

- `_ENERGY_TOLERANCE` is **0.35**. The prose said "~1%", which describes how
  closely the captured day agreed, not the threshold; porting 0.01 would drop
  dishes the PC keeps. That paragraph now says so.
- `jsonEncode(435)` emits `435` where Python emits `435.0`, so every Dart
  macro is forced to `double` and a canonical-encoding test pins it.
- Dart's `List.sort` is unstable where Python's `sorted` is stable, so the
  Dart comparator carries the payload index as a final tiebreak.

The real dishes live in their own file rather than as a literal in the
generator: transcribing them by hand corrupted three of the five, and the
error was invisible until the generated fixture was diffed against the
capture.

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

83 lines
2.7 KiB
Dart

/// One delivered catering dish, with its portion macros.
///
/// The Dart mirror of `diet_guard/_kuchnia_parse.py`'s `Dish`. Every macro is
/// the total *for this portion*, not per 100 g -- the same convention
/// [Nutrition] uses.
///
/// KEEP IN SYNC WITH `diet_guard/_kuchnia_parse.py`. Both sides are gated by
/// `tests/fixtures/kuchnia_day.json`; see `kuchnia_parity_test.dart`.
library;
/// A dish the caterer delivered on a given day.
class KuchniaDish {
/// Creates a dish from its already-validated fields.
const KuchniaDish({
required this.name,
required this.kcal,
required this.proteinG,
required this.carbsG,
required this.fatG,
required this.grams,
required this.priority,
required this.slotLabel,
});
/// The dish itself (`menuMealName`), e.g. "Kaszotto grzybowe...".
///
/// Not `mealName`, which is the slot label ("Kolacja") and would collapse
/// every Monday dinner onto one food-bank entry.
final String name;
/// Calories for this portion.
final double kcal;
/// Protein in grams, for this portion.
final double proteinG;
/// Carbohydrate in grams, for this portion.
final double carbsG;
/// Fat in grams, for this portion.
final double fatG;
/// The portion weight in grams (`nutrition.weight`).
final double grams;
/// The caterer's own eating order, 1..N.
///
/// Falls back to the dish's 1-based position in the payload when
/// `mealPriority` is missing.
final int priority;
/// The caterer's slot label ("Śniadanie", "Obiad", ...), for display only.
final String slotLabel;
/// The curated-bank record for this dish.
///
/// Mirrors `_kuchnia_import.dish_to_record`. `count` is 0: the bank's count
/// ranks foods by how often they were *eaten*, and a delivered dish has not
/// been eaten yet.
///
/// Every macro is forced to `double` before it reaches `jsonEncode`.
/// `jsonEncode(435)` emits `435` where Python emits `435.0`, and a
/// byte-different record for the same dish makes every refresh look like a
/// change -- which republishes the whole curated bank to every peer.
Map<String, dynamic> toBankRecord() => {
'desc': name,
'kcal': kcal.toDouble(),
'protein_g': proteinG.toDouble(),
'carbs_g': carbsG.toDouble(),
'fat_g': fatG.toDouble(),
'grams': grams.toDouble(),
'count': 0,
};
/// The curated bank's key for this dish.
///
/// Python uses `str.casefold()` and this uses `String.toLowerCase()`. They
/// agree across the whole Polish alphabet and diverge only on `ß`,
/// ligatures and final sigma; rekeying to unify them would strand every
/// existing entry, so the agreement is pinned by test instead.
String get bankKey => name.trim().toLowerCase();
}