Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

history-codec.test.ts

18 lines1.0 KB

1import { expect, test } from 'vitest'
2import { encodeBlockObject, decodeBlockObject } from '../../src/block-codec.js'
3import { chunkDigest } from '../../src/chunk-types.js'
4import { advanceOffset, ZERO_OFFSET } from '../../src/offset.js'
5​
6test('an object splits blocks at history transitions and preserves inherited start tokens', () => {
7 const identity = { installationId: '00112233-4455-6677-8899-aabbccddeeff', streamId: '11111111-1111-4111-8111-111111111111', chunkId: '22222222-2222-4222-8222-222222222222' }
8 const history = ['a'.repeat(32), 'a'.repeat(32), 'b'.repeat(32), 'b'.repeat(32)]
9 let cursor = ZERO_OFFSET
10 const frames = history.map((span) => {
11 const start = cursor
12 cursor = advanceOffset(start, span)
13 return { start, end: cursor, payload: Buffer.from('x'), messageEnds: null }
14 })
15 const encoded = encodeBlockObject({ ...identity, frames })
16 expect(encoded.directory).toHaveLength(2)
17 expect(decodeBlockObject(encoded.bytes, identity, chunkDigest(encoded.bytes)).frames).toEqual(frames)
18})