Files
hyperframes/packages/core/src/runtime/adapters/seek-dispatch.test.ts
T
James RussoandClaude Opus 4.8 d580f2a1d8 fix(render): make WebGL video textures deterministic in headless render (#1403)
* fix(render): make WebGL video textures deterministic in headless render

WebGL compositions that sample a `<video>` as a texture (e.g. a faceted
crystal with clips mapped onto its facets) rendered with flickering,
non-deterministic facets: a video would intermittently show a stale frame or
go black, and the same frame differed between two renders.

Two gaps caused this:

1. No WebGL analog of the WebGPU `patchVideoTextureCompat`. Chrome's headless
   compositor can't feed decoded `<video>` frames to the GPU, so the engine
   injects a decoded `<img class="__render_frame__">` sibling per video each
   frame. The WebGPU `copyExternalImageToTexture` path substitutes it, but
   `texImage2D` / `texSubImage2D` did not — so WebGL uploaded a stale/black
   frame. Add `patchWebGLVideoTextureCompat()` mirroring the WebGPU patch
   (shared `resolveRenderFrameImage` helper).

2. Capture ordering. Per frame the runtime seeks (GPU adapters render on
   `hf-seek`) BEFORE the engine injects the decoded frames, so the GPU render
   read a frame that didn't exist yet. After injecting, the engine now calls
   `window.__hfReseekGpu(t)` — a force-dispatch (`forceDispatchSeekEvent`) that
   bypasses the same-time `hf-seek` dedup — so GPU compositions re-upload their
   textures from the freshly-injected, decoded frames, deterministically.

Tests: unit tests for the texImage2D/texSubImage2D substitution and the
force-dispatch, plus a videoFrameInjector regression test asserting the
post-injection GPU reseek fires only when frames were injected. Verified
end-to-end: a WebGL prism with 8 live <video> facets renders byte-identical
across independent runs with no facet flicker.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(render): add producer render-compat regression for WebGL video textures

A WebGL2 canvas samples a <video> as a texture every hf-seek (the natural
author pattern, distilled from the HeyGen prism). The render-compat harness
renders it and compares against the golden: with the video-texture fix the
render reproduces the decoded frames; revert the fix and the canvas renders
black, collapsing the comparison.

Golden verified to contain real, time-varying video content (not black), so a
regression is caught rather than passing vacuously.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 22:37:00 -07:00

46 lines
1.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { dispatchSeekEvent, forceDispatchSeekEvent, resetSeekDispatchState } from "./seek-dispatch";
describe("seek-dispatch", () => {
beforeEach(() => {
resetSeekDispatchState();
});
it("dispatchSeekEvent fires an hf-seek event with the time", () => {
const handler = vi.fn();
window.addEventListener("hf-seek", handler);
dispatchSeekEvent(2.5);
window.removeEventListener("hf-seek", handler);
expect(handler).toHaveBeenCalledTimes(1);
expect((handler.mock.calls[0][0] as CustomEvent).detail.time).toBe(2.5);
});
it("dispatchSeekEvent dedups consecutive same-time dispatches", () => {
const handler = vi.fn();
window.addEventListener("hf-seek", handler);
dispatchSeekEvent(4);
dispatchSeekEvent(4);
window.removeEventListener("hf-seek", handler);
expect(handler).toHaveBeenCalledTimes(1);
});
it("forceDispatchSeekEvent re-fires even at the same time (post-injection re-render)", () => {
const handler = vi.fn();
window.addEventListener("hf-seek", handler);
dispatchSeekEvent(6); // GPU adapters' first render at t=6
forceDispatchSeekEvent(6); // engine re-render after video injection, same t
window.removeEventListener("hf-seek", handler);
expect(handler).toHaveBeenCalledTimes(2);
expect((handler.mock.calls[1][0] as CustomEvent).detail.time).toBe(6);
});
it("after a force dispatch, the same time still dedups on the normal path", () => {
const handler = vi.fn();
window.addEventListener("hf-seek", handler);
forceDispatchSeekEvent(8);
dispatchSeekEvent(8); // deduped — force already recorded t=8
window.removeEventListener("hf-seek", handler);
expect(handler).toHaveBeenCalledTimes(1);
});
});