mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 13:07:40 +01:00
The calendar already said which individual days went over budget, but not whether the week as a whole did. A run of small overages reads as four harmless yellow squares; averaged, it is the thing that actually moves the needle. Three rules are load-bearing, and are documented in CLAUDE.md because getting any of them wrong silently changes what "under budget" means: - The denominator is logged days, not elapsed days. An unlogged day is not a zero-kcal day, and averaging gaps in as zeros would make a badly-tracked week look like the best week of the year. - The yardstick is the mean of the per-day budgets over those same days, resolved through BudgetSchedule -- never today's budget, which would retroactively reclassify past weeks on every budget edit. - Today is excluded. A half-logged today drags the mean far enough to flip the band, so every period ends at yesterday. Band boundaries reuse the calendar's own OVER_BUDGET_YELLOW_CEILING by import rather than by copy, so a period and its days can never disagree about what "over" means; that constant loses its underscore to say so. The MCP get_averages tool returns the average and the band but drops the average budget, keeping the module's existing invariant that the budget number never leaves this machine. The CLI, the gate and the phone all show both, since it is hidden from the network, not from the user. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DjJqpcEsaV5WRhPXyxCThd
235 lines
7.0 KiB
Dart
235 lines
7.0 KiB
Dart
import 'package:diet_guard_app/models/food_entry.dart';
|
|
import 'package:diet_guard_app/models/period_average.dart';
|
|
import 'package:diet_guard_app/services/average_service.dart';
|
|
import 'package:diet_guard_app/services/budget_schedule.dart';
|
|
import 'package:diet_guard_app/services/log_storage_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,
|
|
);
|
|
|
|
BudgetSchedule flat(int budget) => BudgetSchedule(
|
|
[BudgetEntry(effectiveFrom: kEpochDay, kcal: budget, editedAt: '')],
|
|
fallback: budget,
|
|
);
|
|
|
|
DayLog logOf(Map<String, double> kcalByDay) => {
|
|
for (final e in kcalByDay.entries) e.key: [buildEntry(kcal: e.value)],
|
|
};
|
|
|
|
void main() {
|
|
group('bandFor', () {
|
|
test('exactly at budget is under', () {
|
|
expect(bandFor(2000, 2000), AverageBand.under);
|
|
});
|
|
test('one over is slightly over', () {
|
|
expect(bandFor(2001, 2000), AverageBand.slightlyOver);
|
|
});
|
|
test('exactly at the 120% ceiling is slightly over', () {
|
|
expect(bandFor(2400, 2000), AverageBand.slightlyOver);
|
|
});
|
|
test('just past the ceiling is very over', () {
|
|
expect(bandFor(2400.01, 2000), AverageBand.veryOver);
|
|
});
|
|
});
|
|
|
|
group('bandLabel', () {
|
|
test('mirrors the Python labels exactly', () {
|
|
expect(bandLabel(null), 'no data');
|
|
expect(bandLabel(AverageBand.under), 'under');
|
|
expect(bandLabel(AverageBand.slightlyOver), 'slightly over');
|
|
expect(bandLabel(AverageBand.veryOver), 'very over');
|
|
});
|
|
});
|
|
|
|
group('bounds', () {
|
|
test('week starts on Monday', () {
|
|
// 2026-01-07 is a Wednesday.
|
|
final (start, end) = weekBounds(DateTime(2026, 1, 7));
|
|
expect(start, DateTime(2026, 1, 5));
|
|
expect(end, DateTime(2026, 1, 11));
|
|
});
|
|
test('a Sunday looks back to its own Monday', () {
|
|
final (start, _) = weekBounds(DateTime(2026, 1, 11));
|
|
expect(start, DateTime(2026, 1, 5));
|
|
});
|
|
test('month bounds cover a short February', () {
|
|
final (start, end) = monthBounds(DateTime(2026, 2, 14));
|
|
expect(start, DateTime(2026, 2));
|
|
expect(end, DateTime(2026, 2, 28));
|
|
});
|
|
test('month bounds cover a leap February', () {
|
|
final (_, end) = monthBounds(DateTime(2024, 2, 14));
|
|
expect(end, DateTime(2024, 2, 29));
|
|
});
|
|
test('lastCompleteDay is yesterday', () {
|
|
expect(lastCompleteDay(DateTime(2026, 3)), DateTime(2026, 2, 28));
|
|
});
|
|
test('shiftMonths wraps the year', () {
|
|
expect(shiftMonths(DateTime(2026, 1, 20), 1), DateTime(2025, 12));
|
|
});
|
|
});
|
|
|
|
group('periodAverage', () {
|
|
test('averages over logged days only, not elapsed days', () {
|
|
final log = logOf({
|
|
'2026-01-05': 2000,
|
|
'2026-01-06': 3000,
|
|
'2026-01-09': 4000,
|
|
});
|
|
final result = periodAverage(
|
|
log,
|
|
schedule: flat(2500),
|
|
start: DateTime(2026, 1, 5),
|
|
end: DateTime(2026, 1, 9),
|
|
);
|
|
expect(result.avgKcal, 3000);
|
|
expect(result.loggedDays, 3);
|
|
expect(result.elapsedDays, 5);
|
|
});
|
|
|
|
test('tombstoned entries do not make a day count as logged', () {
|
|
final log = <String, List<FoodEntry>>{
|
|
'2026-01-05': [buildEntry(kcal: 9000, deleted: true)],
|
|
};
|
|
final result = periodAverage(
|
|
log,
|
|
schedule: flat(2000),
|
|
start: DateTime(2026, 1, 5),
|
|
end: DateTime(2026, 1, 5),
|
|
);
|
|
expect(result.loggedDays, 0);
|
|
expect(result.avgKcal, isNull);
|
|
});
|
|
|
|
test('an empty period has a null average, not zero', () {
|
|
final result = periodAverage(
|
|
const {},
|
|
schedule: flat(2000),
|
|
start: DateTime(2026, 1, 5),
|
|
end: DateTime(2026, 1, 11),
|
|
);
|
|
expect(result.avgKcal, isNull);
|
|
expect(result.band, isNull);
|
|
expect(result.elapsedDays, 7);
|
|
});
|
|
|
|
test('end before start is zero elapsed, not negative', () {
|
|
final result = periodAverage(
|
|
const {},
|
|
schedule: flat(2000),
|
|
start: DateTime(2026, 1, 5),
|
|
end: DateTime(2026, 1, 4),
|
|
);
|
|
expect(result.elapsedDays, 0);
|
|
});
|
|
|
|
test('the budget is averaged per logged day, not taken from today', () {
|
|
final schedule = BudgetSchedule([
|
|
BudgetEntry(effectiveFrom: kEpochDay, kcal: 1000, editedAt: ''),
|
|
BudgetEntry(effectiveFrom: '2026-01-06', kcal: 3000, editedAt: ''),
|
|
], fallback: 1000);
|
|
final log = logOf({'2026-01-05': 2000, '2026-01-06': 2000});
|
|
final result = periodAverage(
|
|
log,
|
|
schedule: schedule,
|
|
start: DateTime(2026, 1, 5),
|
|
end: DateTime(2026, 1, 6),
|
|
);
|
|
expect(result.avgBudget, 2000);
|
|
expect(result.band, AverageBand.under);
|
|
});
|
|
});
|
|
|
|
group('weeklyAverage', () {
|
|
test('the current week stops at yesterday', () {
|
|
final log = logOf({
|
|
'2026-01-05': 3000,
|
|
'2026-01-06': 3000,
|
|
'2026-01-07': 3000,
|
|
'2026-01-08': 100,
|
|
});
|
|
final result = weeklyAverage(
|
|
log,
|
|
schedule: flat(2000),
|
|
today: DateTime(2026, 1, 8),
|
|
);
|
|
expect(result.avgKcal, 3000);
|
|
expect(result.end, '2026-01-07');
|
|
expect(result.band, AverageBand.veryOver);
|
|
});
|
|
|
|
test('Monday has no complete days this week', () {
|
|
final result = weeklyAverage(
|
|
logOf({'2026-01-05': 3000}),
|
|
schedule: flat(2000),
|
|
today: DateTime(2026, 1, 5),
|
|
);
|
|
expect(result.elapsedDays, 0);
|
|
expect(result.avgKcal, isNull);
|
|
});
|
|
|
|
test('the previous week is whole', () {
|
|
final result = weeklyAverage(
|
|
logOf({'2026-01-05': 2100, '2026-01-11': 2100}),
|
|
schedule: flat(2000),
|
|
weeksAgo: 1,
|
|
today: DateTime(2026, 1, 14),
|
|
);
|
|
expect(result.start, '2026-01-05');
|
|
expect(result.end, '2026-01-11');
|
|
expect(result.elapsedDays, 7);
|
|
expect(result.band, AverageBand.slightlyOver);
|
|
});
|
|
});
|
|
|
|
group('monthlyAverage', () {
|
|
test('the current month stops at yesterday', () {
|
|
final log = logOf({
|
|
'2026-03-01': 1000,
|
|
'2026-03-02': 1000,
|
|
'2026-03-04': 9000,
|
|
});
|
|
final result = monthlyAverage(
|
|
log,
|
|
schedule: flat(2000),
|
|
today: DateTime(2026, 3, 4),
|
|
);
|
|
expect(result.avgKcal, 1000);
|
|
expect(result.end, '2026-03-03');
|
|
expect(result.band, AverageBand.under);
|
|
});
|
|
|
|
test('the first of the month has no complete days', () {
|
|
final result = monthlyAverage(
|
|
const {},
|
|
schedule: flat(2000),
|
|
today: DateTime(2026, 3),
|
|
);
|
|
expect(result.elapsedDays, 0);
|
|
});
|
|
|
|
test('the previous month is whole', () {
|
|
final result = monthlyAverage(
|
|
logOf({'2026-02-10': 2500}),
|
|
schedule: flat(2000),
|
|
monthsAgo: 1,
|
|
today: DateTime(2026, 3, 10),
|
|
);
|
|
expect(result.start, '2026-02-01');
|
|
expect(result.end, '2026-02-28');
|
|
expect(result.elapsedDays, 28);
|
|
expect(result.band, AverageBand.veryOver);
|
|
});
|
|
});
|
|
}
|