Files
feuerwerk/tests/viewport/video-library.test.ts
T
adminandClaude Opus 5.5 dd60a795e1
game-maker / gate (push) Successful in 4m28s
game-maker / suite (push) Failing after 16m24s
Wait for driven videos before a run starts so no sprite flashes (#404)
The game view opens a player for each driven video only when it draws,
and it draws only while its tab is visible. Without a detour over the
Game tab the players came up with the click on Play. Until a video had
loaded, its sprite showed its base colour, white in the user's cut.
Sharing the players of the scene view would not have helped. From the
Animations tab those are discarded after two to four seconds.

- Play holds the run at second 0 until every driven video in the game
  view has its first picture, at most 3 s. The status line says
  "Loading the videos… · Esc stops".
- The game clock stands while it waits. Behaviors and sounds start
  only after it, so picture and sound stay together.
- Load scene in a running game waits the same way.
- A driven video without a picture is not drawn at all instead of
  white. That holds everywhere, also on a switch to the Game tab
  without Play and at a cut whose video is not ready.
- The exported player waits the same way.
- __gm.playInfo() reports waiting for the e2e check.

Tests:
- e2e 66p puts the sprite in front of the camera and waits 4.5 s so
  the game view has no player left. Then it reads every frame of the
  run: 14 frames of waiting on #22252c at time 0, then blue, no white.
- Against the old code 66p fails: 14 frames #ffffff, then blue.
- Unit video-library: 7 passed, 1 new for loading().
- e2e 66 on melektaus-atx: ALL PASSED, 27 checks.
- Gate on melektaus-atx: exit 0, 297 files, 4558 tests.
- tsc, eslint, Prettier clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 17:07:53 +02:00

145 lines
4.8 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
/**
* When the video library lets a player go (#402): after 2 s nobody asked for it, but never while
* the page is hidden. Chrome draws no frames for a hidden page and so nothing asks, while the
* sweeper's timer goes on; a sprite still wearing the video lost it and Pixi warned about the
* destroyed source on the way back.
*/
const KEY = `video:a1b2c3d4e5f6/${'c7'.repeat(32)}.mp4#0~0~1~1`;
let videosMade = 0;
function fakeVideo() {
videosMade += 1;
return {
muted: false,
playsInline: false,
preload: '',
src: '',
currentTime: 0,
loop: false,
paused: true,
addEventListener: () => {},
requestVideoFrameCallback: () => 0,
play: () => Promise.resolve(),
pause: () => {},
removeAttribute: () => {},
load: () => {},
};
}
const page = { hidden: false, listeners: [] as (() => void)[] };
function setHidden(hidden: boolean): void {
page.hidden = hidden;
for (const listener of page.listeners) listener();
}
async function freshLibrary() {
const { VideoLibrary } = await import('../../src/viewport/VideoLibrary');
return new VideoLibrary();
}
describe('VideoLibrary sweeping', () => {
beforeEach(() => {
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval', 'performance'] });
page.hidden = false;
page.listeners = [];
vi.stubGlobal('document', {
get hidden() {
return page.hidden;
},
addEventListener: (_type: string, listener: () => void) => page.listeners.push(listener),
createElement: fakeVideo,
});
});
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
});
it('lets a player go that nobody asked for in 2 s', async () => {
const videos = await freshLibrary();
videos.get(KEY);
vi.advanceTimersByTime(4100);
expect(videos.debugInfo()).toEqual([]);
});
it('keeps it while the page is hidden, however long', async () => {
const videos = await freshLibrary();
videos.get(KEY);
setHidden(true);
vi.advanceTimersByTime(60_000);
expect(videos.debugInfo()).toHaveLength(1);
});
it('does not count the time hidden as idle once the page is back', async () => {
const videos = await freshLibrary();
videos.get(KEY);
setHidden(true);
vi.advanceTimersByTime(60_000);
setHidden(false);
vi.advanceTimersByTime(1900);
expect(videos.debugInfo()).toHaveLength(1);
vi.advanceTimersByTime(2200);
expect(videos.debugInfo()).toEqual([]);
});
it('keeps a driven player that is only worn, as by a sprite switched off', async () => {
const videos = await freshLibrary();
const ref = { file: KEY, smooth: true, rect: null, sourceWidth: 320, sourceHeight: 180, slice: null };
const props = { videoClock: 'driven', videoTime: 1.2 };
videosMade = 0;
for (let ms = 0; ms < 6000; ms += 100) {
videos.wear(ref, props, 'view2:a', false);
vi.advanceTimersByTime(100);
}
// Let go and opened again would be a second video, loading while the sprite shows white.
expect(videosMade).toBe(1);
expect(videos.debugInfo().map(({ key }) => key)).toEqual([`${KEY}@view2:a`]);
});
});
describe('VideoLibrary driven players (#404)', () => {
beforeEach(() => {
vi.stubGlobal('document', { hidden: false, addEventListener: () => {}, createElement: fakeVideo });
});
afterEach(() => vi.unstubAllGlobals());
const REF = { file: KEY, smooth: true, rect: null, sourceWidth: 320, sourceHeight: 180, slice: null };
it('leaves a sprite with a free clock on the shared player', async () => {
const videos = await freshLibrary();
expect(videos.wear(REF, { videoTime: 1 }, 'view1:a', false)).toBe(REF);
expect(videos.debugInfo()).toEqual([]);
});
it('gives every wearer of a driven video a player of its own, opened at its time', async () => {
const videos = await freshLibrary();
const a = videos.wear(REF, { videoClock: 'driven', videoTime: 0.5 }, 'view1:a', false);
const b = videos.wear(REF, { videoClock: 'driven', videoTime: 1.25 }, 'view1:b', false);
expect([a.file, b.file]).toEqual([`${KEY}@view1:a`, `${KEY}@view1:b`]);
expect(videos.debugInfo().map(({ key, time }) => [key, time])).toEqual([
[`${KEY}@view1:a`, 0.5],
[`${KEY}@view1:b`, 1.25],
]);
});
it('tells a view whether one of its driven players still waits for its first picture', async () => {
const videos = await freshLibrary();
videos.get(KEY);
expect(videos.loading('view12')).toBe(false);
const worn = videos.wear(REF, { videoClock: 'driven', videoTime: 0 }, 'view12:a', false);
expect(videos.hasPicture(worn.file)).toBe(false);
// A run waits only for its own view: view12 is neither view1 nor view2.
expect([videos.loading('view12'), videos.loading('view1'), videos.loading('view2')]).toEqual([
true,
false,
false,
]);
});
});