|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, expect, it, vi } from "vitest"; |
| 5 | +import { compileForRender } from "./htmlCompiler.js"; |
| 6 | + |
| 7 | +const downloads = vi.hoisted(() => [] as { url: string; destDir: string }[]); |
| 8 | +vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({ |
| 9 | + ...(await importOriginal<typeof import("../utils/urlDownloader.js")>()), |
| 10 | + downloadToTemp: async (url: string, destDir: string) => { |
| 11 | + downloads.push({ url, destDir }); |
| 12 | + throw new Error("offline test"); |
| 13 | + }, |
| 14 | + fetchPublicHttpsText: async () => { |
| 15 | + throw new Error("offline test"); |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +const dirs: string[] = []; |
| 20 | +afterEach(() => { |
| 21 | + downloads.length = 0; |
| 22 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 23 | +}); |
| 24 | + |
| 25 | +// The download cache is keyed by url and folder: one folder is what makes the second request a cache hit. |
| 26 | +it("asks for a remote video in one folder for both its duration probe and its localisation", async () => { |
| 27 | + const dir = mkdtempSync(join(tmpdir(), "hf-remote-once-")); |
| 28 | + dirs.push(dir); |
| 29 | + const url = "https://media.example.com/clip.mp4"; |
| 30 | + const htmlPath = join(dir, "index.html"); |
| 31 | + writeFileSync( |
| 32 | + htmlPath, |
| 33 | + `<!doctype html><html><body><main data-composition-id="root" data-start="0" data-width="16" data-height="16"><video id="v" muted src="${url}" data-start="0" data-track-index="0"></video></main></body></html>`, |
| 34 | + ); |
| 35 | + await compileForRender(dir, htmlPath, join(dir, "out"), { allowSystemFontCapture: false }); |
| 36 | + const forClip = downloads.filter((call) => call.url === url); |
| 37 | + expect(forClip.length).toBeGreaterThanOrEqual(2); |
| 38 | + expect(new Set(forClip.map((call) => call.destDir)).size).toBe(1); |
| 39 | +}); |
0 commit comments