mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-24 19:06:20 +01:00
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
148 lines
5.9 KiB
Python
148 lines
5.9 KiB
Python
"""The Python half of the cross-language catering parity gate.
|
|
|
|
``app/test/kuchnia_parity_test.dart`` asserts the *same* expectations against
|
|
the *same* ``tests/fixtures/kuchnia_day.json``. Two independently written
|
|
suites from the same prose is not a gate -- one shared input with one shared
|
|
expected result is, because a divergence has to show up as a failure on one
|
|
side rather than as two self-consistent implementations.
|
|
|
|
What the parity actually protects, per ``docs/kuchnia-wikinga.md``:
|
|
|
|
* **Slot assignment.** A slot one device offers while the other does not is a
|
|
checkpoint that can never be satisfied -- a permanent lock.
|
|
* **Which dishes are dropped.** If the two sides disagree, each re-adds what
|
|
the other dropped, ``add_manual_entry`` restamps ``t`` unconditionally, and
|
|
the curated bank republishes to every peer on every refresh.
|
|
* **Bank keys and record values.** Divergence there is the same flood by a
|
|
different route.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from diet_guard._kuchnia_import import dish_to_record
|
|
from diet_guard._kuchnia_parse import Dish, parse_menu
|
|
from diet_guard._kuchnia_spread import assign_slots
|
|
|
|
FIXTURE = Path(__file__).resolve().parents[2] / "tests/fixtures/kuchnia_day.json"
|
|
|
|
|
|
@pytest.fixture(name="fixture")
|
|
def _fixture() -> dict[str, Any]:
|
|
"""The shared payload/expected pair, read from the committed JSON.
|
|
|
|
Typed ``Any`` deliberately: this is decoded JSON whose nested shape is the
|
|
fixture's own contract, and every read below is an assertion about that
|
|
shape. Threading precise types through would restate the fixture's schema
|
|
in the type system without making a divergence any more visible.
|
|
"""
|
|
with FIXTURE.open(encoding="utf-8") as handle:
|
|
data: dict[str, Any] = json.load(handle)
|
|
return data
|
|
|
|
|
|
@pytest.fixture(name="dishes")
|
|
def _dishes(fixture: dict[str, Any]) -> list[Dish]:
|
|
"""The dishes the parser extracts from the shared payload."""
|
|
return parse_menu(fixture["payload"])
|
|
|
|
|
|
def _slots_for(key: str) -> tuple[int, ...]:
|
|
"""Turn a fixture slot key such as ``"8,12,16,20"`` into slot hours."""
|
|
return tuple(int(part) for part in key.split(","))
|
|
|
|
|
|
def test_fixture_is_present() -> None:
|
|
"""The fixture is shared with ``flutter test``; a move breaks both silently.
|
|
|
|
Asserted explicitly so a relocation fails here with a clear reason rather
|
|
than as a confusing KeyError inside another test.
|
|
"""
|
|
assert FIXTURE.is_file(), f"shared parity fixture missing at {FIXTURE}"
|
|
|
|
|
|
def test_parsed_dishes_match_expected(
|
|
fixture: dict[str, Any], dishes: list[Dish]
|
|
) -> None:
|
|
"""Every kept dish matches the shared expectation field for field."""
|
|
expected = fixture["expected"]["dishes"]
|
|
actual = [
|
|
{
|
|
"name": dish.name,
|
|
"kcal": dish.kcal,
|
|
"protein_g": dish.protein_g,
|
|
"carbs_g": dish.carbs_g,
|
|
"fat_g": dish.fat_g,
|
|
"grams": dish.grams,
|
|
"priority": dish.priority,
|
|
"slot_label": dish.slot_label,
|
|
}
|
|
for dish in dishes
|
|
]
|
|
assert actual == expected
|
|
|
|
|
|
def test_dropped_count_matches(fixture: dict[str, Any], dishes: list[Dish]) -> None:
|
|
"""The same meals are refused on both sides.
|
|
|
|
A vacuous assertion against a clean capture, which is why the fixture
|
|
carries a per-100 g mix-up, an absurd portion, a stringly-typed number, a
|
|
non-dict entry and a meal with no nutrition at all.
|
|
"""
|
|
total = len(fixture["payload"]["deliveryMenuMeal"])
|
|
assert total - len(dishes) == fixture["expected"]["dropped_count"]
|
|
assert fixture["expected"]["dropped_count"] > 0
|
|
|
|
|
|
def test_slot_assignment_matches(fixture: dict[str, Any], dishes: list[Dish]) -> None:
|
|
"""``i * S // N`` lands each dish on the slot the Dart side also picks."""
|
|
for key, expected in fixture["expected"]["slots"].items():
|
|
subject = dishes[:3] if key.startswith("first_three_") else dishes
|
|
hours = _slots_for(key.removeprefix("first_three_"))
|
|
actual = [item.slot for item in assign_slots(subject, hours)]
|
|
assert actual == expected, f"slot assignment diverged for {key}"
|
|
|
|
|
|
def test_slot_order_matches(fixture: dict[str, Any], dishes: list[Dish]) -> None:
|
|
"""Ordering is total, so the twin-dish pair cannot reshuffle between runs.
|
|
|
|
Python's ``sorted`` is stable and Dart's ``List.sort`` is not, so two
|
|
dishes sharing both priority and name are the case that forces both
|
|
comparators to be total.
|
|
"""
|
|
ordered = assign_slots(dishes, (8, 12, 16, 20))
|
|
assert [item.dish.name for item in ordered] == fixture["expected"]["slot_order"]
|
|
|
|
|
|
def test_bank_keys_match(fixture: dict[str, Any], dishes: list[Dish]) -> None:
|
|
"""Both devices key the curated bank identically for these dish names."""
|
|
keys = [dish.name.strip().casefold() for dish in dishes]
|
|
assert keys == fixture["expected"]["bank_keys"]
|
|
|
|
|
|
def test_bank_records_match(fixture: dict[str, Any], dishes: list[Dish]) -> None:
|
|
"""A banked record is value-identical to the one Dart would write."""
|
|
records = [dish_to_record(dish) for dish in dishes]
|
|
assert records == fixture["expected"]["bank_records"]
|
|
|
|
|
|
def test_bank_record_numbers_are_floats(dishes: list[Dish]) -> None:
|
|
"""Macros bank as floats, so Dart must ``.toDouble()`` before encoding.
|
|
|
|
``jsonEncode(435)`` emits ``435`` where Python emits ``435.0``. Left
|
|
unpinned, an int-typed Dart macro produces a byte-different record for the
|
|
same dish, every refresh sees a change, and the curated bank republishes to
|
|
every peer -- the same flood a tolerance mismatch causes.
|
|
"""
|
|
for record in (dish_to_record(dish) for dish in dishes):
|
|
for key in ("kcal", "protein_g", "carbs_g", "fat_g", "grams"):
|
|
assert isinstance(record[key], float), f"{key} must bank as a float"
|
|
# ``count`` is the exception: an int on both sides.
|
|
assert isinstance(record["count"], int)
|
|
assert not isinstance(record["count"], bool)
|