diet-guard/diet_guard/tests/test_kuchnia_ids.py
Krzysztof kuhy Rudnicki 03b6a455cb Auto-log due catering before the lock
kuhy's 2026-11-17 override of "delivered is not eaten": before the lock
opens, the PC logs the due slots' dishes by id uuid5(kuchnia📅key),
shared with the phone (fixture-pinned).

Failure paths:
- fetch error, no credentials, > 5 s: fail-closed, nothing is logged,
  the lock opens as before, reason emitted and logged.
- log write error (OSError/ValueError): fail-closed, same.
- gate --check: never logs (a decision, not a write).
- an undone (tombstoned) entry counts as logged: fail-closed against
  resurrecting a meal kuhy removed.
- publish outage after a clearing auto-log: fail-open by design, the
  local entry stays and the reason is emitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
2026-11-17 21:42:34 +01:00

46 lines
1.4 KiB
Python

"""The auto-log entry id: one formula, pinned by the phone's shared fixture."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from diet_guard._kuchnia_ids import autolog_id, dish_key
from diet_guard._kuchnia_parse import parse_menu
FIXTURE = (
Path(__file__).resolve().parents[2] / "tests/fixtures/kuchnia_autolog_ids.json"
)
_CASES = json.loads(FIXTURE.read_text(encoding="utf-8"))["cases"]
@pytest.mark.parametrize("case", _CASES, ids=lambda c: c["name_string"])
def test_ids_match_the_shared_fixture(case: dict[str, object]) -> None:
"""Key and id equal what the Dart port derives from the same input."""
key = dish_key(case["delivery_meal_id"], str(case["name"]))
assert key == case["dish_key"]
assert autolog_id(str(case["delivery_date"]), key) == case["expected_id"]
def test_parser_carries_the_panel_meal_id() -> None:
"""``Dish.dish_key`` comes from ``deliveryMealId``, else the name."""
meal = {
"menuMealName": "Zupa",
"mealPriority": 1,
"nutrition": {
"calories": 200.0,
"protein": 10.0,
"carbohydrate": 25.0,
"fat": 6.0,
"weight": 300.0,
},
}
with_id, without = (
parse_menu({"deliveryMenuMeal": [{**meal, **extra}]})[0]
for extra in ({"deliveryMealId": 42}, {})
)
assert with_id.dish_key == "42"
assert without.dish_key == "zupa"