Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
import { describe, expect, it } from "vitest";
import { parseHTML } from "linkedom";
import { addScenePartsManifest } from "./scenePartsManifest";
const doc = (sceneA: string, root: string, signature = "1") => `<!doctype html><html><head>
<meta name="sig" content="${signature}"><style>.root{}</style><style data-hf-scene="a">${sceneA}</style>
</head><body><div data-composition-id="main">${root}
<div data-composition-id="a" data-hf-scene="a"><p>${sceneA}</p></div>
<div data-composition-id="b" data-hf-scene="b"><p>B</p></div></div>
<script data-hf-scene="a">/* ${sceneA} */</script></body></html>`;
const manifestOf = (html: string) =>
JSON.parse(
parseHTML(html)
.document.querySelector('meta[name="hf-scene-parts"]')
?.getAttribute("content") ?? "",
);
describe("addScenePartsManifest", () => {
it("changes only the edited scene's hash when only that scene changes", () => {
const before = manifestOf(
addScenePartsManifest(doc("one", "<h1>t</h1>"), ['meta[name="sig"]']),
);
const after = manifestOf(
addScenePartsManifest(doc("two", "<h1>t</h1>", "2"), ['meta[name="sig"]']),
);
expect(Object.keys(before.scenes).sort()).toEqual(["a", "b"]);
expect(after.shared).toBe(before.shared);
expect(after.scenes.b).toBe(before.scenes.b);
expect(after.scenes.a).not.toBe(before.scenes.a);
});
it("changes the shared hash for any change outside the scenes, including an unignored one", () => {
const base = manifestOf(addScenePartsManifest(doc("one", "<h1>t</h1>"), ['meta[name="sig"]']));
const root = manifestOf(addScenePartsManifest(doc("one", "<h1>u</h1>"), ['meta[name="sig"]']));
const sig = manifestOf(addScenePartsManifest(doc("one", "<h1>t</h1>", "2")));
expect(root.shared).not.toBe(base.shared);
expect(root.scenes).toEqual(base.scenes);
expect(sig.shared).not.toBe(manifestOf(addScenePartsManifest(doc("one", "<h1>t</h1>"))).shared);
});
it.each([
["a double-quoted >", '<html><head data-title="a>b">'],
["a single-quoted >", "<html><head data-title='a>b'>"],
[
"an earlier <head> in an html attribute and a comment",
'<html data-x="<head>"><!-- <head> --><head>',
],
])("adds the manifest inside the head when the page holds %s", (_, opening) => {
const html = doc("one", "<h1>t</h1>").replace("<html><head>", opening);
const { document } = parseHTML(addScenePartsManifest(html));
expect(document.head.querySelector('meta[name="hf-scene-parts"]')).not.toBeNull();
expect(document.head.querySelector('style[data-hf-scene="a"]')).not.toBeNull();
expect(document.body.textContent).not.toMatch(/b["']>|head>/);
});
it("adds the manifest right after a head tag with attributes when a script precedes the doctype", () => {
const html = `<script src="gsap.js"></script>${doc("one", "<h1>t</h1>").replace("<head>", '<head lang="en">')}`;
expect(addScenePartsManifest(html)).toContain('<head lang="en">\n<meta name="hf-scene-parts"');
});
it("leaves a document without scene parts untouched", () => {
const html = "<!doctype html><html><head></head><body><p>x</p></body></html>";
expect(addScenePartsManifest(html)).toBe(html);
});
it("puts the manifest right after a head tag with attributes, in linear time", () => {
const withLang = addScenePartsManifest(doc("one", "").replace("<head>", '<head lang="en">'));
const head = parseHTML(withLang).document.head;
expect([head.getAttribute("lang"), head.firstElementChild?.getAttribute("name")]).toEqual([
"en",
"hf-scene-parts",
]);
const unclosed = `<body><div data-hf-scene="a"></div><head `;
expect(addScenePartsManifest(unclosed)).toMatch(/^<meta name="hf-scene-parts"[^>]*>\n<body>/);
const started = performance.now();
addScenePartsManifest(`<body><div data-hf-scene="a"></div>${"<head ".repeat(40_000)}`);
expect(performance.now() - started).toBeLessThan(1000);
});
it("changes the shared hash when scenes are reordered", () => {
const page = (first: string, second: string) => `<html><head></head><body><div id="main">
<div data-hf-scene="${first}"><p>${first}</p></div><div data-hf-scene="${second}"><p>${second}</p></div>
</div></body></html>`;
const ab = manifestOf(addScenePartsManifest(page("a", "b")));
const ba = manifestOf(addScenePartsManifest(page("b", "a")));
expect(ba.scenes).toEqual(ab.scenes);
expect(ba.shared).not.toBe(ab.shared);
});
it("leaves the shared fonts style out of the shared hash, so a scene that brings a font still swaps", () => {
const withFonts = (faces: string) =>
doc("one", "<h1>t</h1>").replace(
"<head>",
`<head><style data-hf-scene-fonts>${faces}</style>`,
);
const one = manifestOf(
addScenePartsManifest(withFonts('@font-face{font-family:"A";src:url(a.woff2)}')),
);
const two = manifestOf(
addScenePartsManifest(
withFonts(
'@font-face{font-family:"A";src:url(a.woff2)}\n@font-face{font-family:"B";src:url(b.woff2)}',
),
),
);
expect(two.shared).toBe(one.shared);
});
});