Skip to content

Commit b37a7d9

Browse files
committed
test(events): prove the shared pre-open authorization through the stream's writes
1 parent acde14b commit b37a7d9

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎apps/sim/lib/events/sse-endpoint.test.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,16 @@ describe('createSSEStream', () => {
291291
])
292292
})
293293

294-
it('authorizes the events that arrived before it opened once', async () => {
294+
it('writes every event queued before it opened once one authorization passes', async () => {
295295
let live: () => void = () => {}
296296
let publish: (eventName: string, data: Record<string, unknown>) => void = () => {}
297-
const revalidate = vi.fn(async () => {})
297+
const authorizations: Array<() => void> = []
298298
const response = createSSEStream(new NextRequest(new URL('https://sim.test/api/test/stream')), {
299299
label: 'test',
300-
revalidate,
300+
revalidate: () =>
301+
new Promise<void>((resolve) => {
302+
authorizations.push(resolve)
303+
}),
301304
subscriptions: [
302305
{
303306
subscribe: (send) => {
@@ -314,13 +317,16 @@ describe('createSSEStream', () => {
314317
const chunks: string[] = []
315318
void collect(response.body as ReadableStream<Uint8Array>, chunks)
316319
for (let n = 1; n <= 10; n += 1) publish('changed', { n })
317-
318320
live()
319321
await vi.advanceTimersByTimeAsync(0)
320322

321-
expect(revalidate).toHaveBeenCalledTimes(1)
322-
expect(chunks).toHaveLength(11)
323-
expect(chunks.at(-1)).toBe('event: changed\ndata: {"n":10}\n\n')
323+
authorizations[0]()
324+
await vi.advanceTimersByTimeAsync(0)
325+
326+
expect(chunks).toEqual([
327+
OPENED_COMMENT,
328+
...Array.from({ length: 10 }, (_, index) => `event: changed\ndata: {"n":${index + 1}}\n\n`),
329+
])
324330
})
325331

326332
it('clears its timers when it closes before it opens', async () => {

0 commit comments

Comments
 (0)