// fallow-ignore-file code-duplication
import { parseHTML } from "linkedom";
import postcss from "postcss";
import { describe, expect, it } from "vitest";
import { readMediaOffsetSeconds } from "@hyperframes/parsers/media-duration";
import {
removeElementFromHtml,
splitElementInHtml,
relinkSplitHalvesInHtml,
unwrapElementsFromHtml,
wrapElementsInHtml,
} from "./sourceMutation.js";
describe("splitElementInHtml — hfId clone isolation", () => {
it("does not copy data-hf-id to the cloned second half", () => {
const source = `
`;
const { html, matched } = splitElementInHtml(source, { id: "clip1" }, 5, "clip2");
expect(matched).toBe(true);
const { document } = parseHTML(html);
const occurrences = (html.match(/data-hf-id="hf-abc123"/g) ?? []).length;
expect(occurrences).toBe(1);
expect(document.getElementById("clip2")?.getAttribute("data-hf-id")).toMatch(/^hf-/);
expect(document.getElementById("clip2")?.getAttribute("data-hf-id")).not.toBe("hf-abc123");
});
});
// A second split at the same playhead: the first one wrote the left half's end rounded to the millisecond (2.346 for a
// playhead at 2.3456), so the same cut lands a fraction inside that edge and its sliver rounded to data-duration="0".
describe("splitElementInHtml — a cut at an existing edge", () => {
const clip = (start: number, duration: number) =>
``;
const durations = (html: string) =>
[...parseHTML(html).document.querySelectorAll(".clip")].map((el) =>
Number(el.getAttribute("data-duration")),
);
it("leaves the file as it was when the cut is within the split margin of either edge", () => {
for (const at of [2.3456, 2.346, 1, 1.0004, 2.33]) {
const source = clip(1, 1.346);
const result = splitElementInHtml(source, { id: "a" }, at, "a-split");
expect(result.matched, `a cut at ${at}`).toBe(false);
expect(result.html, `a cut at ${at}`).toBe(source);
}
});
it("still cuts at the margin itself, and no half it writes is ever zero long", () => {
const result = splitElementInHtml(clip(1, 1.346), { id: "a" }, 1.03, "a-split");
expect(result.matched).toBe(true);
expect(durations(result.html)).toEqual([0.03, 1.316]);
});
});
describe("splitElementInHtml", () => {
const source = ``;
it("splits element at the given time", () => {
const result = splitElementInHtml(source, { id: "box" }, 3, "box-split");
expect(result.matched).toBe(true);
expect(result.html).toContain('data-duration="2"');
expect(result.html).toContain('id="box-split"');
expect(result.html).toContain('data-start="3"');
expect(result.html).toContain('data-duration="4"');
});
it("pins both halves to the caller-supplied track so an unauthored clip's split half can't drift to a new row", () => {
// #box has no data-track-index/data-layer — this is the common, unauthored
// case that relies on the runtime's positional-index fallback. Without an
// explicit stamp, splitting only the clone changes DOM sibling order and
// the runtime resolves the clone to a different fallback track.
const result = splitElementInHtml(source, { id: "box" }, 3, "box-split", {
start: 1,
duration: 6,
track: 2,
});
expect(result.matched).toBe(true);
expect(result.html.match(/data-track-index="2"/g)).toHaveLength(2);
});
it("canonicalizes legacy timing attributes on both split halves", () => {
const legacy = source.replace(
'data-start="1" data-duration="6"',
'data-start="1" data-end="7" data-layer="3"',
);
const result = splitElementInHtml(legacy, { id: "box" }, 3, "box-split");
expect(result.matched).toBe(true);
expect(result.html).not.toContain("data-end=");
expect(result.html).not.toContain("data-layer=");
expect(result.html.match(/data-track-index="3"/g)).toHaveLength(2);
expect(result.html).toContain('data-duration="2"');
expect(result.html).toContain('data-duration="4"');
});
it("duplicates CSS rules for the new element ID", () => {
const result = splitElementInHtml(source, { id: "box" }, 3, "box-split");
expect(result.html).toContain("#box-split");
expect(result.html).toContain("background: red");
const cssMatches = result.html.match(/#box-split\s*\{/g);
expect(cssMatches?.length).toBeGreaterThanOrEqual(1);
});
it("writes a new id into the stylesheet only as one escaped id selector", () => {
const result = splitElementInHtml(source, { id: "box" }, 3, "x{} body { display: none } #q");
const css = parseHTML(result.html).document.querySelector("style")!.textContent!;
const selectors = postcss.parse(css).nodes.map((rule) => (rule as postcss.Rule).selector);
expect(selectors).toEqual(["#box", String.raw`#x\{\}\ body\ \{\ display\:\ none\ \}\ \#q`]);
});
it("deduplicates IDs when the requested newId already exists", () => {
const withExisting = source.replace(
"",
'Existing
',
);
const result = splitElementInHtml(withExisting, { id: "box" }, 3, "box-split");
expect(result.matched).toBe(true);
expect(result.html).toContain('id="box-split-2"');
});
it("keeps clip class on the cloned element", () => {
const result = splitElementInHtml(source, { id: "box" }, 3, "box-split");
expect(result.html).toMatch(/id="box-split"[^>]*class="clip"/);
});
it("keeps every half of a split composition mount on the root it mounts", () => {
const composition = source.replace(
'id="box" class="clip"',
'id="box" class="clip" data-composition-id="headline" data-composition-src="headline.html"',
);
const first = splitElementInHtml(composition, { id: "box" }, 3, "box-split");
const second = splitElementInHtml(first.html, { id: "box-split" }, 4, "box-split-2");
const { document } = parseHTML(second.html);
const mounts = Array.from(document.querySelectorAll("[data-composition-src]"));
expect(mounts.map((element) => element.getAttribute("data-composition-id"))).toEqual([
"headline",
"headline",
"headline",
]);
expect(new Set(mounts.map((element) => element.id)).size).toBe(3);
});
it("keeps a split inline-template mount on its template", () => {
const composition = source
.replace('id="box" class="clip"', 'id="box" class="clip" data-composition-id="headline"')
.replace(
"Hi