Skip to content

src/youtube-range.test.ts

28 lines · 1023 B · TypeScript
Raw
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { sliceVideoRangeBody } from './youtube.ts'

describe('sliceVideoRangeBody', () => {
	it('passes through an exact Range-sized body', () => {
		const body = new Uint8Array([1, 2, 3, 4]).buffer
		const out = sliceVideoRangeBody(body, 0, 3)
		assert.equal(out.byteLength, 4)
		assert.deepEqual([...new Uint8Array(out)], [1, 2, 3, 4])
	})

	it('slices when the origin ignored Range and returned the whole object', () => {
		const full = new Uint8Array([10, 11, 12, 13, 14, 15, 16, 17]).buffer
		const first = sliceVideoRangeBody(full, 0, 3)
		const second = sliceVideoRangeBody(full, 4, 7)
		assert.deepEqual([...new Uint8Array(first)], [10, 11, 12, 13])
		assert.deepEqual([...new Uint8Array(second)], [14, 15, 16, 17])
	})

	it('throws on a true short read', () => {
		const body = new Uint8Array([1, 2]).buffer
		assert.throws(
			() => sliceVideoRangeBody(body, 0, 7),
			/Video range short read at 0-7: got 2, expected 8/,
		)
	})
})