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>
200 lines
6.7 KiB
Dart
200 lines
6.7 KiB
Dart
/// Tests for the interaction-only analytics buffer and Firebase flush.
|
|
@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:shared_preferences/shared_preferences.dart';
|
|
import 'package:todo/analytics/analytics_event.dart';
|
|
import 'package:todo/analytics/analytics_service.dart';
|
|
|
|
import 'analytics_helpers.dart';
|
|
|
|
FirebaseRestClient _fakeClient(
|
|
Future<http.Response> Function(http.Request request) handler,
|
|
) => 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: http_testing.MockClient(handler),
|
|
);
|
|
|
|
void main() {
|
|
setUp(() => SharedPreferences.setMockInitialValues({}));
|
|
|
|
group('AnalyticsService.flush', () {
|
|
test('does nothing when client is null', () async {
|
|
const service = AnalyticsService(nodeId: 'device-a');
|
|
await service.logEvent(
|
|
AnalyticsEvent(name: 'app_open', timestamp: DateTime.now()),
|
|
);
|
|
|
|
await service.flush(null);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
expect(prefs.getString('analytics.buffer'), isNotNull);
|
|
});
|
|
|
|
test('does nothing when the buffer is empty', () async {
|
|
final requests = <String>[];
|
|
final client = _fakeClient((request) async {
|
|
requests.add(request.method);
|
|
return http.Response('{}', 200);
|
|
});
|
|
const service = AnalyticsService(nodeId: 'device-a');
|
|
|
|
await service.flush(client);
|
|
|
|
expect(requests, isEmpty);
|
|
});
|
|
|
|
test('pushes the buffer as one batch and clears it on success', () async {
|
|
String? putPath;
|
|
String? putBody;
|
|
final client = _fakeClient((request) async {
|
|
if (request.method == 'PUT') {
|
|
putPath = request.url.path;
|
|
putBody = request.body;
|
|
}
|
|
return http.Response(request.body, 200);
|
|
});
|
|
const service = AnalyticsService(nodeId: 'device-a');
|
|
await service.logEvent(
|
|
AnalyticsEvent(name: 'app_open', timestamp: DateTime(2026)),
|
|
);
|
|
await service.logEvent(
|
|
AnalyticsEvent(name: 'note_created', timestamp: DateTime(2026)),
|
|
);
|
|
|
|
await service.flush(client);
|
|
|
|
expect(putPath, startsWith('/analytics/device-a/'));
|
|
final decodedBody = jsonDecode(jsonDecode(putBody!) as String) as List;
|
|
expect(decodedBody, hasLength(2));
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
expect(prefs.getString('analytics.buffer'), isNull);
|
|
});
|
|
|
|
test('leaves the buffer untouched on a push failure', () async {
|
|
final client = _fakeClient((_) async => http.Response('error', 500));
|
|
const service = AnalyticsService(nodeId: 'device-a');
|
|
await service.logEvent(
|
|
AnalyticsEvent(name: 'app_open', timestamp: DateTime.now()),
|
|
);
|
|
|
|
await service.flush(client);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final decoded = jsonDecode(prefs.getString('analytics.buffer')!) as List;
|
|
expect(decoded, hasLength(1));
|
|
});
|
|
|
|
test('keeps an event logged after the batch was already claimed', () async {
|
|
// The HTTP handler runs mid-`putFileText`, strictly after flush() has
|
|
// already claimed (cleared) the buffer — so writing here lands in a
|
|
// buffer this flush() call no longer owns, unlike writing to prefs
|
|
// before calling flush() (which would just become part of the claimed
|
|
// batch itself).
|
|
final client = _fakeClient((request) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(
|
|
'analytics.buffer',
|
|
jsonEncode([
|
|
AnalyticsEvent(
|
|
name: 'mid_flush_event',
|
|
timestamp: DateTime.now(),
|
|
).toJson(),
|
|
]),
|
|
);
|
|
return http.Response(request.body, 200);
|
|
});
|
|
const service = AnalyticsService(nodeId: 'device-a');
|
|
await service.logEvent(
|
|
AnalyticsEvent(name: 'app_open', timestamp: DateTime.now()),
|
|
);
|
|
|
|
await service.flush(client);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final remaining =
|
|
jsonDecode(prefs.getString('analytics.buffer')!) as List;
|
|
expect(remaining, hasLength(1));
|
|
expect((remaining.single as Map)['name'], 'mid_flush_event');
|
|
});
|
|
|
|
test(
|
|
'restores the claimed batch ahead of events logged mid-flush on '
|
|
'failure',
|
|
() async {
|
|
// The HTTP handler runs mid-`putFileText`, strictly after flush()
|
|
// has already claimed (cleared) the buffer, so writing here is a
|
|
// genuine race rather than something that could instead land before
|
|
// or after `flush()` depending on scheduling.
|
|
final client = _fakeClient((_) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(
|
|
'analytics.buffer',
|
|
jsonEncode([
|
|
AnalyticsEvent(
|
|
name: 'mid_flush_event',
|
|
timestamp: DateTime(2026),
|
|
).toJson(),
|
|
]),
|
|
);
|
|
return http.Response('error', 500);
|
|
});
|
|
const service = AnalyticsService(nodeId: 'device-a');
|
|
await service.logEvent(
|
|
AnalyticsEvent(name: 'app_open', timestamp: DateTime(2026)),
|
|
);
|
|
|
|
await service.flush(client);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final remaining =
|
|
jsonDecode(prefs.getString('analytics.buffer')!) as List;
|
|
expect(remaining, hasLength(2));
|
|
expect((remaining[0] as Map)['name'], 'app_open');
|
|
expect((remaining[1] as Map)['name'], 'mid_flush_event');
|
|
},
|
|
);
|
|
|
|
test(
|
|
'a RemoteSyncError from an expired session restores the buffer',
|
|
() async {
|
|
final client = FirebaseRestClient(
|
|
databaseUrl: 'https://x-rtdb.europe-west1.firebasedatabase.app',
|
|
auth: FirebaseTokenProvider(
|
|
apiKey: 'AIzaKey',
|
|
store: InMemoryCredentialStore(null),
|
|
),
|
|
);
|
|
const service = AnalyticsService(nodeId: 'device-a');
|
|
await service.logEvent(
|
|
AnalyticsEvent(name: 'app_open', timestamp: DateTime(2026)),
|
|
);
|
|
|
|
await service.flush(client);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final decoded =
|
|
jsonDecode(prefs.getString('analytics.buffer')!) as List;
|
|
expect(decoded, hasLength(1));
|
|
},
|
|
);
|
|
});
|
|
}
|