|
| 1 | +import { afterEach, beforeEach, expect, it, vi } from "vitest"; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { resolveConfig } from "@hyperframes/engine"; |
| 6 | +import { CaptureStageError } from "./render/captureStageError.js"; |
| 7 | +import { createRenderJob, executeRenderJob } from "./renderOrchestrator.js"; |
| 8 | + |
| 9 | +// Entry: executeRenderJob on the streaming route, with the streaming stage scripted per attempt. |
| 10 | +// An attempt with no scripted failure throws RETRY_REACHED, so a test ends there without running assembly. |
| 11 | +const { state, RETRY_REACHED } = vi.hoisted(() => ({ |
| 12 | + state: { |
| 13 | + attempts: [] as boolean[], |
| 14 | + failures: [] as unknown[], |
| 15 | + compileForcesScreenshot: false, |
| 16 | + }, |
| 17 | + RETRY_REACHED: "streaming retry reached", |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("@hyperframes/engine", async (importOriginal) => ({ |
| 21 | + ...(await importOriginal<typeof import("@hyperframes/engine")>()), |
| 22 | + assertConfiguredFfmpegBinariesExist: () => {}, |
| 23 | + resolveBrowserGpuMode: async () => "software", |
| 24 | + resolveHeadlessShellPath: () => "/opt/chrome-headless-shell", |
| 25 | + createCaptureSession: async () => ({ browserConsoleBuffer: [], options: {} }), |
| 26 | + initializeSession: async () => {}, |
| 27 | + closeCaptureSession: async () => {}, |
| 28 | +})); |
| 29 | +vi.mock("./fileServer.js", async (importOriginal) => ({ |
| 30 | + ...(await importOriginal<typeof import("./fileServer.js")>()), |
| 31 | + createFileServer: async () => ({ |
| 32 | + url: "http://127.0.0.1:1", |
| 33 | + port: 1, |
| 34 | + close: () => {}, |
| 35 | + addPreHeadScript: () => {}, |
| 36 | + }), |
| 37 | +})); |
| 38 | +vi.mock("./render/stages/compileStage.js", () => ({ |
| 39 | + runCompileStage: async () => ({ |
| 40 | + compiled: { |
| 41 | + html: "<div></div>", |
| 42 | + renderModeHints: { reasons: [] }, |
| 43 | + hasShaderTransitions: false, |
| 44 | + unresolvedCompositions: [], |
| 45 | + }, |
| 46 | + composition: { width: 320, height: 180, duration: 4, videos: [], images: [], audios: [] }, |
| 47 | + deviceScaleFactor: 1, |
| 48 | + outputWidth: 320, |
| 49 | + outputHeight: 180, |
| 50 | + compileOnlyMs: 0, |
| 51 | + forceScreenshot: state.compileForcesScreenshot, |
| 52 | + }), |
| 53 | +})); |
| 54 | +vi.mock("./render/stages/probeStage.js", () => ({ |
| 55 | + runProbeStage: async ({ compiled }: { compiled: object }) => ({ |
| 56 | + compiled, |
| 57 | + fileServer: null, |
| 58 | + probeSession: null, |
| 59 | + lastBrowserConsole: [], |
| 60 | + duration: 4, |
| 61 | + totalFrames: 120, |
| 62 | + browserProbeMs: 0, |
| 63 | + }), |
| 64 | +})); |
| 65 | +vi.mock("./render/stages/extractVideosStage.js", async (importOriginal) => ({ |
| 66 | + ...(await importOriginal<typeof import("./render/stages/extractVideosStage.js")>()), |
| 67 | + runExtractVideosStage: async () => ({ |
| 68 | + extractionResult: null, |
| 69 | + frameLookup: null, |
| 70 | + videoReadinessSkipIds: new Set<string>(), |
| 71 | + videoMetadataHints: new Map(), |
| 72 | + nativeHdrVideoIds: new Set<string>(), |
| 73 | + videoTransfers: new Map(), |
| 74 | + nativeHdrImageIds: new Set<string>(), |
| 75 | + imageTransfers: new Map(), |
| 76 | + hdrImageSrcPaths: new Map(), |
| 77 | + imageColorSpaces: [], |
| 78 | + videoExtractMs: 0, |
| 79 | + failureToEnforce: undefined, |
| 80 | + }), |
| 81 | +})); |
| 82 | +vi.mock("./render/stages/audioStage.js", () => ({ |
| 83 | + runAudioStage: async () => ({ audioOutputPath: undefined, hasAudio: false, audioProcessMs: 0 }), |
| 84 | +})); |
| 85 | +vi.mock("./render/captureCost.js", async (importOriginal) => ({ |
| 86 | + ...(await importOriginal<typeof import("./render/captureCost.js")>()), |
| 87 | + resolveRenderWorkerCount: () => 1, |
| 88 | + runCaptureCalibration: async (input: { forceScreenshot: boolean; probeSession: unknown }) => ({ |
| 89 | + calibration: undefined, |
| 90 | + forceScreenshot: input.forceScreenshot, |
| 91 | + probeSession: input.probeSession, |
| 92 | + lastBrowserConsole: [], |
| 93 | + }), |
| 94 | +})); |
| 95 | +vi.mock("./render/stages/captureStreamingStage.js", async (importOriginal) => ({ |
| 96 | + ...(await importOriginal<typeof import("./render/stages/captureStreamingStage.js")>()), |
| 97 | + runCaptureStreamingStage: async (input: { plan: { forceScreenshot: boolean } }) => { |
| 98 | + state.attempts.push(input.plan.forceScreenshot); |
| 99 | + const failure = state.failures.shift(); |
| 100 | + if (failure === undefined) throw new Error(RETRY_REACHED); |
| 101 | + throw new CaptureStageError({ cause: failure, browserConsole: [] }); |
| 102 | + }, |
| 103 | +})); |
| 104 | + |
| 105 | +const platform = Object.getOwnPropertyDescriptor(process, "platform"); |
| 106 | +const dirs: string[] = []; |
| 107 | +beforeEach(() => { |
| 108 | + state.attempts = []; |
| 109 | + state.failures = []; |
| 110 | + state.compileForcesScreenshot = false; |
| 111 | + Object.defineProperty(process, "platform", { value: "linux" }); |
| 112 | + vi.stubEnv("HF_SEGMENTED_CAPTURE", "false"); |
| 113 | +}); |
| 114 | +afterEach(() => { |
| 115 | + if (platform) Object.defineProperty(process, "platform", platform); |
| 116 | + vi.unstubAllEnvs(); |
| 117 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 118 | +}); |
| 119 | + |
| 120 | +function render(): Promise<void> { |
| 121 | + const root = mkdtempSync(join(tmpdir(), "hf-beginframe-retry-")); |
| 122 | + dirs.push(root); |
| 123 | + writeFileSync(join(root, "index.html"), "<div></div>"); |
| 124 | + const job = createRenderJob({ |
| 125 | + fps: 30, |
| 126 | + quality: "standard", |
| 127 | + hdrMode: "force-sdr", |
| 128 | + producerConfig: resolveConfig({ forceScreenshot: false, browserGpuMode: "software" }), |
| 129 | + logger: { info() {}, warn() {}, error() {}, debug() {} }, |
| 130 | + }); |
| 131 | + return executeRenderJob(job, root, join(root, "output.mp4")); |
| 132 | +} |
| 133 | + |
| 134 | +const beginFrameOnlyFailure = () => |
| 135 | + new Error("Runtime.callFunctionOn timed out while drawing frame 37"); |
| 136 | + |
| 137 | +it("re-renders a BeginFrame attempt that failed for any capture reason on a screenshot session", async () => { |
| 138 | + state.failures = [beginFrameOnlyFailure()]; |
| 139 | + await expect(render()).rejects.toThrow(RETRY_REACHED); |
| 140 | + expect(state.attempts).toEqual([false, true]); |
| 141 | +}); |
| 142 | + |
| 143 | +it("does not retry when the failed attempt already captured in screenshot mode", async () => { |
| 144 | + state.compileForcesScreenshot = true; |
| 145 | + state.failures = [beginFrameOnlyFailure()]; |
| 146 | + await expect(render()).rejects.toThrow("Runtime.callFunctionOn timed out"); |
| 147 | + expect(state.attempts).toEqual([true]); |
| 148 | +}); |
| 149 | + |
| 150 | +it("does not retry a second failure after the screenshot re-render", async () => { |
| 151 | + state.failures = [beginFrameOnlyFailure(), new Error("Runtime.callFunctionOn timed out again")]; |
| 152 | + await expect(render()).rejects.toThrow("timed out again"); |
| 153 | + expect(state.attempts).toEqual([false, true]); |
| 154 | +}); |
0 commit comments