| 1 | // The updates wire (live.go's updatesGet): each update framed as a |
| 2 | // 4-byte big-endian length then the bytes. A body that does not divide |
| 3 | // cleanly is an error rather than a short read — a client that guessed |
| 4 | // would be a client that silently lost the last update. |
| 5 | import test from 'node:test' |
| 6 | import assert from 'node:assert/strict' |
| 7 | import { parseFrames, frame } from './frames.mjs' |
| 8 | |
| 9 | test('an empty body is no frames', () => { |
| 10 | assert.deepEqual(parseFrames(new Uint8Array(0)), []) |
| 11 | }) |
| 12 | |
| 13 | test('frames come back in order with their exact bytes', () => { |
| 14 | const a = new Uint8Array([1, 2, 3]) |
| 15 | const b = new Uint8Array([]) |
| 16 | const c = new Uint8Array([9]) |
| 17 | const body = new Uint8Array([...frame(a), ...frame(b), ...frame(c)]) |
| 18 | const got = parseFrames(body) |
| 19 | assert.equal(got.length, 3) |
| 20 | assert.deepEqual(Array.from(got[0]), [1, 2, 3]) |
| 21 | assert.deepEqual(Array.from(got[1]), []) |
| 22 | assert.deepEqual(Array.from(got[2]), [9]) |
| 23 | }) |
| 24 | |
| 25 | test('a length prefix is big-endian', () => { |
| 26 | const body = new Uint8Array([0, 0, 1, 0, ...new Array(256).fill(7)]) |
| 27 | assert.equal(parseFrames(body)[0].length, 256) |
| 28 | }) |
| 29 | |
| 30 | test('a truncated frame throws rather than returning a short update', () => { |
| 31 | assert.throws(() => parseFrames(new Uint8Array([0, 0, 0, 5, 1, 2])), /frame/) |
| 32 | assert.throws(() => parseFrames(new Uint8Array([0, 0])), /frame/) |
| 33 | }) |
| 34 | |