|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { captureFrameToBuffer, createCaptureSession } from "./frameCapture.js"; |
| 6 | +import { beginFrameCapture } from "./screenshotService.js"; |
| 7 | + |
| 8 | +const calls: string[] = []; |
| 9 | + |
| 10 | +vi.mock("./browserManager.js", async (importOriginal) => ({ |
| 11 | + ...(await importOriginal<typeof import("./browserManager.js")>()), |
| 12 | + resolveBrowserGpuMode: async () => "software", |
| 13 | + acquireBrowser: async () => ({ |
| 14 | + captureMode: "beginframe", |
| 15 | + browser: { |
| 16 | + version: async () => "HeadlessChrome/150.0.0.0", |
| 17 | + newPage: async () => ({ |
| 18 | + mainFrame: () => ({}), |
| 19 | + evaluateOnNewDocument: async () => {}, |
| 20 | + setViewport: async () => {}, |
| 21 | + evaluate: async () => undefined, |
| 22 | + createCDPSession: async () => ({ |
| 23 | + send: async ( |
| 24 | + method: string, |
| 25 | + params: { frameTimeTicks?: number; noDisplayUpdates?: boolean }, |
| 26 | + ) => { |
| 27 | + calls.push( |
| 28 | + `${method} ticks=${params.frameTimeTicks} noDisplayUpdates=${params.noDisplayUpdates}`, |
| 29 | + ); |
| 30 | + return {}; |
| 31 | + }, |
| 32 | + }), |
| 33 | + }), |
| 34 | + }, |
| 35 | + }), |
| 36 | +})); |
| 37 | +vi.mock("./screenshotService.js", async (importOriginal) => ({ |
| 38 | + ...(await importOriginal<typeof import("./screenshotService.js")>()), |
| 39 | + beginFrameCapture: vi.fn(async (_page: unknown, _options: unknown, frameTimeTicks: number) => { |
| 40 | + calls.push(`capture ticks=${frameTimeTicks}`); |
| 41 | + return { buffer: Buffer.alloc(8), hasDamage: true }; |
| 42 | + }), |
| 43 | +})); |
| 44 | + |
| 45 | +const dirs: string[] = []; |
| 46 | +afterEach(() => { |
| 47 | + calls.length = 0; |
| 48 | + vi.mocked(beginFrameCapture).mockClear(); |
| 49 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 50 | +}); |
| 51 | + |
| 52 | +async function beginFrameSession(baseTicks: number) { |
| 53 | + const dir = mkdtempSync(join(tmpdir(), "hf-first-beginframe-")); |
| 54 | + dirs.push(dir); |
| 55 | + const session = await createCaptureSession( |
| 56 | + "http://localhost:0", |
| 57 | + dir, |
| 58 | + { width: 1920, height: 1080, fps: { num: 30, den: 1 }, format: "jpeg" }, |
| 59 | + null, |
| 60 | + {}, |
| 61 | + ); |
| 62 | + session.isInitialized = true; |
| 63 | + session.captureMode = "beginframe"; |
| 64 | + session.beginFrameTimeTicks = baseTicks; |
| 65 | + return session; |
| 66 | +} |
| 67 | + |
| 68 | +describe("first BeginFrame capture of a session", () => { |
| 69 | + // Measured on Linux headless-shell: without this, a chunk's first frame came back as the |
| 70 | + // frame composited during init (the composition at t=0) in about half the runs. |
| 71 | + it("displays the seek once before the first capture, between the probe tick and the capture tick", async () => { |
| 72 | + const session = await beginFrameSession(2000); |
| 73 | + const interval = session.beginFrameIntervalMs; |
| 74 | + |
| 75 | + await captureFrameToBuffer(session, 0, 70 / 30); |
| 76 | + await captureFrameToBuffer(session, 1, 71 / 30); |
| 77 | + |
| 78 | + expect(calls).toEqual([ |
| 79 | + `HeadlessExperimental.beginFrame ticks=${2000 - interval / 2} noDisplayUpdates=false`, |
| 80 | + "capture ticks=2000", |
| 81 | + `capture ticks=${2000 + interval}`, |
| 82 | + ]); |
| 83 | + // The producer's liveness probe runs at base - 5 intervals; ticks must stay monotonic. |
| 84 | + expect(2000 - interval / 2).toBeGreaterThan(2000 - 5 * interval); |
| 85 | + }); |
| 86 | + |
| 87 | + it("does it again on a fresh session (worker or retry page)", async () => { |
| 88 | + await captureFrameToBuffer(await beginFrameSession(2000), 0, 0); |
| 89 | + await captureFrameToBuffer(await beginFrameSession(2000), 0, 0); |
| 90 | + expect(calls.filter((c) => c.startsWith("HeadlessExperimental.beginFrame"))).toHaveLength(2); |
| 91 | + }); |
| 92 | +}); |
0 commit comments