mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 19:07:17 +01:00
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.
292 lines
9.4 KiB
Dart
292 lines
9.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart' as http_testing;
|
|
import 'package:test/test.dart';
|
|
|
|
http.Response _response(int statusCode, [Object? jsonBody]) =>
|
|
http.Response(jsonEncode(jsonBody ?? {}), statusCode);
|
|
|
|
/// Sentinel meaning "this GET throws a network exception", since a real
|
|
/// [http.Response] can't carry an invalid status code.
|
|
final _networkError = Object();
|
|
|
|
/// Builds a client whose GETs return [getResponses] in call order (each
|
|
/// either an [http.Response] or [_networkError]), and whose PUT returns
|
|
/// [putResponse] (or throws [putError] if network fails).
|
|
GitHubClient _client(
|
|
List<Object> getResponses, {
|
|
http.Response? putResponse,
|
|
Object? putError,
|
|
http.Response? deleteResponse,
|
|
Object? deleteError,
|
|
}) {
|
|
var getIndex = 0;
|
|
final mock = http_testing.MockClient((request) async {
|
|
if (request.method == 'GET') {
|
|
final response = getResponses[getIndex];
|
|
getIndex++;
|
|
if (identical(response, _networkError)) {
|
|
throw http.ClientException('offline');
|
|
}
|
|
return response as http.Response;
|
|
}
|
|
if (request.method == 'PUT') {
|
|
if (putError != null) throw putError;
|
|
return putResponse!;
|
|
}
|
|
if (request.method == 'DELETE') {
|
|
if (deleteError != null) throw deleteError;
|
|
return deleteResponse!;
|
|
}
|
|
throw UnsupportedError('unexpected method ${request.method}');
|
|
});
|
|
return GitHubClient(
|
|
owner: 'kuhyx',
|
|
repo: 'crdt-sync-demo',
|
|
token: 'fake-token',
|
|
httpClient: mock,
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
group('getFileText', () {
|
|
test('returns decoded content on success', () async {
|
|
final encoded = base64.encode(utf8.encode('hello world'));
|
|
final client = _client([
|
|
_response(200, {'content': encoded}),
|
|
]);
|
|
expect(await client.getFileText('devices/pc/log.json'), 'hello world');
|
|
});
|
|
|
|
test('returns null for an unused path on a real repo', () async {
|
|
final client = _client([_response(404), _response(200)]);
|
|
expect(await client.getFileText('devices/phone/log.json'), isNull);
|
|
});
|
|
|
|
test('throws RepoNotFoundError when the repo itself is missing', () async {
|
|
final client = _client([_response(404), _response(404)]);
|
|
expect(
|
|
() => client.getFileText('devices/pc/log.json'),
|
|
throwsA(isA<RepoNotFoundError>()),
|
|
);
|
|
});
|
|
|
|
test('throws GitHubSyncError on a non-2xx non-404', () async {
|
|
final client = _client([_response(500)]);
|
|
expect(
|
|
() => client.getFileText('devices/pc/log.json'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
|
|
test('throws GitHubSyncError on a network exception', () async {
|
|
final client = _client([_networkError]);
|
|
expect(
|
|
() => client.getFileText('devices/pc/log.json'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
|
|
test(
|
|
'treats a network error during the repo check as repo missing',
|
|
() async {
|
|
final client = _client([_response(404), _networkError]);
|
|
expect(
|
|
() => client.getFileText('devices/pc/log.json'),
|
|
throwsA(isA<RepoNotFoundError>()),
|
|
);
|
|
},
|
|
);
|
|
});
|
|
|
|
group('listDirectory', () {
|
|
test('returns entry names regardless of type', () async {
|
|
final payload = [
|
|
{'name': 'pc', 'type': 'dir'},
|
|
{'name': 'phone', 'type': 'dir'},
|
|
{'not_a_name': 'x'},
|
|
];
|
|
final client = _client([_response(200, payload)]);
|
|
expect(await client.listDirectory('devices'), ['pc', 'phone']);
|
|
});
|
|
|
|
test('returns an empty list when the response is not a list', () async {
|
|
final client = _client([
|
|
_response(200, {'unexpected': 'shape'}),
|
|
]);
|
|
expect(await client.listDirectory('devices'), isEmpty);
|
|
});
|
|
|
|
test('returns an empty list for an unused path on a real repo', () async {
|
|
final client = _client([_response(404), _response(200)]);
|
|
expect(await client.listDirectory('devices'), isEmpty);
|
|
});
|
|
|
|
test('throws RepoNotFoundError when the repo itself is missing', () async {
|
|
final client = _client([_response(404), _response(404)]);
|
|
expect(
|
|
() => client.listDirectory('devices'),
|
|
throwsA(isA<RepoNotFoundError>()),
|
|
);
|
|
});
|
|
|
|
test('throws GitHubSyncError on a non-2xx non-404', () async {
|
|
final client = _client([_response(500)]);
|
|
expect(
|
|
() => client.listDirectory('devices'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
});
|
|
|
|
group('putFileText', () {
|
|
test('creates a new file with no sha when none existed', () async {
|
|
final client = _client([
|
|
_response(404),
|
|
_response(200),
|
|
], putResponse: _response(201));
|
|
await client.putFileText('devices/pc/log.json', '{}', message: 'm');
|
|
});
|
|
|
|
test('updates an existing file by including its sha', () async {
|
|
final client = _client([
|
|
_response(200, {'sha': 'abc123'}),
|
|
], putResponse: _response(200));
|
|
await client.putFileText('devices/pc/log.json', '{}', message: 'm');
|
|
});
|
|
|
|
test(
|
|
'throws RepoNotFoundError when checking sha on a missing repo',
|
|
() async {
|
|
final client = _client([_response(404), _response(404)]);
|
|
expect(
|
|
() => client.putFileText('devices/pc/log.json', '{}', message: 'm'),
|
|
throwsA(isA<RepoNotFoundError>()),
|
|
);
|
|
},
|
|
);
|
|
|
|
test('throws GitHubSyncError when the sha check itself fails', () async {
|
|
final client = _client([_response(500)]);
|
|
expect(
|
|
() => client.putFileText('devices/pc/log.json', '{}', message: 'm'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
|
|
test('throws GitHubSyncError on a put network exception', () async {
|
|
final client = _client([
|
|
_response(404),
|
|
_response(200),
|
|
], putError: http.ClientException('offline'));
|
|
expect(
|
|
() => client.putFileText('devices/pc/log.json', '{}', message: 'm'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
|
|
test('throws GitHubSyncError on a put non-2xx response', () async {
|
|
final client = _client([
|
|
_response(404),
|
|
_response(200),
|
|
], putResponse: _response(422));
|
|
expect(
|
|
() => client.putFileText('devices/pc/log.json', '{}', message: 'm'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
});
|
|
|
|
group('canAccessRepo', () {
|
|
test('is true when the repo endpoint returns 2xx', () async {
|
|
expect(await _client([_response(200)]).canAccessRepo(), isTrue);
|
|
});
|
|
|
|
test('is false when the repo is missing (404)', () async {
|
|
expect(await _client([_response(404)]).canAccessRepo(), isFalse);
|
|
});
|
|
|
|
test('is false on a network error', () async {
|
|
expect(await _client([_networkError]).canAccessRepo(), isFalse);
|
|
});
|
|
});
|
|
|
|
group('canAccessRemote', () {
|
|
// The RemoteStore spelling must behave exactly like the legacy name.
|
|
test('is true when the repo endpoint returns 2xx', () async {
|
|
expect(await _client([_response(200)]).canAccessRemote(), isTrue);
|
|
});
|
|
|
|
test('is false when the repo is missing (404)', () async {
|
|
expect(await _client([_response(404)]).canAccessRemote(), isFalse);
|
|
});
|
|
|
|
test('is false on a network error', () async {
|
|
expect(await _client([_networkError]).canAccessRemote(), isFalse);
|
|
});
|
|
});
|
|
|
|
group('RemoteStore contract', () {
|
|
test('GitHubClient is a RemoteStore', () {
|
|
expect(_client([]), isA<RemoteStore>());
|
|
});
|
|
|
|
test('RepoNotFoundError is catchable as either error type', () {
|
|
// Backend-neutral callers catch RemoteNotFoundError; existing GitHub
|
|
// callers catch GitHubSyncError. One exception must satisfy both.
|
|
final error = RepoNotFoundError('gone');
|
|
expect(error, isA<GitHubSyncError>());
|
|
expect(error, isA<RemoteNotFoundError>());
|
|
expect(error, isA<RemoteSyncError>());
|
|
});
|
|
|
|
test('errors stringify as their own type', () {
|
|
expect(GitHubSyncError('boom').toString(), 'GitHubSyncError: boom');
|
|
expect(RepoNotFoundError('gone').toString(), 'RepoNotFoundError: gone');
|
|
});
|
|
});
|
|
|
|
group('deleteFile', () {
|
|
test('deletes an existing file (resolves its own sha)', () async {
|
|
final client = _client(
|
|
[_response(200, {'sha': 'abc123'})],
|
|
deleteResponse: _response(200),
|
|
);
|
|
await expectLater(client.deleteFile('devices/pc/log.json'), completes);
|
|
});
|
|
|
|
test('is a no-op when the file does not exist', () async {
|
|
// sha lookup 404s, repo-exists check 200s -> null sha -> no DELETE sent.
|
|
final client = _client([
|
|
_response(404),
|
|
_response(200),
|
|
], deleteError: StateError('DELETE must not be called'));
|
|
await expectLater(client.deleteFile('devices/pc/gone.json'), completes);
|
|
});
|
|
|
|
test('throws GitHubSyncError on a delete non-2xx response', () async {
|
|
final client = _client(
|
|
[_response(200, {'sha': 'abc123'})],
|
|
deleteResponse: _response(500),
|
|
);
|
|
expect(
|
|
() => client.deleteFile('devices/pc/log.json'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
|
|
test('throws GitHubSyncError on a delete network exception', () async {
|
|
final client = _client(
|
|
[_response(200, {'sha': 'abc123'})],
|
|
deleteError: http.ClientException('offline'),
|
|
);
|
|
expect(
|
|
() => client.deleteFile('devices/pc/log.json'),
|
|
throwsA(isA<GitHubSyncError>()),
|
|
);
|
|
});
|
|
});
|
|
}
|