utils/sync_settings_ui/test/firebase_sync_controller_test.dart
Krzysztof kuhy Rudnicki 23c693da4e Add sync_settings_ui: shared Firebase-only sync settings screen
Extracts the near-identical "Sync settings" UI duplicated across 5 apps
(todo, diet_guard_app, workout_app, wake_alarm/phone_app, home_inventory)
into a single tested package. Firebase sync (primary) + optional local
backup slot; GitHub mirror UI was dropped per kuhy's explicit decision
mid-build in favor of Firebase-only, so existing apps keep their local
GitHub code untouched, just disconnected from this shared screen.

100% line coverage, flutter analyze clean under very_good_analysis.
Generalized check_no_crdt_sync_override.sh to guard any utils git
dependency (crdt_sync, sync_settings_ui), not just crdt_sync by name.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KdSp6Nbrs9xvsWkCMDN9a1
2026-08-12 23:10:17 +02:00

219 lines
7.5 KiB
Dart

import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:sync_settings_ui/sync_settings_ui.dart';
FirebaseRestClient _fakeClient() => FirebaseRestClient(
databaseUrl: 'https://example.firebasedatabase.app',
auth: FirebaseTokenProvider(
apiKey: 'test-key',
store: InMemoryCredentialStore(),
),
);
void main() {
group('loadStatus', () {
test('reports connected with the stored email', () async {
final controller = FirebaseSyncController(
accountLoader: () async =>
const FirebaseAccount(email: 'a@b.com', password: 'x'),
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => true,
firebaseFactory: () async => null,
);
final status = await controller.loadStatus();
expect(status.connected, isTrue);
expect(status.email, 'a@b.com');
});
test(
'reports not connected with a null email when nothing stored',
() async {
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
);
final status = await controller.loadStatus();
expect(status.connected, isFalse);
expect(status.email, isNull);
},
);
test(
'trusts the session probe over account presence -- a marker with no '
'live session must read as not connected',
() async {
final controller = FirebaseSyncController(
accountLoader: () async =>
const FirebaseAccount(email: 'stale@b.com', password: 'x'),
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
);
final status = await controller.loadStatus();
expect(status.connected, isFalse);
},
);
});
group('connectWithPassword', () {
test('saves the account and reports connected on success', () async {
FirebaseAccount? saved;
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (account) async => saved = account,
accountClearer: () async {},
sessionProbe: () async => true,
firebaseFactory: () async => _fakeClient(),
);
final result = await controller.connectWithPassword(
email: 'a@b.com',
password: 'pw',
);
expect(result.outcome, FirebaseConnectOutcome.connected);
expect(result.email, 'a@b.com');
expect(saved?.email, 'a@b.com');
});
test(
'clears the account and reports rejected when the client is null',
() async {
var cleared = false;
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async => cleared = true,
sessionProbe: () async => false,
firebaseFactory: () async => null,
);
final result = await controller.connectWithPassword(
email: 'a@b.com',
password: 'wrong',
);
expect(result.outcome, FirebaseConnectOutcome.rejected);
expect(cleared, isTrue);
},
);
});
group('connectWithGoogle', () {
test('throws StateError when no googleFirebaseFactory is wired', () async {
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
);
expect(controller.supportsGoogle, isFalse);
expect(controller.connectWithGoogle, throwsStateError);
});
test('reports cancelled when the picker returns null', () async {
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
googleFirebaseFactory: () async => null,
);
expect(controller.supportsGoogle, isTrue);
final result = await controller.connectWithGoogle();
expect(result.outcome, FirebaseConnectOutcome.cancelled);
});
test('reports connected and the reported email on success', () async {
final controller = FirebaseSyncController(
accountLoader: () async =>
const FirebaseAccount(email: 'g@b.com', password: ''),
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => true,
firebaseFactory: () async => null,
googleFirebaseFactory: () async => _fakeClient(),
);
final result = await controller.connectWithGoogle();
expect(result.outcome, FirebaseConnectOutcome.connected);
expect(result.email, 'g@b.com');
});
test(
'reports signedInButNotPersisted with the stale account email when '
'the client came back non-null but no session actually stuck -- the '
'invariant this package exists to centralize',
() async {
final controller = FirebaseSyncController(
accountLoader: () async =>
const FirebaseAccount(email: 'stale@b.com', password: ''),
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
googleFirebaseFactory: () async => _fakeClient(),
);
final result = await controller.connectWithGoogle();
expect(
result.outcome,
FirebaseConnectOutcome.signedInButNotPersisted,
);
expect(result.email, 'stale@b.com');
},
);
test(
'reports wrongAccount with the error message on FirebaseAuthError',
() async {
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
googleFirebaseFactory: () async =>
throw FirebaseAuthError('wrong uid'),
);
final result = await controller.connectWithGoogle();
expect(result.outcome, FirebaseConnectOutcome.wrongAccount);
expect(result.message, 'wrong uid');
},
);
test(
'reports failed with the stringified error on any other failure',
() async {
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
googleFirebaseFactory: () async => throw StateError('boom'),
);
final result = await controller.connectWithGoogle();
expect(result.outcome, FirebaseConnectOutcome.failed);
expect(result.message, contains('boom'));
},
);
});
group('disconnect', () {
test('calls the account clearer', () async {
var cleared = false;
final controller = FirebaseSyncController(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async => cleared = true,
sessionProbe: () async => false,
firebaseFactory: () async => null,
);
await controller.disconnect();
expect(cleared, isTrue);
});
});
}