// @vitest-environment jsdom import { ensureHfIds } from "@hyperframes/parsers"; import { describe, expect, it } from "vitest"; import { pasteElementHtml, pasteTimelineClips, resolveFreeTrack, type PlacedClip, } from "./useClipboard"; import { extendRootDurationInSource } from "../utils/rootDuration"; import type { TimelineClipboardClip } from "../utils/clipboardPayload"; import type { TimelineElement } from "../player"; const ROOT = '
'; const hfIdsIn = (html: string) => Array.from(html.matchAll(/data-hf-id=["']([^"']+)/g), (m) => m[1]); function clip(id: string, start: number, duration: number, track: number): TimelineClipboardClip { return { html: ``, start, duration, track, }; } function liveElement(track: number, start: number, duration: number): TimelineElement { return { id: `x-${track}`, tag: "audio", start, duration, track, authoredTrack: track }; } describe("resolveFreeTrack", () => { it("keeps the preferred track when nothing occupies it", () => { const track = resolveFreeTrack({ track: 5, start: 10, duration: 1 }, []); expect(track).toBe(5); }); it("bumps to a new track when the preferred one overlaps in time", () => { const taken: PlacedClip[] = [{ track: 5, start: 10.2, duration: 0.5 }]; const track = resolveFreeTrack({ track: 5, start: 10, duration: 1 }, taken); expect(track).toBe(6); }); it("keeps the preferred track when an occupant on it does not overlap in time", () => { const taken: PlacedClip[] = [{ track: 5, start: 50, duration: 1 }]; const track = resolveFreeTrack({ track: 5, start: 10, duration: 1 }, taken); expect(track).toBe(5); }); it("picks the next track above the highest taken one, not just +1 from preferred", () => { const taken: PlacedClip[] = [ { track: 5, start: 10, duration: 1 }, { track: 9, start: 30, duration: 1 }, ]; const track = resolveFreeTrack({ track: 5, start: 10.1, duration: 1 }, taken); expect(track).toBe(10); }); }); describe("pasteTimelineClips", () => { it("anchors a single clip's data-start at the anchor time and keeps its track", () => { const { content, ids } = pasteTimelineClips(ROOT, [clip("sfx-23", 21.61, 0.57, 132)], 40, []); expect(content).toContain('data-start="40"'); expect(content).toContain('data-track-index="132"'); expect(ids).toEqual(["sfx-23"]); }); it("preserves each clip's relative offset from the group's earliest clip", () => { const clips = [ clip("sfx-23", 21.61, 0.57, 132), clip("sfx-24", 21.73, 0.57, 133), clip("sfx-25", 21.84, 0.57, 134), ]; const { content } = pasteTimelineClips(ROOT, clips, 40, []); // Offsets from the earliest clip (21.61): 0, 0.12, 0.23 -> 40, 40.12, 40.23 expect(content).toContain('id="sfx-23" src="typenew.mp3" data-start="40"'); expect(content).toContain('id="sfx-24" src="typenew.mp3" data-start="40.12"'); expect(content).toContain('id="sfx-25" src="typenew.mp3" data-start="40.23"'); }); it("moves a clip to a new track when its original track is occupied at the target time", () => { const live = [liveElement(132, 40, 0.57)]; const { content } = pasteTimelineClips(ROOT, [clip("sfx-23", 21.61, 0.57, 132)], 40, live); expect(content).not.toContain('data-track-index="132"'); expect(content).toMatch(/data-track-index="133"/); }); it("gives a pasted clip a fresh data-hf-id, never the copied one", () => { const clipWithHfId: TimelineClipboardClip = { html: '', start: 7.8, duration: 0.57, track: 109, }; const { content } = pasteTimelineClips(ROOT, [clipWithHfId], 40, []); expect(content).not.toContain("hf-75o7"); expect(hfIdsIn(content)).toHaveLength(1); }); it("leaves nothing for the server to stamp, so the file undo recorded is what stays on disk", () => { const stamped = ensureHfIds( ROOT.replace("", `${clip("sfx-1", 1, 2, 0).html}${clip("sfx-2", 3, 2, 0).html}`), ); const copies = hfIdsIn(stamped).length; const original = (id: string) => ({ ...clip(id, 1, 2, 0), html: stamped.match(new RegExp(`]*id="${id}"[^>]*>`))![0], }); const { content } = pasteTimelineClips(stamped, [original("sfx-1"), original("sfx-2")], 11, []); const ids = hfIdsIn(content); expect(ids).toHaveLength(copies + 2); expect(new Set(ids).size).toBe(ids.length); expect(hfIdsIn(ensureHfIds(content))).toHaveLength(ids.length); }); it("remints a nested clip's descendants clear of their originals already in the file", () => { const nested = '
ab
'; const stamped = ensureHfIds(ROOT.replace("", `${nested}`)); const copied = stamped.match(/]*id="comp-1"[^>]*>.*?<\/div>/)![0]; const { content } = pasteTimelineClips( stamped, [{ html: copied, start: 1, duration: 2, track: 0 }], 11, [], ); const ids = hfIdsIn(content); expect(ids).toHaveLength(hfIdsIn(stamped).length + 3); expect(new Set(ids).size).toBe(ids.length); expect(hfIdsIn(ensureHfIds(content))).toHaveLength(ids.length); }); it("remints a nested descendant's data-hf-id too, not just the root's", () => { // A composition-instance clip's descendants carry their own hf-ids; a // clone must remint all of them, not just the root, or the descendants // collide with their originals the same way the root would. const nested: TimelineClipboardClip = { html: '
', start: 7.8, duration: 0.57, track: 109, }; const { content } = pasteTimelineClips(ROOT, [nested], 40, []); expect(content).not.toContain("hf-root"); expect(content).not.toContain("hf-child"); expect(hfIdsIn(content)).toHaveLength(2); }); it("does not strip text content that happens to contain the data-hf-id string", () => { const withText: TimelineClipboardClip = { html: '
talking about data-hf-id="hf-xyz" in my video
', start: 7.8, duration: 0.57, track: 109, }; const { content } = pasteTimelineClips(ROOT, [withText], 40, []); expect(content).toContain('talking about data-hf-id="hf-xyz" in my video'); }); it("bumps the second clip in a batch off the first clip's own new track when their offsets collide", () => { // Both clips start life a track apart but paste to overlapping times on // the SAME track, so the second must dodge the first's placement, not // just the live elements it was checked against. const clips = [clip("sfx-23", 21.61, 0.57, 132), clip("sfx-24", 21.7, 0.57, 132)]; const { content } = pasteTimelineClips(ROOT, clips, 40, []); expect(content).toContain( 'id="sfx-23" src="typenew.mp3" data-start="40" data-duration="0.57" data-track-index="132"', ); expect(content).toContain( 'id="sfx-24" src="typenew.mp3" data-start="40.09" data-duration="0.57" data-track-index="133"', ); }); it("dedupes an id that already exists in the target file", () => { const withExisting = ROOT.replace( "", '', ); const { content, ids } = pasteTimelineClips( withExisting, [clip("sfx-23", 21.61, 0.57, 132)], 40, [], ); expect(ids[0]).not.toBe("sfx-23"); expect(ids[0]).toMatch(/^sfx-23-\d+$/); expect(content).toContain(`id="${ids[0]}"`); }); it("remints a single-quoted data-hf-id, not just a double-quoted one", () => { const singleQuoted: TimelineClipboardClip = { html: '', start: 7.8, duration: 0.57, track: 109, }; const { content } = pasteTimelineClips(ROOT, [singleQuoted], 40, []); expect(content).not.toContain("hf-75o7"); expect(hfIdsIn(content)).toHaveLength(1); }); it("remints data-hf-id even when an earlier attribute value contains a literal >", () => { // A `>` inside title's value would close a bracket-scoped tag regex // early, leaving data-hf-id (which comes after it) outside the matched // span and unstripped. A real parser doesn't have this failure mode. const gtInAttr: TimelineClipboardClip = { html: '
', start: 7.8, duration: 0.57, track: 109, }; const { content } = pasteTimelineClips(ROOT, [gtInAttr], 40, []); expect(content).not.toContain("hf-xyz"); expect(hfIdsIn(content)).toHaveLength(1); }); it("refuses bare text with no element root", () => { const noRoot: TimelineClipboardClip = { html: 'text mentioning data-hf-id="hf-leak" with no tag at all', start: 0, duration: 1, track: 0, }; expect(() => pasteTimelineClips(ROOT, [noRoot], 0, [])).toThrow(/^Couldn't paste/); }); it("gives a tag a document would hoist out of body (e.g. title) a fresh data-hf-id, never the copied one", () => { // Only an in-file copy gets that far: from anywhere else a is refused. const hoisted: TimelineClipboardClip = { html: '<title data-hf-id="hf-leak">x', readFrom: "index.html", start: 0, duration: 1, track: 0, }; const { content } = pasteTimelineClips(ROOT, [hoisted], 0, [], true, "index.html"); expect(content).not.toContain("hf-leak"); expect(() => pasteTimelineClips(ROOT, [{ ...hoisted, readFrom: null }], 0, [])).toThrow( /^Couldn't paste/, ); }); it("captures the root's own id, not a data-id that happens to precede it", () => { const withDataId: TimelineClipboardClip = { html: '', start: 21.61, duration: 0.57, track: 132, }; const { ids } = pasteTimelineClips(ROOT, [withDataId], 40, []); expect(ids).toEqual(["sfx-23"]); }); it("writes a whole-number track when the copied or the live clips' tracks are not numbers", () => { const textTrack = '0" onload="x' as unknown as number; const live = [ { ...liveElement(5, 0, 1), authoredTrack: "$' onclick=\"y" as unknown as number }, ]; for (const saved of [false, true]) { const copied = { ...clip("sfx-23", 21.61, 0.57, 132), track: textTrack, saved }; const { content } = pasteTimelineClips(ROOT, [copied], 40, live); expect(content).toMatch(/