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/) }) })