diet-guard/app/test/services/sync_body_test.dart
Krzysztof kuhy Rudnicki 617e1ec879
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
E2E-encrypt diet_guard sync and export keys
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>
2026-11-12 10:31:44 +01:00

70 lines
2.2 KiB
Dart

import 'dart:convert';
import 'package:diet_guard_app/models/body_document.dart';
import 'package:diet_guard_app/services/body_service.dart';
import 'package:diet_guard_app/services/sync_body.dart';
import 'package:diet_guard_app/services/sync_device_id.dart';
import 'package:diet_guard_app/services/sync_merge_body.dart';
import 'package:flutter_test/flutter_test.dart';
import 'body_test_support.dart';
const _dir = 'diet-guard-sync/devices-e2e';
void main() {
tearDown(BodyService.resetForTesting);
test('no-ops when the body service is not initialised', () async {
final remote = MemoryRemote();
await syncBody(remote, devicesDir: _dir);
expect(remote.files, isEmpty);
});
test('merges a peer, ingests the weigh-in, pushes the result', () async {
await BodyService.initForTesting(MemoryDocStore());
await BodyService.instance.setProfile(
heightCm: 180,
now: DateTime.parse('2026-09-01T10:00:00'),
);
final peer = BodyDocument(
profile: const {
profileBirth: Stamped('1995-03-01', '2026-09-02T10:00:00+02:00'),
},
activity: const ActivityData(
sessions: [
{'day': '2026-09-26', 'kind': 'strength', 'min': 80},
],
t: '2026-09-27T10:00:00+02:00',
),
);
final remote = MemoryRemote({
'$_dir/pc-uuid/body.json': encodeBodyForPush(bodyToLog(peer)),
'wake-alarm-sync/devices-e2e/phone/alarm.json': jsonEncode({
'alarm': {
'fields': {
'morning_sessions': [
[
{'date': '2026-09-27', 'weight_kg': 80.5},
],
'2026-09-27T05:00:00.000Z-0000-phone',
],
},
},
}),
});
await syncBody(remote, devicesDir: _dir, wakeAlarm: remote);
final body = BodyService.instance;
expect(body.birth, '1995-03-01');
expect(body.heightCm, 180);
expect(body.latestWeight, ('2026-09-27', 80.5));
expect(body.sessions, hasLength(1));
final pushed = remote.files['$_dir/$currentSyncDeviceId/body.json'];
expect(pushed, isNotNull);
final pushedDoc = logToBody(parseRemoteBody(pushed!));
expect(pushedDoc.weights.keys, ['2026-09-27']);
expect(pushedDoc.profile.keys.toSet(), {profileBirth, profileHeight});
});
}