Files
heygen-com--hyperframes/packages/engine/src/services/frameCapture-discardWarmup.test.ts
T
wehub-resource-sync 85453da49f
regression / regression-shards (style-16-prod style-9-prod style-17-prod iframe-render-compat variables-prod mp4-h265-sdr, shard-4) (push) Has been cancelled
regression / regression-shards (style-4-prod style-11-prod style-2-prod animejs-adapter typegpu-adapter parallel-capture-regression, shard-5) (push) Has been cancelled
regression / regression-shards (style-7-prod style-8-prod style-10-prod css-spinner-render-compat webm-transparency mp4-h264-sdr webm-vp9, shard-3) (push) Has been cancelled
regression / regression-shards (sub-composition-video style-18-prod raf-ball-render-compat font-variant-numeric sub-comp-t0 sub-comp-id-selector, shard-7) (push) Has been cancelled
Windows render verification / Detect changes (push) Has been cancelled
Windows render verification / Preflight (lint + format) (push) Has been cancelled
Windows render verification / Render on windows-latest (push) Has been cancelled
Windows render verification / Tests on windows-latest (push) Has been cancelled
CI / Detect changes (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Fallow audit (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Typecheck (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Producer: integration tests (push) Has been cancelled
CI / Producer: unit tests (push) Has been cancelled
CI / File size check (push) Has been cancelled
CI / Test: skills (push) Has been cancelled
CI / Skills: manifest in sync (push) Has been cancelled
CI / CLI: npx shim (macos-latest) (push) Has been cancelled
CI / CLI: npx shim (ubuntu-latest) (push) Has been cancelled
CI / CLI: npx shim (windows-latest) (push) Has been cancelled
CI / SDK: unit + contract + smoke (push) Has been cancelled
CI / Test: runtime contract (push) Has been cancelled
CI / Studio: load smoke (push) Has been cancelled
CI / Smoke: global install (push) Has been cancelled
CI / CLI smoke (required) (push) Has been cancelled
CI / Semantic PR title (push) Has been cancelled
Player perf / Detect changes (push) Has been cancelled
Player perf / Preflight (lint + format) (push) Has been cancelled
Player perf / player-perf (push) Has been cancelled
Player perf / Perf: drift (push) Has been cancelled
Player perf / Perf: fps (push) Has been cancelled
Player perf / Perf: parity (push) Has been cancelled
Player perf / Perf: scrub (push) Has been cancelled
Player perf / Perf: load (push) Has been cancelled
preview-regression / Detect changes (push) Has been cancelled
preview-regression / Preflight (lint + format) (push) Has been cancelled
preview-regression / Preview parity (push) Has been cancelled
preview-regression / preview-regression (push) Has been cancelled
regression / regression (push) Has been cancelled
regression / Detect changes (push) Has been cancelled
regression / Preflight (lint + format) (push) Has been cancelled
regression / regression-shards (hdr-regression style-5-prod style-3-prod mov-prores, shard-1) (push) Has been cancelled
regression / regression-shards (overlay-montage-prod style-12-prod chat missing-host-comp-id png-sequence portrait-edge-bleed, shard-6) (push) Has been cancelled
regression / regression-shards (style-13-prod style-6-prod vignelli-stacking gsap-letters-render-compat audio-mux-parity, shard-8) (push) Has been cancelled
regression / regression-shards (style-15-prod hdr-hlg-regression style-1-prod many-cuts vfr-screen-recording render-symlinked-assets, shard-2) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Docs / Validate docs (push) Has been cancelled
Sync skills to ClawHub / Publish changed skills (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:58:35 +08:00

184 lines
6.7 KiB
TypeScript

/**
* Tests for `discardWarmupCapture` — the helper distributed chunk workers
* run before their first real capture to prime `lastFrameCache`.
*
* The helper is a thin wrapper around the inner `captureFrameCore`
* machinery, so its testable contract is post-conditional rather than
* pixel-level:
*
* 1. The wrapper invokes the inner capture exactly once with the supplied
* `(frameIndex, time)`.
* 2. After the wrapper returns, the session's perf and BeginFrame damage
* counters look exactly as they did before — even though the inner
* capture mutated them.
* 3. The wrapper writes no file to disk (no path is plumbed through;
* asserted indirectly by observing that `outputDir` is never read).
* 4. State is restored even if the inner capture throws.
*
* The inner capture is stubbed (the helper accepts an injectable
* `innerCapture` for exactly this reason). We don't need a real Chrome.
*/
import { describe, expect, it } from "vitest";
import { existsSync, readdirSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { discardWarmupCapture, type CaptureSession } from "./frameCapture.js";
function makeFakeSession(): CaptureSession {
// The discardWarmupCapture wrapper only reads `capturePerf`,
// `beginFrameHasDamageCount`, `beginFrameNoDamageCount`. Everything else
// is unused — leave it as bare-minimum stubs cast through `unknown`.
return {
browser: {} as unknown,
page: {} as unknown,
options: { width: 1, height: 1, fps: { num: 30, den: 1 } },
serverUrl: "",
outputDir: mkdtempSync(join(tmpdir(), "discard-warmup-")),
onBeforeCapture: null,
isInitialized: true,
browserConsoleBuffer: [],
capturePerf: {
frames: 7,
seekMs: 100,
beforeCaptureMs: 50,
screenshotMs: 200,
totalMs: 350,
},
captureMode: "beginframe",
beginFrameTimeTicks: 0,
beginFrameIntervalMs: 33,
beginFrameHasDamageCount: 4,
beginFrameNoDamageCount: 3,
} as unknown as CaptureSession;
}
describe("discardWarmupCapture", () => {
it("calls the inner capture exactly once with (frameIndex=0, time=0) by default", async () => {
const session = makeFakeSession();
try {
let calls = 0;
let receivedFrameIndex = -1;
let receivedTime = -1;
await discardWarmupCapture(session, undefined, undefined, async (_s, fi, t) => {
calls++;
receivedFrameIndex = fi;
receivedTime = t;
return { buffer: Buffer.alloc(0), quantizedTime: t, captureTimeMs: 0 };
});
expect(calls).toBe(1);
expect(receivedFrameIndex).toBe(0);
expect(receivedTime).toBe(0);
} finally {
rmSync(session.outputDir, { recursive: true, force: true });
}
});
it("passes through caller-supplied (frameIndex, time)", async () => {
const session = makeFakeSession();
try {
let received: { fi: number; t: number } | null = null;
await discardWarmupCapture(session, 240, 8, async (_s, fi, t) => {
received = { fi, t };
return { buffer: Buffer.alloc(0), quantizedTime: t, captureTimeMs: 0 };
});
expect(received).toEqual({ fi: 240, t: 8 });
} finally {
rmSync(session.outputDir, { recursive: true, force: true });
}
});
it("restores perf counters after the inner capture mutates them", async () => {
const session = makeFakeSession();
const before = { ...session.capturePerf };
try {
await discardWarmupCapture(session, 0, 0, async (s) => {
s.capturePerf.frames += 1;
s.capturePerf.seekMs += 12;
s.capturePerf.beforeCaptureMs += 5;
s.capturePerf.screenshotMs += 33;
s.capturePerf.totalMs += 50;
return { buffer: Buffer.alloc(0), quantizedTime: 0, captureTimeMs: 50 };
});
expect(session.capturePerf).toEqual(before);
} finally {
rmSync(session.outputDir, { recursive: true, force: true });
}
});
it("restores BeginFrame damage counters after the inner capture mutates them", async () => {
const session = makeFakeSession();
const hasBefore = session.beginFrameHasDamageCount;
const noBefore = session.beginFrameNoDamageCount;
try {
await discardWarmupCapture(session, 0, 0, async (s) => {
s.beginFrameHasDamageCount += 10;
s.beginFrameNoDamageCount += 1;
return { buffer: Buffer.alloc(0), quantizedTime: 0, captureTimeMs: 0 };
});
expect(session.beginFrameHasDamageCount).toBe(hasBefore);
expect(session.beginFrameNoDamageCount).toBe(noBefore);
} finally {
rmSync(session.outputDir, { recursive: true, force: true });
}
});
it("restores state even when the inner capture throws", async () => {
const session = makeFakeSession();
const perfBefore = { ...session.capturePerf };
const hasBefore = session.beginFrameHasDamageCount;
const noBefore = session.beginFrameNoDamageCount;
try {
let thrown: unknown;
try {
await discardWarmupCapture(session, 0, 0, async (s) => {
s.capturePerf.frames += 5;
s.beginFrameNoDamageCount += 2;
throw new Error("simulated capture failure");
});
} catch (err) {
thrown = err;
}
expect((thrown as Error).message).toBe("simulated capture failure");
// The whole point of `finally { restore }`: failure must not leak
// inflated counters into the real capture summary.
expect(session.capturePerf).toEqual(perfBefore);
expect(session.beginFrameHasDamageCount).toBe(hasBefore);
expect(session.beginFrameNoDamageCount).toBe(noBefore);
} finally {
rmSync(session.outputDir, { recursive: true, force: true });
}
});
it("writes no output file to the session's outputDir", async () => {
const session = makeFakeSession();
try {
expect(existsSync(session.outputDir)).toBe(true);
const before = readdirSync(session.outputDir);
await discardWarmupCapture(session, 0, 0, async () => ({
buffer: Buffer.from([0xff, 0xff, 0xff]),
quantizedTime: 0,
captureTimeMs: 0,
}));
const after = readdirSync(session.outputDir);
expect(after).toEqual(before);
} finally {
rmSync(session.outputDir, { recursive: true, force: true });
}
});
it("returns undefined (no result type, so the buffer can't escape)", async () => {
const session = makeFakeSession();
try {
const result = await discardWarmupCapture(session, 0, 0, async () => ({
buffer: Buffer.from([0x01]),
quantizedTime: 0,
captureTimeMs: 1,
}));
expect(result).toBeUndefined();
} finally {
rmSync(session.outputDir, { recursive: true, force: true });
}
});
});