// @vitest-environment happy-dom
import { describe, expect, it } from "vitest";
import {
canDetachAudio,
canLinkPair,
detachAudioInSource,
linkInSource,
findMergePair,
mergeAudioInSource,
pickDetachedAudioTrack,
removeElementInSource,
setLinkInSource,
sharesSourceFile,
} from "./mediaLinkEdits";
const automation = JSON.stringify({
version: 1,
lanes: [
{ target: "volume", points: [{ t: 0, v: 1 }] },
{ target: "rate", points: [{ t: 0, v: 2 }] },
],
}).replaceAll('"', """);
const VIDEO_TAG =
'';
const SOURCE = [
'
',
` ${VIDEO_TAG}`,
'
',
"
",
].join("\n");
const video = { id: "talk" };
function attrsOf(source: string, selector: string): Record {
const el = new DOMParser().parseFromString(source, "text/html").querySelector(selector);
if (!el) throw new Error(`missing ${selector}`);
return Object.fromEntries(Array.from(el.attributes, (a) => [a.name, a.value]));
}
describe("detachAudioInSource", () => {
const result = detachAudioInSource(SOURCE, { target: video, videoId: "talk", track: 2 });
it("inserts a linked audio with the video's file, window and sound", () => {
expect(result).not.toBeNull();
const audio = attrsOf(result?.html ?? "", "audio#talk-audio");
expect(audio).toMatchObject({
src: "assets/talk.mp4",
"data-start": "2",
"data-duration": "6",
"data-media-start": "1",
"data-playback-rate": "1",
"data-track-index": "2",
"data-link": result?.linkId,
"data-sync-origin": result?.linkId,
"data-volume": "0.5",
"data-fade-in": "0.3",
"data-fade-out": "0.2",
"data-fx-chain": "eq",
"data-fx-carve": "carve",
"data-audio-group": "voiceover",
});
expect(audio["data-automation"]).toContain("volume");
});
it("mutes the video, strips its sound attributes and links it", () => {
const after = attrsOf(result?.html ?? "", "video#talk");
expect(after).toHaveProperty("muted");
expect(after["data-link"]).toBe(result?.linkId);
expect(after["data-sync-origin"]).toBe(result?.linkId);
for (const gone of [
"data-has-audio",
"data-volume",
"data-fade-in",
"data-fade-out",
"data-fx-chain",
"data-fx-carve",
"data-audio-group",
]) {
expect(after).not.toHaveProperty(gone);
}
expect(after["data-automation"]).toContain("rate");
expect(after["data-automation"]).not.toContain("volume");
});
it("suffixes the audio id when taken", () => {
const taken = SOURCE.replace('id="music"', 'id="talk-audio"');
expect(detachAudioInSource(taken, { target: video, videoId: "talk", track: 2 })?.audioId).toBe(
"talk-audio-2",
);
});
});
describe("mergeAudioInSource", () => {
it("round-trips detach back to the original attributes", () => {
const detached = detachAudioInSource(SOURCE, { target: video, videoId: "talk", track: 2 });
const merged = mergeAudioInSource(detached?.html ?? "", {
videoTarget: video,
audioTarget: { id: "talk-audio" },
});
const original = attrsOf(SOURCE, "video#talk");
const roundTrip = attrsOf(merged ?? "", "video#talk");
const lanes = (raw: string | undefined) =>
JSON.parse(raw ?? "{}").lanes.map((lane: { target: string }) => lane.target);
expect(lanes(roundTrip["data-automation"]).sort()).toEqual(
lanes(original["data-automation"]).sort(),
);
delete original["data-automation"];
delete roundTrip["data-automation"];
expect(roundTrip).toEqual(original);
expect(merged).not.toContain('