forked from reconbot/graphql-lambda-subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration-events-test.ts
162 lines (137 loc) · 7.59 KB
/
integration-events-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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { assert } from 'chai'
import { start as sandboxStart, end as sandboxEnd } from '@architect/sandbox'
import { collect, map } from 'streaming-iterables'
import { executeDoubleQuery, executeQuery, executeToComplete, executeToDisconnect } from './execute-helper'
import { startGqlWSServer } from './graphql-ws-schema'
import { join } from 'path'
describe('Events', () => {
before(async () => {
await sandboxStart({ cwd: join(process.cwd(), './mocks/arc-basic-events'), quiet: true })
})
after(async () => {
// pending ddb writes need to finish, this is annoying
await new Promise(resolve => setTimeout(resolve, 100))
await sandboxEnd()
})
describe('Basic Events', () => {
it('queries', async () => {
const { url, stop } = await startGqlWSServer()
const lambdaResult = await collect(executeQuery('{ hello }'))
const gqlWSResult = await collect(executeQuery('{ hello }', { url }))
assert.deepEqual(lambdaResult, gqlWSResult)
await stop()
})
it('subscribes', async () => {
const { url, stop } = await startGqlWSServer()
const lambdaResult = await collect(executeQuery('subscription { greetings }'))
const gqlWSResult = await collect(executeQuery('subscription { greetings }', { url }))
assert.deepEqual(lambdaResult, gqlWSResult)
await stop()
})
it('errors on query validation failures', async () => {
const { url, stop } = await startGqlWSServer()
const lambdaResult = await collect(executeQuery('{ yo }'))
const gqlWSResult = await collect(executeQuery('{ yo }', { url }))
assert.deepEqual(lambdaResult, gqlWSResult)
await stop()
})
it('errors when a resolver errors', async () => {
const { url, stop } = await startGqlWSServer()
const lambdaResult = await collect(executeQuery('subscription { onResolveError }'))
const gqlWSResult = await collect(executeQuery('subscription { onResolveError }', { url }))
assert.deepEqual(lambdaResult, gqlWSResult)
await stop()
})
it('errors with validation errors', async () => {
const { url, stop } = await startGqlWSServer()
const lambdaResult = await collect(executeQuery('subscription { onSubscribeError }'))
const gqlWSResult = await collect(executeQuery('subscription { onSubscribeError }', { url }))
assert.deepEqual(lambdaResult, gqlWSResult)
await stop()
})
// it('errors when duplicating subscription ids with queries', async () => {
// const { url, stop } = await startGqlWSServer()
// const lambdaError = await collect(executeDoubleQuery('{ dontResolve }', { id: 1, skipWaitingForFirstMessage: true }))
// const gqlWSError = await collect(executeDoubleQuery('{ dontResolve }', { id: 1, skipWaitingForFirstMessage: true, url }))
// console.log({ lambdaError, gqlWSError })
// assert.deepEqual(lambdaError[0], gqlWSError[0])
// // This would be exactly equal but apigateway doesn't support close reasons *eye roll*
// assert.containSubset(lambdaError[1], { type: 'close' })
// assert.containSubset(gqlWSError[1], { type: 'close' })
// await stop()
// })
it('errors when duplicating subscription ids with subscriptions', async () => {
const { url, stop } = await startGqlWSServer()
const lambdaError = await collect(executeDoubleQuery('subscription { oneEvent }', { id: 1 }))
const gqlWSError = await collect(executeDoubleQuery('subscription { oneEvent }', { url, id: 1 }))
assert.deepEqual(lambdaError[0], gqlWSError[0])
// This would be exactly equal but apigateway doesn't support close reasons *eye roll*
assert.containSubset(lambdaError[1], { type: 'close' })
assert.containSubset(gqlWSError[1], { type: 'close' })
await stop()
})
})
describe('Filter Events', () => {
it('subscribes', async () => {
const values = executeQuery('subscription { filterTest }')
const greetings = await collect(map((value: any) => value.payload?.data?.filterTest ?? value.type, values))
assert.deepEqual(greetings, ['oh yes!', 'Missing fields also work', 'complete'])
})
})
describe('onSubscribe', () => {
it('gets the right args')
it('disconnects when it throws an error', async () => {
const sideChannel = executeQuery('subscription { sideChannel }')
assert.deepEqual((await sideChannel.next() as any).value.payload.data, { sideChannel: 'start' })
const subscribeErrors = executeQuery('subscription { onSubscribeError }')
assert.containSubset(await collect(subscribeErrors), [{ type: 'error', payload: [{ message: 'onSubscribeError' }] }])
assert.containSubset(await collect(sideChannel), [
{ type: 'next', payload: { data: { sideChannel: 'onSubscribe' } } },
])
})
})
describe('onComplete', () => {
it('fires when the client disconnects at least once', async () => {
const sideChannel = executeQuery('subscription { sideChannel }')
assert.containSubset(await sideChannel.next(), { done: false, value: { type: 'next', payload: { data: { sideChannel: 'start' } } } })
const disconnect = await executeToDisconnect('subscription { onCompleteTestClientDisconnect }')
assert.containSubset(await sideChannel.next(), { done: false, value: { type: 'next', payload: { data: { sideChannel: 'subscribed' } } } })
disconnect()
assert.containSubset((await collect(sideChannel))[0], { type: 'next', payload: { data: { sideChannel: 'onComplete' } } })
})
it('fires when the client completes', async () => {
// non lazy connections don't disconnect when unsubscribed
const sideChannel = executeQuery('subscription { sideChannel }')
assert.containSubset(await sideChannel.next(), { done: false, value: { type: 'next', payload: { data: { sideChannel: 'start' } } } })
const unsubscribe = await executeToComplete('subscription { onCompleteTestClientDisconnect }')
assert.containSubset(await sideChannel.next(), { done: false, value: { type: 'next', payload: { data: { sideChannel: 'subscribed' } } } })
await unsubscribe()
assert.containSubset((await collect(sideChannel)), [{ type: 'next', payload: { data: { sideChannel: 'onComplete' } } }, { type: 'complete' }])
})
it('fires when the resolver errors', async () => {
const sideChannel = executeQuery('subscription { sideChannel }')
assert.containSubset(await sideChannel.next(), { done: false, value: { type: 'next', payload: { data: { sideChannel: 'start' } } } })
const errorValuesGen = executeQuery('subscription { onResolveError }')
assert.containSubset(await collect(errorValuesGen), [
{ type: 'next', payload: { errors: [{ message: 'resolver error' }] } },
{ type: 'complete' },
])
assert.containSubset(await collect(sideChannel), [
{ type: 'next', payload: { data: { sideChannel: 'onComplete' } } },
{ type: 'complete' },
])
})
it('fires when the server completes', async () => {
const sideChannel = executeQuery('subscription { sideChannel }')
assert.containSubset(await sideChannel.next(), { done: false, value: { type: 'next', payload: { data: { sideChannel: 'start' } } } })
const values = executeQuery('subscription { onCompleteServerComplete }')
assert.containSubset(await collect(values), [{ type:'complete' }])
assert.containSubset((await collect(sideChannel)), [
{ type: 'next', payload: { data: { sideChannel: 'subscribed' } } },
{ type: 'next', payload: { data: { sideChannel: 'onComplete' } } },
{ type:'complete' },
])
})
})
})