mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-24 08:26:25 +01:00
Tenth batch of the 250-line sweep (over-cap count 18 -> 12; all Python is
already clear, so the remainder is Dart).
sync_merge_test.dart 694 -> 232 (+ 4 siblings)
github_mirror_screen_test.dart 512 -> 250 (+ 3 siblings)
foodbank_service_test.dart 395 -> 190 (+ manual sibling)
sync_service_test.dart 394 -> 203 (+ budget sibling + support)
log_storage_service_test.dart 385 -> 182 (+ edit sibling)
log_meal_screen_test.dart 362 -> 243 (+ nav sibling)
sync_service_test's ~100-line fixture block is shared through
`sync_service_test_support.dart` rather than duplicated: copying it would have
put both siblings back over the cap on their own. The helpers had to become
public (`_FakeGitHub` -> `FakeGitHub`, `_manual` -> `manualNutrition`,
`_remoteBudgetJson` -> `remoteBudgetJson`) with doc comments, since a leading
underscore is library-private in Dart and the siblings could not otherwise see
them.
The block-boundary matcher earned two fixes, both caught by comparing block
counts against HEAD rather than by the suite:
- A test name wrapped onto its own line (dart format does this past 80 cols)
was invisible to a same-line-only pattern, so the neighbouring span sliced
through it -- the file stopped compiling. Spans now end at the matching
close paren of the call, not at a guess past the closure brace.
- A double-quoted name containing an apostrophe ("skips its own device id
('phone')") was excluded by a character class that banned both quote
characters. That block fell into the shared prologue and was duplicated into
every output: the suite went GREEN at 559 tests instead of 555. Reverted and
redone; text between two blocks is now treated as a shared helper.
Verified: flutter test 555 (unchanged, and each split family's block count
matches HEAD exactly), 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
191 lines
5.7 KiB
Dart
191 lines
5.7 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/models/food_entry.dart';
|
|
import 'package:diet_guard_app/models/meal_component.dart';
|
|
import 'package:diet_guard_app/services/document_store_io.dart';
|
|
import 'package:diet_guard_app/services/foodbank_service.dart';
|
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
FoodEntry _entry({
|
|
required String id,
|
|
required String time,
|
|
required String desc,
|
|
double kcal = 100,
|
|
List<MealComponent>? components,
|
|
bool deleted = false,
|
|
}) => FoodEntry(
|
|
id: id,
|
|
time: time,
|
|
desc: desc,
|
|
grams: 100,
|
|
kcal: kcal,
|
|
proteinG: 1,
|
|
carbsG: 1,
|
|
fatG: 1,
|
|
source: components != null ? 'meal' : 'manual',
|
|
components: components,
|
|
deleted: deleted,
|
|
);
|
|
|
|
void main() {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Manual bank — addManualEntry / mergedEntries
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// IO error paths — FileSystemException and FormatException handlers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
group('FoodBankService.rebuild (pure)', () {
|
|
test('upserts a single-food entry by normalized name', () {
|
|
final log = {
|
|
'2026-06-22': [
|
|
_entry(id: '1', time: '2026-06-22T08:00:00+02:00', desc: 'Toast'),
|
|
],
|
|
};
|
|
final bank = FoodBankService.rebuild(log);
|
|
expect(bank['toast']!.desc, 'Toast');
|
|
expect(bank['toast']!.count, 1);
|
|
});
|
|
|
|
test('increments count and keeps the latest macros on repeat', () {
|
|
final log = {
|
|
'2026-06-22': [
|
|
_entry(
|
|
id: '1',
|
|
time: '2026-06-22T08:00:00+02:00',
|
|
desc: 'toast',
|
|
),
|
|
_entry(
|
|
id: '2',
|
|
time: '2026-06-22T12:00:00+02:00',
|
|
desc: 'toast',
|
|
kcal: 120,
|
|
),
|
|
],
|
|
};
|
|
final bank = FoodBankService.rebuild(log);
|
|
expect(bank['toast']!.count, 2);
|
|
expect(bank['toast']!.kcal, 120);
|
|
});
|
|
|
|
test('skips a tombstoned entry entirely', () {
|
|
final log = {
|
|
'2026-06-22': [
|
|
_entry(
|
|
id: '1',
|
|
time: '2026-06-22T08:00:00+02:00',
|
|
desc: 'toast',
|
|
deleted: true,
|
|
),
|
|
],
|
|
};
|
|
expect(FoodBankService.rebuild(log), isEmpty);
|
|
});
|
|
|
|
test("records a composite entry's components and total separately", () {
|
|
const components = [
|
|
MealComponent(
|
|
name: 'rice',
|
|
kcal: 200,
|
|
proteinG: 4,
|
|
carbsG: 44,
|
|
fatG: 1,
|
|
grams: 150,
|
|
),
|
|
];
|
|
final log = {
|
|
'2026-06-22': [
|
|
_entry(
|
|
id: '1',
|
|
time: '2026-06-22T20:00:00+02:00',
|
|
desc: 'dinner',
|
|
kcal: 200,
|
|
components: components,
|
|
),
|
|
],
|
|
};
|
|
final bank = FoodBankService.rebuild(log);
|
|
expect(bank['rice'], isNotNull);
|
|
expect(bank['dinner']!.components, ['rice']);
|
|
});
|
|
|
|
test('orders replay by time then id so two devices converge', () {
|
|
final logA = {
|
|
'2026-06-22': [
|
|
_entry(id: 'b', time: '2026-06-22T08:00:00+02:00', desc: 'x'),
|
|
_entry(id: 'a', time: '2026-06-22T08:00:00+02:00', desc: 'x'),
|
|
],
|
|
};
|
|
final logB = {
|
|
'2026-06-22': [
|
|
_entry(id: 'a', time: '2026-06-22T08:00:00+02:00', desc: 'x'),
|
|
_entry(id: 'b', time: '2026-06-22T08:00:00+02:00', desc: 'x'),
|
|
],
|
|
};
|
|
expect(
|
|
FoodBankService.rebuild(logA)['x']!.count,
|
|
FoodBankService.rebuild(logB)['x']!.count,
|
|
);
|
|
});
|
|
});
|
|
group('FoodBankService search/persistence', () {
|
|
late Directory tempDir;
|
|
|
|
setUp(() async {
|
|
tempDir = await Directory.systemTemp.createTemp('diet_guard_bank_');
|
|
FoodBankService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
LogStorageService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
});
|
|
|
|
tearDown(() async {
|
|
FoodBankService.resetForTesting();
|
|
LogStorageService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
test('readBank returns empty map when no file exists', () async {
|
|
expect(await FoodBankService.instance.readBank(), isEmpty);
|
|
});
|
|
|
|
test('rebuildAndPersist writes the bank to disk', () async {
|
|
final log = {
|
|
'2026-06-22': [
|
|
_entry(id: '1', time: '2026-06-22T08:00:00+02:00', desc: 'egg'),
|
|
],
|
|
};
|
|
await FoodBankService.instance.rebuildAndPersist(log);
|
|
final reread = await FoodBankService.instance.readBank();
|
|
expect(reread['egg'], isNotNull);
|
|
});
|
|
|
|
test('search ranks an exact substring match above a fuzzy one', () async {
|
|
final log = {
|
|
'2026-06-22': [
|
|
_entry(id: '1', time: '2026-06-22T08:00:00+02:00', desc: 'banana'),
|
|
_entry(id: '2', time: '2026-06-22T08:01:00+02:00', desc: 'banama'),
|
|
],
|
|
};
|
|
await FoodBankService.instance.rebuildAndPersist(log);
|
|
final results = await FoodBankService.instance.search('banana');
|
|
expect(results.first.name, 'banana');
|
|
});
|
|
|
|
test('empty query returns the most-logged foods first', () async {
|
|
final log = {
|
|
'2026-06-22': [
|
|
_entry(id: '1', time: '2026-06-22T08:00:00+02:00', desc: 'rare'),
|
|
_entry(id: '2', time: '2026-06-22T08:01:00+02:00', desc: 'common'),
|
|
_entry(id: '3', time: '2026-06-22T08:02:00+02:00', desc: 'common'),
|
|
],
|
|
};
|
|
await FoodBankService.instance.rebuildAndPersist(log);
|
|
final results = await FoodBankService.instance.search('');
|
|
expect(results.first.name, 'common');
|
|
});
|
|
});
|
|
}
|