Files
hyperframes/packages/core/src/compiler/standaloneEntry.test.ts
T
Jerrai caa62fe68b fix(core): detect template entries structurally (#7249)
* fix(core): detect template entries structurally

isTemplateEntry only matched a raw file that starts with <template>, so the
full-document sub-composition shape (<!doctype html><html><body><template>
<div data-composition-id>...</template></body></html>) was not recognised and
resolveEntryHtmlPath compiled the raw file, whose root sits in an inert
template: no timeline, no duration, "Composition has zero duration".

A template entry is now a document whose <template> holds the composition
root with no live composition root outside it. Detection and extraction share
one lookup (locateSceneRoot), the same one readSceneRoot uses.

— Jerrai

* fix(core): drop comments from the template lookup

The comment ratchet allows no net rise in a file's comment share; the PR
description carries the explanation.

— Jerrai

* fix(core): find the scene template among several templates

locateSceneRoot read only the first <template>, so a scene template that
follows another template was treated as not a template entry. Scan every
template and take the first whose content holds a composition root.

* fix(core): drop the added comment (comment ratchet)

* chore: raise the inspector unpacked size budget by 0.5%

The inspector bundles core; the multi-template scene lookup added a few
hundred bytes and put it 10 bytes over the 1% tolerance.

* fix(core): refuse a file with several scene templates, naming them

* chore: trim the inspector size budget bump to what is needed

* fix(core): render the first scene template and lint the ambiguity instead of failing

A file with several scene templates and no live root rendered its first one
before; locateSceneRoot threw and stopped such projects rendering. It returns
the first again, and the multiple_scene_templates lint warning names each.

Co-authored-by: Jerrai <jerrai@heygen.com>

* chore(core): drop the locateSceneRoot comment line the comment ratchet counts

Co-authored-by: Jerrai <jerrai@heygen.com>

* fix(core): one shared scene-template pick for the compiler, the inliner and the loader

A scene template after a card template was detected by locateSceneRoot but
mounted from the first template of any kind, so it rendered blank.
findSceneTemplate picks the first template holding a composition root and
locateSceneRoot, resolveSubCompositionContent and the loader all use it.

Co-authored-by: Jerrai <jerrai@heygen.com>
2026-11-09 15:09:04 +00:00

281 lines
12 KiB
TypeScript

import { parseHTML } from "linkedom";
import { describe, expect, it } from "vitest";
import { locateSceneRoot } from "./htmlDocument";
import { extractStandaloneEntryFromIndex, isTemplateEntry } from "./standaloneEntry";
const ROOT = `<div id="root" data-composition-id="scene" data-width="640" data-height="360" data-duration="3"><p id="label">Hello</p></div>`;
const SCENE_SCRIPT = `<script>window.__timelines = window.__timelines || {}; const tl = gsap.timeline({ paused: true }); tl.to("#label", { opacity: 1, duration: 3 }, 0); window.__timelines["scene"] = tl;</script>`;
const SCENE_STYLE = `<style>#root { position: absolute; inset: 0; background: #123456; }</style>`;
describe("isTemplateEntry", () => {
it.each([
`<template>${ROOT}</template>`,
` \n<!-- Scene author notes -->\n<template id='scene'>${ROOT}</template>`,
`<!-- a --><!-- <template> is inert --><TEMPLATE>${ROOT}</TEMPLATE>`,
`<!--><template>${ROOT}</template>`,
])("recognizes a template-first fragment entry: %s", (html) => {
expect(isTemplateEntry(html)).toBe(true);
});
it("recognizes the full-document shape whose composition root lives in the body template", () => {
const html = `<!doctype html>
<html>
<head><meta charset="UTF-8" /></head>
<body>
<template>
${SCENE_STYLE}
${ROOT}
${SCENE_SCRIPT}
</template>
</body>
</html>`;
expect(isTemplateEntry(html)).toBe(true);
});
it.each([
"<template-card></template-card>",
`<template-card>${ROOT}</template-card>`,
`<!doctype html><html><body><template></template></body></html>`,
`<div></div><template></template>`,
`<!-- <template>${ROOT}</template> -->`,
`<!doctype html><html><body>${ROOT}</body></html>`,
])("does not mistake other entries for a template scene: %s", (html) => {
expect(isTemplateEntry(html)).toBe(false);
});
it("keeps a full document with a live composition root outside the template a plain entry", () => {
const html = `<!doctype html>
<html>
<body>
<div id="live" data-composition-id="live" data-width="640" data-height="360" data-duration="3"></div>
<template>${ROOT}</template>
</body>
</html>`;
expect(isTemplateEntry(html)).toBe(false);
});
it("keeps a template-first fragment with a live composition root beside it a plain entry", () => {
const html = `<template>${ROOT}</template><div data-composition-id="live"></div>`;
expect(isTemplateEntry(html)).toBe(false);
});
it.each([
`<template id="card"><div class="card">Card</div></template><template id="scene">${ROOT}</template>`,
`<!doctype html><html><body><template><svg><symbol id="i"></symbol></svg></template><template>${ROOT}</template></body></html>`,
`<template></template><template><p>x</p></template><template>${ROOT}</template>`,
])("finds the scene template when an earlier template holds no composition root: %s", (html) => {
expect(isTemplateEntry(html)).toBe(true);
expect(locateSceneRoot(html).root?.getAttribute("data-composition-id")).toBe("scene");
});
it("renders the first scene template when several hold a composition root and none is live", () => {
const html = `<template id="intro">${ROOT}</template><template><div data-composition-id="later"></div></template>`;
expect(locateSceneRoot(html).root?.getAttribute("data-composition-id")).toBe("scene");
expect(isTemplateEntry(html)).toBe(true);
});
it("does not refuse when a live root makes the file a plain entry", () => {
const html = `<template>${ROOT}</template><template>${ROOT}</template><div data-composition-id="live"></div>`;
expect(isTemplateEntry(html)).toBe(false);
});
it("reports no root when no template holds a composition root", () => {
const lookup = locateSceneRoot(`<template><p>a</p></template><template><p>b</p></template>`);
expect(lookup).toMatchObject({ root: null, hasTemplate: true, hasLiveRoot: false });
});
it("reads a single template and a template-free document as before", () => {
expect(locateSceneRoot(`<template>${ROOT}</template>`).root?.id).toBe("root");
expect(locateSceneRoot(`<!doctype html><html><body>${ROOT}</body></html>`)).toMatchObject({
hasTemplate: false,
hasLiveRoot: true,
});
});
});
describe("extractStandaloneEntryFromIndex", () => {
it("reuses the index wrapper and keeps only the requested composition host", () => {
const indexHtml = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>body { background: #111; }</style>
</head>
<body>
<div id="main" data-composition-id="root" data-width="1920" data-height="1080">
<div id="intro" data-composition-id="intro" data-composition-src="compositions/intro.html" data-start="5"></div>
<div id="outro" data-composition-id="outro" data-composition-src="compositions/outro.html" data-start="12"></div>
</div>
</body>
</html>`;
const extracted = extractStandaloneEntryFromIndex(indexHtml, "compositions/outro.html");
expect(extracted).toContain('data-composition-id="root"');
expect(extracted).toContain('id="outro"');
expect(extracted).toContain('data-composition-src="compositions/outro.html"');
expect(extracted).toContain('data-start="0"');
expect(extracted).not.toContain('id="intro"');
expect(extracted).toContain("<style>body { background: #111; }</style>");
});
it("keeps the index's body library scripts ahead of the scene and drops its inline scripts", () => {
const indexHtml = `<!DOCTYPE html><html><head></head><body>
<div id="main" data-composition-id="root" data-width="1920" data-height="1080">
<div id="scene" data-composition-id="scene" data-composition-src="compositions/scene.html"></div>
</div>
<script src="https://cdn.example/gsap.min.js"></script>
<script>window.__timelines = { root: gsap.timeline() };</script>
</body></html>`;
const extracted = extractStandaloneEntryFromIndex(indexHtml, "compositions/scene.html") ?? "";
expect(extracted).toContain('<script src="https://cdn.example/gsap.min.js"></script>');
expect(extracted.indexOf("gsap.min.js")).toBeLessThan(extracted.indexOf('id="scene"'));
expect(extracted).not.toContain("window.__timelines = { root");
});
it("matches normalized data-composition-src paths", () => {
const indexHtml = `<!DOCTYPE html>
<html>
<body>
<div data-composition-id="root" data-width="1920" data-height="1080">
<div id="intro" data-composition-id="intro" data-composition-src="./compositions/intro.html" data-start="3"></div>
</div>
</body>
</html>`;
const extracted = extractStandaloneEntryFromIndex(indexHtml, "compositions/intro.html");
expect(extracted).not.toBeNull();
expect(extracted).toContain('data-start="0"');
expect(extracted).toContain('data-composition-src="./compositions/intro.html"');
});
it("returns null when index.html does not mount the requested entry file", () => {
const indexHtml = `<!DOCTYPE html>
<html>
<body>
<div data-composition-id="root" data-width="1920" data-height="1080">
<div id="intro" data-composition-id="intro" data-composition-src="compositions/intro.html"></div>
</div>
</body>
</html>`;
const extracted = extractStandaloneEntryFromIndex(indexHtml, "compositions/outro.html");
expect(extracted).toBeNull();
});
it("re-points the wrapper duration at the scene's own, not the master's", () => {
const indexHtml = `<!DOCTYPE html>
<html>
<body>
<div data-composition-id="master" data-width="640" data-height="360" data-duration="12">
<div id="scene1" data-composition-id="scene1" data-composition-src="compositions/scene1.html" data-start="0" data-duration="2"></div>
</div>
</body>
</html>`;
const sceneHtml = `<template id="scene1-template"><div data-composition-id="scene1" data-width="640" data-height="360" data-duration="3"></div></template>`;
const extracted = extractStandaloneEntryFromIndex(
indexHtml,
"compositions/scene1.html",
sceneHtml,
);
// The extracted standalone advertises the scene file's 3s, not the mount's 2s or master's 12s.
expect(extracted).toContain('data-duration="3"');
expect(extracted).not.toContain('data-duration="12"');
});
it("falls back to the mount's data-duration when the scene file isn't supplied", () => {
const indexHtml = `<!DOCTYPE html>
<html>
<body>
<div data-composition-id="master" data-width="640" data-height="360" data-duration="12">
<div id="scene1" data-composition-id="scene1" data-composition-src="compositions/scene1.html" data-start="0" data-duration="2"></div>
</div>
</body>
</html>`;
const extracted = extractStandaloneEntryFromIndex(indexHtml, "compositions/scene1.html");
expect(extracted).toContain('data-duration="2"');
expect(extracted).not.toContain('data-duration="12"');
});
describe("master bus chain", () => {
const hostIndexHtml = `<!DOCTYPE html>
<html>
<body>
<div id="scene1" data-composition-id="scene1" data-composition-src="compositions/scene1.html" data-width="640" data-height="360" data-fx-chain="{&quot;version&quot;:1,&quot;nodes&quot;:[{&quot;type&quot;:&quot;truepeak&quot;,&quot;id&quot;:&quot;index&quot;}]}"></div>
</body>
</html>`;
const indexHtml = `<!DOCTYPE html>
<html>
<body>
<div data-composition-id="master" data-width="640" data-height="360" data-fx-chain="{&quot;version&quot;:1,&quot;nodes&quot;:[{&quot;type&quot;:&quot;truepeak&quot;,&quot;id&quot;:&quot;index&quot;}]}">
<div id="scene1" data-composition-id="scene1" data-composition-src="compositions/scene1.html" data-start="0"></div>
</div>
</body>
</html>`;
const chainOf = (html: string): string | null => {
const { document } = parseHTML(html);
const roots = Array.from(document.querySelectorAll("[data-composition-id]")) as Element[];
const top = roots.find((el) => !el.parentElement?.closest("[data-composition-id]"));
return top?.getAttribute("data-fx-chain") ?? null;
};
it("carries the scene's own chain, not the index's", () => {
const chain = '{"version":1,"nodes":[{"type":"truepeak","id":"scene"}]}';
const sceneHtml = `<template id="t"><div data-composition-id="scene1" data-width="640" data-height="360" data-fx-chain='${chain}'></div></template>`;
const extracted = extractStandaloneEntryFromIndex(
indexHtml,
"compositions/scene1.html",
sceneHtml,
);
expect(chainOf(extracted ?? "")).toBe(chain);
});
it("drops the index's chain when the scene declares none", () => {
const sceneHtml = `<template id="t"><div data-composition-id="scene1" data-width="640" data-height="360"></div></template>`;
const extracted = extractStandaloneEntryFromIndex(
indexHtml,
"compositions/scene1.html",
sceneHtml,
);
expect(chainOf(extracted ?? "")).toBeNull();
});
it("carries the scene's chain when the index's top-level root is the mount itself", () => {
const chain = '{"version":1,"nodes":[{"type":"truepeak","id":"scene"}]}';
const sceneHtml = `<template id="t"><div data-composition-id="scene1" data-width="640" data-height="360" data-fx-chain='${chain}'></div></template>`;
const extracted = extractStandaloneEntryFromIndex(
hostIndexHtml,
"compositions/scene1.html",
sceneHtml,
);
expect(chainOf(extracted ?? "")).toBe(chain);
});
it("drops the index's chain from a top-level mount when the scene declares none", () => {
const sceneHtml = `<template id="t"><div data-composition-id="scene1" data-width="640" data-height="360"></div></template>`;
const extracted = extractStandaloneEntryFromIndex(
hostIndexHtml,
"compositions/scene1.html",
sceneHtml,
);
expect(extracted).not.toBeNull();
expect(chainOf(extracted ?? "")).toBeNull();
});
});
});