todo-app/tool/sync_smoke.dart
Krzysztof kuhy Rudnicki b9d9ac6521 Migrate notes storage from sqlite_crdt to the shared crdt_sync LogStore
NoteRepository is rewritten on crdt_sync's LogStore: each Note is stored *as*
a Record (per-field LWW + sticky tombstone), so the on-disk log IS the sync
wire format. Its public API is unchanged (upsert/delete/listNotes/watchNotes/
watchCount/importNotes/changes/nodeId), so the UI and most tests are
untouched; only the internals, the two sync methods (getChangeset/merge ->
exportLog/importLog over a Log), and the changeset-specific tests change.
Filtering/sorting now run in Dart over the in-memory log; watchNotes/watchCount
seed synchronously in onListen to avoid a subscription race.

SyncService moves from per-device sqlite changesets to per-device crdt_sync
Logs under a new todo-sync/notes/ path (clean format cutover; old clients
can't misread a new file). todo's own GitHubClient transport is kept.

One-time migration in NoteRepository.open: reads the legacy sqlite DB (kept
untouched as a safety net; the dep stays only for this), seeds every note --
tombstones included -- into the log with each field's HLC derived from its
updated_at (so a pre-cutover edit still wins by real edit time across two
independently-migrating devices), reuses the legacy node id, and sets a
persisted "migrated" flag so it never re-runs.

Verified: flutter analyze + dart format clean; 178 tests at 100% coverage
(incl. migration, tombstone, restore-race, and corrupt-peer cases). Device
verified on dev.kuhy.todo: real backlog of 27 live notes (+5 tombstones)
migrated intact -- confirmed against a pulled sqlite backup, the pulled
LogStore JSON, and the app's own "27 saved" -- notes render with status/
priority/dates, no crash, migration one-time.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019KwUCPESvE5CjCtmUFz8xQ
2026-07-11 23:28:54 +02:00

94 lines
3.0 KiB
Dart

// Headless end-to-end proof of the sync engine against a REAL GitHub repo.
//
// Simulates two devices (A and B), each creating a note offline, then syncs
// them through the repo and asserts both devices converge to both notes.
// Cleans up its throwaway log files afterwards so the real `notes/` directory
// is never touched.
//
// Run: GH_TOKEN=$(gh auth token) dart run tool/sync_smoke.dart
import 'dart:io';
import 'package:todo/data/note.dart';
import 'package:todo/data/note_repository.dart';
import 'package:todo/sync/github_client.dart';
import 'package:todo/sync/sync_service.dart';
Future<void> main() async {
final token = Platform.environment['GH_TOKEN'];
if (token == null || token.isEmpty) {
stderr.writeln('Set GH_TOKEN (e.g. GH_TOKEN=\$(gh auth token)).');
exit(2);
}
// Throwaway directory so we never pollute the real `notes/`.
const service = SyncService(notesDir: 'todo-sync/notes_smoketest');
GitHubClient client() =>
GitHubClient(owner: 'kuhyx', repo: 'syncs', token: token);
final deviceA = await NoteRepository.openInMemory();
final deviceB = await NoteRepository.openInMemory();
final stamp = DateTime.now().toIso8601String();
await _insert(deviceA, 'Idea from device A @ $stamp');
await _insert(deviceB, 'Idea from device B @ $stamp');
stdout.writeln('Device A nodeId: ${deviceA.nodeId}');
stdout.writeln('Device B nodeId: ${deviceB.nodeId}');
// Sync order: A pushes, B pulls A + pushes, A pulls B. Both converge.
final ghA = client();
final ghB = client();
stdout.writeln('A.sync(): ${await service.sync(deviceA, ghA)}');
stdout.writeln('B.sync(): ${await service.sync(deviceB, ghB)}');
stdout.writeln('A.sync(): ${await service.sync(deviceA, ghA)}');
final aNotes = (await deviceA.listNotes()).map((n) => n.text).toSet();
final bNotes = (await deviceB.listNotes()).map((n) => n.text).toSet();
stdout.writeln('\nDevice A sees: $aNotes');
stdout.writeln('Device B sees: $bNotes');
final expected = {
'Idea from device A @ $stamp',
'Idea from device B @ $stamp',
};
final converged =
aNotes.containsAll(expected) && bNotes.containsAll(expected);
// Cleanup: remove the throwaway log files.
final cleanup = client();
for (final f in await cleanup.listDirectory('todo-sync/notes_smoketest')) {
await cleanup.deleteFile(f.path, f.sha, message: 'smoke test cleanup');
}
stdout.writeln('Cleaned up throwaway log files.');
ghA.close();
ghB.close();
cleanup.close();
await deviceA.close();
await deviceB.close();
if (converged) {
stdout.writeln(
'\n✅ PASS: both devices converged to both notes via GitHub.',
);
exit(0);
} else {
stdout.writeln('\n❌ FAIL: devices did not converge. Expected $expected.');
exit(1);
}
}
Future<void> _insert(NoteRepository repo, String text) async {
final now = DateTime.now();
await repo.upsert(
Note(
id: '${now.microsecondsSinceEpoch}-${text.hashCode}',
text: text,
priority: Priority.medium,
status: Status.todo,
createdAt: now,
updatedAt: now,
),
);
}