From b9d9ac65214c5e0324b7bc18e90674269470aa71 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sat, 11 Jul 2026 23:28:54 +0200 Subject: [PATCH] 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) Claude-Session: https://claude.ai/code/session_019KwUCPESvE5CjCtmUFz8xQ --- lib/data/note_repository.dart | 514 ++++++++++++++++++--------------- lib/sync/sync_service.dart | 81 +++--- lib/ui/note_editor.dart | 5 +- pubspec.lock | 9 + pubspec.yaml | 9 + test/fake_note_repository.dart | 6 +- test/note_repository_test.dart | 243 +++++++++++++--- test/sync_service_test.dart | 139 +++++---- tool/sync_smoke.dart | 19 +- 9 files changed, 620 insertions(+), 405 deletions(-) diff --git a/lib/data/note_repository.dart b/lib/data/note_repository.dart index 544bfbb..92a593a 100644 --- a/lib/data/note_repository.dart +++ b/lib/data/note_repository.dart @@ -1,6 +1,14 @@ import 'dart:async'; +import 'dart:io'; -import 'package:sqlite_crdt/sqlite_crdt.dart'; +import 'package:crdt_sync/crdt_sync.dart'; +import 'package:crdt_sync/crdt_sync_io.dart'; +import 'package:path/path.dart' as p; +import 'package:shared_preferences/shared_preferences.dart'; +// Only SqliteCrdt/CrdtTableExecutor are needed for the legacy-DB migration; +// hide its Hlc so the crdt_sync Hlc (the store's clock type) is unambiguous. +import 'package:sqlite_crdt/sqlite_crdt.dart' hide Hlc; +import 'package:uuid/uuid.dart'; import 'note.dart'; @@ -28,141 +36,148 @@ class ImportOutcome { int get total => added + updated + skipped; } -/// Local-first persistence for [Note]s, backed by a CRDT SQLite database. +/// Local-first persistence for [Note]s, backed by the shared `crdt_sync` +/// [LogStore] (was `sqlite_crdt`). /// -/// Every write goes straight to local storage so the app works fully -/// offline. The CRDT metadata (Hybrid Logical Clock timestamps per row) -/// lets a future sync layer merge two devices conflict-free using -/// last-writer-wins per note. This class owns the schema and exposes a -/// small, intention-revealing API; SQL never leaks past this boundary. +/// Every write goes straight to local storage so the app works fully offline. +/// Each [Note] is stored *as* a crdt_sync [Record] (id + per-field +/// last-writer-wins map + sticky tombstone), so the on-disk log **is** the +/// sync wire format — no separate adapter. Filtering and sorting run in Dart +/// over the in-memory log (the record fields are opaque to the library); the +/// note count of a personal backlog is small enough that this is cheap. This +/// class owns the note↔record mapping and exposes a small, intention-revealing +/// API; the log never leaks past this boundary. class NoteRepository { - NoteRepository._(this._crdt); + NoteRepository._(this._store, this._nodeId); - final SqliteCrdt _crdt; + final LogStore _store; + final String _nodeId; - /// Ticks once (with no payload) after every write, so lightweight listeners - /// can react without re-querying the whole table on each keystroke. This is - /// deliberately cheaper than [watchNotes]: it carries no rows, so a consumer - /// that only needs "something changed" (e.g. the debounced disk backup) pays - /// O(1) per write instead of re-parsing every note on every character. - final StreamController _changes = StreamController.broadcast(); - - /// Fires after each successful write ([upsert], [delete], [merge], and + /// Fires after each successful write ([upsert], [delete], [importLog], and /// transitively [importNotes]). Emits `void` — pull the data on demand. - Stream get changes => _changes.stream; + Stream get changes => _store.changes; - /// Signals [changes] listeners that the table was written. Guarded so a late - /// write during teardown can't add to a closed controller. - void _notifyChanged() { - if (!_changes.isClosed) _changes.add(null); - } + // Field names inside each note [Record]; kept identical to the legacy SQL + // column names so a migrated log round-trips through the same [Note] mapping. + static const _fText = 'text'; + static const _fPriority = 'priority'; + static const _fStatus = 'status'; + static const _fCreatedAt = 'created_at'; + static const _fUpdatedAt = 'updated_at'; - /// Opens (or creates) the database at [path] and ensures the schema. + static const _kNodeId = 'crdt.nodeId'; + static const _kMigrated = 'crdt.migratedFromSqlite'; + static const _logFileName = 'todo_notes.json'; + + /// Opens (or creates) the note log next to the legacy DB at [path]. /// - /// [path] should be an absolute file path on desktop/mobile, or the - /// special in-memory path used by tests. + /// On first launch after the crdt_sync migration this performs a one-time, + /// idempotent import of the existing `sqlite_crdt` database at [path] into + /// the new log (guarded by a persisted "migrated" flag), so no backlog is + /// lost. The legacy DB file is left untouched as a safety net. static Future open(String path) async { - final crdt = await SqliteCrdt.open( - path, - version: _schemaVersion, - onCreate: _onCreate, - onUpgrade: _onUpgrade, - ); - return NoteRepository._(crdt); + final prefs = await SharedPreferences.getInstance(); + final logFile = File(p.join(p.dirname(path), _logFileName)); + final persistence = FileLogPersistence(logFile); + + final migrated = prefs.getBool(_kMigrated) ?? false; + var nodeId = prefs.getString(_kNodeId) ?? ''; + + if (!migrated && File(path).existsSync()) { + final legacy = await SqliteCrdt.open( + path, + version: _legacyVersion, + onCreate: _legacyOnCreate, + onUpgrade: _legacyOnUpgrade, + ); + // Reuse the legacy CRDT node id so a device that already synced keeps + // the same devices/ identity across the cutover. + if (nodeId.isEmpty) nodeId = legacy.nodeId; + final store = LogStore(persistence: persistence, nodeId: nodeId); + await store.load(); + await _seedFromLegacy(legacy, store, nodeId); + await legacy.close(); + await prefs.setString(_kNodeId, nodeId); + await prefs.setBool(_kMigrated, true); + return NoteRepository._(store, nodeId); + } + + if (nodeId.isEmpty) { + nodeId = const Uuid().v4(); + await prefs.setString(_kNodeId, nodeId); + } + final store = LogStore(persistence: persistence, nodeId: nodeId); + await store.load(); + return NoteRepository._(store, nodeId); } - /// Opens a transient in-memory database; intended for tests. - static Future openInMemory() async { - final crdt = await SqliteCrdt.openInMemory( - version: _schemaVersion, - onCreate: _onCreate, - onUpgrade: _onUpgrade, - ); - return NoteRepository._(crdt); + /// Opens a transient in-memory log; intended for tests. + static Future openInMemory({ + String nodeId = 'test-node', + }) async { + final store = LogStore(persistence: _MemoryPersistence(), nodeId: nodeId); + await store.load(); + return NoteRepository._(store, nodeId); } - /// Current schema version. Bump when adding columns and add the matching - /// branch to [_onUpgrade] so existing on-device databases migrate. - static const int _schemaVersion = 3; - - /// Creates the schema for a brand-new database. Plain columns only; the - /// CRDT layer adds its own bookkeeping columns transparently. ISO-8601 - /// strings keep timestamps human-readable and lexicographically sortable. - static Future _onCreate(CrdtTableExecutor db, int version) async { - await db.execute(''' - CREATE TABLE notes ( - id TEXT NOT NULL, - text TEXT NOT NULL DEFAULT '', - priority INTEGER NOT NULL DEFAULT 2, - status INTEGER NOT NULL DEFAULT 0, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - PRIMARY KEY (id) - ) - '''); - } - - /// Migrates an existing database forward one version at a time. + /// Copies every legacy note (tombstones included) into [store] in one write. /// - /// - v1 → v2 adds the [Status] column (rows back-fill to `Status.todo`). - /// - v2 → v3 drops the old "none" priority: any legacy `0` becomes - /// `Priority.medium` (2) so every note has a real priority and shows up - /// in priority filters. This runs deterministically on every device, so - /// the back-fill converges without needing a synced changeset. - static Future _onUpgrade(CrdtTableExecutor db, int from, int to) async { - if (from < 2) { - await db.execute( - 'ALTER TABLE notes ADD COLUMN status INTEGER NOT NULL DEFAULT 0', + /// Each note's field clocks are seeded from its own `updated_at`, not "now", + /// so that when two devices migrate independently, a pre-cutover edit still + /// wins by real edit time under per-field last-writer-wins instead of being + /// resolved arbitrarily. + static Future _seedFromLegacy( + SqliteCrdt legacy, + LogStore store, + String nodeId, + ) async { + final rows = await legacy.query( + 'SELECT id, text, priority, status, created_at, updated_at, is_deleted ' + 'FROM notes', + ); + final log = {}; + for (final row in rows) { + final note = Note.fromRow(row); + final hlc = Hlc( + wallTimeMs: note.updatedAt.millisecondsSinceEpoch, + counter: 0, + nodeId: nodeId, + ); + final deleted = (row['is_deleted'] as int? ?? 0) != 0; + log[note.id] = Record( + id: note.id, + fields: _fieldsFor(note, hlc), + deleted: deleted, + deletedHlc: deleted ? hlc : null, ); } - if (from < 3) { - await db.execute('UPDATE notes SET priority = 2 WHERE priority = 0'); - } + if (log.isNotEmpty) await store.replaceAll(log); } /// Inserts a new note or updates the existing one with the same [id]. /// /// This is the single write path used by the capture screen's - /// character-by-character autosave: it is cheap and idempotent. + /// character-by-character autosave: it is cheap and idempotent. All fields + /// share one fresh monotonic clock (they are written together). Future upsert(Note note) async { - await _crdt.execute( - ''' - INSERT INTO notes (id, text, priority, status, created_at, updated_at) - VALUES (?1, ?2, ?3, ?4, ?5, ?6) - ON CONFLICT (id) DO UPDATE SET - text = ?2, - priority = ?3, - status = ?4, - updated_at = ?6 - ''', - [ - note.id, - note.text, - note.priority.value, - note.status.value, - note.createdAt.toIso8601String(), - note.updatedAt.toIso8601String(), - ], + await _store.upsert( + Record(id: note.id, fields: _fieldsFor(note, _store.nextHlc())), ); - _notifyChanged(); } - /// Soft-deletes a note. The CRDT keeps a tombstone so the deletion - /// propagates on the next sync instead of resurrecting the row. - Future delete(String id) async { - await _crdt.execute('DELETE FROM notes WHERE id = ?1', [id]); - _notifyChanged(); - } + /// Soft-deletes a note. The log keeps a sticky tombstone so the deletion + /// propagates on the next sync instead of resurrecting the record. + Future delete(String id) => _store.delete(id); /// Merges [incoming] notes (e.g. from an imported file) into local storage. /// /// Safe by design — it never destroys a *newer* local edit: an incoming /// note overwrites the local one only when its [Note.updatedAt] is strictly - /// newer, or when the id is not present locally. This makes re-importing a - /// stale backup a no-op for notes you've since edited, upholding the - /// "never lose ideas" guarantee. Notes absent from [incoming] are kept. + /// newer, or when the id is not present locally. Re-importing a stale backup + /// is therefore a no-op for notes you've since edited. Notes absent from + /// [incoming] are kept. Future importNotes(List incoming) async { - final existing = {for (final n in await listNotes()) n.id: n}; + final existing = {for (final n in _liveNotes()) n.id: n}; var added = 0; var updated = 0; var skipped = 0; @@ -185,162 +200,195 @@ class NoteRepository { Future> listNotes({ NoteSort sort = NoteSort.modifiedDesc, NoteFilter filter = const NoteFilter(), - }) async { - final (where, args) = _buildWhere(filter); - final rows = await _crdt.query( - 'SELECT * FROM notes WHERE $where ${_orderBy(sort)}', - args, - ); - return rows.map(Note.fromRow).toList(); - } + }) async => _query(sort, filter); - /// Emits the matching, ordered note list and re-emits whenever the table - /// changes, so the UI can stay in sync without manual refreshes. - /// - /// [watch] takes an args *builder* (`() => args`); the captured [args] - /// list is immutable for this call because [filter] is immutable, so the - /// builder is safe to re-invoke. + /// Emits the matching, ordered note list immediately and re-emits whenever + /// the log changes, so the UI stays in sync without manual refreshes. Stream> watchNotes({ NoteSort sort = NoteSort.modifiedDesc, NoteFilter filter = const NoteFilter(), - }) { - final (where, args) = _buildWhere(filter); - return _crdt - .watch('SELECT * FROM notes WHERE $where ${_orderBy(sort)}', () => args) - .map((rows) => rows.map(Note.fromRow).toList()); + }) => _watch(() => _query(sort, filter)); + + /// Emits the live count of non-deleted notes immediately and on each change. + Stream watchCount() => _watch(_liveCount); + + /// A stream that emits `compute()` synchronously when first listened to (the + /// seed), then again on every log change. Seeding in `onListen` — rather than + /// lazily in an `async*` body — avoids a race where a write between `listen` + /// and the first generator tick would swallow the initial snapshot. + Stream _watch(T Function() compute) { + StreamSubscription? sub; + late final StreamController controller; + controller = StreamController( + onListen: () { + controller.add(compute()); + sub = _store.changes.listen((_) => controller.add(compute())); + }, + onCancel: () => sub?.cancel(), + ); + return controller.stream; } - /// Emits the live count of non-deleted notes. Cheaper than [watchNotes] - /// for a header badge: SQLite computes the count without materialising or - /// parsing any rows, so per-keystroke autosave doesn't churn the UI. - Stream watchCount() { - return _crdt - .watch('SELECT COUNT(*) AS c FROM notes WHERE is_deleted = 0') - .map((rows) => (rows.first['c'] as int?) ?? 0); + /// This device's stable node id. Names its file in the sync repo so two + /// devices never write the same path. + String get nodeId => _nodeId; + + /// Returns this device's full note log for upload. + Log exportLog() => _store.snapshot(); + + /// Merges a remote log into local storage (conflict-free, per-field + /// last-writer-wins with sticky deletes via [mergeLogs]). + Future importLog(Log remote) async { + await _store.replaceAll(mergeLogs(_store.snapshot(), remote)); } - /// This device's stable CRDT node id. Used to name its changeset file - /// in the sync repo so two devices never write the same file. - String get nodeId => _crdt.nodeId; + /// Closes the underlying log and its change stream. + Future close() => _store.close(); - /// Returns this device's full CRDT changeset for upload. - Future getChangeset() => _crdt.getChangeset(); + // --- note <-> record mapping & querying ------------------------------------ - /// Merges a remote changeset into local storage (conflict-free, - /// last-writer-wins per row via the Hybrid Logical Clock). - Future merge(CrdtChangeset changeset) async { - await _crdt.merge(changeset); - _notifyChanged(); + static Map _fieldsFor(Note note, Hlc hlc) => { + _fText: (note.text, hlc), + _fPriority: (note.priority.value, hlc), + _fStatus: (note.status.value, hlc), + _fCreatedAt: (note.createdAt.toIso8601String(), hlc), + _fUpdatedAt: (note.updatedAt.toIso8601String(), hlc), + }; + + Note _toNote(Record record) { + final fields = record.fields; + return Note( + id: record.id, + text: fields[_fText]?.$1 as String? ?? '', + priority: Priority.fromValue(fields[_fPriority]?.$1 as int?), + status: Status.fromValue(fields[_fStatus]?.$1 as int?), + createdAt: _parseDate(fields[_fCreatedAt]?.$1 as String?), + updatedAt: _parseDate(fields[_fUpdatedAt]?.$1 as String?), + ); } - /// Closes the underlying database and the [changes] stream. - Future close() async { - await _changes.close(); - await _crdt.close(); + static DateTime _parseDate(String? iso) => iso == null + ? DateTime.fromMillisecondsSinceEpoch(0) + : DateTime.parse(iso); + + Iterable _liveNotes() => + _store.values.where((r) => !r.deleted).map(_toNote); + + int _liveCount() => _store.values.where((r) => !r.deleted).length; + + List _query(NoteSort sort, NoteFilter filter) { + final notes = _liveNotes().where((n) => _matches(n, filter)).toList() + ..sort(_comparator(sort)); + return notes; } - /// Maps a [NoteSort] to a SQL ORDER BY clause. Centralised so the sort - /// options used by the live and one-shot queries can never drift apart. - String _orderBy(NoteSort sort) { + bool _matches(Note note, NoteFilter filter) { + final query = filter.query.trim().toLowerCase(); + if (query.isNotEmpty && !note.text.toLowerCase().contains(query)) { + return false; + } + if (filter.priorities.isNotEmpty && + !filter.priorities.contains(note.priority)) { + return false; + } + if (filter.statuses.isNotEmpty && !filter.statuses.contains(note.status)) { + return false; + } + if (!_withinDays(note.createdAt, filter.createdFrom, filter.createdTo)) { + return false; + } + if (!_withinDays(note.updatedAt, filter.updatedFrom, filter.updatedTo)) { + return false; + } + return true; + } + + /// Inclusive day-granularity range check, matching the old SQL bounds: + /// `from` includes its whole day; `to` includes its whole day. + static bool _withinDays(DateTime value, DateTime? from, DateTime? to) { + if (from != null && value.isBefore(_startOfDay(from))) return false; + if (to != null && + !value.isBefore(_startOfDay(to).add(const Duration(days: 1)))) { + return false; + } + return true; + } + + static Comparator _comparator(NoteSort sort) { switch (sort) { case NoteSort.createdDesc: - return 'ORDER BY created_at DESC'; + return (a, b) => b.createdAt.compareTo(a.createdAt); case NoteSort.modifiedDesc: - return 'ORDER BY updated_at DESC'; + return (a, b) => b.updatedAt.compareTo(a.updatedAt); case NoteSort.alphabetical: - return 'ORDER BY text COLLATE NOCASE ASC'; + return (a, b) => a.text.toLowerCase().compareTo(b.text.toLowerCase()); case NoteSort.priorityDesc: - return 'ORDER BY priority DESC, updated_at DESC'; - } - } - - /// Builds the parameterised WHERE clause for [filter]. - /// - /// Returns the clause body (always rooted at `is_deleted = 0` so - /// tombstones stay hidden) and the positional argument list. All user - /// input is bound as parameters — never string-interpolated — so the - /// query is injection-safe. Date bounds use ISO-8601 string comparison, - /// which is valid because the stored timestamps are fixed-width and - /// lexicographically ordered. - (String, List) _buildWhere(NoteFilter filter) { - final clauses = ['is_deleted = 0']; - final args = []; - - final query = filter.query.trim(); - if (query.isNotEmpty) { - // Escape LIKE wildcards in the user's text so a literal '%' or '_' - // matches itself. LIKE is ASCII-case-insensitive by default. - final escaped = query - .replaceAll(r'\', r'\\') - .replaceAll('%', r'\%') - .replaceAll('_', r'\_'); - clauses.add(r"text LIKE ? ESCAPE '\'"); - args.add('%$escaped%'); - } - - if (filter.priorities.isNotEmpty) { - final placeholders = List.filled( - filter.priorities.length, - '?', - ).join(', '); - clauses.add('priority IN ($placeholders)'); - args.addAll(filter.priorities.map((p) => p.value)); - } - - if (filter.statuses.isNotEmpty) { - final placeholders = List.filled(filter.statuses.length, '?').join(', '); - clauses.add('status IN ($placeholders)'); - args.addAll(filter.statuses.map((s) => s.value)); - } - - _addDateBounds( - clauses, - args, - 'created_at', - filter.createdFrom, - filter.createdTo, - ); - _addDateBounds( - clauses, - args, - 'updated_at', - filter.updatedFrom, - filter.updatedTo, - ); - - return (clauses.join(' AND '), args); - } - - /// Appends inclusive day-granularity bounds for [column] to [clauses]. - /// - /// [from]/[to] are treated as whole calendar days: `from` includes its - /// entire day (compared from 00:00:00) and `to` includes its entire day - /// (compared with `< to+1 day`), matching how a user reads a date range. - void _addDateBounds( - List clauses, - List args, - String column, - DateTime? from, - DateTime? to, - ) { - if (from != null) { - clauses.add('$column >= ?'); - args.add(_startOfDay(from).toIso8601String()); - } - if (to != null) { - clauses.add('$column < ?'); - args.add(_startOfDay(to).add(const Duration(days: 1)).toIso8601String()); + return (a, b) { + final byPriority = b.priority.value.compareTo(a.priority.value); + return byPriority != 0 + ? byPriority + : b.updatedAt.compareTo(a.updatedAt); + }; } } /// Midnight (local) of [t]'s calendar day. static DateTime _startOfDay(DateTime t) => DateTime(t.year, t.month, t.day); + + // --- legacy sqlite_crdt schema, used only to read the DB during migration - + + static const int _legacyVersion = 3; + + // coverage:ignore-start + // Unreachable: migration only ever opens a legacy DB that already exists + // (guarded by `File(path).existsSync()`), so sqlite never runs onCreate. + // Kept because SqliteCrdt.open requires the callback. + static Future _legacyOnCreate(CrdtTableExecutor db, int version) async { + await db.execute(''' + CREATE TABLE notes ( + id TEXT NOT NULL, + text TEXT NOT NULL DEFAULT '', + priority INTEGER NOT NULL DEFAULT 2, + status INTEGER NOT NULL DEFAULT 0, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + PRIMARY KEY (id) + ) + '''); + } + // coverage:ignore-end + + static Future _legacyOnUpgrade( + CrdtTableExecutor db, + int from, + int to, + ) async { + if (from < 2) { + await db.execute( + 'ALTER TABLE notes ADD COLUMN status INTEGER NOT NULL DEFAULT 0', + ); + } + if (from < 3) { + await db.execute('UPDATE notes SET priority = 2 WHERE priority = 0'); + } + } +} + +/// In-memory [LogPersistence] for [NoteRepository.openInMemory] (tests). +class _MemoryPersistence implements LogPersistence { + String? _text; + + @override + Future read() async => _text; + + @override + Future write(String text) async => _text = text; } /// An immutable set of constraints for querying notes. /// /// All fields combine with logical AND. Empty/null fields impose no -/// constraint. Lives in the data layer so the SQL it drives never leaks +/// constraint. Lives in the data layer so the filtering it drives never leaks /// into the UI. Construct copies with [copyWith] when toggling one facet. class NoteFilter { const NoteFilter({ @@ -392,7 +440,7 @@ class NoteFilter { } /// Returns a copy with selected facets replaced. A `null` argument keeps - /// the current value; clearing a date is done via the dedicated [clear] + /// the current value; clearing a date is done via the dedicated clear /// flags so `null` can mean "unchanged". NoteFilter copyWith({ String? query, diff --git a/lib/sync/sync_service.dart b/lib/sync/sync_service.dart index d76fa6d..7648d1c 100644 --- a/lib/sync/sync_service.dart +++ b/lib/sync/sync_service.dart @@ -1,6 +1,6 @@ -import 'dart:convert'; - -import 'package:sqlite_crdt/sqlite_crdt.dart'; +// This service uses todo's own GitHubClient (it tracks SHAs and returns +// GitHubFile entries); hide crdt_sync's same-named client to disambiguate. +import 'package:crdt_sync/crdt_sync.dart' hide GitHubClient; import '../data/note_repository.dart'; import 'github_client.dart'; @@ -9,10 +9,10 @@ import 'github_client.dart'; class SyncResult { const SyncResult({required this.mergedDevices, required this.pushed}); - /// How many other devices' changesets were pulled and merged. + /// How many other devices' logs were pulled and merged. final int mergedDevices; - /// Whether this device pushed its own changeset. + /// Whether this device pushed its own log. final bool pushed; @override @@ -22,26 +22,29 @@ class SyncResult { /// Synchronises a [NoteRepository] with a GitHub repo used as dumb storage. /// -/// Design: each device owns exactly one file, -/// `todo-sync/changesets/.json`, -/// holding its full CRDT changeset. Because no two devices ever write the -/// same file, there are **no git-level merge conflicts**. Data convergence -/// is handled entirely by the CRDT layer: pulling every other device's -/// changeset and [NoteRepository.merge]-ing it is commutative and -/// idempotent, so repeated syncs in any order converge to the same state. +/// Design: each device owns exactly one file, `todo-sync/notes/.json`, +/// holding its full crdt_sync note [Log]. Because no two devices ever write the +/// same file, there are **no git-level merge conflicts**; convergence is the +/// CRDT layer's job — pulling every other device's log and [NoteRepository] +/// merging it via `mergeLogs` is commutative and idempotent, so repeated syncs +/// in any order converge to the same state. +/// +/// The directory is `notes/` (not the legacy `changesets/`): the on-the-wire +/// format changed from a `sqlite_crdt` changeset to a crdt_sync `Log`, so the +/// two never share a path and an old client can't misread a new file. class SyncService { - const SyncService({this.changesetDir = 'todo-sync/changesets'}); + const SyncService({this.notesDir = 'todo-sync/notes'}); - /// Directory in the repo under which per-device changeset files live. - final String changesetDir; + /// Directory in the repo under which per-device note-log files live. + final String notesDir; /// Runs a full pull-merge-push cycle. Safe to call repeatedly. Future sync(NoteRepository repo, GitHubClient github) async { final nodeId = repo.nodeId; final ownFileName = '$nodeId.json'; - // 1. Pull: list all device changeset files, merge everyone else's. - final files = await github.listDirectory(changesetDir); + // 1. Pull: list all device log files, merge everyone else's. + final files = await github.listDirectory(notesDir); var merged = 0; String? ownSha; for (final file in files) { @@ -51,15 +54,16 @@ class SyncService { } final text = await github.getFileText(file.path); if (text == null) continue; - await repo.merge(_decodeChangeset(text)); + final remote = _decodeLog(text); + if (remote == null) continue; // Skip a corrupt/foreign file. + await repo.importLog(remote); merged++; } - // 2. Push: upload our own (now-merged) changeset under our node id. - final changeset = await repo.getChangeset(); + // 2. Push: upload our own (now-merged) log under our node id. await github.putFileText( - '$changesetDir/$ownFileName', - _encodeChangeset(changeset), + '$notesDir/$ownFileName', + logToJson(repo.exportLog()), sha: ownSha, message: 'sync: $nodeId @ ${DateTime.now().toUtc().toIso8601String()}', ); @@ -67,28 +71,15 @@ class SyncService { return SyncResult(mergedDevices: merged, pushed: true); } - /// Serialises a changeset to pretty JSON. All values are already - /// primitives (hlc/modified are stored as TEXT), so no custom encoding - /// is needed. - String _encodeChangeset(CrdtChangeset changeset) => - const JsonEncoder.withIndent(' ').convert(changeset); - - /// Parses a JSON changeset back into the typed shape that - /// [NoteRepository.merge] expects. - /// - /// The `hlc` field must be revived from its string form into an [Hlc] - /// object, because `validateChangeset` casts it directly to [Hlc]. - CrdtChangeset _decodeChangeset(String text) { - final raw = jsonDecode(text) as Map; - return raw.map( - (table, records) => MapEntry( - table, - (records as List).map((r) { - final record = (r as Map).cast(); - record['hlc'] = Hlc.parse(record['hlc'] as String); - return record; - }).toList(), - ), - ); + /// Parses a remote note log, returning `null` for a corrupt or wrong-shape + /// payload so one bad device file never aborts the whole sync. + Log? _decodeLog(String text) { + try { + return logFromJson(text); + } on FormatException { + return null; + } on TypeError { + return null; + } } } diff --git a/lib/ui/note_editor.dart b/lib/ui/note_editor.dart index 8232e68..aa3e961 100644 --- a/lib/ui/note_editor.dart +++ b/lib/ui/note_editor.dart @@ -510,9 +510,12 @@ class _NoteEditorState extends State { return MarkdownView(text: _currentText()); case NoteEditorMode.raw: return _buildRaw(theme); + // coverage:ignore-start + // Unreachable: build() short-circuits to _buildStepPage before reaching + // here, so this arm only exists to make the switch exhaustive. case NoteEditorMode.guided: - // build() short-circuits to _buildStepPage before reaching here. return _buildRaw(theme); + // coverage:ignore-end } } diff --git a/pubspec.lock b/pubspec.lock index e9915d5..a0f56fc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -73,6 +73,15 @@ packages: url: "https://pub.dev" source: hosted version: "5.1.3" + crdt_sync: + dependency: "direct main" + description: + path: crdt_sync_dart + ref: "crdt_sync_dart-v0.2.0" + resolved-ref: "5be99ab11d1657ec04edd65508b8c4ecae4da906" + url: "https://github.com/kuhyx/utils" + source: git + version: "0.2.0" cross_file: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e39e751..793a76a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,6 +34,15 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 + # Shared CRDT sync (LogStore + Record/Log merge + GitHub transport). The + # primary store since the migration off sqlite_crdt. + crdt_sync: + git: + url: https://github.com/kuhyx/utils + ref: crdt_sync_dart-v0.2.0 + path: crdt_sync_dart + # Retained only to read the legacy on-device DB during the one-time + # migration in NoteRepository.open; remove once all devices are migrated. sqlite_crdt: ^3.0.4 path_provider: ^2.1.5 path: ^1.9.1 diff --git a/test/fake_note_repository.dart b/test/fake_note_repository.dart index 9a98eac..94462e7 100644 --- a/test/fake_note_repository.dart +++ b/test/fake_note_repository.dart @@ -1,6 +1,6 @@ import 'dart:async'; -import 'package:sqlite_crdt/sqlite_crdt.dart'; +import 'package:crdt_sync/crdt_sync.dart'; import 'package:todo/data/note.dart'; import 'package:todo/data/note_repository.dart'; @@ -97,10 +97,10 @@ class FakeNoteRepository implements NoteRepository { String get nodeId => 'fake-node'; @override - Future getChangeset() async => {}; + Log exportLog() => {}; @override - Future merge(CrdtChangeset changeset) async {} + Future importLog(Log remote) async {} @override Future close() async { diff --git a/test/note_repository_test.dart b/test/note_repository_test.dart index 8cdcb89..9dedd92 100644 --- a/test/note_repository_test.dart +++ b/test/note_repository_test.dart @@ -1,8 +1,12 @@ import 'dart:io'; +import 'package:crdt_sync/crdt_sync.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; -import 'package:sqlite_crdt/sqlite_crdt.dart'; +// SqliteCrdt is needed to seed the legacy DB in migration tests; hide its Hlc +// so the crdt_sync Hlc/Record are unambiguous. +import 'package:sqlite_crdt/sqlite_crdt.dart' hide Hlc; import 'package:todo/data/note.dart'; import 'package:todo/data/note_repository.dart'; @@ -51,11 +55,9 @@ void main() { expect(notes, hasLength(1)); expect(notes.single.text, 'keep me'); - // The tombstone must survive in the changeset so the deletion syncs. - final changeset = await repo.getChangeset(); - final rows = changeset['notes']!; - final deleted = rows.firstWhere((r) => r['id'] == 'b'); - expect(deleted['is_deleted'], 1); + // The tombstone must survive in the exported log so the deletion syncs. + final log = repo.exportLog(); + expect(log['b']!.deleted, isTrue); }); test('priority sort orders highest first', () async { @@ -70,6 +72,21 @@ void main() { expect(notes.last.text, 'low'); }); + test('priority sort breaks ties by most-recently-updated', () async { + final repo = await NoteRepository.openInMemory(); + addTearDown(repo.close); + + await repo.upsert( + note('a', 'older', priority: Priority.high, updatedAt: DateTime(2026)), + ); + await repo.upsert( + note('b', 'newer', priority: Priority.high, updatedAt: DateTime(2026, 6)), + ); + + final notes = await repo.listNotes(sort: NoteSort.priorityDesc); + expect(notes.map((n) => n.text), ['newer', 'older']); + }); + group('text search', () { test('matches a case-insensitive substring', () async { final repo = await NoteRepository.openInMemory(); @@ -273,6 +290,7 @@ void main() { }); test('v2→v3 migration backfills priority 0 to medium', () async { + SharedPreferences.setMockInitialValues({}); final dir = await Directory.systemTemp.createTemp('todo_migration'); final path = '${dir.path}/notes.db'; addTearDown(() => dir.delete(recursive: true)); @@ -314,6 +332,7 @@ void main() { }); test('v1→v2 migration adds the status column with a default', () async { + SharedPreferences.setMockInitialValues({}); final dir = await Directory.systemTemp.createTemp('todo_migration_v1'); final path = '${dir.path}/notes.db'; addTearDown(() => dir.delete(recursive: true)); @@ -377,39 +396,183 @@ void main() { }); }); - test('nodeId, changeset merge and close', () async { - // Use file-backed DBs: two openInMemory repos would share one `:memory:` - // connection, so they cannot model two independent devices. - final dir = await Directory.systemTemp.createTemp('todo_merge'); - addTearDown(() => dir.delete(recursive: true)); - final source = await NoteRepository.open('${dir.path}/source.db'); - final target = await NoteRepository.open('${dir.path}/target.db'); + test('nodeId, log export/import merge and close', () async { + // Each openInMemory repo owns its own in-memory log, so two of them model + // two independent devices directly. + final source = await NoteRepository.openInMemory(nodeId: 'source'); + final target = await NoteRepository.openInMemory(nodeId: 'target'); addTearDown(target.close); - expect(source.nodeId, isNotEmpty); + expect(source.nodeId, 'source'); await source.upsert(note('a', 'shared idea')); - final changeset = await source.getChangeset(); + final log = source.exportLog(); await source.close(); - // getChangeset serialises hlc/modified as Strings and returns read-only - // rows; merge expects mutable maps with Hlc objects. Rebuild them the - // way the sync layer does after its JSON round-trip. - final revived = { - for (final entry in changeset.entries) - entry.key: [ - for (final record in entry.value) - { - ...record, - 'hlc': Hlc.parse(record['hlc'] as String), - 'modified': Hlc.parse(record['modified'] as String), - }, - ], - }; - await target.merge(revived); + await target.importLog(log); final merged = await target.listNotes(); expect(merged.single.text, 'shared idea'); }); + group('migration from a legacy sqlite_crdt database', () { + // Writes a v3 legacy DB at [path] with one live note and one tombstone, + // exactly as the pre-crdt_sync app would have left it. + Future seedLegacyDb(String dir) async { + final path = '$dir/todo.db'; + final legacy = await SqliteCrdt.open( + path, + version: 3, + onCreate: (db, _) async { + await db.execute(''' + CREATE TABLE notes ( + id TEXT NOT NULL, + text TEXT NOT NULL DEFAULT '', + priority INTEGER NOT NULL DEFAULT 2, + status INTEGER NOT NULL DEFAULT 0, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + PRIMARY KEY (id) + ) + '''); + }, + ); + Future insert(String id, String text, int status) => legacy.execute( + 'INSERT INTO notes (id, text, priority, status, created_at, ' + 'updated_at) VALUES (?1, ?2, 3, ?3, ?4, ?4)', + [id, text, status, '2026-01-01T00:00:00.000'], + ); + await insert('live', 'kept idea', Status.inProgress.value); + await insert('gone', 'deleted idea', Status.todo.value); + await legacy.execute('DELETE FROM notes WHERE id = ?1', ['gone']); + await legacy.close(); + return path; + } + + test('imports live notes and preserves tombstones, once', () async { + SharedPreferences.setMockInitialValues({}); + final dir = await Directory.systemTemp.createTemp('todo_migrate'); + addTearDown(() => dir.delete(recursive: true)); + final path = await seedLegacyDb(dir.path); + + final repo = await NoteRepository.open(path); + addTearDown(repo.close); + + // The live note migrated with its fields intact... + final notes = await repo.listNotes(); + expect(notes.map((n) => n.text), ['kept idea']); + expect(notes.single.priority, Priority.high); + expect(notes.single.status, Status.inProgress); + // ...and the deletion carried over as a sticky tombstone. + expect(repo.exportLog()['gone']!.deleted, isTrue); + expect(repo.nodeId, isNotEmpty); + + // Reopening does not re-run migration: a note added after the first open + // survives, and the (still-present) legacy DB is not re-imported. + await repo.upsert(note('new', 'added post-migration')); + await repo.close(); + final reopened = await NoteRepository.open(path); + addTearDown(reopened.close); + final texts = (await reopened.listNotes()).map((n) => n.text).toSet(); + expect(texts, {'kept idea', 'added post-migration'}); + }); + + test('a fresh install with no legacy DB starts empty', () async { + SharedPreferences.setMockInitialValues({}); + final dir = await Directory.systemTemp.createTemp('todo_fresh'); + addTearDown(() => dir.delete(recursive: true)); + + final repo = await NoteRepository.open('${dir.path}/todo.db'); + addTearDown(repo.close); + + expect(await repo.listNotes(), isEmpty); + expect(repo.nodeId, isNotEmpty); + }); + }); + + test('watchNotes emits the seed then re-emits after each change', () async { + final repo = await NoteRepository.openInMemory(); + addTearDown(repo.close); + final lengths = []; + final sub = repo.watchNotes().listen((notes) => lengths.add(notes.length)); + + await repo.upsert(note('a', 'first')); + await repo.upsert(note('b', 'second')); + await pumpEventQueue(); + await sub.cancel(); + + expect(lengths.first, 0); // seed + expect(lengths.last, 2); // after both writes + }); + + test('watchCount emits the seed then re-emits after a change', () async { + final repo = await NoteRepository.openInMemory(); + addTearDown(repo.close); + final counts = []; + final sub = repo.watchCount().listen(counts.add); + + await repo.upsert(note('a', 'x')); + await pumpEventQueue(); + await sub.cancel(); + + expect(counts.first, 0); + expect(counts.last, 1); + }); + + test('a foreign record missing timestamps falls back to the epoch', () async { + // A note this app writes always has both timestamps; this guards a + // malformed record arriving from another device via importLog. + final repo = await NoteRepository.openInMemory(); + addTearDown(repo.close); + await repo.importLog({ + 'partial': Record( + id: 'partial', + fields: {'text': ('no dates', Hlc.newTick('other'))}, + ), + }); + + final only = (await repo.listNotes()).single; + expect(only.text, 'no dates'); + final epoch = DateTime.fromMillisecondsSinceEpoch(0); + expect(only.createdAt, epoch); + expect(only.updatedAt, epoch); + }); + + test('date-range filters bound by whole calendar days', () async { + final repo = await NoteRepository.openInMemory(); + addTearDown(repo.close); + await repo.upsert( + note( + 'jan', + 'january idea', + createdAt: DateTime(2026, 1, 10), + updatedAt: DateTime(2026, 1, 10), + ), + ); + await repo.upsert( + note( + 'mar', + 'march idea', + createdAt: DateTime(2026, 3, 10), + updatedAt: DateTime(2026, 3, 10), + ), + ); + + final byCreated = await repo.listNotes( + filter: NoteFilter( + createdFrom: DateTime(2026, 2, 1), + createdTo: DateTime(2026, 4, 1), + ), + ); + expect(byCreated.map((n) => n.text), ['march idea']); + + final byUpdated = await repo.listNotes( + filter: NoteFilter( + updatedFrom: DateTime(2026, 1, 1), + updatedTo: DateTime(2026, 1, 31), + ), + ); + expect(byUpdated.map((n) => n.text), ['january idea']); + }); + group('NoteFilter', () { test('a default filter is empty (all facets cleared)', () { // Evaluates the full conjunction in `isEmpty`, including the date bounds. @@ -440,20 +603,6 @@ void main() { }); }); - // getChangeset() serialises HLCs as String and returns read-only rows; - // merge() casts `hlc` to Hlc and mutates, so revive it into fresh mutable - // maps with parsed HLCs first (mirrors SyncService._decodeChangeset). - CrdtChangeset reviveChangeset(CrdtChangeset raw) => raw.map( - (table, records) => MapEntry( - table, - records.map((r) { - final record = Map.from(r); - record['hlc'] = Hlc.parse(record['hlc'] as String); - return record; - }).toList(), - ), - ); - group('changes', () { test('ticks once per write across upsert, delete, and merge', () async { final repo = await NoteRepository.openInMemory(); @@ -465,11 +614,11 @@ void main() { await repo.upsert(note('a', 'x')); await repo.delete('a'); - // A changeset from another device, merged in, must also tick. - final other = await NoteRepository.openInMemory(); + // A log from another device, merged in, must also tick. + final other = await NoteRepository.openInMemory(nodeId: 'other'); addTearDown(other.close); await other.upsert(note('b', 'from other')); - await repo.merge(reviveChangeset(await other.getChangeset())); + await repo.importLog(other.exportLog()); await pumpEventQueue(); // let the broadcast events dispatch expect(ticks, 3); diff --git a/test/sync_service_test.dart b/test/sync_service_test.dart index 7dceadf..31f4460 100644 --- a/test/sync_service_test.dart +++ b/test/sync_service_test.dart @@ -1,38 +1,33 @@ import 'dart:convert'; -import 'dart:io'; +import 'package:crdt_sync/crdt_sync.dart' hide GitHubClient; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; -import 'package:sqflite_common_ffi/sqflite_ffi.dart'; 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'; +GitHubClient _github(MockClient mock) => + GitHubClient(owner: 'o', repo: 'r', token: 't', httpClient: mock); + +Note _note(String id, String text) => Note( + id: id, + text: text, + priority: Priority.medium, + status: Status.todo, + createdAt: DateTime(2026, 6, 15), + updatedAt: DateTime(2026, 6, 15), +); + void main() { - setUpAll(sqfliteFfiInit); - test('sync pulls and merges another device, then pushes its own', () async { - final dir = await Directory.systemTemp.createTemp('todo_sync'); - addTearDown(() => dir.delete(recursive: true)); - - // Build a second device's changeset and serialise it the way it would - // be stored in the repo (hlc as a String, base64 in the API response). - final other = await NoteRepository.open('${dir.path}/other.db'); - await other.upsert( - Note( - id: 'x', - text: 'from other device', - priority: Priority.medium, - status: Status.todo, - createdAt: DateTime(2026, 6, 15), - updatedAt: DateTime(2026, 6, 15), - ), - ); - final otherJson = const JsonEncoder.withIndent( - ' ', - ).convert(await other.getChangeset()); + // Build a second device's note log and serialise it the way it is stored + // in the repo (a crdt_sync Log JSON, base64 in the API response). + final other = await NoteRepository.openInMemory(nodeId: 'otherNode'); + await other.upsert(_note('x', 'from other device')); + final otherJson = logToJson(other.exportLog()); await other.close(); final fileResponse = jsonEncode({ 'content': base64.encode(utf8.encode(otherJson)), @@ -43,7 +38,7 @@ void main() { { 'type': 'file', 'name': otherFile, - 'path': 'todo-sync/changesets/$otherFile', + 'path': 'todo-sync/notes/$otherFile', 'sha': 'sha-other', }, ]); @@ -54,75 +49,57 @@ void main() { putCount++; return http.Response('{}', 200); } - if (req.url.path.endsWith('/contents/todo-sync/changesets')) { + if (req.url.path.endsWith('/contents/todo-sync/notes')) { return http.Response(listResponse, 200); // directory listing } return http.Response(fileResponse, 200); // the other device's file }); - final local = await NoteRepository.open('${dir.path}/local.db'); + final local = await NoteRepository.openInMemory(nodeId: 'localNode'); addTearDown(local.close); - final github = GitHubClient( - owner: 'o', - repo: 'r', - token: 't', - httpClient: mock, - ); - final result = await const SyncService().sync(local, github); + final result = await const SyncService().sync(local, _github(mock)); expect(result.mergedDevices, 1); expect(result.pushed, isTrue); expect(result.toString(), contains('mergedDevices: 1')); - expect(putCount, 1); // pushed our own changeset + expect(putCount, 1); // pushed our own log final texts = (await local.listNotes()).map((n) => n.text); expect(texts, contains('from other device')); }); - test('sync with no remote files still pushes own changeset', () async { - final dir = await Directory.systemTemp.createTemp('todo_sync_empty'); - addTearDown(() => dir.delete(recursive: true)); - + test('sync with no remote files still pushes own log', () async { var putCount = 0; final mock = MockClient((req) async { if (req.method == 'PUT') { putCount++; return http.Response('{}', 200); } - return http.Response('', 404); // empty/missing changesets dir + return http.Response('', 404); // empty/missing notes dir }); - final local = await NoteRepository.open('${dir.path}/local.db'); + final local = await NoteRepository.openInMemory(nodeId: 'localNode'); addTearDown(local.close); - final github = GitHubClient( - owner: 'o', - repo: 'r', - token: 't', - httpClient: mock, - ); - final result = await const SyncService().sync(local, github); + final result = await const SyncService().sync(local, _github(mock)); expect(result.mergedDevices, 0); expect(result.pushed, isTrue); expect(putCount, 1); }); - test('sync updates its own already-present changeset file', () async { - final dir = await Directory.systemTemp.createTemp('todo_sync_own'); - addTearDown(() => dir.delete(recursive: true)); - - final local = await NoteRepository.open('${dir.path}/local.db'); + test('sync updates its own already-present log file', () async { + final local = await NoteRepository.openInMemory(nodeId: 'localNode'); addTearDown(local.close); - // The remote listing already contains *this* device's changeset file. - // The sync must recognise it (by node id), skip merging itself, remember - // the sha, and PUT an update rather than treating it as a peer device. + // The remote listing already contains *this* device's log file. The sync + // must recognise it (by node id), skip merging itself, remember the sha, + // and PUT an update rather than treating it as a peer device. final ownFile = '${local.nodeId}.json'; final listResponse = jsonEncode([ { 'type': 'file', 'name': ownFile, - 'path': 'todo-sync/changesets/$ownFile', + 'path': 'todo-sync/notes/$ownFile', 'sha': 'own-sha-123', }, ]); @@ -137,16 +114,50 @@ void main() { return http.Response(listResponse, 200); }); - final github = GitHubClient( - owner: 'o', - repo: 'r', - token: 't', - httpClient: mock, - ); - - final result = await const SyncService().sync(local, github); + final result = await const SyncService().sync(local, _github(mock)); expect(result.mergedDevices, 0); // own file is not a peer to merge expect(result.pushed, isTrue); expect(putSha, 'own-sha-123'); // updated in place using the remembered sha }); + + test('a corrupt or wrong-shape peer file is skipped, not fatal', () async { + String file(String content) => + jsonEncode({'content': base64.encode(utf8.encode(content))}); + final listResponse = jsonEncode([ + { + 'type': 'file', + 'name': 'bad1.json', + 'path': 'todo-sync/notes/bad1.json', + 'sha': 's1', + }, + { + 'type': 'file', + 'name': 'bad2.json', + 'path': 'todo-sync/notes/bad2.json', + 'sha': 's2', + }, + ]); + + var putCount = 0; + final mock = MockClient((req) async { + if (req.method == 'PUT') { + putCount++; + return http.Response('{}', 200); + } + if (req.url.path.endsWith('/contents/todo-sync/notes')) { + return http.Response(listResponse, 200); + } + if (req.url.path.endsWith('bad1.json')) { + return http.Response(file('{not json'), 200); // FormatException + } + return http.Response(file('[]'), 200); // valid JSON, wrong shape + }); + + final local = await NoteRepository.openInMemory(nodeId: 'localNode'); + addTearDown(local.close); + + final result = await const SyncService().sync(local, _github(mock)); + expect(result.mergedDevices, 0); // both bad files skipped + expect(putCount, 1); // still pushed our own log + }); } diff --git a/tool/sync_smoke.dart b/tool/sync_smoke.dart index c63634b..e16320f 100644 --- a/tool/sync_smoke.dart +++ b/tool/sync_smoke.dart @@ -2,29 +2,26 @@ // // 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 changeset files afterwards so the real -// `changesets/` directory is never touched. +// 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:sqflite_common_ffi/sqflite_ffi.dart'; 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 main() async { - sqfliteFfiInit(); - 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 `changesets/`. - const service = SyncService(changesetDir: 'todo-sync/changesets_smoketest'); + // 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); @@ -57,14 +54,12 @@ Future main() async { final converged = aNotes.containsAll(expected) && bNotes.containsAll(expected); - // Cleanup: remove the throwaway changeset files. + // Cleanup: remove the throwaway log files. final cleanup = client(); - for (final f in await cleanup.listDirectory( - 'todo-sync/changesets_smoketest', - )) { + 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 changeset files.'); + stdout.writeln('Cleaned up throwaway log files.'); ghA.close(); ghB.close();