summaryrefslogtreecommitdiff
path: root/web/test
diff options
context:
space:
mode:
Diffstat (limited to 'web/test')
-rw-r--r--web/test/connection.test.ts16
-rw-r--r--web/test/fake-socket.ts7
2 files changed, 20 insertions, 3 deletions
diff --git a/web/test/connection.test.ts b/web/test/connection.test.ts
index 27958ba..3426881 100644
--- a/web/test/connection.test.ts
+++ b/web/test/connection.test.ts
@@ -53,6 +53,22 @@ describe('GameConnection', () => {
expect(statuses).toContain('open');
});
+ // Regression: the open gate must compare against the real
+ // `WebSocket.OPEN` readyState (1). A wrong constant (e.g. 0 = CONNECTING)
+ // silently dropped every outbound frame — hello, ping, and every command
+ // — because the readyState check never matched, while the unit tests
+ // passed only because the fake mirrored the same wrong constant.
+ it('sends frames only when readyState is the real WebSocket.OPEN (1)', () => {
+ conn.connect();
+ const socket = sockets.current;
+ // Before open the socket is CONNECTING (0): nothing may be sent.
+ expect(socket.sent).toHaveLength(0);
+ expect(socket.readyState).toBe(0);
+ socket.open(); // → OPEN (1)
+ expect(socket.readyState).toBe(1);
+ expect(socket.lastJson()).toEqual({ t: 'hello', v: 1 });
+ });
+
it('forwards update, seats, and error to their handlers', () => {
const messages: string[] = [];
let errorMessage: string | null = null;
diff --git a/web/test/fake-socket.ts b/web/test/fake-socket.ts
index 290f9a8..61208e4 100644
--- a/web/test/fake-socket.ts
+++ b/web/test/fake-socket.ts
@@ -9,13 +9,14 @@ export class FakeSocket implements Transport {
onclose: ((ev?: unknown) => void) | null = null;
onerror: ((ev?: unknown) => void) | null = null;
onmessage: ((ev: { data: unknown }) => void) | null = null;
- readyState = 1; // CONNECTING
+ // Real WebSocket readyState: 0 CONNECTING, 1 OPEN, 2 CLOSING, 3 CLOSED.
+ readyState = 0; // CONNECTING
readonly sent: string[] = [];
closed = false;
send(data: string): void {
- if (this.readyState !== 0) throw new Error('not open');
+ if (this.readyState !== 1) throw new Error('not open');
this.sent.push(data);
}
@@ -29,7 +30,7 @@ export class FakeSocket implements Transport {
// test drivers --------------------------------------------------------
open(): void {
- this.readyState = 0;
+ this.readyState = 1; // OPEN
this.onopen?.();
}