import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:test/test.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:x_haven_server/net/app.dart'; /// Reads messages from a [WebSocketChannel] one at a time. The channel's /// stream is single-subscription, so this reader holds the one subscription /// for the test's lifetime. class _MessageReader { final WebSocketChannel channel; final StreamIterator> _iterator; _MessageReader(this.channel) : _iterator = StreamIterator( channel.stream.map( (Object? raw) => jsonDecode(raw as String) as Map, ), ); Future> next() async { final advanced = await _iterator.moveNext().timeout( const Duration(seconds: 5), ); if (!advanced) { fail('connection closed before the expected message arrived'); } return _iterator.current; } Future dispose() => _iterator.cancel(); } void main() { late HttpServer server; setUp(() async { server = await startServer(port: 0, staticDir: 'nonexistent-static-dir'); }); tearDown(() async { await server.close(force: true); }); WebSocketChannel connect() { final channel = WebSocketChannel.connect( Uri.parse('ws://localhost:${server.port}/ws'), ); addTearDown(() => unawaited(channel.sink.close())); return channel; } test('serves a stub page at / when the static dir is missing', () async { final client = HttpClient(); addTearDown(client.close); final request = await client.getUrl( Uri.parse('http://localhost:${server.port}/'), ); final response = await request.close(); expect(response.statusCode, 200); final body = await response.transform(utf8.decoder).join(); expect(body, contains('X-Haven server')); }); test('answers healthz', () async { final client = HttpClient(); addTearDown(client.close); final request = await client.getUrl( Uri.parse('http://localhost:${server.port}/healthz'), ); final response = await request.close(); expect(response.statusCode, 200); final body = jsonDecode(await response.transform(utf8.decoder).join()); expect(body, {'status': 'ok'}); }); test('answers hello with welcome', () async { final channel = connect(); final reader = _MessageReader(channel); addTearDown(reader.dispose); channel.sink.add(jsonEncode({'t': 'hello', 'v': 1})); final welcome = await reader.next(); expect(welcome['t'], 'welcome'); expect(welcome['v'], 1); expect(welcome['role'], 'spectator'); expect(welcome['index'], 0); expect(welcome['state'], isEmpty); expect(welcome['seats'], isEmpty); expect(welcome['session'], isNotEmpty); }); test('answers ping with pong', () async { final channel = connect(); final reader = _MessageReader(channel); addTearDown(reader.dispose); channel.sink.add(jsonEncode({'t': 'ping'})); expect(await reader.next(), {'t': 'pong'}); }); test('rejects malformed and unknown messages with error', () async { final channel = connect(); final reader = _MessageReader(channel); addTearDown(reader.dispose); channel.sink.add('this is not json'); expect((await reader.next())['code'], 'badRequest'); channel.sink.add(jsonEncode({'t': 'warpSpeed'})); expect((await reader.next())['code'], 'unknownMessage'); }); }