diet-guard/diet_guard/tests/test_slots_nearest.py
Krzysztof kuhy Rudnicki 8bc1812147 slots: minute-of-day contract (phase 1 of 3: pure model + shared fixture)
Meal-slot checkpoints move from whole hours to minute-of-day ints
(0..1439) in the pure model and slot arithmetic of both languages:
MealSchedule(first_minute, last_minute, count) / (firstMinute:,
lastMinute:, count:), interior slots snapped to the 15-minute grid,
endpoints exact, integer-only. New: nearest_slot, satisfied_slots,
slot_fields / entry_slot_minute (additive "slot_min" wire field) and
schedule_to_wire / schedule_from_wire ("fm"/"lm" only when off the hour,
so whole-hour data stays byte-identical), parse_hhmm.

tests/fixtures/meal_schedule_vectors.json (scripts/
build_meal_schedule_fixture.py) is read by both suites; kuchnia_day.json
slot values regenerated as minutes.

Consumers (CLI, gate, storage, sync, app screens, kuchnia, notifications)
are intentionally broken until phase 2. patch-targets skipped: it fails
only because diet_guard._gatelock no longer imports.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015npsYVB2PdtWkG7MPAi4tn
2026-10-10 19:22:43 +02:00

55 lines
2.2 KiB
Python

"""Tests for _slots.nearest_slot / satisfied_slots.
How a recorded slot minute -- possibly written under an older schedule, or as a
legacy whole hour -- counts toward today's checkpoints. Split out of
test_slots.py for the repo's 250-line cap.
"""
from __future__ import annotations
from diet_guard._meal_schedule import DEFAULT_SCHEDULE, MealSchedule
from diet_guard._slots import nearest_slot, satisfied_slots
class TestNearestSlot:
"""The closest slot, ties to the earlier."""
def test_exact_slot_is_itself(self) -> None:
"""A minute that is a slot maps to that slot."""
assert nearest_slot(720, DEFAULT_SCHEDULE) == 720
def test_exact_halfway_goes_to_the_earlier_slot(self) -> None:
"""10:00 is equidistant from 08:00 and 12:00; the earlier wins."""
assert nearest_slot(600, DEFAULT_SCHEDULE) == 480
assert nearest_slot(601, DEFAULT_SCHEDULE) == 720
def test_outside_the_window_clamps_to_the_ends(self) -> None:
"""Before the first slot and after the last, the end slots win."""
assert nearest_slot(-60, DEFAULT_SCHEDULE) == 480
assert nearest_slot(1800, DEFAULT_SCHEDULE) == 1200
class TestSatisfiedSlots:
"""Which checkpoints a day's recorded slot minutes cover."""
def test_empty_input_satisfies_nothing(self) -> None:
"""No meals, no satisfied slots."""
assert satisfied_slots([], DEFAULT_SCHEDULE) == set()
def test_meals_survive_a_schedule_move(self) -> None:
"""The user's case: logged at 07:00 and 10:00, then moved to 07:15.
Under 07:15-19:00 x5 the slots are 07:15, 10:15, 13:15, 16:00, 19:00;
each old meal lands on its own nearest slot, so both still count.
"""
moved = MealSchedule(435, 1140, 5)
assert satisfied_slots([420, 600], moved) == {435, 615}
def test_two_meals_on_one_slot_satisfy_only_that_slot(self) -> None:
"""A double breakfast does not also clear lunch."""
assert satisfied_slots([480, 540], DEFAULT_SCHEDULE) == {480}
def test_legacy_hour_entries_map_onto_five_meals(self) -> None:
"""Entries tagged 08 and 12 under four meals, read under five."""
assert satisfied_slots([480, 720], MealSchedule(480, 1200, 5)) == {480, 660}