Files
hyperframes/packages/core/src/runtime/webglContextRecovery.test.ts
T
Miguel Ángel e8ffaa410f fix(core): preview recovers its WebGL scenes and effects after a lost context (#7028)
* fix(core): preview repaints and rebuilds effects after the GPU restores a lost WebGL context

* fix(core): a paused scene redraws and a playing one repaints effects after a context restore

* test(core): a paused scene is redrawn after a WebGL context restore

* test(core): give the restore redraw test a clip

* fix(core): shorten the context recovery header comment

* fix(core): rebuild effects only after every lost context has restored

* test(core): the restore rebuild waits while another context is still lost

* test(core): count context requests to show the rebuild waits

* test(core): the mock reports the lost flag the test sets
2026-11-02 17:47:44 +00:00

48 lines
1.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { watchSceneWebglContexts } from "./webglContextRecovery";
function canvas() {
const el = document.createElement("canvas");
document.body.appendChild(el);
return el;
}
describe("watchSceneWebglContexts", () => {
afterEach(() => {
vi.restoreAllMocks();
document.body.innerHTML = "";
});
it("lets a scene canvas restore, then repaints once after its own restore handler", () => {
const frames: FrameRequestCallback[] = [];
vi.spyOn(window, "requestAnimationFrame").mockImplementation((cb) => frames.push(cb));
const order: string[] = [];
const repaint = vi.fn(() => order.push("repaint"));
const stop = watchSceneWebglContexts(repaint);
const scene = canvas();
scene.addEventListener("webglcontextrestored", () => order.push("scene"));
const lost = new Event("webglcontextlost", { cancelable: true });
scene.dispatchEvent(lost);
expect(lost.defaultPrevented).toBe(true);
scene.dispatchEvent(new Event("webglcontextrestored"));
canvas().dispatchEvent(new Event("webglcontextrestored"));
expect(repaint).not.toHaveBeenCalled();
frames.splice(0).forEach((cb) => cb(0));
expect(order).toEqual(["scene", "repaint"]);
stop();
});
it("does nothing after it is stopped", () => {
const raf = vi.spyOn(window, "requestAnimationFrame");
const repaint = vi.fn();
watchSceneWebglContexts(repaint)();
const lost = new Event("webglcontextlost", { cancelable: true });
canvas().dispatchEvent(lost);
canvas().dispatchEvent(new Event("webglcontextrestored"));
expect(lost.defaultPrevented).toBe(false);
expect(raf).not.toHaveBeenCalled();
});
});