mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 02:54:21 +01:00
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
Every sync payload (food log, banks, budget, catering credential, Body, export keyring) now lives on diet-guard-sync/devices-e2e, sealed with the Argon2id key from the shared crdt-sync passphrase; record ids, tombstones and clocks stay clear. Old plaintext nodes are copied once and never written or deleted. Locked = no pull, no push. - PC: _sync_e2e.open_e2e over crdt-sync 0.11.0, key and copy memo in ~/.local/share/diet_guard/e2e; undecryptable peers are skipped. - Phone/desktop: sync_e2e.dart, crdt_sync_flutter 0.8.1, lock icon in Settings; pullExportKeys goes through the same open. - wake-alarm weigh-ins read from wake-alarm-sync/devices-e2e (own header), on both sides. Todo: fda71ed1 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
4.1 KiB
Dart
114 lines
4.1 KiB
Dart
// Mirrors `test_sync.py`'s `TestRunSync` and `TestSyncBudget` cases
|
|
// (own-id-skip, no-prior-push, non-object payload, corrupt JSON, remote
|
|
// merge, food bank rebuild, budget last-writer-wins), plus one Dart-specific
|
|
// that has no PC-side equivalent.
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/services/app_settings_service.dart';
|
|
import 'package:diet_guard_app/services/budget_history_service.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:diet_guard_app/services/sync_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import '../fake_secure_storage.dart';
|
|
import 'sync_service_test_support.dart';
|
|
|
|
void main() {
|
|
late Directory tempDir;
|
|
|
|
setUp(() async {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
installFakeSecureStorage();
|
|
tempDir = await Directory.systemTemp.createTemp('diet_guard_sync_test_');
|
|
LogStorageService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
FoodBankService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
BudgetHistoryService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
});
|
|
|
|
tearDown(() async {
|
|
LogStorageService.resetForTesting();
|
|
FoodBankService.resetForTesting();
|
|
AppSettingsService.resetForTesting();
|
|
BudgetHistoryService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
group('budget sync', () {
|
|
test('pushes the local budget when no other devices have synced', () async {
|
|
await AppSettingsService.instance.saveDailyKcalGoal(2000);
|
|
final fake = FakeGitHub();
|
|
await runSync(fake.buildClient());
|
|
|
|
expect(
|
|
fake.putsByPath.containsKey(
|
|
'diet-guard-sync/devices-e2e/phone/budget.json',
|
|
),
|
|
isTrue,
|
|
);
|
|
});
|
|
|
|
test('remote-only budget is adopted locally', () async {
|
|
final remoteJson = remoteBudgetJson(kcal: 1800, t: '2026-01-01T09:00:00');
|
|
final fake = FakeGitHub(
|
|
deviceDirs: const ['pc'],
|
|
files: {'diet-guard-sync/devices-e2e/pc/budget.json': remoteJson},
|
|
);
|
|
await runSync(fake.buildClient());
|
|
expect(AppSettingsService.dailyKcalGoal, 1800);
|
|
});
|
|
|
|
test('a local edit later than a remote edit wins', () async {
|
|
await AppSettingsService.instance.saveDailyKcalGoal(1500); // now
|
|
final remoteJson = remoteBudgetJson(kcal: 1800, t: '2020-01-01T09:00:00');
|
|
final fake = FakeGitHub(
|
|
deviceDirs: const ['pc'],
|
|
files: {'diet-guard-sync/devices-e2e/pc/budget.json': remoteJson},
|
|
);
|
|
await runSync(fake.buildClient());
|
|
expect(AppSettingsService.dailyKcalGoal, 1500);
|
|
});
|
|
|
|
test('a remote edit later than a local edit wins', () async {
|
|
await AppSettingsService.instance.saveDailyKcalGoal(1500); // now
|
|
final remoteJson = remoteBudgetJson(kcal: 1800, t: '2999-01-01T09:00:00');
|
|
final fake = FakeGitHub(
|
|
deviceDirs: const ['pc'],
|
|
files: {'diet-guard-sync/devices-e2e/pc/budget.json': remoteJson},
|
|
);
|
|
await runSync(fake.buildClient());
|
|
expect(AppSettingsService.dailyKcalGoal, 1800);
|
|
});
|
|
|
|
test('a malformed remote budget is skipped, not a crash', () async {
|
|
await AppSettingsService.instance.saveDailyKcalGoal(2000);
|
|
final fake = FakeGitHub(
|
|
deviceDirs: const ['pc'],
|
|
files: const {
|
|
'diet-guard-sync/devices-e2e/pc/budget.json': '{not valid json',
|
|
},
|
|
);
|
|
await runSync(fake.buildClient());
|
|
expect(AppSettingsService.dailyKcalGoal, 2000);
|
|
});
|
|
|
|
test(
|
|
'a fresh install with no budget ever set contributes nothing',
|
|
() async {
|
|
final fake = FakeGitHub();
|
|
await runSync(fake.buildClient());
|
|
|
|
final pushedText = (await fake.opened(
|
|
'diet-guard-sync/devices-e2e/phone/budget.json',
|
|
))!;
|
|
expect(jsonDecode(pushedText), isEmpty);
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
},
|
|
);
|
|
});
|
|
}
|