utils/crdt_sync_dart/tool/interop_check.dart
Krzysztof Rudnicki 02e2e13790 crdt_sync: add a Firebase Realtime Database transport behind a RemoteStore seam
Extracts the three-method remote surface GitHub was reached through into a
`RemoteStore` interface, then implements it against Firebase Realtime
Database over REST, so apps can move backend with a constructor change.

Why REST and not the SDKs: official FlutterFire has no Linux desktop
support, and `wake_alarm`'s PC side is a dependency-free systemd job. The
RTDB REST endpoints are plain HTTPS and behave identically on Linux,
Android and headless. Firebase is a transport, never the datastore -- each
app keeps its local log as the source of truth, so offline-first survives.

Why RTDB and not Firestore: on the Spark plan RTDB bills only storage and
bandwidth with no per-operation quota, so a runaway loop cannot exhaust a
daily budget and silently stop syncing mid-day.

Two savings make the no-cost tier a guarantee rather than a hope. Measured
against the GitHub-backed history this replaces: 4894 of 5542 commits
(88.3%) were byte-identical no-ops, and every tick re-downloaded every
peer's whole log. Both are now driven by one sha256 revision -- an
unchanged log is not re-pushed, and an unchanged peer is not re-fetched.
That turns egress from "ticks x log size" (~700 MB/month, and growing with
the log) into "edits" (~40 MB/month against a 10 GiB cap).

Revisions are one node per device, written only by its owner, rather than
one shared map -- a whole-map write would erase every peer's entry, after
which those peers would look permanently unchanged and never be fetched
again. Per-device keys make that impossible rather than merely avoided.

`.` is illegal in an RTDB key and the REST `.json` is a format suffix, so
paths get a reversible `~XX` escape (`log.json` -> `log~2Ejson`). Lossy
extension-stripping would break `todo`, whose listings return filenames.

`MirrorStore` / `MirrorSyncClient` dual-writes during the cutover. Reads
consult *both* backends: with primary-only reads a migrated device would
never see an un-migrated peer's writes while still mirroring its own back
-- silent one-directional convergence with no error raised. Writes are
asymmetric on purpose: a primary failure fails the tick, a mirror failure
is logged and survived.

Auth is Firebase email/password over identitytoolkit REST, with mandatory
refresh-token rotation. No service-account key anywhere: it would bypass
the security rules and is trivially extractable from an APK.

Verified live against a real database, not only mocks: sign-in and refresh,
key escaping round-tripping byte-for-byte, an unchanged tick writing
nothing, two devices converging, a revoked session throwing rather than
returning null, and a log written by the Python client being read by the
Dart one.

No app is migrated by this commit; every app still runs on GitHub.

Dart: 194 tests. Python: 203 tests at 100% branch coverage. No lint
suppressions added.
2026-08-03 18:14:25 +02:00

72 lines
2.2 KiB
Dart

/// Live cross-language check: reads what the Python client wrote, merges a
/// Dart-side record, and confirms both survive. The two implementations must
/// agree byte-for-byte or a phone on Dart would not see a PC on Python.
library;
import 'dart:convert';
import 'dart:io';
import 'package:crdt_sync/crdt_sync.dart';
/// Environment-specific values (project, account, database URL) live here,
/// mode 0600 -- never in source, because this repository is public.
final _configDir = '${Platform.environment['HOME']}/.config/crdt-sync';
Log _decode(String t) => (jsonDecode(t) as Map<String, dynamic>)
.map((k, v) => MapEntry(k, Record.fromJson(v as Map<String, dynamic>)));
String _encode(Log l) =>
jsonEncode(l.map((k, v) => MapEntry(k, v.toJson())));
Future<void> main(List<String> args) async {
final cfg = jsonDecode(
File('$_configDir/firebase.json').readAsStringSync(),
) as Map<String, dynamic>;
final auth = FirebaseTokenProvider(
apiKey: cfg['apiKey'] as String,
store: InMemoryCredentialStore(),
);
await auth.signIn(
email: cfg['email'] as String,
password: File('$_configDir/password').readAsStringSync(),
);
final client = FirebaseRestClient(
databaseUrl: cfg['databaseUrl'] as String,
auth: auth,
);
final merged = await syncLog(
client: client,
deviceId: 'dartdev',
pathPrefix: '_interop/devices',
localLog: {
'dart-rec': Record(
id: 'dart-rec',
fields: {
'value': (
'written-by-dart',
Hlc(wallTimeMs: 1000, counter: 0, nodeId: 'node-dart'),
),
},
),
},
encode: _encode,
decode: _decode,
stateStore: InMemorySyncStateStore(),
);
final ok = merged.containsKey('py-rec') && merged.containsKey('dart-rec');
stdout.writeln('dart sees records: ${merged.keys.toList()}');
stdout.writeln(
'python record value: ${merged['py-rec']?.fields['value']?.$1}',
);
stdout.writeln('revs: ${await client.getStringMap('_interop/revs')}');
// Clean up both devices' data.
for (final d in ['pydev', 'dartdev']) {
await client.deleteFile('_interop/devices/$d/log.json');
await client.deleteFile('_interop/revs/$d');
}
stdout.writeln(ok ? '\nINTEROP OK' : '\nINTEROP FAILED');
exit(ok ? 0 : 1);
}