-
Notifications
You must be signed in to change notification settings - Fork 554
/
subscribe-repos.test.ts
370 lines (321 loc) · 11.1 KB
/
subscribe-repos.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import AtpAgent from '@atproto/api'
import {
cborDecode,
HOUR,
MINUTE,
readFromGenerator,
wait,
} from '@atproto/common'
import { randomStr } from '@atproto/crypto'
import * as repo from '@atproto/repo'
import {
getWriteLog,
MemoryBlockstore,
readCar,
WriteOpAction,
} from '@atproto/repo'
import { byFrame, ErrorFrame, Frame, MessageFrame } from '@atproto/xrpc-server'
import { WebSocket } from 'ws'
import {
Commit as CommitEvt,
Handle as HandleEvt,
} from '../../src/lexicon/types/com/atproto/sync/subscribeRepos'
import { AppContext, Database } from '../../src'
import { SeedClient } from '../seeds/client'
import basicSeed from '../seeds/basic'
import { CloseFn, runTestServer } from '../_util'
import { CID } from 'multiformats/cid'
describe('repo subscribe repos', () => {
let serverHost: string
let db: Database
let ctx: AppContext
let agent: AtpAgent
let sc: SeedClient
let alice: string
let bob: string
let carol: string
let dan: string
let close: CloseFn
beforeAll(async () => {
const server = await runTestServer({
dbPostgresSchema: 'repo_subscribe_repos',
})
serverHost = server.url.replace('http://', '')
ctx = server.ctx
db = server.ctx.db
close = server.close
agent = new AtpAgent({ service: server.url })
sc = new SeedClient(agent)
await basicSeed(sc)
alice = sc.dids.alice
bob = sc.dids.bob
carol = sc.dids.carol
dan = sc.dids.dan
})
afterAll(async () => {
await close()
})
const getRepo = async (did: string) => {
const car = await agent.api.com.atproto.sync.getRepo({ did })
const storage = new MemoryBlockstore()
const synced = await repo.loadFullRepo(
storage,
new Uint8Array(car.data),
did,
ctx.repoSigningKey.did(),
)
return repo.Repo.load(storage, synced.root)
}
const getHandleEvts = (frames: Frame[]): HandleEvt[] => {
const evts: HandleEvt[] = []
for (const frame of frames) {
if (frame instanceof MessageFrame && frame.header.t === '#handle') {
evts.push(frame.body)
}
}
return evts
}
const verifyHandleEvent = (evt: HandleEvt, did: string, handle: string) => {
expect(evt.did).toBe(did)
expect(evt.handle).toBe(handle)
expect(typeof evt.time).toBe('string')
expect(typeof evt.seq).toBe('number')
}
const getCommitEvents = (userDid: string, frames: Frame[]) => {
const evts: CommitEvt[] = []
for (const frame of frames) {
if (frame instanceof MessageFrame && frame.header.t === '#commit') {
const body = frame.body as CommitEvt
if (body.repo === userDid) {
evts.push(frame.body)
}
}
}
return evts
}
const verifyCommitEvents = async (frames: Frame[]) => {
await verifyRepo(alice, getCommitEvents(alice, frames))
await verifyRepo(bob, getCommitEvents(bob, frames))
await verifyRepo(carol, getCommitEvents(carol, frames))
await verifyRepo(dan, getCommitEvents(dan, frames))
}
const verifyRepo = async (did: string, evts: CommitEvt[]) => {
const didRepo = await getRepo(did)
const writeLog = await getWriteLog(didRepo.storage, didRepo.cid, null)
const commits = await didRepo.storage.getCommits(didRepo.cid, null)
if (!commits) {
return expect(commits !== null)
}
expect(evts.length).toBe(commits.length)
expect(evts.length).toBe(writeLog.length)
for (let i = 0; i < commits.length; i++) {
const commit = commits[i]
const evt = evts[i]
expect(evt.repo).toEqual(did)
expect(evt.commit.toString()).toEqual(commit.commit.toString())
expect(evt.prev?.toString()).toEqual(commits[i - 1]?.commit?.toString())
const car = await repo.readCarWithRoot(evt.blocks as Uint8Array)
expect(car.root.equals(commit.commit))
expect(car.blocks.equals(commit.blocks))
const writes = writeLog[i].map((w) => ({
action: w.action,
path: w.collection + '/' + w.rkey,
cid: w.action === WriteOpAction.Delete ? null : w.cid.toString(),
}))
const sortedOps = evt.ops
.sort((a, b) => a.path.localeCompare(b.path))
.map((op) => ({ ...op, cid: op.cid?.toString() ?? null }))
const sortedWrites = writes.sort((a, b) => a.path.localeCompare(b.path))
expect(sortedOps).toEqual(sortedWrites)
}
}
const randomPost = (by: string) => sc.post(by, randomStr(8, 'base32'))
const makePosts = async () => {
for (let i = 0; i < 3; i++) {
await Promise.all([
randomPost(alice),
randomPost(bob),
randomPost(carol),
randomPost(dan),
])
}
}
const readTillCaughtUp = async <T>(
gen: AsyncGenerator<T>,
waitFor?: Promise<unknown>,
) => {
const isDone = async (evt: any) => {
if (evt === undefined) return false
if (evt instanceof ErrorFrame) return true
const curr = await db.db
.selectFrom('repo_seq')
.select('seq')
.limit(1)
.orderBy('seq', 'desc')
.executeTakeFirst()
return curr !== undefined && evt.body.seq === curr.seq
}
return readFromGenerator(gen, isDone, waitFor)
}
it('sync backfilled events', async () => {
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${-1}`,
)
const gen = byFrame(ws)
const evts = await readTillCaughtUp(gen)
ws.terminate()
await verifyCommitEvents(evts)
})
it('syncs new events', async () => {
const postPromise = makePosts()
const readAfterDelay = async () => {
await wait(200) // wait just a hair so that we catch it during cutover
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${-1}`,
)
const evts = await readTillCaughtUp(byFrame(ws), postPromise)
ws.terminate()
return evts
}
const [evts] = await Promise.all([readAfterDelay(), postPromise])
await verifyCommitEvents(evts)
})
it('handles no backfill', async () => {
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos`,
)
const makePostsAfterWait = async () => {
// give them just a second to get subscriptions set up
await wait(200)
await makePosts()
}
const postPromise = makePostsAfterWait()
const [evts] = await Promise.all([
readTillCaughtUp(byFrame(ws), postPromise),
postPromise,
])
ws.terminate()
expect(evts.length).toBe(12)
await wait(100) // Let cleanup occur on server
expect(ctx.sequencer.listeners('events').length).toEqual(0)
})
it('backfills only from provided cursor', async () => {
const seqs = await db.db
.selectFrom('repo_seq')
.selectAll()
.orderBy('seq', 'asc')
.execute()
const midPoint = Math.floor(seqs.length / 2)
const midPointSeq = seqs[midPoint].seq
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${midPointSeq}`,
)
const evts = await readTillCaughtUp(byFrame(ws))
ws.terminate()
const seqSlice = seqs.slice(midPoint + 1)
expect(evts.length).toBe(seqSlice.length)
for (let i = 0; i < evts.length; i++) {
const evt = evts[i].body as CommitEvt
const seq = seqSlice[i]
const seqEvt = cborDecode(seq.event) as { commit: CID }
expect(evt.time).toEqual(seq.sequencedAt)
expect(evt.commit.equals(seqEvt.commit)).toBeTruthy()
expect(evt.repo).toEqual(seq.did)
}
})
it('syncs handle changes', async () => {
await sc.updateHandle(alice, 'alice2.test')
await sc.updateHandle(bob, 'bob2.test')
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${-1}`,
)
const gen = byFrame(ws)
const evts = await readTillCaughtUp(gen)
ws.terminate()
await verifyCommitEvents(evts)
const handleEvts = getHandleEvts(evts.slice(-2))
verifyHandleEvent(handleEvts[0], alice, 'alice2.test')
verifyHandleEvent(handleEvts[1], bob, 'bob2.test')
})
it('does not return invalidated events', async () => {
await sc.updateHandle(alice, 'alice3.test')
await sc.updateHandle(alice, 'alice4.test')
await sc.updateHandle(alice, 'alice5.test')
await sc.updateHandle(bob, 'bob3.test')
await sc.updateHandle(bob, 'bob4.test')
await sc.updateHandle(bob, 'bob5.test')
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${-1}`,
)
const gen = byFrame(ws)
const evts = await readTillCaughtUp(gen)
ws.terminate()
const handleEvts = getHandleEvts(evts)
expect(handleEvts.length).toBe(2)
verifyHandleEvent(handleEvts[0], alice, 'alice5.test')
verifyHandleEvent(handleEvts[1], bob, 'bob5.test')
})
it('sync rebases', async () => {
const prevHead = await agent.api.com.atproto.sync.getHead({ did: alice })
await ctx.db.transaction((dbTxn) =>
ctx.services.repo(dbTxn).rebaseRepo(alice, new Date().toISOString()),
)
const currHead = await agent.api.com.atproto.sync.getHead({ did: alice })
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${-1}`,
)
const gen = byFrame(ws)
const frames = await readTillCaughtUp(gen)
ws.terminate()
const aliceEvts = getCommitEvents(alice, frames)
expect(aliceEvts.length).toBe(1)
const evt = aliceEvts[0]
expect(evt.rebase).toBe(true)
expect(evt.tooBig).toBe(false)
expect(evt.commit.toString()).toEqual(currHead.data.root)
expect(evt.prev?.toString()).toEqual(prevHead.data.root)
expect(evt.ops).toEqual([])
expect(evt.blobs).toEqual([])
const car = await readCar(evt.blocks)
expect(car.blocks.size).toBe(1)
expect(car.roots.length).toBe(1)
expect(car.roots[0].toString()).toEqual(currHead.data.root)
// did not affect other users
const bobEvts = getCommitEvents(bob, frames)
expect(bobEvts.length).toBeGreaterThan(10)
})
it('sends info frame on out of date cursor', async () => {
// we rewrite the sequenceAt time for existing seqs to be past the backfill cutoff
// then we create some new posts
const overAnHourAgo = new Date(Date.now() - HOUR - MINUTE).toISOString()
await db.db
.updateTable('repo_seq')
.set({ sequencedAt: overAnHourAgo })
.execute()
await makePosts()
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${-1}`,
)
const [info, ...evts] = await readTillCaughtUp(byFrame(ws))
ws.terminate()
if (!(info instanceof MessageFrame)) {
throw new Error('Expected first frame to be a MessageFrame')
}
expect(info.header.t).toBe('#info')
const body = info.body as Record<string, unknown>
expect(body.name).toEqual('OutdatedCursor')
expect(evts.length).toBe(12)
})
it('errors on future cursor', async () => {
const ws = new WebSocket(
`ws://${serverHost}/xrpc/com.atproto.sync.subscribeRepos?cursor=${100000}`,
)
const frames = await readTillCaughtUp(byFrame(ws))
ws.terminate()
expect(frames.length).toBe(1)
if (!(frames[0] instanceof ErrorFrame)) {
throw new Error('Expected ErrorFrame')
}
expect(frames[0].body.error).toBe('FutureCursor')
})
})