mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-10 20:27:01 +01:00
Every split is by test-group/concern, with the shared setup moved to a
sibling helper the splits import. All 313 tests are preserved -- the count
is unchanged and `test('…')` names are byte-identical to the originals
(verified by diffing the sorted name lists).
note_repository_test 677 -> 4 files + note_repository_helpers
capture_screen_test 660 -> 4 files + capture_screen_harness
note_editor_test 502 -> 3 files + note_editor_harness
port_guard_test 405 -> 3 files + port_guard_fakes
wrapper_server_test 402 -> 2 files + wrapper_server_harness
settings_screen_test 391 -> 2 files + settings_screen_harness
github_mirror_screen_test 365 -> 2 files + github_mirror_harness
notes_list_screen_test 347 -> 2 files + notes_list_harness
analytics_service_test 300 -> 2 files + analytics_helpers
Helpers are deliberately NOT named `*_test.dart`; the runner would collect
them and fail on the missing `main()`.
Three things did not survive a mechanical split and were handled explicitly:
* wrapper_server's `enabledOrigin` closes over the `late root` that setUp
assigns, so a top-level function cannot see it. It is now a
WrapperHarness class with an install() that registers the setUp/tearDown
pair -- the only structural change in this commit.
* note_repository's splits each repeat `setUpAll(sqfliteFfiInit)`, and
analytics' each repeat `@TestOn('vm')`. Omitting either fails quietly
(uninitialised sqflite / wrong platform) rather than loudly.
* capture_screen's `recordingMock` was declared mid-file between two
tests, so it moved to the harness rather than being dropped.
Verified: 313 tests pass (same as before the refactor), 100% coverage.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
147 lines
5.0 KiB
Dart
147 lines
5.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:todo/analytics/analytics_service.dart';
|
|
import 'package:todo/data/app_settings.dart';
|
|
import 'package:todo/data/note.dart';
|
|
import 'package:todo/data/note_template.dart';
|
|
import 'package:todo/sync/local_backup.dart';
|
|
import 'package:todo/sync/notes_markdown.dart';
|
|
import 'package:todo/ui/capture_screen.dart';
|
|
import 'package:todo/ui/github_mirror_screen.dart';
|
|
import 'package:todo/ui/settings_screen.dart';
|
|
|
|
import 'fake_note_repository.dart';
|
|
import 'fake_secure_storage.dart';
|
|
|
|
import 'capture_screen_harness.dart';
|
|
|
|
void main() {
|
|
testWidgets(
|
|
'auto-sync adopts a newer advancedMode value from Firebase',
|
|
(tester) async {
|
|
final remoteTime = DateTime(2026, 6);
|
|
final firebase = FirebaseRestClient(
|
|
databaseUrl: 'https://x-rtdb.europe-west1.firebasedatabase.app',
|
|
auth: FirebaseTokenProvider(
|
|
apiKey: 'AIzaKey',
|
|
store: InMemoryCredentialStore(
|
|
FirebaseCredentials(
|
|
idToken: 'id',
|
|
refreshToken: 'refresh',
|
|
expiresAt: DateTime.now().add(const Duration(hours: 1)),
|
|
),
|
|
),
|
|
),
|
|
httpClient: MockClient((request) async {
|
|
if (request.url.path == '/settings/advancedMode.json') {
|
|
return http.Response(
|
|
'{"value": "true", '
|
|
'"updatedAtMillis": "${remoteTime.millisecondsSinceEpoch}"}',
|
|
200,
|
|
);
|
|
}
|
|
if (request.method == 'PUT') return http.Response(request.body, 200);
|
|
return http.Response('null', 200);
|
|
}),
|
|
);
|
|
final appSettings = ValueNotifier(
|
|
AppSettings(
|
|
advancedMode: false,
|
|
// Older than remoteTime, and non-null, so the reconciliation
|
|
// guard's `currentAt != null && !reconciledAt.isAfter(currentAt)`
|
|
// check actually evaluates both operands.
|
|
advancedModeUpdatedAt: remoteTime.subtract(
|
|
const Duration(days: 1),
|
|
),
|
|
),
|
|
);
|
|
|
|
await pumpCapture(
|
|
tester,
|
|
prefs: configuredPrefs,
|
|
firebaseFactory: () async => firebase,
|
|
appSettings: appSettings,
|
|
);
|
|
await tester.pump(); // settings load → auto-sync (pull) …
|
|
await tester.pump(); // … then push, then reconcile
|
|
|
|
expect(appSettings.value.advancedMode, isTrue);
|
|
},
|
|
);
|
|
|
|
testWidgets('logs app_open when analytics is injected', (tester) async {
|
|
const analytics = AnalyticsService(nodeId: 'device-a');
|
|
await pumpCapture(tester, analytics: analytics);
|
|
await tester.pump(); // flush the unawaited logEvent's prefs write
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final buffer = prefs.getString('analytics.buffer');
|
|
expect(buffer, isNotNull);
|
|
expect(buffer, contains('app_open'));
|
|
});
|
|
|
|
testWidgets('auto-syncs on launch when configured', (tester) async {
|
|
final methods = <String>[];
|
|
await pumpCapture(
|
|
tester,
|
|
prefs: configuredPrefs,
|
|
httpClient: recordingMock(methods),
|
|
);
|
|
await tester.pump(); // settings load → auto-sync (pull) …
|
|
await tester.pump(); // … then push
|
|
|
|
expect(methods, contains('PUT')); // this device pushed its changeset
|
|
});
|
|
|
|
testWidgets('does not auto-sync when unconfigured', (tester) async {
|
|
final methods = <String>[];
|
|
await pumpCapture(tester, httpClient: recordingMock(methods)); // no token
|
|
await tester.pump();
|
|
await tester.pump();
|
|
|
|
expect(methods, isEmpty);
|
|
});
|
|
|
|
testWidgets('auto-syncs again when the app is backgrounded', (tester) async {
|
|
final methods = <String>[];
|
|
await pumpCapture(
|
|
tester,
|
|
prefs: configuredPrefs,
|
|
httpClient: recordingMock(methods),
|
|
);
|
|
await tester.pump();
|
|
await tester.pump();
|
|
methods.clear();
|
|
|
|
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
|
|
await tester.pump();
|
|
await tester.pump();
|
|
|
|
// A sync ran. Not `contains('PUT')`: the launch sync above already pushed
|
|
// this log, so revision tracking correctly suppresses the second, unchanged
|
|
// push -- that suppression is the point of the revision cache.
|
|
expect(methods, isNotEmpty);
|
|
});
|
|
|
|
testWidgets('auto-sync failure shows no snackbar but flags the status line', (
|
|
tester,
|
|
) async {
|
|
final mock = MockClient((_) async => throw Exception('offline'));
|
|
await pumpCapture(tester, prefs: configuredPrefs, httpClient: mock);
|
|
await tester.pump();
|
|
await tester.pump();
|
|
|
|
// Capture must never be interrupted by a snackbar…
|
|
expect(find.byType(SnackBar), findsNothing);
|
|
// …but the failure is no longer swallowed: the status line shows it.
|
|
expect(find.textContaining('Sync failed at'), findsOneWidget);
|
|
expect(find.textContaining('offline'), findsOneWidget);
|
|
});
|
|
}
|