import { test } from '@japa/runner' import { KEEPALIVE, decodeFrames, encodeFrame, isKeepalive } from '#lib/edge/index' test.group('edge / stream frames', () => { test('frames survive being split anywhere; keep-alive frames are recognised', async ({ assert }) => { const whole = Buffer.concat([ encodeFrame({ from: 160_000_000_001, to: 160_000_050_000, data: Buffer.from('first') }), encodeFrame(KEEPALIVE), encodeFrame({ from: 160_000_050_001, to: 160_000_100_000, data: Buffer.from('second chunk') }), ]) const pieces = [whole.subarray(0, 7), whole.subarray(7, 30), whole.subarray(30)] const frames = [] for await (const frame of decodeFrames(pieces)) frames.push(frame) assert.deepEqual(frames.map((f) => [f.from, f.to, f.data.toString(), isKeepalive(f)]), [ [160_000_000_001, 160_000_050_000, 'first', false], [0, 0, '', true], [160_000_050_001, 160_000_100_000, 'second chunk', false], ]) }) test('a stream that ends inside a frame is an error', async ({ assert }) => { const cut = encodeFrame({ from: 1, to: 2, data: Buffer.from('abc') }).subarray(0, 21) await assert.rejects(async () => { for await (const _ of decodeFrames([cut])) { } }, /inside a frame/) }) test('frames come out whole whatever the piece sizes, also pieces holding several frames', async ({ assert }) => { const sent = Array.from({ length: 12 }, (_, i) => ({ from: i * 10 + 1, to: i * 10 + 10, data: Buffer.alloc((i * 997) % 5000, i) })) const whole = Buffer.concat(sent.map(encodeFrame)) for (const size of [1, 3, 20, 21, 64, 1000, 4096, whole.length]) { const pieces = [] for (let at = 0; at < whole.length; at += size) pieces.push(new Uint8Array(whole.subarray(at, at + size))) const frames = [] for await (const frame of decodeFrames(pieces)) frames.push(frame) assert.deepEqual(frames.map((f) => [f.from, f.to, f.data.toString('hex')]), sent.map((f) => [f.from, f.to, f.data.toString('hex')]), `pieces of ${size}`) } }) })