| 1 | // fixtures/talk-store.test.mjs — what a presenting window fetches with |
| 2 | // the talk (talk-store.mjs): a clip's video is warmed into the browser's |
| 3 | // cache one at a time, its body read and dropped as it streams (never |
| 4 | // held whole), warming stops once 300 MiB have come, and no slide waits |
| 5 | // for any of it. |
| 6 | import { test } from 'node:test' |
| 7 | import assert from 'node:assert/strict' |
| 8 | import { loadTalk } from './talk-store.mjs' |
| 9 | |
| 10 | const MiB = 1024 * 1024 |
| 11 | |
| 12 | // A response whose body comes in chunks the test lets through. |
| 13 | function streamed (size) { |
| 14 | const chunks = [Math.floor(size / 2), size - Math.floor(size / 2)] |
| 15 | const r = { |
| 16 | ok: true, |
| 17 | status: 200, |
| 18 | reads: 0, |
| 19 | cancelled: false, |
| 20 | release: null, |
| 21 | headers: { get: k => (k.toLowerCase() === 'content-length' ? String(size) : null) }, |
| 22 | arrayBuffer () { throw new Error('a clip was held whole') }, |
| 23 | blob () { throw new Error('a clip was held whole') } |
| 24 | } |
| 25 | let gate = new Promise(resolve => { r.release = resolve }) |
| 26 | r.body = { |
| 27 | getReader: () => ({ |
| 28 | read: async () => { |
| 29 | await gate |
| 30 | if (r.reads >= chunks.length) return { done: true } |
| 31 | return { done: false, value: { byteLength: chunks[r.reads++] } } |
| 32 | }, |
| 33 | cancel: async () => { r.cancelled = true } |
| 34 | }) |
| 35 | } |
| 36 | r.open = () => { gate = Promise.resolve(); r.release() } |
| 37 | return r |
| 38 | } |
| 39 | |
| 40 | const tick = () => new Promise(resolve => setTimeout(resolve, 0)) |
| 41 | |
| 42 | test('clips are warmed one at a time, streamed and dropped, up to 300 MiB, and no slide waits for them', async () => { |
| 43 | const sizes = { '/w/c1': 200 * MiB, '/w/c2': 200 * MiB, '/w/c3': MiB } |
| 44 | const asked = [] |
| 45 | const responses = {} |
| 46 | const payload = { |
| 47 | deckSeq: 1, |
| 48 | slides: [ |
| 49 | { id: 'a', html: '', presses: 0, assets: [], clips: ['/w/c1', '/w/c2'] }, |
| 50 | { id: 'b', html: '', presses: 0, assets: [], clips: ['/w/c1', '/w/c3'] } |
| 51 | ] |
| 52 | } |
| 53 | const fetchImpl = async url => { |
| 54 | asked.push(url) |
| 55 | if (url === '/talk') return { ok: true, status: 200, headers: { get: () => '"1"' }, json: async () => payload } |
| 56 | responses[url] = streamed(sizes[url]) |
| 57 | return responses[url] |
| 58 | } |
| 59 | const store = await loadTalk({ href: '/talk', fonts: () => null, fetchImpl, picture: async () => {} }) |
| 60 | assert.equal(await store.ready('a'), true, 'a slide is ready while its clip is still coming') |
| 61 | assert.equal(await store.ready('b'), true) |
| 62 | await tick() |
| 63 | assert.deepEqual(asked.filter(u => u !== '/talk'), ['/w/c1'], 'one at a time') |
| 64 | responses['/w/c1'].open() |
| 65 | for (let i = 0; i < 10; i++) await tick() |
| 66 | assert.equal(responses['/w/c1'].reads, 2) |
| 67 | // 200 MiB in; the next would take it past 300, so warming stops there. |
| 68 | assert.deepEqual(asked.filter(u => u !== '/talk'), ['/w/c1', '/w/c2']) |
| 69 | for (let i = 0; i < 10; i++) await tick() |
| 70 | assert.equal(responses['/w/c2'].cancelled, true) |
| 71 | assert.equal(responses['/w/c2'].reads, 0) |
| 72 | assert.deepEqual(asked.filter(u => u !== '/talk'), ['/w/c1', '/w/c2'], 'nothing after the limit; nothing twice') |
| 73 | }) |
| 74 | |