import assert from 'node:assert/strict'; import { test } from 'node:test'; import { feedVideoMute, streamVideo } from './video.js'; function mockFeedSound() { const video = Object.assign(new EventTarget(), { muted: true, paused: false }); let muted = false; const controls = feedVideoMute(video as unknown as HTMLVideoElement, (value) => (muted = value)); return { video, controls, get muted() { return muted; } }; } void test('toggling feed sound updates only the selected video and its control', () => { const first = mockFeedSound(); const second = mockFeedSound(); assert.equal(first.muted, true); first.controls.toggle(); assert.equal(first.video.muted, false); assert.equal(first.muted, false); assert.equal(second.video.muted, true); assert.equal(second.muted, true); first.controls.toggle(); assert.equal(first.video.muted, true); assert.equal(first.muted, true); }); void test('pauses reset feed sound and toggling cannot pre-arm a paused video', () => { const { video, controls } = mockFeedSound(); controls.toggle(); video.paused = true; video.dispatchEvent(new Event('pause')); assert.equal(video.muted, true); controls.toggle(); assert.equal(video.muted, true); video.paused = false; controls.toggle(); assert.equal(video.muted, false); // Viewport and playback-slot cleanup mute synchronously, before pausing. controls.mute(); assert.equal(video.muted, true); }); void test('feed sound follows native mute changes and disconnects on teardown', () => { const feed = mockFeedSound(); feed.video.muted = false; feed.video.dispatchEvent(new Event('volumechange')); assert.equal(feed.muted, false); feed.controls.destroy(); assert.equal(feed.video.muted, true); assert.equal(feed.muted, true); feed.controls.toggle(); assert.equal(feed.video.muted, true); feed.video.muted = false; feed.video.dispatchEvent(new Event('volumechange')); feed.video.dispatchEvent(new Event('pause')); assert.equal(feed.muted, true); assert.equal(feed.video.muted, false); }); function mockVideo(nativeHls = true) { const calls: string[] = []; let preload = 'metadata'; const video = { muted: false, src: '', get preload() { return preload; }, set preload(value: string) { calls.push(`preload:${value}`); preload = value; }, canPlayType(type: string) { assert.equal(type, 'application/vnd.apple.mpegurl'); return nativeHls ? 'probably' : ''; }, load() { calls.push(`load:${video.src}`); }, play() { calls.push('play'); return Promise.resolve(); }, pause() { calls.push('pause'); }, removeAttribute(name: string) { assert.equal(name, 'src'); calls.push('remove:src'); video.src = ''; } }; return { video, element: video as unknown as HTMLVideoElement, calls }; } void test('native HLS assigns the source, loads and autoplays muted', () => { const { video, element, calls } = mockVideo(); const src = 'https://example.com/video.m3u8?token=test'; const stream = streamVideo(element, src); assert.equal(typeof stream.setLoading, 'function'); assert.equal(typeof stream.destroy, 'function'); assert.equal(video.muted, true); assert.equal(video.src, src); assert.equal(video.preload, 'metadata'); assert.deepEqual(calls, [`load:${src}`, 'play']); }); void test('non-HLS sources load directly without native HLS support', () => { const { video, element, calls } = mockVideo(false); const src = 'https://example.com/video.mp4'; streamVideo(element, src); assert.equal(video.src, src); assert.equal(video.muted, true); assert.deepEqual(calls, [`load:${src}`, 'play']); }); void test('autoplay false still loads the native source without playing', () => { const { video, element, calls } = mockVideo(); streamVideo(element, 'video.m3u8', { autoplay: false }); assert.equal(video.muted, true); assert.deepEqual(calls, ['load:video.m3u8']); }); void test('native autoplay rejection is swallowed without reporting a stream error', async () => { const { video, element } = mockVideo(); let errors = 0; let caught = false; const rejection = Promise.reject(new Error('autoplay blocked')); const catchRejection = rejection.catch.bind(rejection); rejection.catch = (handler) => { caught = true; return catchRejection(handler); }; video.play = () => rejection; streamVideo(element, 'video.m3u8', { onerror: () => errors++ }); assert.equal(caught, true); await Promise.resolve(); assert.equal(errors, 0); }); void test('setLoading changes preload only on transitions without reloading or playing', () => { const { video, element, calls } = mockVideo(); const stream = streamVideo(element, 'video.m3u8'); calls.length = 0; stream.setLoading(true); assert.equal(video.preload, 'metadata'); assert.deepEqual(calls, []); stream.setLoading(false); assert.equal(video.preload, 'none'); stream.setLoading(false); assert.deepEqual(calls, ['preload:none']); stream.setLoading(true); assert.equal(video.preload, 'auto'); stream.setLoading(true); assert.deepEqual(calls, ['preload:none', 'preload:auto']); assert.equal(video.src, 'video.m3u8'); }); void test('destroy pauses, removes the source and reloads the empty element in order', () => { const { video, element, calls } = mockVideo(); const stream = streamVideo(element, 'video.m3u8'); calls.length = 0; stream.destroy(); assert.equal(video.src, ''); assert.deepEqual(calls, ['pause', 'remove:src', 'load:']); }); void test('setLoading is a no-op after disposal regardless of the previous loading state', () => { for (const loading of [true, false]) { const { video, element, calls } = mockVideo(); const stream = streamVideo(element, 'video.m3u8'); stream.setLoading(loading); stream.destroy(); const preload = video.preload; calls.length = 0; stream.setLoading(!loading); stream.setLoading(loading); assert.equal(video.preload, preload); assert.equal(video.src, ''); assert.deepEqual(calls, []); } });