-
Notifications
You must be signed in to change notification settings - Fork 42
/
sessions.e2e.ts
116 lines (105 loc) · 2.97 KB
/
sessions.e2e.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
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { Session, SessionEvent } from '@stencila/stencila'
import { Client, connect, disconnect } from './client'
import { start, stop, subscribe, unsubscribe } from './sessions'
jest.setTimeout(10000)
const clientId = 'cl-session-tests'
let client: Client
beforeAll(async () => {
client = await connect(
'pr-session-tests',
clientId,
process.env.SERVER_URL ?? 'ws://127.0.0.1:9000'
)
})
afterAll(() => {
disconnect(client)
})
// Test of the basic session workflow of starting a session, subscribing
// to it, receiving events and then stopping it.
test('basic', async () => {
let session: Session
// Start the session
session = await start(client, 'projectId')
expect(session).toEqual(
expect.objectContaining({
id: expect.stringMatching(/^se-[0-9a-zA-Z]{20}/),
project: 'projectId',
status: 'Started',
})
)
// Subscribe to updates
const updates: SessionEvent[] = []
session = await subscribe(client, session.id, 'updated', (event) => {
expect(event.type).toBe('Updated')
updates.push(event)
})
expect(session).toEqual(
expect.objectContaining({
subscriptions: { updated: [clientId] },
})
)
// Subscribe to heartbeats
const heartbeats: SessionEvent[] = []
session = await subscribe(client, session.id, 'heartbeat', (event) => {
expect(event.type).toBe('Heartbeat')
heartbeats.push(event)
})
expect(session).toEqual(
expect.objectContaining({
subscriptions: { updated: [clientId], heartbeat: [clientId] },
})
)
// The above subscription should fire an `updated` event
// Sleep for a little to allow it to be propagated.
await new Promise<void>((resolve) => setTimeout(resolve, 100))
expect(updates).toEqual(
expect.arrayContaining([
expect.objectContaining({
type: 'Updated',
session: expect.objectContaining({
id: session.id,
}),
}),
])
)
// Wait for a heartbeat. If this isn't working, this test should timeout
await new Promise<void>((resolve) => {
setInterval(() => {
if (heartbeats.length > 0) {
return resolve()
}
}, 1000)
})
expect(heartbeats).toEqual(
expect.arrayContaining([
expect.objectContaining({
type: 'Heartbeat',
session: expect.objectContaining({
id: session.id,
}),
}),
])
)
// Unsubscribe from heartbeats; should still be subscribed to updates
session = await unsubscribe(client, session.id, 'heartbeat')
expect(session).toEqual(
expect.objectContaining({
subscriptions: { updated: [clientId] },
})
)
// Unsubscribe from updates
session = await unsubscribe(client, session.id, 'updated')
expect(session).toEqual(
expect.objectContaining({
subscriptions: {},
})
)
// Stop the session
session = await stop(client, session.id)
expect(session).toEqual(
expect.objectContaining({
status: 'Stopped',
})
)
})