todo-app/test/wrapper_server_harness.dart
Krzysztof kuhy Rudnicki 10a546f6d6 Split nine oversized test files under the cap
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>
2026-08-14 16:42:13 +02:00

100 lines
3.7 KiB
Dart

/// The temp web root, the served instance, and enabledOrigin.
///
/// Shared by the files `wrapper_server_test.dart` was split into for the
/// 250-line cap. Deliberately NOT named `*_test.dart`: the runner would
/// collect it and fail on the missing `main()`.
library;
import 'dart:convert';
import 'dart:io';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as p;
import 'package:todo/desktop/wrapper_server.dart';
/// One test's temp web root and the [WrapperServer] serving it.
///
/// A class rather than bare `setUp`/`late` globals because [enabledOrigin]
/// needs the temp root, and a top-level function cannot close over a `late`
/// that `setUp` assigns. [install] wires the lifecycle; each test file calls
/// it once at the top of its `main()`.
class WrapperHarness {
/// Temp directory holding the web root, the state dir, and the configs.
late Directory root;
/// The server under test, listening on an OS-assigned port.
late WrapperServer server;
/// Origin of [server], e.g. `http://localhost:41234`.
late String base;
/// Registers the setUp/tearDown pair that owns this harness.
void install() {
setUp(() async {
root = await Directory.systemTemp.createTemp('wrapper');
final webRoot = Directory(p.join(root.path, 'web'))..createSync();
File(
p.join(webRoot.path, 'index.html'),
).writeAsStringSync('<h1>todo</h1>');
File(p.join(webRoot.path, 'main.dart.js')).writeAsStringSync('console;');
File(p.join(webRoot.path, 'canvaskit.wasm')).writeAsStringSync('binary');
server = WrapperServer(
webRoot: webRoot.path,
backlogPath: p.join(root.path, 'todo', 'BACKLOG.md'),
logPath: p.join(root.path, 'state', 'todo_notes.json'),
// Without this, the default falls back to the real
// $HOME/.config/todo/firebase_auth.json -- on a machine that has
// actually signed in (a real desktop session), that file exists, so
// the "no credentials file exists" test below observes 200 instead
// of 404. Point it at this test's own throwaway root instead.
todoCredentialsPath: p.join(
root.path,
'todo-config',
'firebase_auth.json',
),
);
// Port 0 lets the OS pick, so tests never collide with a running app.
await server.start(0);
base = 'http://localhost:${server.port}';
});
tearDown(() async {
await server.stop();
root.deleteSync(recursive: true);
});
}
Future<String> enabledOrigin(
Map<String, String> files, {
String? credentialsJson,
}) async {
final configDir = Directory(p.join(root.path, 'crdt-sync'))
..createSync(recursive: true);
files.forEach((name, contents) {
File(p.join(configDir.path, name)).writeAsStringSync(contents);
});
String? credentialsPath;
if (credentialsJson != null) {
final credentialsDir = Directory(p.join(root.path, 'todo-config'))
..createSync(recursive: true);
credentialsPath = p.join(credentialsDir.path, 'firebase_auth.json');
File(credentialsPath).writeAsStringSync(credentialsJson);
}
final enabled = WrapperServer(
webRoot: p.join(root.path, 'web'),
backlogPath: p.join(root.path, 'todo', 'BACKLOG.md'),
logPath: p.join(root.path, 'state', 'todo_notes.json'),
serveSyncAccount: true,
syncConfigDir: configDir.path,
todoCredentialsPath:
credentialsPath ?? p.join(root.path, 'todo-config', 'absent.json'),
);
await enabled.start(0);
addTearDown(enabled.stop);
return 'http://localhost:${enabled.port}';
}
}