|
| 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 { extractSafeRenderErrorMetadata } from "../server.js"; |
| 8 | +import { createRenderJob, executeRenderJob } from "./renderOrchestrator.js"; |
| 9 | + |
| 10 | +// Entry: executeRenderJob on the streaming route; calibration and the streaming stage are scripted. |
| 11 | +const { state } = vi.hoisted(() => ({ |
| 12 | + state: { calibrationEvaluateTimedOut: false, framesBeforeFailure: 0 }, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("@hyperframes/engine", async (importOriginal) => ({ |
| 16 | + ...(await importOriginal<typeof import("@hyperframes/engine")>()), |
| 17 | + assertConfiguredFfmpegBinariesExist: () => {}, |
| 18 | + resolveBrowserGpuMode: async () => "software", |
| 19 | + resolveHeadlessShellPath: () => "/opt/chrome-headless-shell", |
| 20 | + createCaptureSession: async () => ({ browserConsoleBuffer: [], options: {} }), |
| 21 | + initializeSession: async () => {}, |
| 22 | + closeCaptureSession: async () => {}, |
| 23 | +})); |
| 24 | +vi.mock("./fileServer.js", async (importOriginal) => ({ |
| 25 | + ...(await importOriginal<typeof import("./fileServer.js")>()), |
| 26 | + createFileServer: async () => ({ |
| 27 | + url: "http://127.0.0.1:1", |
| 28 | + port: 1, |
| 29 | + close: () => {}, |
| 30 | + addPreHeadScript: () => {}, |
| 31 | + }), |
| 32 | +})); |
| 33 | +vi.mock("./render/stages/compileStage.js", () => ({ |
| 34 | + runCompileStage: async () => ({ |
| 35 | + compiled: { |
| 36 | + html: "<div></div>", |
| 37 | + renderModeHints: { reasons: [] }, |
| 38 | + hasShaderTransitions: false, |
| 39 | + unresolvedCompositions: [], |
| 40 | + }, |
| 41 | + composition: { width: 320, height: 180, duration: 4, videos: [], images: [], audios: [] }, |
| 42 | + deviceScaleFactor: 1, |
| 43 | + outputWidth: 320, |
| 44 | + outputHeight: 180, |
| 45 | + compileOnlyMs: 0, |
| 46 | + forceScreenshot: false, |
| 47 | + }), |
| 48 | +})); |
| 49 | +vi.mock("./render/stages/probeStage.js", () => ({ |
| 50 | + runProbeStage: async ({ compiled }: { compiled: object }) => ({ |
| 51 | + compiled, |
| 52 | + fileServer: null, |
| 53 | + probeSession: null, |
| 54 | + lastBrowserConsole: [], |
| 55 | + duration: 4, |
| 56 | + totalFrames: 120, |
| 57 | + browserProbeMs: 0, |
| 58 | + }), |
| 59 | +})); |
| 60 | +vi.mock("./render/stages/extractVideosStage.js", async (importOriginal) => ({ |
| 61 | + ...(await importOriginal<typeof import("./render/stages/extractVideosStage.js")>()), |
| 62 | + runExtractVideosStage: async () => ({ |
| 63 | + extractionResult: null, |
| 64 | + frameLookup: null, |
| 65 | + videoReadinessSkipIds: new Set<string>(), |
| 66 | + videoMetadataHints: new Map(), |
| 67 | + nativeHdrVideoIds: new Set<string>(), |
| 68 | + videoTransfers: new Map(), |
| 69 | + nativeHdrImageIds: new Set<string>(), |
| 70 | + imageTransfers: new Map(), |
| 71 | + hdrImageSrcPaths: new Map(), |
| 72 | + imageColorSpaces: [], |
| 73 | + videoExtractMs: 0, |
| 74 | + failureToEnforce: undefined, |
| 75 | + }), |
| 76 | +})); |
| 77 | +vi.mock("./render/stages/audioStage.js", () => ({ |
| 78 | + runAudioStage: async () => ({ audioOutputPath: undefined, hasAudio: false, audioProcessMs: 0 }), |
| 79 | +})); |
| 80 | +vi.mock("./render/captureCost.js", async (importOriginal) => ({ |
| 81 | + ...(await importOriginal<typeof import("./render/captureCost.js")>()), |
| 82 | + resolveRenderWorkerCount: () => 1, |
| 83 | + runCaptureCalibration: async (input: { forceScreenshot: boolean; probeSession: unknown }) => ({ |
| 84 | + calibration: undefined, |
| 85 | + forceScreenshot: input.forceScreenshot, |
| 86 | + probeSession: input.probeSession, |
| 87 | + lastBrowserConsole: [], |
| 88 | + calibrationEvaluateTimedOut: state.calibrationEvaluateTimedOut, |
| 89 | + }), |
| 90 | +})); |
| 91 | +vi.mock("./render/stages/captureStreamingStage.js", async (importOriginal) => ({ |
| 92 | + ...(await importOriginal<typeof import("./render/stages/captureStreamingStage.js")>()), |
| 93 | + runCaptureStreamingStage: async (input: { job: { framesRendered?: number } }) => { |
| 94 | + input.job.framesRendered = state.framesBeforeFailure; |
| 95 | + throw new CaptureStageError({ cause: new Error(EVALUATE_TIMEOUT), browserConsole: [] }); |
| 96 | + }, |
| 97 | +})); |
| 98 | + |
| 99 | +const EVALUATE_TIMEOUT = "Runtime.evaluate timed out. Increase the 'protocolTimeout' setting"; |
| 100 | + |
| 101 | +const dirs: string[] = []; |
| 102 | +beforeEach(() => { |
| 103 | + state.calibrationEvaluateTimedOut = false; |
| 104 | + state.framesBeforeFailure = 0; |
| 105 | + vi.stubEnv("HF_SEGMENTED_CAPTURE", "false"); |
| 106 | +}); |
| 107 | +afterEach(() => { |
| 108 | + vi.unstubAllEnvs(); |
| 109 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 110 | +}); |
| 111 | + |
| 112 | +function render(): Promise<void> { |
| 113 | + const root = mkdtempSync(join(tmpdir(), "hf-page-never-ready-")); |
| 114 | + dirs.push(root); |
| 115 | + writeFileSync(join(root, "index.html"), "<div></div>"); |
| 116 | + const job = createRenderJob({ |
| 117 | + fps: 30, |
| 118 | + quality: "standard", |
| 119 | + hdrMode: "force-sdr", |
| 120 | + producerConfig: resolveConfig({ forceScreenshot: true, browserGpuMode: "software" }), |
| 121 | + logger: { info() {}, warn() {}, error() {}, debug() {} }, |
| 122 | + }); |
| 123 | + return executeRenderJob(job, root, join(root, "output.mp4")); |
| 124 | +} |
| 125 | + |
| 126 | +it("fails once as non-retryable when calibration and capture both timed out with no frame", async () => { |
| 127 | + state.calibrationEvaluateTimedOut = true; |
| 128 | + const error = await render().then( |
| 129 | + () => undefined, |
| 130 | + (caught: unknown) => caught, |
| 131 | + ); |
| 132 | + expect(error).toMatchObject({ code: "PAGE_NEVER_READY", owner: "user", retryable: false }); |
| 133 | + expect(extractSafeRenderErrorMetadata(error)).toMatchObject({ |
| 134 | + errorCode: "PAGE_NEVER_READY", |
| 135 | + retryable: false, |
| 136 | + }); |
| 137 | + expect((error as Error).message).toContain("never became ready"); |
| 138 | +}); |
| 139 | + |
| 140 | +it("keeps the original error when calibration did not time out", async () => { |
| 141 | + await expect(render()).rejects.toThrow(EVALUATE_TIMEOUT); |
| 142 | +}); |
| 143 | + |
| 144 | +it("keeps the original error once a frame was captured", async () => { |
| 145 | + state.calibrationEvaluateTimedOut = true; |
| 146 | + state.framesBeforeFailure = 3; |
| 147 | + await expect(render()).rejects.toThrow(EVALUATE_TIMEOUT); |
| 148 | +}); |
0 commit comments