diet-guard/app/test/services/day_status_service_test.dart
Krzysztof kuhy Rudnicki f7f06aee53
Some checks failed
pre-commit / pre-commit (push) Has been cancelled
Tests / test (3.10) (push) Has been cancelled
Tests / test (3.11) (push) Has been cancelled
Tests / test (3.12) (push) Has been cancelled
Remove unused features; fix reminder false alarms; share all state
Four asks, plus the fixes two review passes turned up.

Removals (all unused):
- "Repeat last meal" button, and the reward/temptation-bundling prompt it
  was the sole trigger for. Replaced with a post-log progress card showing
  kcal against budget, macros, and the adherence streak.
- The multi-item meal builder, on both the gate and the phone. The stored
  `components` field stays: nothing writes it now, but historical composite
  entries and the food bank still read it and it is part of the wire format.
- Meal photos entirely: 13 files, `imagePath`, the photo filter, the
  wrapper's /blobs/ route, image_picker, and the CAMERA permission.

Reminder false alarms had three real causes, all fixed:
- Logging never reconciled notifications, so a reminder sat on screen up to
  15 minutes after the meal was logged -- on the phone itself.
- The background check never pulled, so a PC-logged meal nagged forever
  until the app was opened. Now mirrors _cli_gate._should_lock: cheap local
  check, network only when a slot looks due, then re-check.
- Meals before 08:00 / after 22:00 were attributed to no slot at all. New
  slot_for_log/slotForLog clamps them to the first/last slot, byte-identical
  across Python and Dart. Deliberately separate from elapsed_slots, which
  would otherwise make every slot fall due at 23:00.

Budget history, so lowering the budget stops rewriting the past:
- Forward-only (effective_from, kcal) entries; each day is judged against
  the budget in force then. Verified live: setting 2000 left a 36-day
  adherence streak and the whole calendar pixel-identical.
- Rides as hist:<date> fields on the existing budget record. crdt_sync
  merges per-field over the union of field names and both devices push the
  merged log, so a device that predates the feature relays them untouched --
  no coordinated rollout, no second document.
- The seed guard tests emptiness, not file presence: _sync can write an
  empty history after merging with a pre-feature peer, and a presence check
  would then never seed again, silently reclassifying every past day.

One shared source of truth for every piece of data:
- Body weight now travels as its own `weight` CRDT field. Inside `value` it
  was collateral damage of whole-map LWW -- any phone budget edit deleted it,
  and the protein target with it.
- Both halves of the food bank sync. The curated half (foods added without
  eating them) was the last device-local state. The derived half merges with
  count as the clock, so LWW means max-count-wins; the log stays
  authoritative for membership, because a CRDT union never shrinks and a
  peer's stale copy would otherwise resurrect a deleted food permanently.
- Bank adapters honour tombstones, and Dart parses bank records defensively:
  a cast failure there is an Error, not an Exception, so one malformed
  remote record would have broken phone sync permanently.

_test_guard.py blocks any pathlib write to real user state while pytest is
in the process, whatever conftest is loaded. A throwaway test run outside
the package conftest destroyed a real GitHub token during this work; this
makes that class of accident impossible rather than merely discouraged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 16:39:52 +02:00

271 lines
7.5 KiB
Dart

