|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import puppeteer from "puppeteer-core"; |
| 3 | +import { resolveConfig } from "../../../../engine/src/config.js"; |
| 4 | +import { resolveHeadlessShellPath } from "../../../../engine/src/services/browserManager.js"; |
| 5 | +import { referenceBackgroundResult } from "../../../../engine/src/services/referenceBackground.js"; |
| 6 | + |
| 7 | +const state = vi.hoisted(() => ({ create: vi.fn() })); |
| 8 | +vi.mock("@hyperframes/engine", () => ({ |
| 9 | + createCaptureSession: state.create, |
| 10 | + closeCaptureSession: async () => {}, |
| 11 | + readReferenceBackground: async () => ({ kind: "solid", rgb: [10, 46, 138] }), |
| 12 | + referenceBackgroundResult, |
| 13 | +})); |
| 14 | +vi.mock("../fileServer.js", () => ({ |
| 15 | + createFileServer: async () => ({ url: "http://inspection.test" }), |
| 16 | + closeFileServerSafely: () => {}, |
| 17 | + VIRTUAL_TIME_SHIM: "", |
| 18 | +})); |
| 19 | +import { readPlanReferenceBackground } from "./planReferenceBackground.js"; |
| 20 | + |
| 21 | +const chromePath = resolveHeadlessShellPath(); |
| 22 | + |
| 23 | +describe.skipIf(!chromePath)("reference readiness with parked animation frames", () => { |
| 24 | + it("observes readiness after the first check without waiting for a render seek", async () => { |
| 25 | + const browser = await puppeteer.launch({ |
| 26 | + executablePath: chromePath, |
| 27 | + headless: true, |
| 28 | + args: ["--no-sandbox", "--disable-dev-shm-usage"], |
| 29 | + }); |
| 30 | + try { |
| 31 | + const page = await browser.newPage(); |
| 32 | + vi.spyOn(page, "goto").mockImplementation(async () => { |
| 33 | + await page.setContent(`<script> |
| 34 | + window.rafCalls = 0; |
| 35 | + window.requestAnimationFrame = () => { window.rafCalls++; return 1; }; |
| 36 | + const film = { duration: 0, seek: () => {} }; |
| 37 | + let first = true; |
| 38 | + Object.defineProperty(window, '__hf', { get() { |
| 39 | + if (first) { first = false; setTimeout(() => { film.duration = 1; }, 50); } |
| 40 | + return film; |
| 41 | + }}); |
| 42 | + </script>`); |
| 43 | + return null; |
| 44 | + }); |
| 45 | + state.create.mockResolvedValue({ page }); |
| 46 | + const result = await readPlanReferenceBackground({ |
| 47 | + projectDir: "/project", |
| 48 | + workDir: "/work", |
| 49 | + cfg: resolveConfig({ playerReadyTimeout: 750 }), |
| 50 | + options: { width: 320, height: 240, fps: { num: 30, den: 1 } }, |
| 51 | + log: { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} }, |
| 52 | + }); |
| 53 | + expect(result).toEqual({ reference_background: { kind: "solid", rgb: [10, 46, 138] } }); |
| 54 | + expect(await page.evaluate("window.rafCalls")).toBe(0); |
| 55 | + } finally { |
| 56 | + await browser.close(); |
| 57 | + } |
| 58 | + }, 10_000); |
| 59 | +}); |
0 commit comments