|
| 1 | +import { afterAll, beforeAll, describe, expect, it } from "bun:test"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { createRenderJob, executeRenderJob, RenderCancelledError } from "../renderOrchestrator.js"; |
| 7 | +import { renderSpan } from "./renderSpan.js"; |
| 8 | + |
| 9 | +const ROOT_HTML = `<!doctype html> |
| 10 | +<html><head><meta charset="utf-8"> |
| 11 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script> |
| 12 | +<style>html,body{margin:0;width:160px;height:90px;overflow:hidden;background:#0f172a} |
| 13 | +#main{position:relative;width:160px;height:90px} |
| 14 | +@keyframes slide{from{transform:translateX(0)}to{transform:translateX(120px)}} |
| 15 | +@keyframes ramp{from{background:#000}to{background:#fff}} |
| 16 | +#bg{position:absolute;inset:0;animation:ramp 3s linear forwards} |
| 17 | +#box{position:absolute;left:0;top:10px;width:30px;height:30px;background:#f97316;animation:slide 3s linear forwards} |
| 18 | +#sub{position:absolute;inset:0}</style></head> |
| 19 | +<body><div id="main" data-composition-id="main" data-width="160" data-height="90" data-start="0" data-duration="3"> |
| 20 | +<div id="bg"></div> |
| 21 | +<div id="box"></div> |
| 22 | +<div id="sub" data-composition-id="sub" data-composition-src="compositions/sub.html" data-start="1" data-duration="1" data-track-index="1"></div> |
| 23 | +</div> |
| 24 | +<script>const tl=gsap.timeline({paused:true});window.__timelines=window.__timelines||{};window.__timelines["main"]=tl;tl.to({},{duration:3},0);</script> |
| 25 | +</body></html>`; |
| 26 | + |
| 27 | +const SUB_HTML = `<template id="sub-template"> |
| 28 | +<div id="sub-root" data-composition-id="sub" data-start="0" data-duration="1" data-width="160" data-height="90"> |
| 29 | +<div id="chip"></div></div> |
| 30 | +<style>[data-composition-id="sub"]{position:absolute;inset:0;width:160px;height:90px} |
| 31 | +@keyframes grow{from{width:10px}to{width:150px}} |
| 32 | +[data-composition-id="sub"] #chip{position:absolute;left:5px;top:60px;height:20px;width:10px;background:#22c55e;animation:grow 1s linear forwards}</style> |
| 33 | +<script>const t=gsap.timeline({paused:true});window.__timelines=window.__timelines||{};window.__timelines["sub"]=t;t.to({},{duration:1},0);</script> |
| 34 | +</template>`; |
| 35 | + |
| 36 | +function frames(path: string): Buffer[] { |
| 37 | + const out = spawnSync( |
| 38 | + "ffmpeg", |
| 39 | + ["-v", "error", "-i", path, "-f", "rawvideo", "-pix_fmt", "rgb24", "-"], |
| 40 | + { maxBuffer: 1 << 28 }, |
| 41 | + ); |
| 42 | + const size = 160 * 90 * 3; |
| 43 | + const all = out.stdout; |
| 44 | + return Array.from({ length: Math.floor(all.length / size) }, (_, i) => |
| 45 | + all.subarray(i * size, (i + 1) * size), |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +function meanDiff(a: Buffer, b: Buffer): number { |
| 50 | + let sum = 0; |
| 51 | + for (let i = 0; i < a.length; i++) sum += Math.abs(a[i]! - b[i]!); |
| 52 | + return sum / a.length; |
| 53 | +} |
| 54 | + |
| 55 | +describe("renderSpan", () => { |
| 56 | + let root: string; |
| 57 | + let projectDir: string; |
| 58 | + |
| 59 | + beforeAll(() => { |
| 60 | + root = mkdtempSync(join(tmpdir(), "hf-render-span-")); |
| 61 | + projectDir = join(root, "project"); |
| 62 | + mkdirSync(join(projectDir, "compositions"), { recursive: true }); |
| 63 | + writeFileSync(join(projectDir, "index.html"), ROOT_HTML); |
| 64 | + writeFileSync(join(projectDir, "compositions", "sub.html"), SUB_HTML); |
| 65 | + }); |
| 66 | + |
| 67 | + afterAll(() => rmSync(root, { recursive: true, force: true })); |
| 68 | + |
| 69 | + it("renders the frames of the span the way a full render renders them, sub-composition included", async () => { |
| 70 | + const fullPath = join(root, "full.mp4"); |
| 71 | + const job = createRenderJob({ fps: { num: 30, den: 1 }, quality: "draft", format: "mp4" }); |
| 72 | + await executeRenderJob(job, projectDir, fullPath); |
| 73 | + |
| 74 | + const spanPath = join(root, "span.mp4"); |
| 75 | + const { framesEncoded } = await renderSpan( |
| 76 | + projectDir, |
| 77 | + { fps: 30, width: 160, height: 90, format: "mp4", quality: "draft", hdrMode: "force-sdr" }, |
| 78 | + { startSeconds: 0.5, endSeconds: 2 }, |
| 79 | + spanPath, |
| 80 | + ); |
| 81 | + expect(framesEncoded).toBe(45); |
| 82 | + |
| 83 | + const full = frames(fullPath); |
| 84 | + const span = frames(spanPath); |
| 85 | + expect(span.length).toBe(45); |
| 86 | + const worstMatch = Math.max(...span.map((frame, i) => meanDiff(frame, full[15 + i]!))); |
| 87 | + // The backdrop brightens about 2.8 levels a frame, so a span one frame off its place misses by more than this. |
| 88 | + expect(worstMatch).toBeLessThan(1.5); |
| 89 | + const oneFrameLate = span.map((frame, i) => meanDiff(frame, full[16 + i]!)); |
| 90 | + expect(Math.min(...oneFrameLate)).toBeGreaterThan(worstMatch); |
| 91 | + }, 240_000); |
| 92 | + |
| 93 | + it("stops when it is cancelled after planning, with no output written", async () => { |
| 94 | + const scratch = join(root, "scratch"); |
| 95 | + mkdirSync(scratch, { recursive: true }); |
| 96 | + const previousTmp = process.env.TMPDIR; |
| 97 | + process.env.TMPDIR = scratch; |
| 98 | + const controller = new AbortController(); |
| 99 | + const outputPath = join(root, "cancelled.mp4"); |
| 100 | + try { |
| 101 | + const running = renderSpan( |
| 102 | + projectDir, |
| 103 | + { |
| 104 | + fps: 30, |
| 105 | + width: 160, |
| 106 | + height: 90, |
| 107 | + format: "mp4", |
| 108 | + quality: "draft", |
| 109 | + hdrMode: "force-sdr", |
| 110 | + chunkSize: 15, |
| 111 | + abortSignal: controller.signal, |
| 112 | + }, |
| 113 | + { startSeconds: 0 }, |
| 114 | + outputPath, |
| 115 | + ); |
| 116 | + const firstPart = () => |
| 117 | + readdirSync(scratch).some((dir) => |
| 118 | + existsSync(join(scratch, dir, "parts", "part-0000.mp4")), |
| 119 | + ); |
| 120 | + while (!firstPart()) await new Promise((done) => setTimeout(done, 20)); |
| 121 | + controller.abort(); |
| 122 | + await expect(running).rejects.toBeInstanceOf(RenderCancelledError); |
| 123 | + expect(existsSync(outputPath)).toBe(false); |
| 124 | + } finally { |
| 125 | + if (previousTmp === undefined) delete process.env.TMPDIR; |
| 126 | + else process.env.TMPDIR = previousTmp; |
| 127 | + } |
| 128 | + }, 240_000); |
| 129 | +}); |
0 commit comments