OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
import { describe, expect, it, vi } from "vitest";
import type { Clip, Project, TextClip } from "@openreel/core";
import {
getLinkedCaptions,
moveLinkedCaptions,
trimLinkedCaptions,
type LinkedCaptionEditStore,
} from "./linked-caption-edit";
const source = {
id: "video-1",
trackId: "video-track",
startTime: 2,
duration: 8,
} as Clip;
const captions = [
{ id: "c1", trackId: "captions", startTime: 2, duration: 2, metadata: {} },
{ id: "c2", trackId: "captions", startTime: 8, duration: 2, metadata: {} },
{
id: "explicit",
trackId: "other-text",
startTime: 30,
duration: 2,
metadata: { captionSourceClipId: "video-1" },
},
] as TextClip[];
const project = {
timeline: {
tracks: [
{ id: "video-track", groupId: "g1" },
{ id: "captions", groupId: "g1" },
{ id: "other-text" },
],
},
} as Project;
function makeStore(): LinkedCaptionEditStore {
return {
project,
getAllTextClips: () => captions,
updateOverlayClipTiming: vi.fn(),
deleteTextClip: vi.fn(() => true),
};
}
describe("linked caption edits", () => {
it.each(["left", "right"])("keeps root-linked captions on the overlapping %s split piece", side => {
const left = { ...source, duration: 4, metadata: { splitSourceClipId: source.id } };
const right = { ...source, id: "piece", startTime: 6, duration: 4, metadata: { splitSourceClipId: source.id } };
const store = makeStore();
store.project = { ...project, timeline: { ...project.timeline, tracks: project.timeline.tracks.map(track => ({ ...track, clips: track.id === source.trackId ? [left, right] : [] })) } };
store.getAllTextClips = () => [
{ ...captions[0], metadata: { captionSourceClipId: source.id } },
{ ...captions[1], metadata: { captionSourceClipId: source.id } },
{ ...captions[1], id: "foreign", metadata: { captionSourceClipId: "unrelated" } },
];
expect(getLinkedCaptions(store, side === "left" ? left : right).map(clip => clip.id)).toEqual([side === "left" ? "c1" : "c2"]);
});
it("finds grouped overlapping captions and explicit source links", () => {
expect(getLinkedCaptions(makeStore(), source).map((clip) => clip.id)).toEqual([
"c1",
"c2",
"explicit",
]);
});
it("moves linked captions by the source clip delta", () => {
const store = makeStore();
moveLinkedCaptions(store, source, 5);
expect(store.updateOverlayClipTiming).toHaveBeenCalledWith("c1", {
startTime: 5,
});
expect(store.updateOverlayClipTiming).toHaveBeenCalledWith("c2", {
startTime: 11,
});
});
it("clips partial captions and removes captions outside a trim", () => {
const store = makeStore();
trimLinkedCaptions(store, source, 3, 8.5);
expect(store.updateOverlayClipTiming).toHaveBeenCalledWith("c1", {
startTime: 3,
duration: 1,
});
expect(store.updateOverlayClipTiming).toHaveBeenCalledWith("c2", {
startTime: 8,
duration: 0.5,
});
expect(store.deleteTextClip).toHaveBeenCalledWith("explicit");
});
it("preserves locked captions for both grouped and explicit source links", () => {
const store = makeStore();
store.project = {
...project,
timeline: {
...project.timeline,
tracks: project.timeline.tracks.map((track) => ({
...track,
locked: track.id !== source.trackId,
})),
},
};
expect(getLinkedCaptions(store, source)).toEqual([]);
expect(moveLinkedCaptions(store, source, 5)).toBe(0);
expect(trimLinkedCaptions(store, source, 3, 5)).toEqual({ updated: 0, removed: 0 });
expect(store.updateOverlayClipTiming).not.toHaveBeenCalled();
expect(store.deleteTextClip).not.toHaveBeenCalled();
});
});