This repository was archived by the owner on May 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgetFilteredSubs-test.ts
More file actions
166 lines (152 loc) · 4.22 KB
/
getFilteredSubs-test.ts
File metadata and controls
166 lines (152 loc) · 4.22 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { tables } from '@architect/sandbox'
import { assert } from 'chai'
import { mockServerContext } from '../test/mockServer'
import { collapseKeys, getFilteredSubs } from './getFilteredSubs'
describe('collapseKeys', () => {
it('makes the deep objects into dots', () => {
assert.deepEqual(collapseKeys({}), {})
assert.deepEqual(
collapseKeys({ a: 4, b: { c: 5, d: 'hi', e: { f: false } } }),
{
a: 4,
'b.c': 5,
'b.d': 'hi',
'b.e.f': false,
},
)
assert.deepEqual(
collapseKeys({ a: [1, 2, 3, { b: 4, c: [], d: null, e: undefined }] }),
{
'a.0': 1,
'a.1': 2,
'a.2': 3,
'a.3.b': 4,
},
)
})
})
// since we're not resetting the db every time we need to change this
let count = 1
const makeTopic = () => `topic-${count++}`
describe('getFilteredSubs', () => {
before(async () => {
await tables.start({ cwd: './mocks/arc-basic-events', quiet: true })
})
after(async () => {
await tables.end()
})
it('can match on no filter', async () => {
const topic = makeTopic()
const server = await mockServerContext()
const subscription = {
id: '1',
topic,
filter: {},
subscriptionId: '1',
subscription: {} as any,
connectionId: 'abcd',
connectionInitPayload: {},
requestContext: {} as any,
ttl: Math.floor(Date.now() / 1000) + 100000,
createdAt: Date.now(),
}
await server.models.subscription.put(subscription)
assert.containSubset(
await getFilteredSubs({
server,
event: { topic, payload: { language: 'en' } },
}),
[{ topic, id: '1' }],
)
})
it('can match on payload', async () => {
const topic = makeTopic()
const server = await mockServerContext()
const subscription = {
id: '2',
topic,
filter: { language: 'en' },
subscriptionId: '2',
subscription: {} as any,
connectionId: 'abcd',
connectionInitPayload: {},
requestContext: {} as any,
ttl: Math.floor(Date.now() / 1000) + 100000,
createdAt: Date.now(),
}
await server.models.subscription.put(subscription)
assert.containSubset(
await getFilteredSubs({
server,
event: { topic, payload: { language: 'en' } },
}),
[{ topic, id: '2' }],
)
assert.deepEqual(
await getFilteredSubs({
server,
event: { topic, payload: { language: 'en-gb' } },
}),
[],
)
})
it('can match on nested payload', async () => {
const topic = makeTopic()
const server = await mockServerContext()
const subscription = {
id: '2',
topic,
filter: { meta: { user: 'foo' }, message: { content: 'hi' } },
subscriptionId: '2',
subscription: {} as any,
connectionId: 'abcd',
connectionInitPayload: {},
requestContext: {} as any,
ttl: Math.floor(Date.now() / 1000) + 100000,
createdAt: Date.now(),
}
await server.models.subscription.put(subscription)
assert.containSubset(
await getFilteredSubs({
server,
event: {
topic,
payload: { meta: { user: 'foo' }, message: { content: 'hi' } },
},
}),
[{ topic, id: '2' }],
)
assert.deepEqual(
await getFilteredSubs({
server,
event: {
topic,
payload: { meta: { user: 'lol' }, message: { content: 'bye' } },
},
}),
[],
)
})
it('can match on no payload', async () => {
const topic = makeTopic()
const server = await mockServerContext()
const subscription = {
id: '1234567',
topic,
filter: { language: 'en ' },
subscriptionId: '12345',
subscription: {} as any,
connectionId: '1234',
connectionInitPayload: {},
requestContext: {} as any,
ttl: Math.floor(Date.now() / 1000) + 100000,
createdAt: Date.now(),
}
await server.models.subscription.put(subscription)
const subscriptions = await getFilteredSubs({ server, event: { topic } })
assert.containSubset(subscriptions, [{ topic, id: '1234567' }])
})
it('can match on connectionId')
it('can match on topic key')
})