Open History to see the last commit
1import { afterAll, beforeAll, expect, it } from 'vitest'
2import { DurableStream, stream } from '@durable-streams/client'
3import { DurableStreamTestServer } from '@durable-streams/server'
4import { EnvelopeConsumer } from '../../packages/protocol/src/consumer'
5import type { Envelope } from '../../packages/protocol/src/index'
6
7const server = new DurableStreamTestServer({ port: 0 })
8beforeAll(async () => {
9 await server.start()
10})
11afterAll(async () => {
12 await server.stop()
13})
14
15it('retains a synthetic logical cut inside one real response containing several publications', async () => {
16 const writer = await DurableStream.create({
17 url: `${server.url}/cut`,
18 contentType: 'application/json',
19 })
20 const objects: Envelope[] = [
21 {
22 version: 1,
23 seq: '1',
24 kind: 'initializing',
25 handle: 'cut',
26 expiresAt: '2026-10-15T12:15:00Z',
27 },
28 {
29 version: 1,
30 seq: '2',
31 kind: 'snapshot-complete',
32 coveredSeq: '1',
33 evidence: { snapshot: { xmin: '1', xmax: '1', xip_list: [] } },
34 },
35 ...(['3', '4', '5'] as const).map(
36 (seq): Envelope => ({
37 version: 1,
38 seq,
39 kind: 'publication',
40 evidence: {},
41 changes: [],
42 })
43 ),
44 ]
45 for (const object of objects) await writer.append(JSON.stringify(object))
46 const consumer = new EnvelopeConsumer()
47 const session = await stream<Envelope>({
48 url: writer.url,
49 live: false,
50 json: true,
51 warnOnHttp: false,
52 })
53 const positions: string[] = []
54 let responseCount = 0
55 session.subscribeJson(async (batch) => {
56 responseCount++
57 expect(batch.items).toHaveLength(5)
58 await consumer.consume(batch, async (envelope) => {
59 if (envelope.kind === 'publication')
60 positions.push(
61 `${envelope.seq}:${BigInt(envelope.seq) <= BigInt(4) ? 'before' : 'after'}`
62 )
63 expect(consumer.state.checkpoint.offset).toBe('-1')
64 })
65 })
66 await session.closed
67 expect(responseCount).toBe(1)
68 expect(positions).toEqual(['3:before', '4:before', '5:after'])
69 expect(consumer.state.checkpoint.offset).toBe(session.offset)
70})
71
72it('reads complete publication objects using the real reader and reference server', async () => {
73 const writer = await DurableStream.create({
74 url: `${server.url}/publication`,
75 contentType: 'application/json',
76 })
77 const init: Envelope = {
78 version: 1,
79 seq: '1',
80 kind: 'initializing',
81 handle: 'a',
82 expiresAt: '2026-10-15T12:15:00Z',
83 }
84 const baseline: Envelope = {
85 version: 1,
86 seq: '2',
87 kind: 'snapshot-complete',
88 coveredSeq: '1',
89 evidence: { snapshot: { xmin: '1', xmax: '1', xip_list: [] } },
90 }
91 const changes: Envelope = {
92 version: 1,
93 seq: '3',
94 kind: 'publication',
95 evidence: { txids: ['4294967300'] },
96 changes: [
97 { key: 'a', value: { id: '1' }, headers: { operation: 'insert' } },
98 { key: 'b', value: { id: '2' }, headers: { operation: 'insert' } },
99 ],
100 }
101 await writer.append(JSON.stringify(init))
102 const consumer = new EnvelopeConsumer()
103 const seen: Envelope[] = []
104 async function read() {
105 const session = await stream<Envelope>({
106 url: writer.url,
107 offset: consumer.state.checkpoint.offset,
108 live: false,
109 json: true,
110 warnOnHttp: false,
111 })
112 session.subscribeJson(async (batch) => {
113 await consumer.consume(batch, async (envelope) => {
114 seen.push(envelope)
115 })
116 })
117 await session.closed
118 }
119 await read()
120 expect(consumer.state.ready).toBe(false)
121 const earlyCheckpoint = consumer.state.checkpoint.offset
122 await writer.append(JSON.stringify(baseline))
123 await writer.append(JSON.stringify(changes))
124 await read()
125 expect(consumer.state.ready).toBe(true)
126 expect(seen).toEqual([init, baseline, changes])
127 expect(consumer.state.checkpoint.offset).not.toBe(earlyCheckpoint)
128 await read()
129 expect(seen).toHaveLength(3)
130})