mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-07-04 13:23:11 +02:00
Add the diet_guard package: a screen-locking meal-logging gate that fires on 4-hour slots (08/12/16/20) and records calories/macros, persisting an autocompleting food bank. - Trigger fix: the systemd timer fires at session start (Persistent=true) before lightdm has written ~/.Xauthority, so the gate crashed with a TclError instead of locking the screen. Add wait_for_display() / _display_is_ready() in _gatelock.py and wire it into _cli._cmd_gate so the gate retries on the next tick instead of crashing; add Environment=XAUTHORITY=%h/.Xauthority to the service as belt-and-suspenders. - Food-bank hardening: a transiently corrupt food_bank.json was warned about on every keystroke and then silently overwritten (data loss). _read_bank now quarantines it via _quarantine_corrupt_bank() (warn-once + timestamped backup) before starting fresh. - Multi-item meals: new _meal.py (MealItem, meal_total, MEAL_SOURCE), remember_meal() + _upsert() in _foodbank.py, and a "+ Add item" control in the gate that logs both the individual items and the composite meal. - Bundle resolve_nutrition's manual macros into a ManualMacros dataclass to stay within the argument-count limit. diet_guard at 100% branch coverage; full pre-commit suite passes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
"""Tests for _meal.py — composite-meal summing."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from python_pkg.diet_guard._estimator import Nutrition
|
|
from python_pkg.diet_guard._meal import MEAL_SOURCE, MealItem, meal_total
|
|
|
|
|
|
def _item(
|
|
name: str,
|
|
kcal: float,
|
|
macros: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 0.0),
|
|
) -> MealItem:
|
|
"""Build a MealItem from a name, calories, and a (protein, carbs, fat, grams)."""
|
|
protein, carbs, fat, grams = macros
|
|
return MealItem(
|
|
name,
|
|
Nutrition(
|
|
kcal=kcal,
|
|
protein_g=protein,
|
|
carbs_g=carbs,
|
|
fat_g=fat,
|
|
grams=grams,
|
|
source="manual",
|
|
),
|
|
)
|
|
|
|
|
|
class TestMealTotal:
|
|
"""Summing a meal's items."""
|
|
|
|
def test_sums_every_field(self) -> None:
|
|
"""Each macro, calories, and weight are added across the items."""
|
|
items = [
|
|
_item("salad", 80, (2, 8, 5, 120)),
|
|
_item("chicken", 330, (62, 0, 7, 200)),
|
|
_item("rice", 260, (5, 56, 1, 180)),
|
|
]
|
|
total = meal_total(items)
|
|
assert total.kcal == 670
|
|
assert total.protein_g == 69
|
|
assert total.carbs_g == 64
|
|
assert total.fat_g == 13
|
|
assert total.grams == 500
|
|
assert total.source == MEAL_SOURCE
|
|
|
|
def test_empty_is_zero(self) -> None:
|
|
"""An empty meal sums to an all-zero composite rather than raising."""
|
|
assert meal_total([]) == Nutrition(
|
|
kcal=0.0,
|
|
protein_g=0.0,
|
|
carbs_g=0.0,
|
|
fat_g=0.0,
|
|
grams=0.0,
|
|
source=MEAL_SOURCE,
|
|
)
|
|
|
|
def test_rounds_to_one_decimal(self) -> None:
|
|
"""Floating sums are rounded to 0.1, like the rest of the log."""
|
|
assert meal_total([_item("a", 0.1), _item("b", 0.2)]).kcal == 0.3
|