mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 19:07:17 +01:00
firebase_auth_rest_test.dart 525 -> 153 + _google 158 + _tokens 206 + _uid 141
sync_revisions_test.dart 451 -> 223 + _peers 228 + _backends 158
+ _order 165 + _persistence 215
sync_settings_screen_test 457 -> 207 + _backup 143 + _google 145
mirror_store_test.dart 452 -> 209 + _reads 172 + _fallback 196 + _revisions 244
firebase_client_test.dart 374 -> 198 + _writes 227
firebase_config_test.dart 360 -> 138 + _client 148 + _client_google 164
device_auth_test.dart 307 -> 128 + _poll 201
github_client_test.dart 291 -> 143 + _writes 201
sync_test.dart 269 -> 160 + _skips 175
Two traps this hit, both invisible to a line count:
- _FakePersistence is declared *after* main(), so a naive cut at the last
brace dropped it; sync_revisions_persistence_test.dart failed to load
until it was carried across. Audited every other split file for trailing
top-level declarations — this was the only one.
- Copying the preamble into each sibling left helpers the sibling never
uses. `dart analyze` flagged 12; all removed rather than left as dead
code, and the analyzer is now clean.
crdt_sync_dart 280 passed, sync_settings_ui 29 passed, github_device_auth
analyze clean — all matching a clean HEAD worktree.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFSGeWxHPY5rHUGrPqhcmG
161 lines
4.9 KiB
Dart
161 lines
4.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'));
|
|
});
|
|
});
|
|
}
|