Open History to see the last commit
1import { expect, test } from 'vitest'
2import fc from 'fast-check'
3import { encodeBlockObject, decodeBlockObject, decodeBlock, unpackBlockDirectory, packBlockDirectory } from '../../src/block-codec.js'
4import { chunkDigest, ChunkIntegrityError } from '../../src/chunk-codec.js'
5import { advanceOffset, ZERO_OFFSET } from '../../src/offset.js'
6import { preparePayload } from '../../src/payload.js'
7
8const identity = { installationId: '00112233-4455-6677-8899-aabbccddeeff', streamId: '11111111-1111-4111-8111-111111111111', chunkId: '22222222-2222-4222-8222-222222222222' }
9const framesFrom = (payloads: readonly Uint8Array[]) => {
10 let next = ZERO_OFFSET
11 return payloads.map((payload) => {
12 const start = next
13 next = advanceOffset(start)
14 return { start, end: next, payload: Buffer.from(payload), messageEnds: null }
15 })
16}
17
18test('block binary encoding has explicit compact lengths and a packed physical directory', () => {
19 const frames = framesFrom([Buffer.from('hi')])
20 const encoded = encodeBlockObject({ ...identity, frames })
21 // Header 68, directory 56 + a 33-byte zero and 49-byte end offset, one 4-byte binary frame.
22 expect(encoded.bytes.length).toBe(210)
23 expect(encoded.directory[0]).toMatchObject({ firstFrame: 0, frameCount: 1, payloadBytes: 2, byteStart: 206, byteLength: 4 })
24 expect(encoded.bytes.subarray(206).toString('hex')).toBe('02006869')
25 expect(encoded.bytes.toString('ascii', 0, 8)).toBe('NDSBLK03')
26 expect(unpackBlockDirectory(encoded.packedDirectory, encoded.bytes.length, 1)).toEqual(encoded.directory)
27 expect(decodeBlockObject(encoded.bytes, identity, chunkDigest(encoded.bytes))).toEqual({ ...identity, frames })
28})
29
30test('generated independent blocks round-trip exact bytes and detect corruption in each selected range', () => {
31 fc.assert(fc.property(fc.array(fc.uint8Array({ minLength: 1, maxLength: 4096 }), { minLength: 1, maxLength: 30 }),
32 fc.integer({ min: 1, max: 8192 }), (payloads, target) => {
33 const frames = framesFrom(payloads)
34 const encoded = encodeBlockObject({ ...identity, frames }, target)
35 const directory = unpackBlockDirectory(encoded.packedDirectory, encoded.bytes.length, frames.length)
36 const decoded = directory.flatMap((block) => decodeBlock(encoded.bytes.subarray(block.byteStart, block.byteStart + block.byteLength), block))
37 expect(decoded).toEqual(frames)
38 for (const block of directory) {
39 const bytes = Buffer.from(encoded.bytes.subarray(block.byteStart, block.byteStart + block.byteLength))
40 bytes[bytes.length - 1]! ^= 1
41 expect(() => decodeBlock(bytes, block)).toThrow(ChunkIntegrityError)
42 }
43 }), { numRuns: 75 })
44})
45
46test('JSON message deltas preserve exact numbers, UTF-8 and batch boundaries across blocks', () => {
47 let next = ZERO_OFFSET
48 const frames = ['[9007199254740993,1e400,-0,"🐘"]', '[{"value":[1,2]},true,null]'].map((input) => {
49 const prepared = preparePayload(Buffer.from(input), 'application/json')
50 const start = next
51 next = advanceOffset(start)
52 return { start, end: next, payload: prepared.bytes, messageEnds: prepared.messageEnds }
53 })
54 const encoded = encodeBlockObject({ ...identity, frames }, 1)
55 expect(encoded.directory).toHaveLength(2)
56 expect(decodeBlockObject(encoded.bytes, identity, chunkDigest(encoded.bytes))).toEqual({ ...identity, frames })
57})
58
59test('framing overhead does not halve blocks containing half-target payloads', () => {
60 const frames = framesFrom([Buffer.alloc(32768), Buffer.alloc(32768), Buffer.alloc(32768)])
61 const encoded = encodeBlockObject({ ...identity, frames }, 65536)
62 expect(encoded.directory.map((block) => block.frameCount)).toEqual([2, 1])
63 expect(decodeBlockObject(encoded.bytes, identity, chunkDigest(encoded.bytes)).frames).toEqual(frames)
64})
65
66test('manifest gaps, overlaps, wrong identity and truncated data fail before frames are returned', () => {
67 const frames = framesFrom([Buffer.from('hello'), Buffer.from('world')])
68 const encoded = encodeBlockObject({ ...identity, frames }, 1)
69 const blocks = encoded.directory
70 for (const directory of [
71 [{ ...blocks[0]!, firstFrame: 1 }, blocks[1]!],
72 [blocks[0]!, { ...blocks[1]!, byteStart: blocks[1]!.byteStart - 1 }],
73 [blocks[0]!, { ...blocks[1]!, start: ZERO_OFFSET }],
74 [{ ...blocks[0]!, byteLength: 0 }, blocks[1]!],
75 ]) expect(() => unpackBlockDirectory(packBlockDirectory(directory), encoded.bytes.length, frames.length)).toThrow(ChunkIntegrityError)
76 expect(() => decodeBlockObject(encoded.bytes, { ...identity, streamId: identity.chunkId }, chunkDigest(encoded.bytes))).toThrow(ChunkIntegrityError)
77 for (const length of [0, 67, 68, encoded.bytes.length - 1]) {
78 const truncated = encoded.bytes.subarray(0, length)
79 expect(() => decodeBlockObject(truncated, identity, chunkDigest(truncated))).toThrow(ChunkIntegrityError)
80 }
81})
82
83test('a valid checksum cannot hide malformed integers, invalid message counts or trailing bytes', () => {
84 const encoded = encodeBlockObject({ ...identity, frames: framesFrom([Buffer.from('hi')]) })
85 const block = encoded.directory[0]!
86 for (const input of [
87 [0, 0],
88 [0x82, 0, 0, 104, 105],
89 [2, 3, 1, 1, 1, 104, 105],
90 [3, 0, 104, 105],
91 [2, 0, 104, 105, 0],
92 [0xff, 0xff, 0xff, 0xff, 0xff, 0],
93 ]) {
94 const bytes = Buffer.from(input)
95 expect(() => decodeBlock(bytes, { ...block, byteLength: bytes.length, sha256: chunkDigest(bytes) })).toThrow(ChunkIntegrityError)
96 }
97})
98
99
100test('tiny frames have a decoding-work bound independent of a large byte target', () => {
101 const frames = framesFrom(Array.from({ length: 8193 }, () => Buffer.from('x')))
102 const encoded = encodeBlockObject({ ...identity, frames }, 16 * 1024 * 1024)
103 expect(encoded.directory.map((block) => block.frameCount)).toEqual([8192, 1])
104 expect(decodeBlockObject(encoded.bytes, identity, chunkDigest(encoded.bytes)).frames).toEqual(frames)
105})