todo-app/test/run_sync_test.dart
Krzysztof kuhy Rudnicki d316563015
Some checks are pending
release-apk / release (APK) (push) Waiting to run
Fix desktop Firebase sync: seed a Google-derived session, not a dead password
The account's password grant is retired fleet-wide (see link_google.py /
seed_session.py in ~/utils/crdt-sync), so desktop's Firebase form could never
have connected regardless of provisioning. Desktop-as-web can't use the
in-browser Google Sign-In button either (google_platform_web.dart hard-codes
googleSignInSupported = false), so it needs the same session-seeding path the
Python daemons already use: seed_session.py now includes todo, and the
wrapper's new /sync-credentials route serves that seeded session to
loadAccount() once per launch, then closes for the rest of the process.

Also stops the status line reporting a bare "Synced" while running over the
GitHub mirror alone with Firebase disconnected -- SyncResult now carries
firebaseConnected so the UI can say "GitHub-only (Firebase not connected)".

Includes the prior session's Google Sign-In one-tap addition
(google_sign_in_backend.dart and friends), which this builds on.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FPsyKQRX8ZwJpUqg37jp83
2026-08-12 19:35:18 +02:00

101 lines
3.4 KiB
Dart

/// Tests for the one place that decides *how* a sync runs.
///
/// The branch that matters is which backend ends up authoritative: a rollback
/// depends on getting exactly that right.
@TestOn('vm')
library;
import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http_testing;
import 'package:todo/data/note_repository.dart';
import 'package:todo/sync/run_sync.dart';
import 'package:todo/sync/sync_settings.dart';
const _settings = SyncSettings(owner: 'kuhyx', repo: 'syncs', token: 'tok');
void main() {
late NoteRepository repo;
setUp(() async {
repo = await NoteRepository.openInMemory(nodeId: 'me');
});
tearDown(() async => repo.close());
test('syncs over GitHub alone when Firebase is not set up', () async {
// The pre-migration path, and the rollback: no Firebase, no mirror.
final paths = <String>[];
final result = await runSync(
repo,
_settings,
httpClient: http_testing.MockClient((request) async {
paths.add('${request.method} ${request.url.host}');
return http.Response(jsonEncode({'content': '', 'sha': 'x'}), 200);
}),
firebaseFactory: () async => null,
stateStore: InMemorySyncStateStore(),
);
expect(result.pushed, isTrue);
expect(paths.every((p) => p.contains('api.github.com')), isTrue);
// The status line's whole reason for existing: this must read as
// GitHub-only, not as an unqualified success indistinguishable from a
// Firebase-connected sync.
expect(result.firebaseConnected, isFalse);
});
test('makes Firebase primary and still mirrors to GitHub', () async {
// The cutover guarantee: both backends receive the write.
final githubPaths = <String>[];
final firebasePaths = <String>[];
final firebase = FirebaseRestClient(
databaseUrl: 'https://x-rtdb.europe-west1.firebasedatabase.app',
auth: FirebaseTokenProvider(
apiKey: 'AIzaKey',
store: InMemoryCredentialStore(
FirebaseCredentials(
idToken: 'id',
refreshToken: 'refresh',
// Real clock: the provider compares against DateTime.now(), so a
// fixture-dated session would look expired and force a refresh.
expiresAt: DateTime.now().add(const Duration(hours: 1)),
),
),
),
httpClient: http_testing.MockClient((request) async {
firebasePaths.add('${request.method} ${request.url.path}');
if (request.method == 'PUT') return http.Response(request.body, 200);
return http.Response('null', 200);
}),
);
final result = await runSync(
repo,
_settings,
httpClient: http_testing.MockClient((request) async {
githubPaths.add(request.method);
return http.Response(jsonEncode({'content': '', 'sha': 'x'}), 200);
}),
firebaseFactory: () async => firebase,
stateStore: InMemorySyncStateStore(),
);
expect(result.pushed, isTrue);
expect(
firebasePaths.any((p) => p.startsWith('PUT')),
isTrue,
reason: 'Firebase is primary and must receive the write',
);
expect(
githubPaths,
contains('PUT'),
reason: 'GitHub must still be mirrored during the cutover',
);
expect(result.firebaseConnected, isTrue);
});
}