Files
hyperframes/packages/core/src/runtime/clipTree.test.ts
T
Miguel Ángel 77b2260f2b fix(core): report one composition size in stage-size and timeline messages (#4496)
* fix(core): report one composition size in stage-size and timeline messages

The runtime posted the composition size twice per timeline post, read two
ways: stage-size used the lenient reader the stage layout uses, the timeline
payload a strict one. A root with data-width="1080px" reached the player as
1080x1920, then 1920x1080, so a portrait film was framed as a cropped
landscape picture. Both messages and the stage layout now share
parseCompositionDimension, and stage-size sends the value unrounded.

* fix(core): the layout diagnostic and nested roots read the same composition size

* fix(core): read composition sizes as whole px, as the renderer and Studio do

* chore(core): one comment line on the composition size reader

* fix(core): keep fractional layout sizes and report the same root in both size messages

Layout (the stage, timed clips, flattened nested roots, the layout
diagnostic) reads data-width/data-height as before, fractions kept. Only the
size the runtime reports in stage-size and timeline reads whole px. Both
messages and the stage layout now take the root from one finder, so an
earlier composition no longer replaces the explicit data-root's size.

* fix(core): use the size messages root for the clip tree and analytics
2026-09-25 21:59:01 +00:00

98 lines
3.8 KiB
TypeScript

import { describe, it, expect, afterEach } from "vitest";
import { createClipTree, stableClipId } from "./clipTree";
describe("stableClipId", () => {
it("prefers id, falls back to data-hf-id, else null", () => {
const withId = document.createElement("div");
withId.id = "real";
withId.setAttribute("data-hf-id", "hf-1");
expect(stableClipId(withId)).toBe("real");
const hfOnly = document.createElement("div");
hfOnly.setAttribute("data-hf-id", "hf-2");
expect(stableClipId(hfOnly)).toBe("hf-2");
expect(stableClipId(document.createElement("div"))).toBeNull();
});
});
describe("createClipTree", () => {
afterEach(() => {
document.body.innerHTML = "";
});
const params = {
startResolver: { resolveStartForElement: () => 0 },
timelineRegistry: {},
rootDuration: 10,
};
it.each(["", " ", "0s", "0abc", "0px", "-1s", "Infinity", "NaN"])(
"keeps a clip with invalid literal data-duration=%j on the fallback window",
(duration) => {
document.body.innerHTML = `
<div data-composition-id="root" data-duration="10" data-start="0" id="root">
<div data-start="0" data-duration="${duration}" id="clip"></div>
</div>`;
expect(createClipTree(params).roots.map((node) => node.id)).toContain("clip");
},
);
it("leaves out the explicit root when another composition comes first", () => {
document.body.innerHTML = `
<div data-composition-id="card" data-start="0" data-duration="3" id="card"></div>
<div data-composition-id="main" data-root="true" data-start="0" data-duration="10" id="main">
<div data-start="0" data-duration="5" id="clip"></div>
</div>`;
const ids = createClipTree(params).roots.map((node) => node.id);
expect(ids).toContain("card");
expect(ids).not.toContain("main");
});
// Regression: id-less children (root index.html uses data-hf-id, not id) must
// get their data-hf-id as the node id — not a synthetic `__clip-N` — so the
// tree aligns with __clipManifest (which also keys on data-hf-id) and inline
// expansion can join parent↔child.
it("ids id-less timed elements by data-hf-id and links them to their parent", () => {
document.body.innerHTML = `
<div data-composition-id="root" data-duration="10" data-start="0" id="root">
<div data-start="0" data-duration="5" id="scene">
<h1 data-start="0" data-hf-id="hf-headline" class="headline">Hi</h1>
</div>
</div>`;
const tree = createClipTree(params);
const scene = tree.roots.find((n) => n.id === "scene");
expect(scene).toBeDefined();
const child = scene!.children.find((n) => n.id === "hf-headline");
expect(child).toBeDefined();
expect(child!.id).not.toMatch(/^__clip-/);
expect(child!.parentId).toBe("scene");
});
it("keeps a timed image that starts at the end of the root, since it has its own default length", () => {
document.body.innerHTML = `
<div data-composition-id="root" data-duration="10" data-start="0" id="root">
<img id="late" data-start="10" />
</div>`;
const late = { resolveStartForElement: () => 10 };
expect(createClipTree({ ...params, startResolver: late }).roots.map((n) => n.id)).toContain(
"late",
);
});
it.each([10, 11])(
"does not replace a known zero media span with root duration (start=%s)",
(start) => {
document.body.innerHTML = `
<div data-composition-id="root" data-duration="100" data-start="0" id="root">
<video id="at-eof" data-start="0" data-media-start="${start}"></video>
</div>`;
const video = document.querySelector("video")!;
Object.defineProperty(video, "duration", { value: 10, configurable: true });
expect(createClipTree({ ...params, rootDuration: 100 }).roots).toEqual([]);
},
);
});