summaryrefslogtreecommitdiff
path: root/server/test
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-08 21:56:29 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-08 21:56:29 +0100
commitf6955e34489c79f6d5b764cb6a39b1387366b433 (patch)
tree83dad99ad81964036820468ad23fdc9ebe5c20a5 /server/test
parent0578ed0e5a08e6790a2e02416c4bf79f8c471f2d (diff)
Phase 0: project scaffolding
- server/: Dart game-server package (shelf + WebSocket) with the hello/welcome handshake, ping/pong, error responses, healthz, static SPA serving with SPA fallback, and smoke tests (5/5) - web/: Svelte 5 + TypeScript SPA with the protocol client, connection-status page, vitest and svelte-check; Vite dev server proxies /ws to the Dart server - game data assets copied from the read-only reference app into server/assets/ (data, images, i18n, fonts, testData) - PLAN.md implementation plan and progress tracker; README rewritten for the web app; AGPLv3 LICENSE inherited from the reference app; .gitignore; scripts/ci.sh (full pipeline) - CLAUDE.md project guidance Verified: scripts/ci.sh passes end-to-end; live smoke confirms the server serves the built SPA at / and answers a WebSocket hello with a welcome message.
Diffstat (limited to 'server/test')
-rw-r--r--server/test/smoke_test.dart109
1 files changed, 109 insertions, 0 deletions
diff --git a/server/test/smoke_test.dart b/server/test/smoke_test.dart
new file mode 100644
index 0000000..b9a4263
--- /dev/null
+++ b/server/test/smoke_test.dart
@@ -0,0 +1,109 @@
+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<Map<String, dynamic>> _iterator;
+
+ _MessageReader(this.channel)
+ : _iterator = StreamIterator(
+ channel.stream
+ .map((Object? raw) => jsonDecode(raw as String) as Map<String, dynamic>));
+
+ Future<Map<String, dynamic>> 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<void> 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');
+ });
+}