|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + cpSync, |
| 4 | + existsSync, |
| 5 | + lstatSync, |
| 6 | + mkdirSync, |
| 7 | + mkdtempSync, |
| 8 | + readFileSync, |
| 9 | + rmSync, |
| 10 | + symlinkSync, |
| 11 | + writeFileSync, |
| 12 | +} from "node:fs"; |
| 13 | +import { tmpdir } from "node:os"; |
| 14 | +import { join } from "node:path"; |
| 15 | +import { createRenderJob, executeRenderJob } from "./renderOrchestrator.js"; |
| 16 | +import { materializeExtractedFramesForCompiledDir } from "./render/shared.js"; |
| 17 | + |
| 18 | +const staging = vi.hoisted((): { requestedCopy?: boolean } => ({})); |
| 19 | +vi.mock("@hyperframes/engine", async (importOriginal) => ({ |
| 20 | + ...(await importOriginal<typeof import("@hyperframes/engine")>()), |
| 21 | + assertConfiguredFfmpegBinariesExist: () => {}, |
| 22 | +})); |
| 23 | +vi.mock("./render/stages/compileStage.js", () => ({ |
| 24 | + runCompileStage: async () => ({ |
| 25 | + compiled: { html: "<div></div>" }, |
| 26 | + composition: { width: 320, height: 180, duration: 1, videos: [], images: [], audios: [] }, |
| 27 | + deviceScaleFactor: 1, |
| 28 | + outputWidth: 320, |
| 29 | + outputHeight: 180, |
| 30 | + compileOnlyMs: 0, |
| 31 | + forceScreenshot: true, |
| 32 | + }), |
| 33 | +})); |
| 34 | +vi.mock("./render/stages/probeStage.js", () => ({ |
| 35 | + runProbeStage: async ({ compiled }: { compiled: object }) => ({ |
| 36 | + compiled, |
| 37 | + fileServer: null, |
| 38 | + probeSession: null, |
| 39 | + lastBrowserConsole: [], |
| 40 | + duration: 1, |
| 41 | + totalFrames: 30, |
| 42 | + browserProbeMs: 0, |
| 43 | + }), |
| 44 | +})); |
| 45 | +vi.mock("./render/stages/extractVideosStage.js", async (importOriginal) => ({ |
| 46 | + ...(await importOriginal<typeof import("./render/stages/extractVideosStage.js")>()), |
| 47 | + runExtractVideosStage: async ({ materializeSymlinks }: { materializeSymlinks?: boolean }) => { |
| 48 | + staging.requestedCopy = materializeSymlinks; |
| 49 | + throw new Error("staging reached"); |
| 50 | + }, |
| 51 | +})); |
| 52 | + |
| 53 | +const dirs: string[] = []; |
| 54 | +function fixture() { |
| 55 | + const root = mkdtempSync(join(tmpdir(), "hf-windows-staging-")); |
| 56 | + dirs.push(root); |
| 57 | + const source = join(root, "cache"); |
| 58 | + mkdirSync(source); |
| 59 | + const frame = join(source, "frame_000001.jpg"); |
| 60 | + writeFileSync(frame, "frame bytes"); |
| 61 | + return { |
| 62 | + root, |
| 63 | + source, |
| 64 | + compiled: join(root, "compiled"), |
| 65 | + extracted: { videoId: "clip", outputDir: source, framePaths: new Map([[0, frame]]) }, |
| 66 | + }; |
| 67 | +} |
| 68 | +afterEach(() => { |
| 69 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 70 | +}); |
| 71 | + |
| 72 | +it("the actual local Windows renderer does not request eager frame copies", async () => { |
| 73 | + const { root } = fixture(); |
| 74 | + writeFileSync(join(root, "index.html"), "<div></div>"); |
| 75 | + const job = createRenderJob({ |
| 76 | + fps: 30, |
| 77 | + quality: "standard", |
| 78 | + hdrMode: "force-sdr", |
| 79 | + logger: { info() {}, warn() {}, error() {}, debug() {} }, |
| 80 | + }); |
| 81 | + const platform = Object.getOwnPropertyDescriptor(process, "platform")!; |
| 82 | + try { |
| 83 | + Object.defineProperty(process, "platform", { value: "win32" }); |
| 84 | + await expect(executeRenderJob(job, root, join(root, "output.mp4"))).rejects.toThrow( |
| 85 | + "staging reached", |
| 86 | + ); |
| 87 | + expect(staging.requestedCopy).not.toBe(true); |
| 88 | + } finally { |
| 89 | + Object.defineProperty(process, "platform", platform); |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | +describe.skipIf(process.platform !== "win32")("real Windows frame staging", () => { |
| 94 | + it("uses a real junction after a symlink privilege denial without copying frames", () => { |
| 95 | + const { source, compiled, extracted } = fixture(); |
| 96 | + const attempts: Array<string | undefined> = []; |
| 97 | + materializeExtractedFramesForCompiledDir([extracted], compiled, { |
| 98 | + fileSystem: { |
| 99 | + existsSync, |
| 100 | + mkdirSync, |
| 101 | + rmSync, |
| 102 | + symlinkSync: (target, path, type) => { |
| 103 | + attempts.push(type); |
| 104 | + if (attempts.length === 1) |
| 105 | + throw Object.assign(new Error("symlink denied"), { code: "EPERM" }); |
| 106 | + symlinkSync(target, path, type); |
| 107 | + }, |
| 108 | + cpSync: () => { |
| 109 | + throw new Error("junction-capable local staging must not copy"); |
| 110 | + }, |
| 111 | + }, |
| 112 | + }); |
| 113 | + expect(attempts).toEqual(["dir", "junction"]); |
| 114 | + expect(lstatSync(extracted.outputDir).isSymbolicLink()).toBe(true); |
| 115 | + writeFileSync(join(source, "frame_000001.jpg"), "updated cache bytes"); |
| 116 | + expect(readFileSync(extracted.framePaths.get(0)!, "utf8")).toBe("updated cache bytes"); |
| 117 | + rmSync(extracted.outputDir, { recursive: true }); |
| 118 | + expect(readFileSync(join(source, "frame_000001.jpg"), "utf8")).toBe("updated cache bytes"); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +it("distributed materialization stays physically self-contained after cache removal", () => { |
| 123 | + const { source, compiled, extracted } = fixture(); |
| 124 | + materializeExtractedFramesForCompiledDir([extracted], compiled, { materializeSymlinks: true }); |
| 125 | + expect(lstatSync(extracted.outputDir).isSymbolicLink()).toBe(false); |
| 126 | + rmSync(source, { recursive: true }); |
| 127 | + expect(readFileSync(extracted.framePaths.get(0)!, "utf8")).toBe("frame bytes"); |
| 128 | +}); |
| 129 | + |
| 130 | +describe("Windows link fallbacks", () => { |
| 131 | + const platform = Object.getOwnPropertyDescriptor(process, "platform")!; |
| 132 | + beforeEach(() => Object.defineProperty(process, "platform", { value: "win32" })); |
| 133 | + afterEach(() => Object.defineProperty(process, "platform", platform)); |
| 134 | + |
| 135 | + it.each(["EPERM", "EACCES", "UNKNOWN", "EINVAL", "ENOSYS", "EOPNOTSUPP", "ENOTSUP"])( |
| 136 | + "copies only after the junction is rejected with %s", |
| 137 | + (code) => { |
| 138 | + const { compiled, extracted } = fixture(); |
| 139 | + const attempts: Array<string | undefined> = []; |
| 140 | + materializeExtractedFramesForCompiledDir([extracted], compiled, { |
| 141 | + fileSystem: { |
| 142 | + existsSync, |
| 143 | + mkdirSync, |
| 144 | + cpSync, |
| 145 | + rmSync, |
| 146 | + symlinkSync: (_target, _path, type) => { |
| 147 | + attempts.push(type); |
| 148 | + throw Object.assign(new Error("link unavailable"), { |
| 149 | + code: type === "junction" ? code : "EPERM", |
| 150 | + }); |
| 151 | + }, |
| 152 | + }, |
| 153 | + }); |
| 154 | + expect(attempts).toEqual(["dir", "junction"]); |
| 155 | + expect(lstatSync(extracted.outputDir).isSymbolicLink()).toBe(false); |
| 156 | + expect(readFileSync(extracted.framePaths.get(0)!, "utf8")).toBe("frame bytes"); |
| 157 | + }, |
| 158 | + ); |
| 159 | + |
| 160 | + it("does not hide a disk failure from the junction attempt", () => { |
| 161 | + const { compiled, extracted } = fixture(); |
| 162 | + const copy = vi.fn(); |
| 163 | + expect(() => |
| 164 | + materializeExtractedFramesForCompiledDir([extracted], compiled, { |
| 165 | + fileSystem: { |
| 166 | + existsSync, |
| 167 | + mkdirSync, |
| 168 | + rmSync, |
| 169 | + cpSync: copy, |
| 170 | + symlinkSync: (_target, _path, type) => { |
| 171 | + throw Object.assign(new Error(type === "junction" ? "disk full" : "denied"), { |
| 172 | + code: type === "junction" ? "ENOSPC" : "EPERM", |
| 173 | + }); |
| 174 | + }, |
| 175 | + }, |
| 176 | + }), |
| 177 | + ).toThrow("disk full"); |
| 178 | + expect(copy).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments