-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWebSocket.test.ts
More file actions
169 lines (135 loc) · 5.69 KB
/
Copy pathuseWebSocket.test.ts
File metadata and controls
169 lines (135 loc) · 5.69 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, renderHook } from '@testing-library/react'
import { FakeWebSocket } from '../test/fakeWebSocket'
import { useWebSocket } from './useWebSocket'
import type { WsMessage } from './protocol'
describe('useWebSocket', () => {
beforeEach(() => {
vi.useFakeTimers()
FakeWebSocket.reset()
vi.stubGlobal('WebSocket', FakeWebSocket)
})
afterEach(() => {
vi.unstubAllGlobals()
vi.useRealTimers()
})
it('sends the JOIN handshake as soon as the socket opens', () => {
renderHook(() => useWebSocket('display', () => {}))
act(() => FakeWebSocket.latest.open())
const join = FakeWebSocket.latest.sentMessages()[0]
expect(join.type).toBe('JOIN')
expect(join.sender).toBeNull()
expect(join.payload).toEqual({ role: 'display' })
})
it('learns its identity from WELCOME and renames from the roster', () => {
const { result } = renderHook(() => useWebSocket('display', () => {}))
act(() => FakeWebSocket.latest.open())
expect(result.current.self).toBeNull()
act(() =>
FakeWebSocket.latest.receive({
type: 'WELCOME',
sender: null,
payload: { id: 'abc123', name: 'display-abc1', role: 'display' },
timestamp: 1,
}),
)
expect(result.current.self).toEqual({ id: 'abc123', name: 'display-abc1', role: 'display' })
act(() =>
FakeWebSocket.latest.receive({
type: 'PRESENCE',
sender: null,
payload: { roster: [{ id: 'abc123', name: 'Nick', role: 'display' }] },
timestamp: 2,
}),
)
expect(result.current.self?.name).toBe('Nick')
expect(result.current.roster).toHaveLength(1)
})
it('rename sends a RENAME frame', () => {
const { result } = renderHook(() => useWebSocket('display', () => {}))
act(() => FakeWebSocket.latest.open())
result.current.rename('Sarah')
const frame = FakeWebSocket.latest.sentMessages().at(-1)!
expect(frame.type).toBe('RENAME')
expect(frame.payload).toEqual({ name: 'Sarah' })
})
it('sendBroadcast wraps the payload in a BROADCAST envelope', () => {
const { result } = renderHook(() => useWebSocket('display', () => {}))
act(() => FakeWebSocket.latest.open())
result.current.sendBroadcast({ kind: 'announcement', text: 'to all' })
const frame = FakeWebSocket.latest.sentMessages().at(-1)!
expect(frame.type).toBe('BROADCAST')
expect(frame.payload).toEqual({ kind: 'announcement', text: 'to all' })
})
it('reports status transitions: connecting, open, closed', () => {
const { result } = renderHook(() => useWebSocket('control', () => {}))
expect(result.current.status).toBe('connecting')
act(() => FakeWebSocket.latest.open())
expect(result.current.status).toBe('open')
act(() => FakeWebSocket.latest.drop())
expect(result.current.status).toBe('closed')
})
it('delivers parsed inbound messages to the callback', () => {
const received: WsMessage[] = []
renderHook(() => useWebSocket('control', (msg) => received.push(msg)))
act(() => FakeWebSocket.latest.open())
const frame: WsMessage = {
type: 'EVENT',
sender: { id: 'd1', name: 'display-d1', role: 'display' },
payload: { kind: 'reaction', emoji: '🎉' },
timestamp: 123,
}
act(() => FakeWebSocket.latest.receive(frame))
expect(received).toEqual([frame])
})
it('sendEvent wraps the payload in an EVENT envelope', () => {
const { result } = renderHook(() => useWebSocket('control', () => {}))
act(() => FakeWebSocket.latest.open())
result.current.sendEvent({ kind: 'announcement', text: 'hi' })
const event = FakeWebSocket.latest.sentMessages().at(-1)!
expect(event.type).toBe('EVENT')
expect(event.sender).toBeNull()
expect(event.payload).toEqual({ kind: 'announcement', text: 'hi' })
})
it('sendEvent is a no-op while the socket is not open', () => {
const { result } = renderHook(() => useWebSocket('control', () => {}))
result.current.sendEvent({ kind: 'announcement', text: 'lost' })
expect(FakeWebSocket.latest.sent).toHaveLength(0)
})
it('reconnects after a drop, with growing backoff', () => {
renderHook(() => useWebSocket('display', () => {}))
act(() => FakeWebSocket.latest.open())
expect(FakeWebSocket.instances).toHaveLength(1)
// First drop: reconnect after 500ms.
act(() => FakeWebSocket.latest.drop())
act(() => vi.advanceTimersByTime(499))
expect(FakeWebSocket.instances).toHaveLength(1)
act(() => vi.advanceTimersByTime(1))
expect(FakeWebSocket.instances).toHaveLength(2)
// Second consecutive failure (never opened): backoff doubles to 1000ms.
act(() => FakeWebSocket.latest.drop())
act(() => vi.advanceTimersByTime(999))
expect(FakeWebSocket.instances).toHaveLength(2)
act(() => vi.advanceTimersByTime(1))
expect(FakeWebSocket.instances).toHaveLength(3)
})
it('a successful open resets the backoff', () => {
renderHook(() => useWebSocket('display', () => {}))
act(() => FakeWebSocket.latest.open())
act(() => FakeWebSocket.latest.drop())
act(() => vi.advanceTimersByTime(500))
expect(FakeWebSocket.instances).toHaveLength(2)
// Open then drop again: delay is back to the initial 500ms, not 1000ms.
act(() => FakeWebSocket.latest.open())
act(() => FakeWebSocket.latest.drop())
act(() => vi.advanceTimersByTime(500))
expect(FakeWebSocket.instances).toHaveLength(3)
})
it('does not reconnect after unmount', () => {
const { unmount } = renderHook(() => useWebSocket('display', () => {}))
act(() => FakeWebSocket.latest.open())
unmount()
act(() => vi.advanceTimersByTime(10_000))
expect(FakeWebSocket.instances).toHaveLength(1)
})
})