diet-guard/app/test/screens/history/history_screen_test.dart
Krzysztof kuhy Rudnicki 895a87cf86 Split history_screen and its test into widget siblings
history_screen.dart (1052) and history_screen_test.dart (1017) were a third of
the remaining over-cap work. Widget extraction only -- no route changes, since
routes are user-visible and repo_invariants_test.dart pins the navigation
surface.

  history_screen.dart          1052 -> 211
  lib/widgets/history/
    history_filter.dart         102  filter criteria + sort enum (pure data)
    history_grouping.dart       206  HistoryItem tree, apply/group/format
    history_list.dart           194  GroupedList, DayHeaderTile, EntryTile
    history_filter_sheet.dart   242  the sheet itself
    history_filter_fields.dart  168  search field, slider labels, range row
    history_filter_sort.dart    140  source chips + sort controls

Names that now cross a file boundary became public (HistoryItem, DayHeader,
EntryRow, buildGroupedItems, formatDay, dateRangeLabel, the tile widgets), each
with the doc comment public_member_api_docs requires -- 49 of them, written
rather than suppressed.

The four macro range sliders (kcal/protein/carbs/fat) were four near-identical
30-line blocks differing only in label, key, unit and which HistoryFilter pair
they read/write. They are now one FilterRangeRow taking min/max plus an
onChanged, which is most of how the sheet got under the cap.

The test file mirrors the source split one-for-one, six files under
test/screens/history/. Its setUp/tearDown and the settle() helper (needed by
every part, because HistoryScreen loads via an untracked initState future) moved
to history_test_support.dart rather than being copy-pasted six times.

Verified: 555 flutter tests (same count as before -- no test lost in the split),
flutter analyze clean over all of lib/ and test/, 701 python tests at 100%
branch coverage, `python -m diet_guard sync` -> 402 entries/exit 0, live
~/.local/share/diet_guard byte-identical. Over-cap files: 42 -> 40.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
2026-08-15 19:35:19 +02:00

192 lines
5.6 KiB
Dart

import 'package:diet_guard_app/models/food_entry.dart';
import 'package:diet_guard_app/screens/history_screen.dart';
import 'package:diet_guard_app/services/budget_history_service.dart';
import 'package:diet_guard_app/services/budget_schedule.dart';
import 'package:diet_guard_app/services/log_storage_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'history_test_support.dart';
void main() {
useTempHistoryStores();
testWidgets('a day header shows the budget that applied on that day', (
tester,
) async {
await tester.runAsync(() async {
// 2200 until 2026-06-15, 2000 from then on.
await BudgetHistoryService.instance.applyMerged(const [
BudgetEntry(
effectiveFrom: kEpochDay,
kcal: 2200,
editedAt: '2026-01-01T00:00:00.000Z',
),
BudgetEntry(
effectiveFrom: '2026-06-15',
kcal: 2000,
editedAt: '2026-06-15T00:00:00.000Z',
),
]);
await LogStorageService.instance.writeLog({
'2026-06-01': [
const FoodEntry(
id: 'before',
time: '2026-06-01T08:00:00+02:00',
desc: 'before the cut',
grams: 100,
kcal: 2100,
proteinG: 5,
carbsG: 10,
fatG: 2,
source: 'manual',
),
],
'2026-06-20': [
const FoodEntry(
id: 'after',
time: '2026-06-20T08:00:00+02:00',
desc: 'after the cut',
grams: 100,
kcal: 2100,
proteinG: 5,
carbsG: 10,
fatG: 2,
source: 'manual',
),
],
});
await tester.pumpWidget(const MaterialApp(home: HistoryScreen()));
await settle(tester);
// Same 2100 kcal on both days, judged against different budgets --
// lowering the budget must not repaint the earlier day.
expect(find.text('2100 / 2200 kcal'), findsOneWidget);
expect(find.text('2100 / 2000 kcal'), findsOneWidget);
});
});
testWidgets('shows a message when nothing has been logged', (tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(const MaterialApp(home: HistoryScreen()));
await settle(tester);
expect(find.text('Nothing logged yet.'), findsOneWidget);
});
});
testWidgets('lists logged entries newest first, excluding tombstones', (
tester,
) async {
await tester.runAsync(() async {
await LogStorageService.instance.writeLog({
'2026-06-01': [
const FoodEntry(
id: 'old',
time: '2026-06-01T08:00:00+02:00',
desc: 'old breakfast',
grams: 100,
kcal: 100,
proteinG: 5,
carbsG: 10,
fatG: 2,
source: 'manual',
),
],
'2026-06-22': [
const FoodEntry(
id: 'new',
time: '2026-06-22T20:00:00+02:00',
desc: 'new dinner',
grams: 100,
kcal: 200,
proteinG: 10,
carbsG: 20,
fatG: 4,
source: 'manual',
),
const FoodEntry(
id: 'gone',
time: '2026-06-22T12:00:00+02:00',
desc: 'undone lunch',
grams: 100,
kcal: 300,
proteinG: 1,
carbsG: 1,
fatG: 1,
source: 'manual',
deleted: true,
),
],
});
await tester.pumpWidget(const MaterialApp(home: HistoryScreen()));
await settle(tester);
expect(find.text('new dinner'), findsOneWidget);
expect(find.text('old breakfast'), findsOneWidget);
expect(find.text('undone lunch'), findsNothing);
final tiles = tester.widgetList<ListTile>(find.byType(ListTile)).toList();
expect((tiles[0].title! as Text).data, 'new dinner');
expect((tiles[1].title! as Text).data, 'old breakfast');
});
});
testWidgets(
'initialDateRange pre-filters to just that day, matching a Calendar '
'screen day tap',
(tester) async {
await tester.runAsync(() async {
await LogStorageService.instance.writeLog({
'2026-06-01': [
const FoodEntry(
id: 'other-day',
time: '2026-06-01T08:00:00+02:00',
desc: 'other day breakfast',
grams: 100,
kcal: 100,
proteinG: 5,
carbsG: 10,
fatG: 2,
source: 'manual',
),
],
'2026-06-22': [
const FoodEntry(
id: 'target-day',
time: '2026-06-22T20:00:00+02:00',
desc: 'target day dinner',
grams: 100,
kcal: 200,
proteinG: 10,
carbsG: 20,
fatG: 4,
source: 'manual',
),
],
});
await tester.pumpWidget(
MaterialApp(
home: HistoryScreen(
initialDateRange: DateTimeRange(
start: DateTime(2026, 6, 22),
end: DateTime(2026, 6, 22),
),
),
),
);
await settle(tester);
expect(find.text('target day dinner'), findsOneWidget);
expect(find.text('other day breakfast'), findsNothing);
});
},
);
// ---------------------------------------------------------------------------
// applyHistoryFilter — pure function tests (no widget required)
// ---------------------------------------------------------------------------
}