|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { afterAll, beforeAll, expect } from "vitest"; |
| 6 | +import { decodePng, getFfmpegBinary, resolveConfig } from "@hyperframes/engine"; |
| 7 | +import { |
| 8 | + compareTiles, |
| 9 | + type Differential, |
| 10 | + type Rgba, |
| 11 | +} from "../../../../../engine/src/services/__fixtures__/blurDifferential.js"; |
| 12 | +import { |
| 13 | + FIXTURES, |
| 14 | + type BlurFixture, |
| 15 | +} from "../../../../../engine/src/services/__fixtures__/motionBlur/fixtures.js"; |
| 16 | +import { |
| 17 | + INVARIANTS, |
| 18 | + RENDER_OUTSIDE_MAX_MAE, |
| 19 | +} from "../../../../../engine/src/services/__fixtures__/motionBlur/thresholds.js"; |
| 20 | +import { createRenderJob, executeRenderJob } from "../../renderOrchestrator.js"; |
| 21 | +import { assemble } from "../assemble.js"; |
| 22 | +import { plan, type DistributedRenderConfig } from "../plan.js"; |
| 23 | +import { renderChunk } from "../renderChunk.js"; |
| 24 | + |
| 25 | +export const HAS_FFMPEG = |
| 26 | + spawnSync(getFfmpegBinary(), ["-version"], { encoding: "utf-8" }).status === 0; |
| 27 | +const FPS = 30; |
| 28 | +/** Four workers over 60 frames start at 0, 15, 30 and 45, so no block starts where a 20-frame chunk does. */ |
| 29 | +export const WHOLE_WORKERS = 4; |
| 30 | +const CHUNK_SIZE = 20; |
| 31 | +export const RENDER_TIMEOUT_MS = 600_000; |
| 32 | + |
| 33 | +const quietLogger = { info() {}, warn() {}, error() {}, debug() {} }; |
| 34 | + |
| 35 | +export const fixtureNamed = (name: string): BlurFixture => { |
| 36 | + const f = FIXTURES.find((x) => x.name === name); |
| 37 | + if (!f) throw new Error(`missing fixture ${name}`); |
| 38 | + return f; |
| 39 | +}; |
| 40 | + |
| 41 | +/** The fixture with its `data-motion-blur-tier` removed, so `auto` itself picks the tier (the filter tier). */ |
| 42 | +function withoutTierAttribute(f: BlurFixture): BlurFixture { |
| 43 | + const html = (blur: "on" | "off"): string => { |
| 44 | + const pinned = f.html(blur); |
| 45 | + const stripped = pinned.replace(/ data-motion-blur-tier="[a-z]+"/, ""); |
| 46 | + if (blur === "on" && stripped === pinned) throw new Error(`${f.name} pins no tier to remove`); |
| 47 | + return stripped; |
| 48 | + }; |
| 49 | + return { ...f, html }; |
| 50 | +} |
| 51 | + |
| 52 | +/** A scratch directory for the suite, whose fontconfig draws grayscale text so DOM glyphs and the capture canvas agree. */ |
| 53 | +export function useBlurScratch(): () => string { |
| 54 | + let root = ""; |
| 55 | + let prior: string | undefined; |
| 56 | + beforeAll(() => { |
| 57 | + root = mkdtempSync(join(tmpdir(), "hf-blur-chunks-")); |
| 58 | + prior = process.env.FONTCONFIG_FILE; |
| 59 | + const conf = join(root, "fonts.conf"); |
| 60 | + writeFileSync( |
| 61 | + conf, |
| 62 | + `<?xml version="1.0"?><!DOCTYPE fontconfig SYSTEM "fonts.dtd"><fontconfig> |
| 63 | +<include ignore_missing="yes">/etc/fonts/fonts.conf</include> |
| 64 | +<match target="font"><edit name="rgba" mode="assign"><const>none</const></edit></match> |
| 65 | +</fontconfig>`, |
| 66 | + ); |
| 67 | + process.env.FONTCONFIG_FILE = conf; |
| 68 | + }); |
| 69 | + afterAll(() => { |
| 70 | + if (prior === undefined) delete process.env.FONTCONFIG_FILE; |
| 71 | + else process.env.FONTCONFIG_FILE = prior; |
| 72 | + rmSync(root, { recursive: true, force: true }); |
| 73 | + }); |
| 74 | + return () => root; |
| 75 | +} |
| 76 | + |
| 77 | +function decodeFrames(dir: string): Rgba[] { |
| 78 | + return readdirSync(dir) |
| 79 | + .filter((file) => file.endsWith(".png")) |
| 80 | + .sort() |
| 81 | + .map((file) => decodePng(readFileSync(join(dir, file)))); |
| 82 | +} |
| 83 | + |
| 84 | +function writeProject(root: string, f: BlurFixture, blur: "on" | "off", tag: string) { |
| 85 | + const workDir = join(root, `${f.name}-${tag}`); |
| 86 | + const projectDir = join(workDir, "project"); |
| 87 | + mkdirSync(projectDir, { recursive: true }); |
| 88 | + writeFileSync(join(projectDir, "index.html"), f.html(blur)); |
| 89 | + return { workDir, projectDir }; |
| 90 | +} |
| 91 | + |
| 92 | +/** The normal in-process render: the page's own `data-motion-blur` decides. */ |
| 93 | +export async function renderWhole( |
| 94 | + root: string, |
| 95 | + f: BlurFixture, |
| 96 | + blur: "on" | "off", |
| 97 | + workers: number, |
| 98 | +): Promise<Rgba[]> { |
| 99 | + const { workDir, projectDir } = writeProject(root, f, blur, `whole-${blur}`); |
| 100 | + const outputDir = join(workDir, "frames"); |
| 101 | + const job = createRenderJob({ |
| 102 | + fps: FPS, |
| 103 | + format: "png-sequence", |
| 104 | + quality: "standard", |
| 105 | + hdrMode: "force-sdr", |
| 106 | + useGpu: false, |
| 107 | + workers, |
| 108 | + logger: quietLogger, |
| 109 | + producerConfig: resolveConfig({ browserGpuMode: "software" }), |
| 110 | + }); |
| 111 | + await executeRenderJob(job, projectDir, outputDir); |
| 112 | + const frames = decodeFrames(outputDir); |
| 113 | + rmSync(workDir, { recursive: true, force: true }); |
| 114 | + return frames; |
| 115 | +} |
| 116 | + |
| 117 | +/** plan() then renderChunk() per chunk then assemble(), the way a Lambda or Cloud Run fan-out runs. */ |
| 118 | +export async function renderChunked( |
| 119 | + root: string, |
| 120 | + f: BlurFixture, |
| 121 | + blur: "on" | "off", |
| 122 | + tag: string, |
| 123 | + config: Partial<DistributedRenderConfig> = {}, |
| 124 | +): Promise<Rgba[]> { |
| 125 | + const { workDir, projectDir } = writeProject(root, f, blur, tag); |
| 126 | + const planDir = join(workDir, "plan"); |
| 127 | + const chunksDir = join(workDir, "chunks"); |
| 128 | + const outputDir = join(workDir, "frames"); |
| 129 | + mkdirSync(planDir, { recursive: true }); |
| 130 | + mkdirSync(chunksDir, { recursive: true }); |
| 131 | + const planned = await plan( |
| 132 | + projectDir, |
| 133 | + { |
| 134 | + fps: FPS, |
| 135 | + width: f.width, |
| 136 | + height: f.height, |
| 137 | + format: "png-sequence", |
| 138 | + chunkSize: CHUNK_SIZE, |
| 139 | + rejectOnSystemFonts: false, |
| 140 | + failClosedFontFetch: false, |
| 141 | + ...config, |
| 142 | + }, |
| 143 | + planDir, |
| 144 | + ); |
| 145 | + const chunkPaths: string[] = []; |
| 146 | + for (let i = 0; i < planned.chunkCount; i++) { |
| 147 | + const chunkPath = join(chunksDir, `chunk-${String(i).padStart(4, "0")}`); |
| 148 | + await renderChunk(planDir, i, chunkPath); |
| 149 | + chunkPaths.push(chunkPath); |
| 150 | + } |
| 151 | + await assemble(planDir, chunkPaths, null, outputDir); |
| 152 | + const frames = decodeFrames(outputDir); |
| 153 | + rmSync(workDir, { recursive: true, force: true }); |
| 154 | + return frames; |
| 155 | +} |
| 156 | + |
| 157 | +const wholeFrameMask = (frame: Rgba): Uint8Array => new Uint8Array(frame.width * frame.height); |
| 158 | + |
| 159 | +function at(frames: readonly Rgba[], n: number): Rgba { |
| 160 | + const frame = frames[n]; |
| 161 | + if (!frame) throw new Error(`missing frame ${n}`); |
| 162 | + return frame; |
| 163 | +} |
| 164 | + |
| 165 | +/** Whole-frame tile comparison of every frame: no trail is masked out, chunk boundaries included. */ |
| 166 | +export function compareEveryFrame( |
| 167 | + reference: readonly Rgba[], |
| 168 | + actual: readonly Rgba[], |
| 169 | +): { frame: number; differential: Differential }[] { |
| 170 | + if (actual.length !== reference.length) { |
| 171 | + throw new Error(`frame count ${actual.length} differs from ${reference.length}`); |
| 172 | + } |
| 173 | + return reference.map((frame, n) => ({ |
| 174 | + frame: n, |
| 175 | + differential: compareTiles(frame, at(actual, n), wholeFrameMask(frame), "outside", { |
| 176 | + block: INVARIANTS.block, |
| 177 | + maxMae: RENDER_OUTSIDE_MAX_MAE, |
| 178 | + }), |
| 179 | + })); |
| 180 | +} |
| 181 | + |
| 182 | +export function worstMae(results: readonly { differential: Differential }[]): number { |
| 183 | + return Math.max(...results.map((x) => x.differential.worstMae)); |
| 184 | +} |
| 185 | + |
| 186 | +export function expectEqualFrames( |
| 187 | + label: string, |
| 188 | + results: readonly { frame: number; differential: Differential }[], |
| 189 | +): void { |
| 190 | + const firstBad = results |
| 191 | + .filter((x) => x.differential.bad.length > 0) |
| 192 | + .slice(0, 5) |
| 193 | + .map((x) => ({ frame: x.frame, bad: x.differential.bad.slice(0, 3) })); |
| 194 | + expect( |
| 195 | + results.flatMap((x) => x.differential.bad), |
| 196 | + `${label} ${JSON.stringify(firstBad)}`, |
| 197 | + ).toEqual([]); |
| 198 | +} |
| 199 | + |
| 200 | +/** The frames of plan() -> renderChunk() -> assemble() with blur on equal a whole in-process render with blur on. */ |
| 201 | +export async function expectChunksEqualWhole( |
| 202 | + root: string, |
| 203 | + name: string, |
| 204 | + tier: "capture" | "auto" = "capture", |
| 205 | +): Promise<void> { |
| 206 | + const f = tier === "auto" ? withoutTierAttribute(fixtureNamed(name)) : fixtureNamed(name); |
| 207 | + const whole = await renderWhole(root, f, "on", WHOLE_WORKERS); |
| 208 | + const chunked = await renderChunked(root, f, "on", "chunked-on"); |
| 209 | + expectEqualFrames(name, compareEveryFrame(whole, chunked)); |
| 210 | +} |
0 commit comments