Repository navigation
Expand file tree
/
Copy pathatem.spec.ts
More file actions
183 lines (145 loc) · 5.07 KB
/
Copy pathatem.spec.ts
File metadata and controls
183 lines (145 loc) · 5.07 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-disable @typescript-eslint/unbound-method */
import { Atem, DEFAULT_MAX_PACKET_SIZE, DEFAULT_PORT } from '../atem'
import { CutCommand } from '../commands'
import { promisify } from 'util'
import { EventEmitter } from 'events'
import { AtemSocket } from '../lib/atemSocket'
jest.mock('../lib/atemSocket.ts')
const setImmediatePromise = promisify(setImmediate)
class MockSocket extends EventEmitter {
destroy(): void {
// Nothing
}
}
describe('Atem', () => {
beforeEach(() => {
;(AtemSocket as any).mockClear()
})
test('constructor test 1', async () => {
const conn = new Atem({ disableMultithreaded: true })
try {
const socket = (conn as any).client.socket as AtemSocket
expect(socket).toBeTruthy()
expect(AtemSocket).toHaveBeenCalledTimes(1)
expect(AtemSocket).toHaveBeenCalledWith({
address: '',
childProcessTimeout: 600,
debugBuffers: false,
disableMultithreaded: true,
log: (conn as any)._log,
port: DEFAULT_PORT,
maxPacketSize: DEFAULT_MAX_PACKET_SIZE,
})
} finally {
await conn.destroy()
}
})
test('constructor test 2', async () => {
const conn = new Atem({ debugBuffers: true, address: 'test1', port: 23, maxPacketSize: 500 })
try {
const socket = (conn as any).client.socket as AtemSocket
expect(socket).toBeTruthy()
expect(AtemSocket).toHaveBeenCalledTimes(1)
expect(AtemSocket).toHaveBeenCalledWith({
address: 'test1',
childProcessTimeout: 600,
debugBuffers: true,
disableMultithreaded: false,
log: (conn as any)._log,
port: 23,
maxPacketSize: 500,
})
} finally {
await conn.destroy()
}
})
test('connect', async () => {
const conn = new Atem({ debugBuffers: true, address: 'test1', port: 23 })
try {
const socket = (conn as any).client.socket as AtemSocket
expect(socket).toBeTruthy()
socket.connect = jest.fn(() => Promise.resolve(5) as any)
const res = conn.connect('127.9.8.7', 98)
expect(await res).toEqual(5)
expect(socket.connect).toHaveBeenCalledTimes(1)
expect(socket.connect).toHaveBeenCalledWith('127.9.8.7', 98)
} finally {
await conn.destroy()
}
})
test('disconnect', async () => {
const conn = new Atem({ debugBuffers: true, address: 'test1', port: 23 })
try {
const socket = (conn as any).client.socket as AtemSocket
expect(socket).toBeTruthy()
socket.disconnect = jest.fn(() => Promise.resolve(35) as any)
const res = await conn.disconnect()
expect(res).toEqual(35)
expect(socket.disconnect).toHaveBeenCalledTimes(1)
expect(socket.disconnect).toHaveBeenCalledWith()
} finally {
await conn.destroy()
}
})
test('sendCommand - good', async () => {
;(AtemSocket as any).mockImplementation(() => new MockSocket())
const conn = new Atem({ debugBuffers: true, address: 'test1', port: 23 })
try {
const socket = (conn as any).client.socket as AtemSocket
expect(socket).toBeTruthy()
let nextId = 123
Object.defineProperty(socket, 'nextPacketTrackingId', {
get: jest.fn(() => nextId++),
set: jest.fn(),
})
expect(socket.nextPacketTrackingId).toEqual(123)
socket.sendCommands = jest.fn(() => Promise.resolve([124]) as any)
const sentQueue = (conn as any).client._sentQueue as Record<string, unknown>
expect(Object.keys(sentQueue)).toHaveLength(0)
const cmd = new CutCommand(0)
const res = conn.sendCommand(cmd)
res.catch(() => null) // Dismiss UnhandledPromiseRejection
await setImmediatePromise()
expect(Object.keys(sentQueue)).toHaveLength(1)
expect(socket.sendCommands).toHaveBeenCalledTimes(1)
expect(socket.sendCommands).toHaveBeenCalledWith([cmd])
// Trigger the ack, and it should switfy resolve
socket.emit('ackPackets', [124])
expect(Object.keys(sentQueue)).toHaveLength(0)
// Finally, it should now resolve without a timeout
expect(await res).toBeUndefined()
} finally {
await conn.destroy()
}
}, 500)
test('sendCommand - send error', async () => {
;(AtemSocket as any).mockImplementation(() => new MockSocket())
const conn = new Atem({ debugBuffers: true, address: 'test1', port: 23 })
try {
const socket = (conn as any).client.socket as AtemSocket
expect(socket).toBeTruthy()
let nextId = 123
Object.defineProperty(socket, 'nextPacketTrackingId', {
get: jest.fn(() => nextId++),
set: jest.fn(),
})
expect(socket.nextPacketTrackingId).toEqual(123)
socket.sendCommands = jest.fn(() => Promise.reject(35) as any)
const sentQueue = (conn as any).client._sentQueue as Record<string, unknown>
expect(Object.keys(sentQueue)).toHaveLength(0)
const cmd = new CutCommand(0)
const res = conn.sendCommand(cmd)
res.catch(() => null) // Dismiss UnhandledPromiseRejection
// Send command should be called
expect(socket.sendCommands).toHaveBeenCalledTimes(1)
expect(socket.sendCommands).toHaveBeenCalledWith([cmd])
expect(Object.keys(sentQueue)).toHaveLength(0)
// Finally, it should now resolve without a timeout
// Should be the error thrown by sendCommand
await expect(res).rejects.toBe(35)
// expect(await res).toEqual(cmd)
} finally {
await conn.destroy()
}
}, 500)
})