Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import fc from 'fast-check'
3import { advanceOffset, parseOffset, ZERO_OFFSET } from '../../src/offset.js'
4import { formatPayloads, preparePayload } from '../../src/payload.js'
5import { streamPath } from '../../src/path.js'
6β
7describe('persistent offset contract', () => {
8 test('preserves the protocol zero token while nonzero checkpoints carry history', () => {
9 expect(ZERO_OFFSET).toBe('0000000000000000_0000000000000000')
10 expect(parseOffset(ZERO_OFFSET)).toEqual({ ordinal: 0n, history: '0'.repeat(32) })
11 const next = advanceOffset(ZERO_OFFSET, 'f'.repeat(32))
12 expect(next).toBe(`0000000000000001_${'f'.repeat(32)}`)
13 expect(() => parseOffset(`0000000000000000_${'0'.repeat(32)}`)).toThrow()
14 })
15 test('increments exactly above the JavaScript safe integer range', () => {
16 const history = 'a'.repeat(32)
17 expect(advanceOffset(`9007199254740992_${history}`)).toBe(`9007199254740993_${history}`)
18 expect(() => advanceOffset(`9999999999999999_${history}`)).toThrow('capacity')
19 })
20 test('offsets order by ordinal across arbitrary history transitions', () => {
21 fc.assert(fc.property(fc.array(fc.uuid(), { maxLength: 200 }), (histories) => {
22 let offset = ZERO_OFFSET, ordinal = 0n
23 for (const history of histories) {
24 const next = advanceOffset(offset, history.replaceAll('-', ''))
25 expect(next > offset).toBe(true)
26 expect(parseOffset(next).ordinal).toBe(++ordinal)
27 expect(parseOffset(next).history).toBe(history.replaceAll('-', ''))
28 offset = next
29 }
30 }))
31 })
32 test.each(['', 'now', '-1', '1_2', '0000000000000000_0000000000000000x'])('sentinels/invalid tokens are not stored offsets: %j', (value) => {
33 expect(() => parseOffset(value)).toThrow()
34 })
35})
36β
37describe('JSON storage contract', () => {
38 test('preserves number lexemes, flattens once, and records UTF-8 message ends', () => {
39 const body = Buffer.from('[9007199254740993,1e400,{"emoji":"π","comma":","},[1,2]]')
40 const prepared = preparePayload(body, 'Application/JSON; charset=utf-8')
41 expect(prepared.bytes.toString()).toBe('9007199254740993,1e400,{"emoji":"π","comma":","},[1,2],')
42 expect(prepared.messageEnds).toHaveLength(4)
43 const ends = prepared.messageEnds!
44 expect(prepared.bytes.subarray(ends[1], ends[2]).toString()).toBe('{"emoji":"π","comma":","},')
45 expect(formatPayloads([prepared.bytes], 'application/json').toString()).toBe(body.toString())
46 })
47 test('replays arbitrary valid JSON values without changing their meaning', () => {
48 fc.assert(fc.property(fc.jsonValue(), (value) => {
49 const input = Buffer.from(JSON.stringify(value))
50 const wireValue: unknown = JSON.parse(input.toString())
51 if (Array.isArray(value) && !value.length) return
52 const prepared = preparePayload(input, 'application/json')
53 expect(JSON.parse(formatPayloads([prepared.bytes], 'application/json').toString())).toEqual(Array.isArray(wireValue) ? wireValue : [wireValue])
54 }))
55 })
56 test('empty create differs from empty append; invalid UTF-8 is rejected', () => {
57 expect(preparePayload(Buffer.from('[]'), 'application/json', true).bytes.length).toBe(0)
58 expect(() => preparePayload(Buffer.from('[]'), 'application/json')).toThrow('Empty')
59 expect(() => preparePayload(Uint8Array.of(34, 255, 34), 'application/json')).toThrow('UTF-8')
60 expect(() => preparePayload(Buffer.from('[1,]'), 'application/json')).toThrow()
61 expect(() => preparePayload(Buffer.from('\u00a0{}'), 'application/json')).toThrow()
62 expect(preparePayload(Buffer.from('-0'), 'application/json').bytes.toString()).toBe('-0,')
63 })
64 test('binary bytes have no text conversion', () => {
65 fc.assert(fc.property(fc.uint8Array(), (body) => {
66 expect(formatPayloads([preparePayload(body, 'application/octet-stream').bytes], 'application/octet-stream')).toEqual(Buffer.from(body))
67 }))
68 })
69 test('multipart JSON formatting owns its bytes and tolerates empty fragments', () => {
70 const first = Buffer.from('9007199254740993,')
71 const second = Buffer.from('"π",')
72 const response = formatPayloads([first, Buffer.alloc(0), second, Buffer.alloc(0)], 'application/json')
73 first.fill(0)
74 second.fill(0)
75 expect(response.toString()).toBe('[9007199254740993,"π"]')
76 })
77})
78β
79describe('mount and path identity', () => {
80 test('decodes once, preserves Unicode, and respects the mount boundary', () => {
81 expect(streamPath(new URL('https://example.test/api/streams/caf%C3%A9'), '/api/streams')).toBe('cafΓ©')
82 expect(streamPath(new URL('https://example.test/api/streamsx/a'), '/api/streams')).toBeNull()
83 })
84 test.each(['a%2Fb', 'a%5Cb', 'a//b', '%252f', '%00', '%zz'])('rejects ambiguous paths: %s', (path) => {
85 expect(() => streamPath(new URL(`https://example.test/api/streams/${path}`), '/api/streams')).toThrow()
86 })
87})