// @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(``))![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 = '