/// Tests for the dual-write decorator used during the GitHub -> Firebase /// cutover. /// /// The asymmetry is the whole point and is what these assert: a primary /// failure must fail the tick, a mirror failure must not, and reads must /// consult both so that a half-migrated app (one device moved, one not) still /// converges in both directions. library; import 'package:crdt_sync/crdt_sync.dart'; import 'package:test/test.dart'; /// A scriptable in-memory [RemoteStore] that can be told to fail. class _FakeStore implements RemoteStore, BulkMapReader { _FakeStore({Map? files, this.failing = false}) : files = files ?? {}; final Map files; bool failing; final List writes = []; bool closed = false; void _guard(String what) { if (failing) throw RemoteSyncError('$what failed'); } @override Future> listDirectory(String path) async { _guard('list'); return files.keys .where((key) => key.startsWith('$path/')) .map((key) => key.substring(path.length + 1).split('/').first) .toSet() .toList(); } @override Future getFileText(String path) async { _guard('read'); return files[path]; } @override Future putFileText( String path, String text, { required String message, }) async { _guard('write'); writes.add(path); files[path] = text; } @override Future> getStringMap(String path) async { _guard('map read'); return { for (final entry in files.entries) if (entry.key.startsWith('$path/')) entry.key.substring(path.length + 1): entry.value, }; } @override Future deleteFile(String path, {String message = ''}) async { _guard('delete'); files.remove(path); } @override Future canAccessRemote() async => !failing; @override void close() => closed = true; } ({ MirrorStore store, _FakeStore primary, _FakeStore mirror, List failures, }) _mirror({ Map? primaryFiles, Map? mirrorFiles, bool primaryFailing = false, bool mirrorFailing = false, }) { final primary = _FakeStore(files: primaryFiles, failing: primaryFailing); final mirror = _FakeStore(files: mirrorFiles, failing: mirrorFailing); final failures = []; return ( store: MirrorStore( primary: primary, mirror: mirror, onMirrorFailure: (operation, _) => failures.add(operation), ), primary: primary, mirror: mirror, failures: failures, ); } void main() { group('writes', () { test('go to both backends', () async { final m = _mirror(); await m.store.putFileText('ns/pc/log.json', '{}', message: 'm'); expect(m.primary.writes, ['ns/pc/log.json']); expect(m.mirror.writes, ['ns/pc/log.json']); }); test('a primary failure fails the tick', () async { // Fail-closed: the primary is authoritative, so a sync that could not // write it must not be reported as successful. final m = _mirror(primaryFailing: true); await expectLater( () => m.store.putFileText('ns/pc/log.json', '{}', message: 'm'), throwsA(isA()), ); }); test('a mirror failure is loud but does not fail the tick', () async { final m = _mirror(mirrorFailing: true); await m.store.putFileText('ns/pc/log.json', '{}', message: 'm'); expect(m.primary.writes, ['ns/pc/log.json']); expect(m.failures, ['putFileText ns/pc/log.json']); }); test('deletes behave the same way', () async { final m = _mirror( primaryFiles: {'ns/pc/log.json': '{}'}, mirrorFiles: {'ns/pc/log.json': '{}'}, mirrorFailing: true, ); await m.store.deleteFile('ns/pc/log.json'); expect(m.primary.files, isEmpty); expect(m.failures, ['deleteFile ns/pc/log.json']); }); test('a primary delete failure fails the tick', () async { final m = _mirror(primaryFailing: true); await expectLater( () => m.store.deleteFile('ns/pc/log.json'), throwsA(isA()), ); }); }); group('reads consult both backends', () { test('prefer the primary when it has the file', () async { final m = _mirror( primaryFiles: {'ns/pc/log.json': 'from-primary'}, mirrorFiles: {'ns/pc/log.json': 'from-mirror'}, ); expect(await m.store.getFileText('ns/pc/log.json'), 'from-primary'); }); test('fall back to the mirror for an un-migrated device', () async { // This is why reads are not primary-only: a migrated desktop must // still see an un-migrated phone's writes, or convergence silently // becomes one-directional with no error raised. final m = _mirror(mirrorFiles: {'ns/phone/log.json': 'from-mirror'}); expect(await m.store.getFileText('ns/phone/log.json'), 'from-mirror'); }); test('return null when neither backend has the file', () async { final m = _mirror(); expect(await m.store.getFileText('ns/nobody/log.json'), isNull); }); test('a mirror read failure degrades to the primary answer', () async { final m = _mirror(mirrorFailing: true); expect(await m.store.getFileText('ns/pc/log.json'), isNull); expect(m.failures, ['getFileText ns/pc/log.json']); }); test('a primary read failure with nothing in the mirror throws', () async { // Named for what it actually exercises: the primary throws AND the // mirror does not hold the file, so neither side has an answer. A // primary failure alone no longer fails the read -- see the // "primary read fallback" group. final m = _mirror(primaryFailing: true); await expectLater( () => m.store.getFileText('ns/pc/log.json'), throwsA(isA()), ); }); }); }