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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { connectSocket } from "./ws";
import type { ChatEvent } from "./reducer";
/** A minimal scriptable WebSocket double. */
class FakeWebSocket {
static OPEN = 1;
static instances: FakeWebSocket[] = [];
url: string;
readyState = 0;
sent: string[] = [];
onopen: (() => void) | null = null;
onmessage: ((event: { data: string }) => void) | null = null;
onclose: (() => void) | null = null;
constructor(url: string) {
this.url = url;
FakeWebSocket.instances.push(this);
}
send(data: string) {
this.sent.push(data);
}
close() {
this.readyState = 3;
this.onclose?.();
}
/* test helpers */
serverOpens() {
this.readyState = FakeWebSocket.OPEN;
this.onopen?.();
}
serverSends(payload: unknown) {
this.onmessage?.({ data: JSON.stringify(payload) });
}
serverDrops() {
this.readyState = 3;
this.onclose?.();
}
}
describe("connectSocket", () => {
let events: ChatEvent[];
const dispatch = (event: ChatEvent) => events.push(event);
beforeEach(() => {
events = [];
FakeWebSocket.instances = [];
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
const connect = () =>
connectSocket({
url: "ws://test/ws",
dispatch,
webSocketImpl: FakeWebSocket as unknown as typeof WebSocket,
initialRetryMs: 100,
});
it("dispatches parsed server messages", () => {
connect();
const ws = FakeWebSocket.instances[0];
ws.serverOpens();
ws.serverSends({ type: "hello", sessionId: "s1" });
expect(events).toContainEqual({
type: "server",
message: { type: "hello", sessionId: "s1" },
});
});
it("sends only while the socket is open", () => {
const socket = connect();
const ws = FakeWebSocket.instances[0];
socket.send({ type: "prompt", text: "too early" });
expect(ws.sent).toHaveLength(0);
ws.serverOpens();
socket.send({ type: "prompt", text: "hello" });
expect(ws.sent).toEqual([
JSON.stringify({ type: "prompt", text: "hello" }),
]);
});
it("reconnects with backoff after a drop", () => {
connect();
FakeWebSocket.instances[0].serverOpens();
FakeWebSocket.instances[0].serverDrops();
expect(events).toContainEqual({ type: "connection", status: "offline" });
expect(FakeWebSocket.instances).toHaveLength(1);
vi.advanceTimersByTime(100);
expect(FakeWebSocket.instances).toHaveLength(2);
// second drop: the delay doubles
FakeWebSocket.instances[1].serverDrops();
vi.advanceTimersByTime(100);
expect(FakeWebSocket.instances).toHaveLength(2);
vi.advanceTimersByTime(100);
expect(FakeWebSocket.instances).toHaveLength(3);
});
it("does not reconnect after close()", () => {
const socket = connect();
FakeWebSocket.instances[0].serverOpens();
socket.close();
vi.advanceTimersByTime(10_000);
expect(FakeWebSocket.instances).toHaveLength(1);
});
it("survives malformed frames", () => {
connect();
const ws = FakeWebSocket.instances[0];
ws.serverOpens();
ws.onmessage?.({ data: "{not json" });
ws.serverSends({ type: "turn_started" });
expect(events).toContainEqual({
type: "server",
message: { type: "turn_started" },
});
});
});
|