diet-guard/app/test/widgets/average_summary_row_test.dart
Krzysztof kuhy Rudnicki e07284d3e1 Show weekly and monthly average intake on both devices
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
2026-08-09 14:59:40 +02:00

68 lines
1.9 KiB
Dart

import 'package:diet_guard_app/models/period_average.dart';
import 'package:diet_guard_app/widgets/average_summary_row.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
PeriodAverage period({double? avgKcal, AverageBand? band}) => PeriodAverage(
start: '2026-01-05',
end: '2026-01-11',
loggedDays: 7,
elapsedDays: 7,
avgKcal: avgKcal,
avgBudget: 2000,
band: band,
);
Future<void> pump(WidgetTester tester, Widget child) =>
tester.pumpWidget(MaterialApp(home: Scaffold(body: child)));
void main() {
testWidgets('renders both averages and their bands', (tester) async {
await pump(
tester,
AverageSummaryRow(
week: period(avgKcal: 2100, band: AverageBand.slightlyOver),
month: period(avgKcal: 1900, band: AverageBand.under),
),
);
expect(
find.text(
'Avg/day (to yesterday): week 2100 kcal (slightly over) · '
'month 1900 kcal (under)',
),
findsOneWidget,
);
});
testWidgets('rounds the average to whole kcal', (tester) async {
await pump(
tester,
AverageSummaryRow(
week: period(avgKcal: 2100.6, band: AverageBand.slightlyOver),
month: period(avgKcal: 1900, band: AverageBand.under),
),
);
expect(find.textContaining('week 2101 kcal'), findsOneWidget);
});
testWidgets('an empty period reads "no data", never 0 kcal', (tester) async {
await pump(
tester,
AverageSummaryRow(week: period(), month: period()),
);
expect(find.textContaining('0 kcal'), findsNothing);
expect(find.textContaining('week no data'), findsOneWidget);
});
testWidgets('the placeholder empty period renders safely', (tester) async {
await pump(
tester,
const AverageSummaryRow(
week: PeriodAverage.empty,
month: PeriodAverage.empty,
),
);
expect(find.textContaining('no data'), findsOneWidget);
});
}