// fixtures/talk-store.test.mjs — what a presenting window fetches with // the talk (talk-store.mjs): a clip's video is warmed into the browser's // cache one at a time, its body read and dropped as it streams (never // held whole), warming stops once 300 MiB have come, and no slide waits // for any of it. import { test } from 'node:test' import assert from 'node:assert/strict' import { loadTalk } from './talk-store.mjs' const MiB = 1024 * 1024 // A response whose body comes in chunks the test lets through. function streamed (size) { const chunks = [Math.floor(size / 2), size - Math.floor(size / 2)] const r = { ok: true, status: 200, reads: 0, cancelled: false, release: null, headers: { get: k => (k.toLowerCase() === 'content-length' ? String(size) : null) }, arrayBuffer () { throw new Error('a clip was held whole') }, blob () { throw new Error('a clip was held whole') } } let gate = new Promise(resolve => { r.release = resolve }) r.body = { getReader: () => ({ read: async () => { await gate if (r.reads >= chunks.length) return { done: true } return { done: false, value: { byteLength: chunks[r.reads++] } } }, cancel: async () => { r.cancelled = true } }) } r.open = () => { gate = Promise.resolve(); r.release() } return r } const tick = () => new Promise(resolve => setTimeout(resolve, 0)) test('clips are warmed one at a time, streamed and dropped, up to 300 MiB, and no slide waits for them', async () => { const sizes = { '/w/c1': 200 * MiB, '/w/c2': 200 * MiB, '/w/c3': MiB } const asked = [] const responses = {} const payload = { deckSeq: 1, slides: [ { id: 'a', html: '', presses: 0, assets: [], clips: ['/w/c1', '/w/c2'] }, { id: 'b', html: '', presses: 0, assets: [], clips: ['/w/c1', '/w/c3'] } ] } const fetchImpl = async url => { asked.push(url) if (url === '/talk') return { ok: true, status: 200, headers: { get: () => '"1"' }, json: async () => payload } responses[url] = streamed(sizes[url]) return responses[url] } const store = await loadTalk({ href: '/talk', fonts: () => null, fetchImpl, picture: async () => {} }) assert.equal(await store.ready('a'), true, 'a slide is ready while its clip is still coming') assert.equal(await store.ready('b'), true) await tick() assert.deepEqual(asked.filter(u => u !== '/talk'), ['/w/c1'], 'one at a time') responses['/w/c1'].open() for (let i = 0; i < 10; i++) await tick() assert.equal(responses['/w/c1'].reads, 2) // 200 MiB in; the next would take it past 300, so warming stops there. assert.deepEqual(asked.filter(u => u !== '/talk'), ['/w/c1', '/w/c2']) for (let i = 0; i < 10; i++) await tick() assert.equal(responses['/w/c2'].cancelled, true) assert.equal(responses['/w/c2'].reads, 0) assert.deepEqual(asked.filter(u => u !== '/talk'), ['/w/c1', '/w/c2'], 'nothing after the limit; nothing twice') })