diet-guard/app/test/services/day_status_service_streaks_test.dart
Krzysztof kuhy Rudnicki c0ed6154ac Split the remaining seven Dart test files
Eleventh batch of the 250-line sweep (over-cap count 12 -> 5). Every test file
in the repo, Python and Dart, is now under cap; the five left are all in
app/lib/.

  wrapper_server_test.dart        349 -> 145  (+ proxy, account siblings)
  app_settings_service_test.dart  321 -> 162  (+ sync sibling)
  average_service_test.dart       299 -> 158  (+ periods sibling)
  settings_screen_test.dart       274 -> 189  (+ battery sibling)
  day_status_service_test.dart    270 -> 161  (+ streaks sibling)
  edit_entry_screen_test.dart     269 -> 159  (+ foodbank sibling)
  day_status_calendar_test.dart   256 -> 237  (+ _test_support sibling)

day_status_calendar_test is a single group whose tests close over group-local
state, so it could not be split by block. Its ~25 lines of file-level fixtures
moved to `day_status_calendar_test_support.dart` instead, which required the
same publicisation as the sync support file (`_theme` -> `appTheme`, `_wrap` ->
`wrapInApp`, `_colorOfDay` -> `colorOfDay`) with doc comments.

Block counts were compared against HEAD per family rather than trusting the
suite total, since the duplication bug in the previous batch passed green at an
inflated count. All seven families match exactly (29/26/34/11/34/8/14).

Verified: flutter test 555 (unchanged), flutter analyze clean, pre-commit
clean.

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

153 lines
4.3 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('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);
});
});
}