wake-alarm/phone_app/test/services/alarm_sync_pull_test.dart
Krzysztof kuhy Rudnicki 9250db4cc1
Some checks failed
dependency freshness / dependencies on newest stable (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,free-days-interop) (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / free-days-interop (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
E2E-encrypt the published alarm record
The phone pushes alarm.json sealed under wake-alarm-sync/devices-e2e
(Argon2id key from the shared crdt-sync passphrase, keystore + lock
icon in Settings). The PC reads it through open_encrypted_client on
both Firebase and the GitHub mirror (now via GitHubSyncClient); no or
wrong passphrase reads as nothing published, never plaintext. The old
plaintext prefix is copied once per process, never deleted.

crdt-sync 0.11.0, crdt_sync_dart 0.14.1, crdt_sync_flutter 0.8.1,
sync_settings_ui 0.8.2, freedays 0.4.2, sickdays 0.5.3. install.sh
now fails if crdt_sync/pynacl/argon2 do not import.

Todo: fda71ed1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:30:27 +01:00

107 lines
3.4 KiB
Dart

import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:wake_alarm_sync/services/alarm_sync_service.dart';
import '../e2e_support.dart';
const _deviceId = 'this-install';
const _peerId = 'previous-install';
const _old = 'wake-alarm-sync/devices';
const _prefix = 'wake-alarm-sync/devices-e2e';
class _MemPersistence implements LogPersistence {
String? text;
@override
Future<String?> read() async => text;
@override
Future<void> write(String value) async => text = value;
}
Map<String, dynamic> _peerRecord(Map<String, Object?> fields, String hlc) => {
'alarm': {
'id': 'alarm',
'fields': {
for (final e in fields.entries) e.key: [e.value, hlc],
},
'deleted': false,
'deleted_hlc': null,
},
};
void main() {
late AlarmSyncService service;
setUp(() async {
TestWidgetsFlutterBinding.ensureInitialized();
installE2eTestStorage();
service = await AlarmSyncService.open(
persistence: _MemPersistence(),
deviceId: _deviceId,
);
});
test('setLocal keeps fields another writer put on the record', () async {
final hlc = Hlc.newTick(_deviceId, wallTimeMsOverride: 1000);
await service.setFields({
'nfc_tag_ids': <String>['a', 'b'],
}, hlc);
await service.setLocal(6, 30);
expect(service.currentAlarm, (hour: 6, minute: 30));
final tags = service.field('nfc_tag_ids');
expect(tags?.$1, ['a', 'b']);
expect(tags?.$2, hlc);
});
test('field is null for an unset field or an empty record', () async {
expect(service.field('nfc_tag_ids'), isNull);
await service.setLocal(6, 30);
expect(service.field('nfc_tag_ids'), isNull);
});
test('pushWith pulls a previous install and takes its newer field', () async {
await service.setLocal(8, 0);
// The previous install never updated: its record sits in plaintext on
// the old prefix and reaches this one through the one-time E2E copy.
final remote = MemGitHub({
'$_old/$_peerId/alarm.json': jsonEncode(
_peerRecord({
'alarm_hour': 7,
'alarm_minute': 0,
'nfc_tag_ids': ['x', 'y'],
}, '2026-09-11T10:26:18.254Z-0000-$_peerId'),
),
});
final github = remote.client();
await service.pushWith(github);
// The peer's tags are merged in; the peer's older alarm loses to the
// fresh local one.
expect(service.field('nfc_tag_ids')?.$1, ['x', 'y']);
expect(service.currentAlarm, (hour: 8, minute: 0));
// And the merged result is what was pushed, under this install's path.
const path = '$_prefix/$_deviceId/alarm.json';
expect(remote.files[path], isNot(contains('nfc_tag_ids')));
final pushed = jsonDecode((await remote.opened(path))!) as Map;
final fields = (pushed['alarm'] as Map)['fields'] as Map;
expect((fields['nfc_tag_ids'] as List).first, ['x', 'y']);
expect((fields['alarm_hour'] as List).first, 8);
// The old plaintext node is copied, never rewritten.
expect(remote.putPaths.where((p) => p.startsWith('$_old/')), isEmpty);
});
test('pushWith with no peers pushes the local log unchanged', () async {
await service.setLocal(8, 0);
final remote = MemGitHub();
await service.pushWith(remote.client());
expect(remote.putPaths, contains('$_prefix/$_deviceId/alarm.json'));
expect(service.field('nfc_tag_ids'), isNull);
});
}