-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathindex.test.ts
343 lines (298 loc) · 10.1 KB
/
index.test.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import type { LoguxSubscribeAction } from '@logux/actions'
import { TestTime } from '@logux/core'
import { restoreAll, type Spy, spyOn } from 'nanospy'
import { setTimeout } from 'node:timers/promises'
import { afterEach, expect, it } from 'vitest'
import { type LoguxActionError, TestClient, TestServer } from '../index.js'
let server: TestServer
afterEach(() => {
restoreAll()
server.destroy()
})
async function catchError(cb: () => Promise<any>): Promise<LoguxActionError> {
let err: LoguxActionError | undefined
try {
await cb()
} catch (e) {
err = e as LoguxActionError
}
if (!err) throw new Error('Error was no thrown')
return err
}
function privateMethods(obj: object): any {
return obj
}
it('connects and disconnect', async () => {
server = new TestServer()
let client1 = new TestClient(server, '10')
let client2 = new TestClient(server, '10')
expect(client1.nodeId).toEqual('10:1:1')
expect(client1.clientId).toEqual('10:1')
expect(client1.userId).toEqual('10')
expect(client2.nodeId).toEqual('10:2:1')
await Promise.all([client1.connect(), client2.connect()])
expect(Array.from(server.clientIds.keys())).toEqual(['10:1', '10:2'])
await client1.disconnect()
expect(Array.from(server.clientIds.keys())).toEqual(['10:2'])
})
it('sends and collect actions', async () => {
server = new TestServer()
server.type('FOO', {
access: () => true,
process(ctx) {
ctx.sendBack({ type: 'BAR' })
}
})
server.type('RESEND', {
access: () => true,
resend: () => ({ user: '10' })
})
let [client1, client2] = await Promise.all([
server.connect('10'),
server.connect('11')
])
client1.log.keepActions()
let received = await client1.collect(async () => {
await client1.log.add({ type: 'FOO' })
await setTimeout(10)
await client2.log.add({ type: 'RESEND' })
await setTimeout(10)
})
expect(received).toEqual([
{ type: 'BAR' },
{ id: '1 10:1:1 0', type: 'logux/processed' },
{ type: 'RESEND' }
])
expect(client1.log.actions()).toEqual([
{ type: 'FOO' },
{ type: 'BAR' },
{ id: '1 10:1:1 0', type: 'logux/processed' },
{ type: 'RESEND' }
])
})
it('allows to change time', () => {
let time = new TestTime()
let server1 = new TestServer({ time })
let server2 = new TestServer({ time })
expect(server1.options.time).toBe(time)
expect(server2.options.time).toBe(time)
expect(server1.nodeId).not.toEqual(server2.nodeId)
})
it('tracks action processing', async () => {
server = new TestServer()
server.type('FOO', {
access: () => true
})
server.type('ERR', {
access: () => true,
process() {
throw new Error('test')
}
})
server.type('DENIED', {
access: () => false
})
server.type('UNDO', {
access: () => true,
process(ctx, action, meta) {
server.undo(action, meta)
}
})
let client = await server.connect('10')
let processed = await client.process({ type: 'FOO' })
expect(processed).toEqual([{ id: '1 10:1:1 0', type: 'logux/processed' }])
let notDenied = await catchError(async () => {
await server.expectDenied(() => client.process({ type: 'FOO' }))
})
expect(notDenied.message).toEqual('Actions passed without error')
let serverError = await catchError(() => client.process({ type: 'ERR' }))
expect(serverError.message).toEqual('test')
expect(serverError.action).toEqual({
action: { type: 'ERR' },
id: '5 10:1:1 0',
reason: 'error',
type: 'logux/undo'
})
let accessError = await catchError(() => client.process({ type: 'DENIED' }))
expect(accessError.message).toEqual('Action was denied')
await server.expectDenied(() => client.process({ type: 'DENIED' }))
let unknownError = await catchError(() => client.process({ type: 'UNKNOWN' }))
expect(unknownError.message).toEqual(
'Server does not have callbacks for UNKNOWN actions'
)
let customError1 = await catchError(() => client.process({ type: 'UNDO' }))
expect(customError1.message).toEqual('Server undid action')
let customError2 = await catchError(async () => {
await server.expectDenied(() => client.process({ type: 'UNDO' }))
})
expect(customError2.message).toEqual('Undo was with error reason, not denied')
await server.expectUndo('error', () => client.process({ type: 'UNDO' }))
let reasonError = await catchError(async () => {
await server.expectUndo('another', () => client.process({ type: 'UNDO' }))
})
expect(reasonError.message).toEqual('Undo was with error reason, not another')
let noReasonError = await catchError(async () => {
await server.expectUndo('error', () => client.process({ type: 'UNKNOWN' }))
})
expect(noReasonError.message).toEqual(
'Server does not have callbacks for UNKNOWN actions'
)
await server.expectError('test', async () => {
await client.process({ type: 'ERR' })
})
await server.expectError(/te/, async () => {
await client.process({ type: 'ERR' })
})
let wrongMessageError = await catchError(async () => {
await server.expectError('te', async () => {
await client.process({ type: 'ERR' })
})
})
expect(wrongMessageError.message).toEqual('test')
let noErrorError = await catchError(async () => {
await server.expectError('te', async () => {
await client.process({ type: 'FOO' })
})
})
expect(noErrorError.message).toEqual('Actions passed without error')
})
it('detects action ID duplicate', async () => {
server = new TestServer()
server.type('FOO', {
access: () => true
})
let client = await server.connect('10')
client.log.keepActions()
let processed = await client.process({ type: 'FOO' }, { id: '1 10:1:1 0' })
expect(processed).toEqual([{ id: '1 10:1:1 0', type: 'logux/processed' }])
let err = await catchError(async () => {
await client.process({ type: 'FOO' }, { id: '1 10:1:1 0' })
})
expect(err.message).toEqual('Action 1 10:1:1 0 was already in log')
})
it('tracks subscriptions', async () => {
server = new TestServer()
server.channel<{}, {}, LoguxSubscribeAction>('foo', {
access: () => true,
load(ctx, action) {
ctx.sendBack({ a: action.filter?.a, since: action.since, type: 'FOO' })
}
})
let client = await server.connect('10')
let actions1 = await client.subscribe('foo')
expect(actions1).toEqual([{ a: undefined, type: 'FOO' }])
await client.unsubscribe('foo')
expect(privateMethods(server).subscribers).toEqual({})
let actions2 = await client.subscribe('foo', { a: 1 })
expect(actions2).toEqual([{ a: 1, type: 'FOO' }])
let actions3 = await client.subscribe('foo', undefined, {
id: '1 1:0:0',
time: 1
})
expect(actions3).toEqual([{ since: { id: '1 1:0:0', time: 1 }, type: 'FOO' }])
await client.unsubscribe('foo', { a: 1 })
expect(privateMethods(server).subscribers).toEqual({
foo: {
'10:1:1': {
filters: {
'{}': true
}
}
}
})
await client.unsubscribe('foo')
expect(privateMethods(server).subscribers).toEqual({})
let actions4 = await client.subscribe({
channel: 'foo',
filter: { a: 2 },
type: 'logux/subscribe'
})
expect(actions4).toEqual([{ a: 2, type: 'FOO' }])
let unknownError = await catchError(() => client.subscribe('unknown'))
expect(unknownError.message).toEqual(
'Server does not have callbacks for unknown channel'
)
})
it('prints server log', async () => {
let reporterStream = {
write() {}
}
spyOn(reporterStream, 'write', () => {})
server = new TestServer({
logger: { stream: reporterStream }
})
await server.connect('10:uuid')
expect((reporterStream.write as any as Spy).callCount).toEqual(2)
})
it('tests authentication', async () => {
server = new TestServer()
server.options.supports = '0.0.0'
server.auth(({ token, userId }) => userId === '10' && token === 'good')
let wrong = await catchError(async () => {
await server.connect('10', { token: 'bad' })
})
expect(wrong.message).toEqual('Wrong credentials')
await server.expectWrongCredentials('10', { token: 'bad' })
let error1 = await catchError(async () => {
await server.connect('10', { subprotocol: '1.0.0' })
})
expect(error1.message).toContain('wrong-subprotocol')
let error2 = await catchError(async () => {
await server.expectWrongCredentials('10', { subprotocol: '1.0.0' })
})
expect(error2.message).toContain('wrong-subprotocol')
await server.connect('10', { token: 'good' })
let notWrong = await catchError(async () => {
await server.expectWrongCredentials('10', { token: 'good' })
})
expect(notWrong.message).toEqual('Credentials passed')
})
it('disables build-in auth', async () => {
server = new TestServer({ auth: false })
expect(privateMethods(server).authenticator).not.toBeDefined()
})
it('sets client headers', async () => {
server = new TestServer()
await server.connect('10', { headers: { locale: 'fr' } })
let node = server.clientIds.get('10:1')?.node
expect(node?.remoteHeaders).toEqual({ locale: 'fr' })
})
it('sets client cookie', async () => {
server = new TestServer()
server.auth(({ cookie }) => cookie.token === 'good')
await server.connect('10', { cookie: { token: 'good' } })
await server.expectWrongCredentials('10', { cookie: { token: 'bad' } })
})
it('sets custom HTTP headers', async () => {
server = new TestServer()
server.auth(({ client }) => client.httpHeaders.authorization === 'good')
await server.connect('10', { httpHeaders: { authorization: 'good' } })
await server.expectWrongCredentials('10', {
httpHeaders: { authorization: 'bad' }
})
await server.expectWrongCredentials('10')
})
it('collects received actions', async () => {
server = new TestServer()
server.type('foo', {
access: () => true,
process(ctx) {
ctx.sendBack({ type: 'bar' })
}
})
let client = await server.connect('10')
let actions = await client.received(async () => {
await client.process({ type: 'foo' })
})
expect(actions).toEqual([
{ type: 'bar' },
{ id: '1 10:1:1 0', type: 'logux/processed' }
])
})
it('destroys on fatal', () => {
server = new TestServer()
// @ts-expect-error
server.emitter.emit('fatal')
// @ts-expect-error
expect(server.destroying).toBe(true)
})