import 'package:diet_guard_app/models/day_status.dart';
import 'package:diet_guard_app/models/food_entry.dart';
import 'package:diet_guard_app/services/budget_schedule.dart';
import 'package:diet_guard_app/services/day_status_service.dart';
import 'package:flutter_test/flutter_test.dart';
FoodEntry buildEntry({required double kcal, bool deleted = false}) => FoodEntry(
time: '2026-01-01T12:00:00',
desc: 'test',
grams: 100,
kcal: kcal,
proteinG: 0,
carbsG: 0,
fatG: 0,
source: 'manual',
deleted: deleted,
);
FoodEntry buildMacroEntry({
required double proteinG,
required double carbsG,
required double fatG,
}) => FoodEntry(
time: '2026-01-01T12:00:00',
desc: 'test',
grams: 100,
kcal: 0,
proteinG: proteinG,
carbsG: carbsG,
fatG: fatG,
source: 'manual',
);
/// A schedule where one budget has applied since the beginning of time.
BudgetSchedule flat(int budget) => BudgetSchedule(
[const BudgetEntry(effectiveFrom: kEpochDay, kcal: 0, editedAt: '')]
.map(
(_) => BudgetEntry(
effectiveFrom: kEpochDay,
kcal: budget,
editedAt: '1970-01-01T00:00:00.000Z',
),
)
.toList(),
fallback: budget,
);
void main() {
group('dayTotalKcal', () {
test('sums entries for the day', () {
final log = {
'2026-01-01': [buildEntry(kcal: 100), buildEntry(kcal: 50)],
};
expect(dayTotalKcal(log, '2026-01-01'), 150);
});
test('missing day is zero', () {
expect(dayTotalKcal(<String, List<FoodEntry>>{}, '2026-01-01'), 0);
});
test('excludes tombstoned entries', () {
final log = {
'2026-01-01': [
buildEntry(kcal: 100),
buildEntry(kcal: 50, deleted: true),
],
};
expect(dayTotalKcal(log, '2026-01-01'), 100);
});
});
group('sumMacros', () {
test('sums protein, carbs and fat across entries', () {
final totals = sumMacros([
buildMacroEntry(proteinG: 10, carbsG: 20, fatG: 5),
buildMacroEntry(proteinG: 5, carbsG: 30, fatG: 2.5),
]);
expect(totals.proteinG, 15);
expect(totals.carbsG, 50);
expect(totals.fatG, 7.5);
});
test('is all zeroes for no entries', () {
final totals = sumMacros(const []);
expect(totals.proteinG, 0);
expect(totals.carbsG, 0);
expect(totals.fatG, 0);
});
});
group('dayStatus', () {
test('missing day is notLogged', () {
final log = <String, List<FoodEntry>>{};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.notLogged);
});
test('a day with only tombstoned entries is notLogged, not green', () {
final log = {
'2026-01-01': [buildEntry(kcal: 100, deleted: true)],
};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.notLogged);
});
test('exactly at budget is green', () {
final log = {
'2026-01-01': [buildEntry(kcal: 2000)],
};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.green);
});
test('under budget is green', () {
final log = {
'2026-01-01': [buildEntry(kcal: 1000)],
};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.green);
});
test('just over budget is yellow', () {
final log = {
'2026-01-01': [buildEntry(kcal: 2001)],
};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.yellow);
});
test('exactly at yellow ceiling is yellow', () {
final log = {
'2026-01-01': [buildEntry(kcal: 2400)],
};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.yellow);
});
test('just over yellow ceiling is red', () {
final log = {
'2026-01-01': [buildEntry(kcal: 2400.01)],
};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.red);
});
test('way over budget is red', () {
final log = {
'2026-01-01': [buildEntry(kcal: 5000)],
};
expect(dayStatus(log, '2026-01-01', 2000), DayStatus.red);
});
});
group('statusMap', () {
test('maps every present day', () {
final log = {
'2026-01-01': [buildEntry(kcal: 1000)],
'2026-01-02': [buildEntry(kcal: 5000)],
};
expect(statusMap(log, schedule: flat(2000)), {
'2026-01-01': DayStatus.green,
'2026-01-02': DayStatus.red,
});
});
test('empty log is empty map', () {
final log = <String, List<FoodEntry>>{};
expect(statusMap(log, schedule: flat(2000)), <String, DayStatus>{});
});
});
group('loggingStreak', () {
test('empty map is zero', () {
expect(loggingStreak({}, today: DateTime(2026, 1, 5)), 0);
});
test('counts consecutive logged days including today', () {
final sm = {
'2026-01-03': DayStatus.green,
'2026-01-04': DayStatus.red,
'2026-01-05': DayStatus.yellow,
};
expect(loggingStreak(sm, today: DateTime(2026, 1, 5)), 3);
});
test('breaks on a gap', () {
final sm = {
'2026-01-01': DayStatus.green,
'2026-01-03': DayStatus.green,
'2026-01-04': DayStatus.green,
};
expect(loggingStreak(sm, today: DateTime(2026, 1, 4)), 2);
});
test('today not logged is not a break', () {
final sm = {
'2026-01-03': DayStatus.green,
'2026-01-04': DayStatus.yellow,
};
expect(loggingStreak(sm, today: DateTime(2026, 1, 5)), 2);
});
test('yesterday not logged breaks streak even if today is', () {
final sm = {
'2026-01-03': DayStatus.green,
'2026-01-05': DayStatus.green,
};
expect(loggingStreak(sm, today: DateTime(2026, 1, 5)), 1);
});
});
group('adherenceStreak', () {
test('counts consecutive green and yellow', () {
final sm = {
'2026-01-03': DayStatus.green,
'2026-01-04': DayStatus.yellow,
'2026-01-05': DayStatus.green,
};
expect(adherenceStreak(sm, today: DateTime(2026, 1, 5)), 3);
});
test('red today breaks the streak immediately', () {
final sm = {
'2026-01-04': DayStatus.green,
'2026-01-05': DayStatus.red,
};
expect(adherenceStreak(sm, today: DateTime(2026, 1, 5)), 0);
});
test('not-logged today is not a break', () {
final sm = {'2026-01-04': DayStatus.green};
expect(adherenceStreak(sm, today: DateTime(2026, 1, 5)), 1);
});
test('red in history breaks the streak', () {
final sm = {
'2026-01-03': DayStatus.green,
'2026-01-04': DayStatus.red,
'2026-01-05': DayStatus.green,
};
expect(adherenceStreak(sm, today: DateTime(2026, 1, 5)), 1);
});
});
group('yearToDateTally', () {
test('counts logged and adherent days this year only', () {
final sm = {
'2026-01-01': DayStatus.green,
'2026-01-02': DayStatus.red,
'2026-01-03': DayStatus.notLogged,
'2025-12-31': DayStatus.green,
};
final tally = yearToDateTally(sm, today: DateTime(2026, 1, 3));
expect(tally.loggedDays, 2);
expect(tally.elapsedDays, 3);
expect(tally.adherentDays, 1);
});
test('future day in map is excluded', () {
final sm = {
'2026-01-01': DayStatus.green,
'2026-06-01': DayStatus.green,
};
final tally = yearToDateTally(sm, today: DateTime(2026));
expect(tally.loggedDays, 1);
expect(tally.elapsedDays, 1);
expect(tally.adherentDays, 1);
});
test('empty map', () {
final tally = yearToDateTally({}, today: DateTime(2026, 3));
expect(tally.loggedDays, 0);
expect(tally.elapsedDays, 60);
expect(tally.adherentDays, 0);
});
});
}