summaryrefslogtreecommitdiff
path: root/server/test/smoke_test.dart
blob: 2ac745e5111c02088fcce25d27c93896a4a578dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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');
  });
}