utils/crdt_sync_dart/test/sync_test.dart
Krzysztof kuhy Rudnicki 6eef99d661
Some checks are pending
crdt-sync pre-commit / pre-commit (push) Waiting to run
crdt-sync tests / test (3.10) (push) Waiting to run
crdt-sync tests / test (3.11) (push) Waiting to run
crdt-sync tests / test (3.12) (push) Waiting to run
Add DeviceIdentity and a legacy device id for the uuid migration
Groundwork for moving diet-guard, screen-locker and wake-alarm off fixed
role ids ('pc'/'phone'/'desktop') onto per-install uuids, the scheme todo
and home_inventory already use. A role constant collides the moment a
second machine takes the same role, and a reinstall silently inherits the
previous install's CRDT identity.

The migration cannot rewrite history: the device id is baked into every HLC
stamp already written, is the record path segment and the revision key. So
a device that has just switched ids must still recognise its former id as
its own -- otherwise skip-own-writes stops matching the old path, and the
device pulls back and re-merges its own pre-migration log as though a peer
had written it. Idempotent under LWW, but pure wasted transfer on every
tick.

Both halves therefore take an optional legacy id and test set membership
(own_ids / ownIds) rather than equality against a single id:

- crdt-sync (Python) 0.5.1 -> 0.6.0: new crdt_sync._device with
  DeviceIdentity + load_device_identity (generate-or-load, persisted); the
  mkdir is deliberately unguarded, since handing back an unpersisted id
  would mint a fresh uuid every restart and strand a device dir each time.
  sync_log gains legacy_device_id.
- crdt_sync_dart 0.6.0 -> 0.7.0: DeviceIdentity value type; syncLog gains
  legacyDeviceId. Persistence stays app-side, where the existing uuid apps
  already keep it in SharedPreferences under 'crdt.nodeId'.

Both changes are backward compatible: the parameter defaults to null, which
reproduces the previous single-id behaviour exactly.

Each half has a positive test and a negative control proving the old path
*is* pulled when no legacy id is declared -- so the skip is verified to be
the parameter's doing, not an artifact of the fixture.

Python: 239 tests, 100% branch coverage. Dart: 248 tests, analyzer clean,
device.dart 11/11 and sync.dart 57/57 lines covered.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GZH89WEqAjuJT8WZdqc7AP
2026-08-09 16:54:33 +02:00

270 lines
7.9 KiB
Dart

