|
| 1 | +import { afterEach, beforeEach, 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 { |
| 6 | + captureFrameToBuffer, |
| 7 | + captureFrameToBufferPipelined, |
| 8 | + captureFramesBatchPipelined, |
| 9 | + createCaptureSession, |
| 10 | +} from "./frameCapture.js"; |
| 11 | +import { |
| 12 | + captureDrawElementFrame, |
| 13 | + produceDrawElementFrame, |
| 14 | + produceDrawElementFrameBatch, |
| 15 | + DE_CANVAS_NOT_INITIALIZED_CODE, |
| 16 | +} from "./drawElementService.js"; |
| 17 | +import { pageScreenshotCapture } from "./screenshotService.js"; |
| 18 | +import { DrawElementCaptureError, isDrawElementCaptureError } from "./drawElementCaptureError.js"; |
| 19 | + |
| 20 | +vi.mock("./browserManager.js", async (importOriginal) => ({ |
| 21 | + ...(await importOriginal<typeof import("./browserManager.js")>()), |
| 22 | + resolveBrowserGpuMode: async () => "hardware", |
| 23 | + acquireBrowser: async () => ({ |
| 24 | + captureMode: "screenshot", |
| 25 | + browser: { |
| 26 | + version: async () => "Chrome/150.0.0.0", |
| 27 | + newPage: async () => ({ |
| 28 | + evaluateOnNewDocument: async () => {}, |
| 29 | + setViewport: async () => {}, |
| 30 | + evaluate: async () => undefined, |
| 31 | + screenshot: async () => { |
| 32 | + throw new Error("diagnostic screenshot unavailable"); |
| 33 | + }, |
| 34 | + }), |
| 35 | + }, |
| 36 | + }), |
| 37 | +})); |
| 38 | +vi.mock("./drawElementService.js", async (importOriginal) => ({ |
| 39 | + ...(await importOriginal<typeof import("./drawElementService.js")>()), |
| 40 | + captureDrawElementFrame: vi.fn(), |
| 41 | + produceDrawElementFrame: vi.fn(), |
| 42 | + produceDrawElementFrameBatch: vi.fn(), |
| 43 | +})); |
| 44 | +vi.mock("./screenshotService.js", async (importOriginal) => ({ |
| 45 | + ...(await importOriginal<typeof import("./screenshotService.js")>()), |
| 46 | + pageScreenshotCapture: vi.fn(), |
| 47 | +})); |
| 48 | + |
| 49 | +const dirs: string[] = []; |
| 50 | +async function session() { |
| 51 | + const dir = mkdtempSync(join(tmpdir(), "hf-fresh-fallback-")); |
| 52 | + dirs.push(dir); |
| 53 | + const result = await createCaptureSession( |
| 54 | + "http://localhost:0", |
| 55 | + dir, |
| 56 | + { |
| 57 | + width: 1920, |
| 58 | + height: 1080, |
| 59 | + fps: { num: 30, den: 1 }, |
| 60 | + format: "jpeg", |
| 61 | + }, |
| 62 | + null, |
| 63 | + { forceScreenshot: true }, |
| 64 | + ); |
| 65 | + result.isInitialized = true; |
| 66 | + result.captureMode = "drawelement"; |
| 67 | + return result; |
| 68 | +} |
| 69 | + |
| 70 | +beforeEach(() => { |
| 71 | + vi.clearAllMocks(); |
| 72 | + vi.stubEnv("HF_FORCE_DRAWELEMENT", "0"); |
| 73 | + vi.stubEnv("HF_FAST_CAPTURE_BOUNDARY_SS", "false"); |
| 74 | +}); |
| 75 | +afterEach(() => { |
| 76 | + vi.unstubAllEnvs(); |
| 77 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("fresh screenshot fallback", () => { |
| 81 | + for (const [name, capture, produce] of [ |
| 82 | + ["serial", captureFrameToBuffer, captureDrawElementFrame], |
| 83 | + ["pipelined", captureFrameToBufferPipelined, produceDrawElementFrame], |
| 84 | + ] as const) { |
| 85 | + it.each(["No cached paint record for element", DE_CANVAS_NOT_INITIALIZED_CODE])( |
| 86 | + `${name} rejects untrusted capture instead of returning the injected canvas`, |
| 87 | + async (message) => { |
| 88 | + vi.mocked(produce).mockRejectedValueOnce(new Error(message)); |
| 89 | + const s = await session(); |
| 90 | + await expect(capture(s, 7, 7 / 30)).rejects.toBeInstanceOf(DrawElementCaptureError); |
| 91 | + expect(pageScreenshotCapture).not.toHaveBeenCalled(); |
| 92 | + expect(s.deNcprFallbacks).toBe(1); |
| 93 | + }, |
| 94 | + ); |
| 95 | + it(`${name} routes explicit boundary screenshots through a fresh page`, async () => { |
| 96 | + vi.stubEnv("HF_FAST_CAPTURE_BOUNDARY_SS", "true"); |
| 97 | + const s = await session(); |
| 98 | + s.clipBoundaryFrames = new Set([7]); |
| 99 | + await expect(capture(s, 7, 7 / 30)).rejects.toThrow("boundary screenshot requested"); |
| 100 | + expect(pageScreenshotCapture).not.toHaveBeenCalled(); |
| 101 | + expect(produce).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + it("rejects a suspect tiny frame without substituting a stale screenshot", async () => { |
| 106 | + vi.mocked(captureDrawElementFrame).mockResolvedValueOnce(Buffer.alloc(100)); |
| 107 | + await expect(captureFrameToBuffer(await session(), 7, 7 / 30)).rejects.toThrow( |
| 108 | + "suspect small frame", |
| 109 | + ); |
| 110 | + expect(pageScreenshotCapture).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("returns a normal drawElement frame unchanged", async () => { |
| 114 | + const buffer = Buffer.alloc(30_000); |
| 115 | + vi.mocked(captureDrawElementFrame).mockResolvedValueOnce(buffer); |
| 116 | + expect((await captureFrameToBuffer(await session(), 7, 7 / 30)).buffer).toBe(buffer); |
| 117 | + expect(pageScreenshotCapture).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it.each(["No cached paint record for element", DE_CANVAS_NOT_INITIALIZED_CODE])( |
| 121 | + "rejects a failed batch and safely abandons pending prefix encodes", |
| 122 | + async (error) => { |
| 123 | + let rejectPrefix: (error: Error) => void = () => {}; |
| 124 | + const prefix = new Promise<Buffer>((_, reject) => { |
| 125 | + rejectPrefix = reject; |
| 126 | + }); |
| 127 | + vi.mocked(produceDrawElementFrameBatch).mockResolvedValueOnce({ |
| 128 | + encodeResults: [prefix], |
| 129 | + failedAt: 1, |
| 130 | + error, |
| 131 | + }); |
| 132 | + await expect( |
| 133 | + captureFramesBatchPipelined(await session(), [6, 7], [6 / 30, 7 / 30]), |
| 134 | + ).rejects.toThrow("frame 7"); |
| 135 | + rejectPrefix(new Error("worker closed during fallback")); |
| 136 | + await new Promise<void>((resolve) => setImmediate(resolve)); |
| 137 | + expect(pageScreenshotCapture).not.toHaveBeenCalled(); |
| 138 | + expect(produceDrawElementFrame).not.toHaveBeenCalled(); |
| 139 | + }, |
| 140 | + ); |
| 141 | + |
| 142 | + it("recognizes wrapped structural errors without misclassifying generic failures", () => { |
| 143 | + expect(isDrawElementCaptureError({ cause: { cause: { deCaptureFailure: true } } })).toBe(true); |
| 144 | + expect(isDrawElementCaptureError(new Error("No cached paint record for element"))).toBe(false); |
| 145 | + const cycle: { cause?: unknown } = {}; |
| 146 | + cycle.cause = cycle; |
| 147 | + expect(isDrawElementCaptureError(cycle)).toBe(false); |
| 148 | + }); |
| 149 | +}); |
0 commit comments