// Notes: the sync's "keep both copies" (SyncStore.forks — the same cases as the dashboard's syncdb // "fork"), saving on an old version, and ticking checkboxes. flutter test test/notes_test.dart import 'dart:io'; import 'package:dash_app/notes/markdown.dart'; import 'package:dash_app/notes/notes_model.dart'; import 'package:dash_app/sync/store.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { final dir = Directory.systemTemp.createTempSync('notes_test'); var k = 0; SyncStore dev(String name) => SyncStore('${dir.path}/${k++}.db', name); /// Everything [from] hasn't sent yet → the others. void send(SyncStore from, List to) { final (changes, mark) = from.changes(); from.markPushed(mark); for (final s in to) { s.apply(changes); } } List show(SyncStore s) => [for (final n in allNotes(s)) '${n.body}${n.origin != null ? ' (copy)' : ''}${n.pinned ? ' (pinned)' : ''}']..sort(); String main(SyncStore s) => allNotes(s).firstWhere((n) => n.origin == null).id; void edit(SyncStore s, String body) { final n = getNote(s, main(s))!; saveNote(s, id: n.id, body: body, base: n.rev); sleep(const Duration(milliseconds: 3)); } test('two devices editing one note keep both texts, and all agree', () { final a = dev('aaaaaaaaaaaa'), b = dev('bbbbbbbbbbbb'), c = dev('cccccccccccc'); saveNote(a, body: 'v0'); send(a, [b, c]); edit(a, 'v1'); edit(a, 'v2'); send(a, [b, c]); expect(show(b), ['v2'], reason: 'one after the other: no copy'); edit(a, 'A-text'); edit(b, 'B-text'); send(a, [b, c]); send(b, [a, c]); expect(show(a), ['A-text (copy)', 'B-text']); expect(show(b), show(a)); expect(show(c), show(a)); // b edits the text, then a (older text) pins: b's text + a's pin, everywhere edit(b, 'B2'); send(b, [c]); setNote(a, main(a), {'pinned': 1}); send(a, [b, c]); send(b, [a, c]); send(c, [a, b]); expect(show(a), ['A-text (copy)', 'B2 (pinned)']); expect(show(b), show(a)); expect(show(c), show(a)); // a copy deleted on one device is gone everywhere and not made again a.delete(noteTable, allNotes(a).firstWhere((n) => n.origin != null).id); send(a, [b, c]); expect(show(b), ['B2 (pinned)']); expect(show(c), ['B2 (pinned)']); }); test('typing goes on in one version until it has been sent', () { final a = dev('aaaaaaaaaaaa'); final (id, _) = saveNote(a, body: 'x'); final r0 = getNote(a, id)!.rev; saveNote(a, id: id, body: 'xy', base: r0); expect(getNote(a, id)!.rev, r0); a.markPushed(a.changes().$2); saveNote(a, id: id, body: 'xyz', base: r0); final n = getNote(a, id)!; expect(n.rev, isNot(r0)); expect(n.r['revs'], '$r0 ${n.rev}'); }); test('a save on an old version becomes a copy', () { final a = dev('aaaaaaaaaaaa'), b = dev('bbbbbbbbbbbb'); final (id, _) = saveNote(a, body: 'start'); send(a, [b]); final seen = getNote(b, id)!.rev; // b opens it … edit(a, 'changed on a'); send(a, [b]); // … a's change arrives while b types final (nid, conflict) = saveNote(b, id: id, body: 'typed on b', base: seen); expect(conflict, isTrue); expect(nid, isNot(id)); expect(show(b), ['changed on a', 'typed on b (copy)']); }); test('checkboxes tick by their number, outside code blocks', () { const src = '- [ ] one\n```\n- [ ] not this\n```\n> - [x] two\n1. [ ] three'; expect(toggleTask(src, 0), contains('- [x] one')); expect(toggleTask(src, 1), contains('> - [ ] two')); expect(toggleTask(src, 2), contains('1. [x] three')); expect(toggleTask(src, 2), contains('- [ ] not this')); }); test('title, tags, folders', () { expect(noteTitle('\n\n## Shopping list\n- milk'), 'Shopping list'); expect(noteTags('#home and #Work/acme, not a#b, `#code`, # heading'), ['home', 'work/acme']); expect(cleanFolder(' Work / Acme/ '), 'Work/Acme'); }); }