mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
* feat(producer): report which element set the film length * fix(core): keep the duration guard and comment share in step with the length helper
230 lines
8.8 KiB
TypeScript
230 lines
8.8 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { parseHTMLContent } from "../compiler/htmlDocument";
|
|
import {
|
|
findRootCompositionElement,
|
|
LOOP_INFLATED_TIMELINE_SECONDS,
|
|
MIN_VALID_TIMELINE_DURATION_SECONDS,
|
|
longestMediaWindow,
|
|
readStaticCompositionMeta,
|
|
resolveCompositionLength,
|
|
resolveCompositionLengthSeconds,
|
|
resolveContentDerivedDuration,
|
|
resolveMediaWindowDurationSeconds,
|
|
} from "./compositionLength";
|
|
import { createRuntimeStartTimeResolver } from "./startResolver";
|
|
|
|
describe("resolveCompositionLengthSeconds", () => {
|
|
const base = {
|
|
declared: null,
|
|
timeline: () => null,
|
|
floors: () => [],
|
|
fallback: 0,
|
|
derived: () => 0,
|
|
};
|
|
|
|
it("says which input set the length", () => {
|
|
expect(resolveCompositionLength({ ...base, declared: 9 }).from).toBe("declared");
|
|
expect(resolveCompositionLength({ ...base, timeline: () => 10, floors: () => [4] }).from).toBe(
|
|
"timeline",
|
|
);
|
|
expect(resolveCompositionLength({ ...base, floors: () => [null, 7] })).toEqual({
|
|
seconds: 7,
|
|
from: "floor",
|
|
floorIndex: 1,
|
|
});
|
|
expect(resolveCompositionLength({ ...base, fallback: 5 }).from).toBe("fallback");
|
|
expect(resolveCompositionLength({ ...base, derived: () => 2 }).from).toBe("derived");
|
|
});
|
|
|
|
it("takes a declared length and reads nothing else", () => {
|
|
const timeline = vi.fn(() => 10);
|
|
const floors = vi.fn(() => [12]);
|
|
const derived = vi.fn(() => 7);
|
|
expect(
|
|
resolveCompositionLengthSeconds({ ...base, declared: 4, timeline, floors, derived }),
|
|
).toBe(4);
|
|
expect(timeline).not.toHaveBeenCalled();
|
|
expect(floors).not.toHaveBeenCalled();
|
|
expect(derived).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not take an infinite declared length", () => {
|
|
expect(
|
|
resolveCompositionLengthSeconds({ ...base, declared: Infinity, timeline: () => 6 }),
|
|
).toBe(6);
|
|
});
|
|
|
|
it("takes the longest of timeline, floors and fallback", () => {
|
|
expect(
|
|
resolveCompositionLengthSeconds({
|
|
...base,
|
|
timeline: () => 6,
|
|
floors: () => [8, null],
|
|
fallback: 5,
|
|
}),
|
|
).toBe(8);
|
|
});
|
|
|
|
it("ignores a timeline of one frame or less", () => {
|
|
const oneFrame = MIN_VALID_TIMELINE_DURATION_SECONDS;
|
|
expect(
|
|
resolveCompositionLengthSeconds({ ...base, timeline: () => oneFrame, derived: () => 2 }),
|
|
).toBe(2);
|
|
});
|
|
|
|
it("plays to the sub-composition floor, not a repeat:-1 timeline's 1e10 s", () => {
|
|
const endless = () => 1e10 + 2;
|
|
expect(resolveCompositionLengthSeconds({ ...base, timeline: endless, floors: () => [6] })).toBe(
|
|
6,
|
|
);
|
|
expect(resolveCompositionLengthSeconds({ ...base, timeline: endless, fallback: 5 })).toBe(5);
|
|
});
|
|
|
|
it("treats a timeline as loop-inflated from LOOP_INFLATED_TIMELINE_SECONDS on", () => {
|
|
const at = (seconds: number) =>
|
|
resolveCompositionLengthSeconds({ ...base, timeline: () => seconds, floors: () => [6] });
|
|
expect(at(LOOP_INFLATED_TIMELINE_SECONDS)).toBe(6);
|
|
expect(at(LOOP_INFLATED_TIMELINE_SECONDS - 1)).toBe(LOOP_INFLATED_TIMELINE_SECONDS - 1);
|
|
});
|
|
|
|
it("keeps a repeat:-1 timeline's length when nothing else gives one", () => {
|
|
expect(resolveCompositionLengthSeconds({ ...base, timeline: () => 1e10 })).toBe(1e10);
|
|
});
|
|
|
|
it("derives the length only when nothing else gives one", () => {
|
|
expect(resolveCompositionLengthSeconds({ ...base, derived: () => 2.5 })).toBe(2.5);
|
|
expect(resolveCompositionLengthSeconds({ ...base, fallback: 9, derived: () => 2.5 })).toBe(9);
|
|
});
|
|
});
|
|
|
|
// Server-side callers parse with linkedom, browsers with DOMParser: both must agree.
|
|
const parsers: Array<[string, (html: string) => Document]> = [
|
|
["linkedom", (html) => parseHTMLContent(html)],
|
|
["DOMParser", (html) => new DOMParser().parseFromString(html, "text/html")],
|
|
];
|
|
|
|
describe.each(parsers)("readStaticCompositionMeta (%s)", (_name, parse) => {
|
|
const meta = (html: string) => readStaticCompositionMeta(parse(html));
|
|
|
|
it("reads the data-root composition's declared length and size", () => {
|
|
const html =
|
|
`<div data-composition-id="card" data-width="800" data-height="600" data-duration="9"></div>` +
|
|
`<div data-composition-id="main" data-root="true" data-width="1080" data-height="1920" data-duration="4"></div>`;
|
|
expect(meta(html)).toEqual({ width: 1080, height: 1920, fps: 30, durationSeconds: 4 });
|
|
});
|
|
|
|
it("extends to a nested video's authored end at its absolute start", () => {
|
|
const html =
|
|
`<div data-composition-id="main"><div data-composition-id="scene" data-start="3">` +
|
|
`<video data-start="1" data-duration="4"></video></div></div>`;
|
|
expect(meta(html)?.durationSeconds).toBe(8);
|
|
});
|
|
|
|
it("extends to a sub-composition's declared end", () => {
|
|
const html = `<div data-composition-id="main"><div data-composition-id="scene" data-start="1" data-duration="5"></div></div>`;
|
|
expect(meta(html)?.durationSeconds).toBe(6);
|
|
});
|
|
|
|
it("derives the length from the clips, and is 0 while one is pending", () => {
|
|
expect(
|
|
meta(`<div data-composition-id="main"><div data-start="1" data-duration="3"></div></div>`)
|
|
?.durationSeconds,
|
|
).toBe(4);
|
|
const pending = `<div data-composition-id="main"><div data-start="0" data-duration="3"></div><video data-start="0"></video></div>`;
|
|
expect(meta(pending)?.durationSeconds).toBe(0);
|
|
});
|
|
|
|
it("is null without a composition", () => {
|
|
expect(meta("<div></div>")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("findRootCompositionElement", () => {
|
|
it("reads the document it is given, not the global one", () => {
|
|
const doc = parseHTMLContent(
|
|
`<div data-composition-id="a"></div><div data-composition-id="b" data-root="true"></div>`,
|
|
);
|
|
expect(findRootCompositionElement(doc)?.getAttribute("data-composition-id")).toBe("b");
|
|
});
|
|
});
|
|
|
|
describe("rules moved unchanged from the runtime", () => {
|
|
const resolverFor = (doc: Document) =>
|
|
createRuntimeStartTimeResolver({
|
|
timelineRegistry: {},
|
|
includeAuthoredTimingAttrs: true,
|
|
documentRef: doc,
|
|
});
|
|
|
|
it("counts only the root's own sub-compositions in the authored floor", () => {
|
|
const html =
|
|
`<div data-composition-id="main"><div data-composition-id="scene" data-start="1" data-duration="2">` +
|
|
`<div data-composition-id="inner" data-start="0" data-duration="50"></div></div></div>`;
|
|
expect(readStaticCompositionMeta(parseHTMLContent(html))?.durationSeconds).toBe(3);
|
|
});
|
|
|
|
it("names the clip missing its trim when its full media length sets the film's length", () => {
|
|
const doc = parseHTMLContent(
|
|
`<div data-composition-id="main">` +
|
|
`<video id="cut" data-start="0" data-duration="3" src="cut.mp4"></video>` +
|
|
`<video id="source" data-start="2" src="full-source.mp4"></video></div>`,
|
|
);
|
|
const natural: Record<string, number> = { cut: 3600, source: 5400 };
|
|
const media = {
|
|
mediaStart: (el: HTMLMediaElement) => Number(el.getAttribute("data-start")),
|
|
mediaDuration: (el: HTMLMediaElement) =>
|
|
el.hasAttribute("data-duration")
|
|
? Number(el.getAttribute("data-duration"))
|
|
: natural[el.id]!,
|
|
};
|
|
const longest = longestMediaWindow(doc, media);
|
|
expect(longest?.element.id).toBe("source");
|
|
expect(longest?.seconds).toBe(5402);
|
|
expect(
|
|
resolveCompositionLength({
|
|
declared: null,
|
|
timeline: () => 3,
|
|
floors: () => [longest?.seconds ?? null, 3],
|
|
fallback: 0,
|
|
derived: () => 0,
|
|
}),
|
|
).toEqual({ seconds: 5402, from: "floor", floorIndex: 0 });
|
|
});
|
|
|
|
it("skips a media clip of one frame or less", () => {
|
|
const doc = parseHTMLContent(
|
|
`<div data-composition-id="main"><video data-start="5"></video></div>`,
|
|
);
|
|
const tooShort = MIN_VALID_TIMELINE_DURATION_SECONDS;
|
|
expect(
|
|
resolveMediaWindowDurationSeconds(doc, {
|
|
mediaStart: () => 5,
|
|
mediaDuration: () => tooShort,
|
|
}),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("holds the derived length while a declared Lottie source has not registered", () => {
|
|
const doc = parseHTMLContent(
|
|
`<div data-composition-id="main"><div data-start="0" data-duration="3"></div>` +
|
|
`<div data-lottie-src="anim.json"></div></div>`,
|
|
);
|
|
const root = findRootCompositionElement(doc)!;
|
|
const result = resolveContentDerivedDuration(root, resolverFor(doc), {
|
|
unregisteredLottie: false,
|
|
});
|
|
expect(result).toMatchObject({ seconds: null, source: "unresolved" });
|
|
});
|
|
|
|
it("holds the derived length while a loaded Lottie library may still register animations", () => {
|
|
const doc = parseHTMLContent(
|
|
`<div data-composition-id="main"><div data-start="0" data-duration="3"></div></div>`,
|
|
);
|
|
const root = findRootCompositionElement(doc)!;
|
|
const result = resolveContentDerivedDuration(root, resolverFor(doc), {
|
|
unregisteredLottie: true,
|
|
});
|
|
expect(result).toMatchObject({ seconds: null, source: "unresolved" });
|
|
});
|
|
});
|