import { EventEmitter } from "node:events"; import * as realFs from "node:fs"; import * as realFsPromises from "node:fs/promises"; import { mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; // fails without the fix: a copy or still moved into place with renameSync held the whole process while another // program kept the new file open, as Windows lets a scanner do. const HELD_MS = 1000; type FakeProc = EventEmitter & { stderr: EventEmitter; stdout: EventEmitter; kill: () => void; pid: number; }; /** Each ffmpeg the module starts writes its output (its last argument, a path in the project) and exits 0. */ function ffmpegThatWrites(projectDir: string) { return (_command: string, args: string[]) => { const proc = Object.assign(new EventEmitter(), { stderr: new EventEmitter(), stdout: new EventEmitter(), kill: () => {}, pid: 4242, }) as FakeProc; setTimeout(() => { const out = args.at(-1) ?? ""; if (out.startsWith(projectDir)) writeFileSync(out, "fake-bytes"); proc.emit("close", 0); }, 5); return proc; }; } /** The module with a rename that is held HELD_MS, as a file another program has open: the sync one holds the thread. */ async function loadHeld(projectDir: string, durationSeconds: number) { vi.resetModules(); vi.doMock("node:fs", () => { const renameSync = (from: realFs.PathLike, to: realFs.PathLike) => { const until = Date.now() + HELD_MS; while (Date.now() < until); realFs.renameSync(from, to); }; return { ...realFs, renameSync, default: { ...realFs, renameSync } }; }); vi.doMock("node:fs/promises", () => { const rename = async (from: realFs.PathLike, to: realFs.PathLike) => { await new Promise((done) => setTimeout(done, HELD_MS)); await realFsPromises.rename(from, to); }; return { ...realFsPromises, rename, default: { ...realFsPromises, rename } }; }); vi.doMock("node:child_process", () => { const mocked = { spawn: ffmpegThatWrites(projectDir) }; return { ...mocked, default: mocked }; }); vi.doMock("@hyperframes/core/preview-proof", () => ({ previewToken: () => "a".repeat(64) })); vi.doMock("@hyperframes/parsers/ff-binaries", async (importOriginal) => ({ ...(await importOriginal()), findFfBinary: () => "/usr/bin/ffmpeg", })); vi.doMock("./mediaMetadata.js", () => ({ probeMediaMetadata: async () => ({ kind: "video", color: { isHdr: false, hdrTransfer: null, codecName: "h264" }, durationSeconds, streams: ["video"], }), probeFirstFrameColour: async () => ({}), })); return import("./proxyTranscoder.js"); } /** The longest the event loop went without running a 20 ms timer while `work` ran. */ async function longestPause(work: () => Promise): Promise { let last = Date.now(); let longest = 0; const tick = setInterval(() => { longest = Math.max(longest, Date.now() - last); last = Date.now(); }, 20); try { await work(); } finally { clearInterval(tick); } return Math.max(longest, Date.now() - last); } function project() { const dir = mkdtempSync(join(tmpdir(), "held-file-")); const source = join(dir, "clip.mov"); writeFileSync(source, "source-bytes"); return { dir, source }; } afterEach(() => { vi.doUnmock("node:fs"); vi.doUnmock("node:fs/promises"); vi.resetModules(); }); describe("a preview copy or still whose new file another program holds", () => { it("keeps the process answering while a whole copy is moved into place", async () => { const { dir, source } = project(); const { resolveProxy } = await loadHeld(dir, 4); let copy = ""; const pause = await longestPause(async () => (copy = await resolveProxy(dir, source))); expect(realFs.existsSync(copy)).toBe(true); expect(pause).toBeLessThan(HELD_MS / 2); }); it("keeps the process answering while each piece of a long copy is moved into place", async () => { const { dir, source } = project(); const { resolveProxy } = await loadHeld(dir, 40); let copy = ""; const pause = await longestPause(async () => (copy = await resolveProxy(dir, source))); expect(realFs.existsSync(copy)).toBe(true); expect(pause).toBeLessThan(HELD_MS / 2); }); it("keeps the process answering while a still is moved into place", async () => { const { dir, source } = project(); const { resolveStill } = await loadHeld(dir, 4); let still = ""; const pause = await longestPause(async () => (still = await resolveStill(dir, source, 1))); expect(realFs.existsSync(still)).toBe(true); expect(pause).toBeLessThan(HELD_MS / 2); }); });