todo-app/test/wrapper_server_static_test.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

135 lines
5.2 KiB
Dart

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';
import 'wrapper_server_harness.dart';
void main() {
final h = WrapperHarness()..install();
test('serves index.html at the root', () async {
final response = await http.get(Uri.parse('${h.base}/'));
expect(response.statusCode, 200);
expect(response.body, contains('todo'));
});
test('serves nested assets and 404s unknown paths', () async {
expect(
(await http.get(Uri.parse('${h.base}/main.dart.js'))).statusCode,
200,
);
expect((await http.get(Uri.parse('${h.base}/nope.js'))).statusCode, 404);
});
test('refuses to serve outside the web root', () async {
// The served directory sits beside the user's files, so traversal must not
// be able to read them. Sent over a raw socket because http.get normalises
// `..` away client-side, so the server would never see the attack.
File(p.join(h.root.path, 'secret')).writeAsStringSync('do not serve me');
// Percent-encoded, because both the http client and Dart's HttpServer
// collapse a literal `..` before any handler sees it; `%2e%2e` survives
// that and is decoded into the path afterwards.
final socket = await Socket.connect('localhost', h.server.port);
socket.write('GET /%2e%2e/secret HTTP/1.1\r\nHost: localhost\r\n\r\n');
await socket.flush();
final response = await utf8.decoder.bind(socket).first;
await socket.close();
// Dart's HttpServer decodes and then normalises, so traversal is already
// collapsed before any handler runs and this comes back 404 rather than
// 403. What matters is the property, not which code rejected it: the file
// outside the web h.root is never served.
expect(response, isNot(contains('do not serve me')));
expect(response, anyOf(contains('403'), contains('404')));
});
test('a failing write reports a server error rather than crashing', () async {
// Parent path is a regular file, so creating the directory for the backup
// throws. The serve loop must survive it and keep answering requests.
final blocked = Directory(p.join(h.root.path, 'blocked'))..createSync();
File(p.join(blocked.path, 'wall')).writeAsStringSync('');
final wedged = WrapperServer(
webRoot: p.join(h.root.path, 'web'),
backlogPath: p.join(blocked.path, 'wall', 'nested', 'BACKLOG.md'),
logPath: p.join(h.root.path, 'state', 'todo_notes.json'),
);
await wedged.start(0);
addTearDown(wedged.stop);
final response = await http.post(
Uri.parse('http://localhost:${wedged.port}/backup/backlog'),
body: 'x',
);
expect(response.statusCode, 500);
// Still alive afterwards.
final after = await http.get(
Uri.parse('http://localhost:${wedged.port}/'),
);
expect(after.statusCode, 200);
});
test('POST then GET round-trips the backlog', () async {
final posted = await http.post(
Uri.parse('${h.base}/backup/backlog'),
body: '# backlog\n\nnote one\n',
);
expect(posted.statusCode, 204);
// Written to the real path, which is what the user's tooling reads.
final onDisk = File(p.join(h.root.path, 'todo', 'BACKLOG.md'));
expect(onDisk.existsSync(), isTrue);
expect(onDisk.readAsStringSync(), contains('note one'));
final fetched = await http.get(Uri.parse('${h.base}/backup/backlog'));
expect(fetched.statusCode, 200);
expect(fetched.body, contains('note one'));
});
test('POST then GET round-trips the note log', () async {
await http.post(Uri.parse('${h.base}/backup/log'), body: '{"a":1}');
final fetched = await http.get(Uri.parse('${h.base}/backup/log'));
expect(fetched.body, '{"a":1}');
});
test('GET on a backup that does not exist yet 404s', () async {
expect((await http.get(Uri.parse('${h.base}/backup/log'))).statusCode, 404);
});
test('rejects unsupported methods on a backup path', () async {
final response = await http.delete(Uri.parse('${h.base}/backup/log'));
expect(response.statusCode, 405);
});
test('labels wasm and js correctly', () async {
// CanvasKit refuses a .wasm served as anything else, and the app then
// renders nothing at all.
final wasm = await http.get(Uri.parse('${h.base}/canvaskit.wasm'));
expect(wasm.headers['content-type'], contains('application/wasm'));
final js = await http.get(Uri.parse('${h.base}/main.dart.js'));
expect(js.headers['content-type'], contains('javascript'));
});
test('contentTypeFor covers the asset kinds the build emits', () {
String kind(String name) => WrapperServer.contentTypeFor(name).mimeType;
expect(kind('a.html'), 'text/html');
expect(kind('a.json'), 'application/json');
expect(kind('a.css'), 'text/css');
expect(kind('a.png'), 'image/png');
expect(kind('a.svg'), 'image/svg+xml');
expect(kind('a.ttf'), 'font/ttf');
expect(kind('a.otf'), 'font/otf');
expect(kind('a.woff2'), 'font/woff2');
expect(kind('a.mjs'), 'text/javascript');
expect(kind('a.bin'), 'application/octet-stream');
expect(kind('a.unknown'), 'application/octet-stream');
});
}