|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterAll, expect, it, vi } from "vitest"; |
| 5 | +import snapshotCommand from "./snapshot.js"; |
| 6 | + |
| 7 | +const root = mkdtempSync(join(tmpdir(), "hf-snapshot-sourceless-video-")); |
| 8 | + |
| 9 | +afterAll(() => rmSync(root, { recursive: true, force: true })); |
| 10 | + |
| 11 | +function writeProject(name: string, clipStart: number): string { |
| 12 | + const dir = join(root, name); |
| 13 | + mkdirSync(dir, { recursive: true }); |
| 14 | + writeFileSync( |
| 15 | + join(dir, "index.html"), |
| 16 | + `<!doctype html><html><body style="margin:0;background:#000"> |
| 17 | +<div id="root" data-composition-id="main" data-start="0" data-duration="5" data-width="320" data-height="180" style="position:relative;width:320px;height:180px"> |
| 18 | + <video id="v" class="clip" data-start="${clipStart}" data-duration="2" data-track-index="0" muted style="position:absolute;inset:0;width:320px;height:180px"></video> |
| 19 | +</div> |
| 20 | +<script> |
| 21 | + window.__timelines = window.__timelines || {}; |
| 22 | + window.__timelines["main"] = { pause() {}, time() { return 0; }, duration() { return 5; }, seek() {} }; |
| 23 | +</script></body></html>`, |
| 24 | + ); |
| 25 | + return dir; |
| 26 | +} |
| 27 | + |
| 28 | +async function warningsAt(dir: string, time: string): Promise<string[]> { |
| 29 | + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 30 | + vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 31 | + vi.spyOn(console, "warn").mockImplementation(() => undefined); |
| 32 | + try { |
| 33 | + await snapshotCommand.run?.({ |
| 34 | + args: { |
| 35 | + dir, |
| 36 | + at: time, |
| 37 | + end: false, |
| 38 | + json: true, |
| 39 | + output: join(dir, "out"), |
| 40 | + describe: "false", |
| 41 | + "browser-gpu": false, |
| 42 | + }, |
| 43 | + } as never); |
| 44 | + return JSON.parse(String(log.mock.calls.at(-1)?.[0])).warnings ?? []; |
| 45 | + } finally { |
| 46 | + vi.restoreAllMocks(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +it("warns when a video on screen has no source to read its frame from", async () => { |
| 51 | + const warnings = await warningsAt(writeProject("on-screen", 1), "1.5"); |
| 52 | + |
| 53 | + expect(warnings).toEqual(["1 video frames missing at 1.5s"]); |
| 54 | +}, 120_000); |
| 55 | + |
| 56 | +it("says nothing about a sourceless video whose clip has not started", async () => { |
| 57 | + const warnings = await warningsAt(writeProject("not-yet", 3), "1.5"); |
| 58 | + |
| 59 | + expect(warnings).toEqual([]); |
| 60 | +}, 120_000); |
0 commit comments