utils/sync_settings_ui/test/sync_settings_screen_test.dart
Krzysztof kuhy Rudnicki 200432a2b4 dart: split ten oversized test files by group
firebase_auth_rest_test.dart  525 -> 153 + _google 158 + _tokens 206 + _uid 141
  sync_revisions_test.dart      451 -> 223 + _peers 228 + _backends 158
                                       + _order 165 + _persistence 215
  sync_settings_screen_test     457 -> 207 + _backup 143 + _google 145
  mirror_store_test.dart        452 -> 209 + _reads 172 + _fallback 196 + _revisions 244
  firebase_client_test.dart     374 -> 198 + _writes 227
  firebase_config_test.dart     360 -> 138 + _client 148 + _client_google 164
  device_auth_test.dart         307 -> 128 + _poll 201
  github_client_test.dart       291 -> 143 + _writes 201
  sync_test.dart                269 -> 160 + _skips 175

Two traps this hit, both invisible to a line count:

  - _FakePersistence is declared *after* main(), so a naive cut at the last
    brace dropped it; sync_revisions_persistence_test.dart failed to load
    until it was carried across. Audited every other split file for trailing
    top-level declarations — this was the only one.
  - Copying the preamble into each sibling left helpers the sibling never
    uses. `dart analyze` flagged 12; all removed rather than left as dead
    code, and the analyzer is now clean.

crdt_sync_dart 280 passed, sync_settings_ui 29 passed, github_device_auth
analyze clean — all matching a clean HEAD worktree.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFSGeWxHPY5rHUGrPqhcmG
2026-08-22 14:54:35 +02:00

208 lines
6.5 KiB
Dart

import 'dart:async';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter/material.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(),
),
);
Widget _wrap(Widget child) => MaterialApp(home: child);
void main() {
// Every test expands the Google/password ExpansionTile, which pushes the
// form fields well past the default 800x600 test surface. A taller
// surface keeps every button reachable without scrolling -- scrolling a
// plain (non-.builder) ListView far enough still unmounts sliver children
// beyond its cache extent, which made an early version of these tests
// lose the Firebase section from the tree entirely.
setUp(() {
final binding = TestWidgetsFlutterBinding.ensureInitialized();
binding.platformDispatcher.views.first.physicalSize = const Size(
800,
2000,
);
binding.platformDispatcher.views.first.devicePixelRatio = 1.0;
addTearDown(binding.platformDispatcher.views.first.resetPhysicalSize);
addTearDown(binding.platformDispatcher.views.first.resetDevicePixelRatio);
});
group('Firebase section', () {
testWidgets('shows connected state with email and Disconnect', (
tester,
) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async =>
const FirebaseAccount(email: 'a@b.com', password: 'x'),
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => true,
firebaseFactory: () async => null,
),
),
);
await tester.pumpAndSettle();
expect(find.text('a@b.com'), findsOneWidget);
expect(find.text('Disconnect'), findsOneWidget);
});
testWidgets('disconnect clears the account and shows status', (
tester,
) async {
var cleared = false;
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async =>
const FirebaseAccount(email: 'a@b.com', password: 'x'),
accountSaver: (_) async {},
accountClearer: () async => cleared = true,
sessionProbe: () async => true,
firebaseFactory: () async => null,
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Disconnect'));
await tester.pumpAndSettle();
expect(cleared, isTrue);
expect(find.text('Firebase disconnected.'), findsOneWidget);
expect(find.text('Connect Firebase'), findsOneWidget);
});
testWidgets('connect with password shows an error when empty', (
tester,
) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
),
),
);
await tester.pumpAndSettle();
// Google unavailable by default -> password form starts expanded.
await tester.tap(find.text('Connect Firebase'));
await tester.pumpAndSettle();
expect(
find.text('Enter the sync account email and password.'),
findsOneWidget,
);
});
testWidgets('connect with password succeeds and shows Connected', (
tester,
) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => _fakeClient(),
),
),
);
await tester.pumpAndSettle();
await tester.enterText(
find.widgetWithText(TextField, 'Sync account email'),
'a@b.com',
);
await tester.enterText(
find.widgetWithText(TextField, 'Sync account password'),
'pw',
);
await tester.tap(find.text('Connect Firebase'));
await tester.pumpAndSettle();
expect(find.text('Connected to Firebase.'), findsOneWidget);
expect(find.text('a@b.com'), findsOneWidget);
});
testWidgets('connect with password reports rejection', (tester) async {
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => false,
firebaseFactory: () async => null,
),
),
);
await tester.pumpAndSettle();
await tester.enterText(
find.widgetWithText(TextField, 'Sync account email'),
'a@b.com',
);
await tester.enterText(
find.widgetWithText(TextField, 'Sync account password'),
'wrong',
);
await tester.tap(find.text('Connect Firebase'));
await tester.pumpAndSettle();
expect(find.text('Firebase rejected that account.'), findsOneWidget);
});
testWidgets(
'shows the Google button when googleAvailable, and connecting '
'succeeds',
(tester) async {
var sessionStored = false;
await tester.pumpWidget(
_wrap(
SyncSettingsScreen(
accountLoader: () async => null,
accountSaver: (_) async {},
accountClearer: () async {},
sessionProbe: () async => sessionStored,
firebaseFactory: () async => null,
googleFirebaseFactory: () async {
sessionStored = true;
return _fakeClient();
},
googleAvailable: true,
),
),
);
await tester.pumpAndSettle();
expect(find.text('Sign in with Google'), findsOneWidget);
// The password form is collapsed behind its ExpansionTile when
// Google is offered -- only the field labels prove that; the tile's
// own title text is present either way.
expect(
find.widgetWithText(TextField, 'Sync account email'),
findsNothing,
);
await tester.tap(find.text('Sign in with Google'));
await tester.pumpAndSettle();
expect(find.text('Connected to Firebase.'), findsOneWidget);
},
);
});
}