import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http_testing;
import 'package:test/test.dart';
Hlc _make(int wallTimeMs, {int counter = 0, String nodeId = 'node-a'}) =>
Hlc(wallTimeMs: wallTimeMs, counter: counter, nodeId: nodeId);
String _encode(Log log) =>
jsonEncode(log.map((id, record) => MapEntry(id, record.toJson())));
Log _decode(String text) => (jsonDecode(text) as Map<String, dynamic>).map(
(id, data) => MapEntry(id, Record.fromJson(data as Map<String, dynamic>)),
);
http.Response _response(int statusCode, [Object? jsonBody]) =>
http.Response(jsonEncode(jsonBody ?? {}), statusCode);
class _PutCall {
_PutCall(this.path, this.body);
final String path;
final Map<String, dynamic> body;
}
/// Builds a [GitHubClient] backed by an in-memory router: GET
/// `.../contents/<key>` returns `contentResponses[key]` (404 if absent), the
/// bare repo-existence GET always succeeds, and every PUT is recorded into
/// [putCalls] and answered with 200.
({GitHubClient client, List<_PutCall> putCalls}) _client({
Map<String, http.Response> contentResponses = const {},
}) {
final putCalls = <_PutCall>[];
final mock = http_testing.MockClient((request) async {
final path = request.url.path;
if (!path.contains('/contents/')) {
return _response(200);
}
final key = path.split('/contents/').last;
if (request.method == 'PUT') {
putCalls.add(
_PutCall(key, jsonDecode(request.body) as Map<String, dynamic>),
);
return _response(200);
}
return contentResponses[key] ?? _response(404);
});
final client = GitHubClient(
owner: 'kuhyx',
repo: 'crdt-sync-demo',
token: 'fake-token',
httpClient: mock,
);
return (client: client, putCalls: putCalls);
}
http.Response _directoryOf(List<String> deviceIds) =>
_response(200, deviceIds.map((id) => {'name': id, 'type': 'dir'}).toList());
http.Response _fileContaining(String text) =>
_response(200, {'content': base64.encode(utf8.encode(text))});
void main() {
group('syncLog', () {
test('pushes the local log when no other devices have synced', () async {
final localLog = <String, Record>{
'a': Record(id: 'a', fields: {'text': ('hello', _make(100))}),
};
final (:client, :putCalls) = _client(
contentResponses: {'devices': _directoryOf([])},
);
final merged = await syncLog(
client: client,
deviceId: 'pc',
pathPrefix: 'devices',
localLog: localLog,
encode: _encode,
decode: _decode,
);
expect(merged, equals(localLog));
expect(putCalls, hasLength(1));
expect(putCalls.single.path, 'devices/pc/log.json');
});
test('skips its own device id when listing', () async {
final (:client, :putCalls) = _client(
contentResponses: {
'devices': _directoryOf(['pc', 'phone']),
'devices/phone/log.json': _fileContaining('{}'),
},
);
await syncLog(
client: client,
deviceId: 'pc',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
);
expect(putCalls, hasLength(1));
});
test('skips its legacy device id as well as its current one', () async {
// The old path holds a record this device pushed before migrating.
// Pulling it back would re-merge its own history as a peer's.
final legacyLog = _encode({
'stale': Record(id: 'stale', fields: {'text': ('old', _make(1))}),
});
final (:client, :putCalls) = _client(
contentResponses: {
'devices': _directoryOf(['pc', 'new-uuid']),
'devices/pc/log.json': _fileContaining(legacyLog),
},
);
final merged = await syncLog(
client: client,
deviceId: 'new-uuid',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
legacyDeviceId: 'pc',
);
expect(merged, isEmpty);
expect(putCalls.single.path, 'devices/new-uuid/log.json');
});
test('pulls the old path when no legacy id is declared', () async {
// Negative control for the test above: absent legacyDeviceId, the old
// path really is treated as a peer.
final legacyLog = _encode({
'stale': Record(id: 'stale', fields: {'text': ('old', _make(1))}),
});
final (:client, :putCalls) = _client(
contentResponses: {
'devices': _directoryOf(['pc', 'new-uuid']),
'devices/pc/log.json': _fileContaining(legacyLog),
},
);
final merged = await syncLog(
client: client,
deviceId: 'new-uuid',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
);
expect(merged.keys, contains('stale'));
});
test('skips a device with no pushed file yet', () async {
final (:client, :putCalls) = _client(
contentResponses: {
'devices': _directoryOf(['phone']),
},
);
final merged = await syncLog(
client: client,
deviceId: 'pc',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
);
expect(merged, isEmpty);
expect(putCalls.single.body['content'], base64.encode(utf8.encode('{}')));
});
test('skips a device whose pushed file is corrupt', () async {
final (:client, :putCalls) = _client(
contentResponses: {
'devices': _directoryOf(['phone']),
'devices/phone/log.json': _fileContaining('{not valid json'),
},
);
final merged = await syncLog(
client: client,
deviceId: 'pc',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
);
expect(merged, isEmpty);
expect(putCalls, hasLength(1));
});
test('skips a device whose pushed JSON has the wrong shape', () async {
// Valid JSON that isn't a record map (e.g. from an incompatible
// writer) must be skipped like corrupt JSON, not crash the whole
// sync -- this is what the `on TypeError` catch in `syncLog` is
// for, not just JSON syntax errors caught by `FormatException`.
final (:client, :putCalls) = _client(
contentResponses: {
'devices': _directoryOf(['phone']),
'devices/phone/log.json': _fileContaining('{"a": 5}'),
},
);
final merged = await syncLog(
client: client,
deviceId: 'pc',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
);
expect(merged, isEmpty);
expect(putCalls, hasLength(1));
});
test('merges in a remote device\'s entries', () async {
final remoteLog = <String, Record>{
'b': Record(id: 'b', fields: {'text': ('from phone', _make(100))}),
};
final (:client, :putCalls) = _client(
contentResponses: {
'devices': _directoryOf(['phone']),
'devices/phone/log.json': _fileContaining(_encode(remoteLog)),
},
);
final merged = await syncLog(
client: client,
deviceId: 'pc',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
);
expect(merged, equals(remoteLog));
});
test('uses a custom filename and commit message', () async {
final (:client, :putCalls) = _client(
contentResponses: {'devices': _directoryOf([])},
);
await syncLog(
client: client,
deviceId: 'pc',
pathPrefix: 'devices',
localLog: {},
encode: _encode,
decode: _decode,
filename: 'notes.json',
commitMessage: 'custom message',
);
expect(putCalls.single.path, 'devices/pc/notes.json');
expect(putCalls.single.body['message'], 'custom message');
});
});
}