|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import sharp from "sharp"; |
| 5 | +import { afterAll, expect, it, vi } from "vitest"; |
| 6 | +import snapshotCommand from "./snapshot.js"; |
| 7 | + |
| 8 | +const dir = mkdtempSync(join(tmpdir(), "hf-snapshot-scene-svg-ids-")); |
| 9 | + |
| 10 | +afterAll(() => rmSync(dir, { recursive: true, force: true })); |
| 11 | + |
| 12 | +// Two scenes drawn with the same SVG ids; the corner piece fills from a gradient both share at page level. |
| 13 | +const portrait = ( |
| 14 | + color: string, |
| 15 | +) => `<svg viewBox="0 0 160 90" width="160" height="90" style="position:absolute;inset:0"> |
| 16 | + <defs><linearGradient id="skin"><stop offset="0" stop-color="${color}"/><stop offset="1" stop-color="${color}"/></linearGradient> |
| 17 | + <clipPath id="face"><circle cx="80" cy="45" r="40"/></clipPath> |
| 18 | + <symbol id="portrait" viewBox="0 0 160 90"><rect width="160" height="90" fill="${color}"/></symbol></defs> |
| 19 | + <rect width="160" height="90" fill="url(#skin)" clip-path="url(#face)"/> |
| 20 | + <use href="#portrait" x="0" y="0" width="40" height="20"/> |
| 21 | + <rect x="140" y="70" width="20" height="20" fill="url(#page)"/> |
| 22 | +</svg>`; |
| 23 | +writeFileSync( |
| 24 | + join(dir, "index.html"), |
| 25 | + `<!doctype html><html><body style="margin:0;background:#000"> |
| 26 | +<svg width="0" height="0" style="position:absolute"><linearGradient id="page"><stop offset="0" stop-color="#0f0"/><stop offset="1" stop-color="#0f0"/></linearGradient></svg> |
| 27 | +<div id="root" data-composition-id="main" data-start="0" data-duration="20" data-width="160" data-height="90" style="position:relative;width:160px;height:90px;background:#000"> |
| 28 | + <div data-start="0" data-duration="10" data-track-index="0" style="position:absolute;inset:0">${portrait("#f00")}</div> |
| 29 | + <div data-start="10" data-duration="10" data-track-index="0" style="position:absolute;inset:0">${portrait("#00f")}</div> |
| 30 | +</div> |
| 31 | +<script> |
| 32 | + window.__timelines = window.__timelines || {}; |
| 33 | + window.__timelines["main"] = { pause() {}, time() { return 0; }, duration() { return 20; }, seek() {} }; |
| 34 | +</script></body></html>`, |
| 35 | +); |
| 36 | + |
| 37 | +const colourAt = async (path: string, x: number, y: number) => { |
| 38 | + const { data, info } = await sharp(path).raw().toBuffer({ resolveWithObject: true }); |
| 39 | + const at = (y * info.width + x) * info.channels; |
| 40 | + const [r, g, b] = [data[at]!, data[at + 1]!, data[at + 2]!]; |
| 41 | + return r > 200 ? "red" : b > 200 ? "blue" : g > 200 ? "green" : "none"; |
| 42 | +}; |
| 43 | + |
| 44 | +// fails without the fix: at 14 s the second scene's face painted nothing and its corner showed the first's red. |
| 45 | +it("shows each scene's own SVG portrait when two scenes reuse the same ids", async () => { |
| 46 | + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 47 | + vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 48 | + vi.spyOn(console, "warn").mockImplementation(() => undefined); |
| 49 | + try { |
| 50 | + await snapshotCommand.run?.({ |
| 51 | + args: { |
| 52 | + dir, |
| 53 | + at: "4,14", |
| 54 | + end: false, |
| 55 | + json: true, |
| 56 | + output: join(dir, "out"), |
| 57 | + describe: "false", |
| 58 | + "browser-gpu": false, |
| 59 | + }, |
| 60 | + } as never); |
| 61 | + const { frames } = JSON.parse(String(log.mock.calls.at(-1)?.[0])) as { |
| 62 | + frames: { path: string; time: number }[]; |
| 63 | + }; |
| 64 | + const seen = await Promise.all( |
| 65 | + frames.map(async ({ path, time }) => [ |
| 66 | + time, |
| 67 | + await colourAt(path, 80, 45), |
| 68 | + await colourAt(path, 10, 5), |
| 69 | + await colourAt(path, 150, 80), |
| 70 | + ]), |
| 71 | + ); |
| 72 | + expect(seen).toEqual([ |
| 73 | + [4, "red", "red", "green"], |
| 74 | + [14, "blue", "blue", "green"], |
| 75 | + ]); |
| 76 | + } finally { |
| 77 | + vi.restoreAllMocks(); |
| 78 | + } |
| 79 | +}, 120_000); |
0 commit comments