Open source CapCut alternative.
TypeScript
import { beforeEach, describe, expect, test } from "bun:test";
import { commitRow, groupTrimEdges, landOnRow, NEW_ROW_PX, partAround, resolveRow } from "./laneTracks";
import { runAiTool } from "./aiTools";
import { clipLen, useEditor } from "./store";
import { emptySubtitles } from "./types";
import type { AudioClip, VideoClip } from "./types";
/**
* One vertical model for every lane kind: the row under the pointer takes
* the item, a reach past either edge of the band opens a new row, an
* outermost row held alone offers nothing on its side, and a commit renumbers
* lanes contiguous. Video and audio go through the same functions; the only
* difference is which way the video stack counts its rows.
*/
let n = 0;
const vclip = (o: Partial<VideoClip>): VideoClip => ({
id: `c${++n}`,
assetId: "x",
track: 0,
start: 0,
in: 0,
out: 2,
muted: false,
...o,
});
const aclip = (o: Partial<AudioClip>): AudioClip => ({
id: `au${++n}`,
assetId: "x",
start: 0,
in: 0,
out: 2,
volume: 1,
...o,
});
const s = () => useEditor.getState();
const clipById = (id: string) => s().clips.find((c) => c.id === id)!;
const audioById = (id: string) => s().audioClips.find((c) => c.id === id)!;
beforeEach(() => {
useEditor.setState({
clips: [],
transitions: [],
audioClips: [],
overlays: [],
subtitles: emptySubtitles(),
selection: null,
multiSelection: [],
});
});
describe("resolveRow", () => {
// Three rows of 40px stacked from y=100.
const rows = [
{ top: 100, h: 40 },
{ top: 140, h: 40 },
{ top: 180, h: 40 },
];
const open = { top: true, bottom: true };
test("the row under the pointer takes the item", () => {
expect(resolveRow(rows, 110, null, open)).toBe(0);
expect(resolveRow(rows, 150, null, open)).toBe(1);
expect(resolveRow(rows, 219, null, open)).toBe(2);
});
test("a pointer between rows opens an insertion", () => {
const gapped = [
{ top: 100, h: 40 },
{ top: 146, h: 40 },
];
expect(resolveRow(gapped, 143, null, open)).toBe(0.5);
expect(resolveRow(gapped, 153, 0.5, open)).toBe(0.5);
expect(resolveRow(gapped, 160, 0.5, open)).toBe(1);
});
test("a new row opens only past the reach beyond either edge", () => {
expect(resolveRow(rows, 100 - NEW_ROW_PX + 1, null, open)).toBe(0);
expect(resolveRow(rows, 100 - NEW_ROW_PX - 1, null, open)).toBe(-1);
expect(resolveRow(rows, 220 + NEW_ROW_PX - 1, null, open)).toBe(2);
expect(resolveRow(rows, 220 + NEW_ROW_PX + 1, null, open)).toBe(3);
});
test("a held new row keeps its hold with no reach until the pointer returns to a row", () => {
expect(resolveRow(rows, 99, -1, open)).toBe(-1);
expect(resolveRow(rows, 101, -1, open)).toBe(0);
expect(resolveRow(rows, 221, 3, open)).toBe(3);
expect(resolveRow(rows, 219, 3, open)).toBe(2);
});
test("a closed edge offers no new row, however far the drag reaches", () => {
expect(resolveRow(rows, 0, null, { top: false, bottom: true })).toBe(0);
expect(resolveRow(rows, 400, null, { top: true, bottom: false })).toBe(2);
});
});
describe("partAround", () => {
const item = (id: string, start: number, len: number) => ({ view: { id, start, len, lane: 0 } });
const rest = (x: { view: { start: number } }) => x.view.start;
test("residents past the pointer slide right as one run", () => {
const a = item("a", 1, 2);
const b = item("b", 3, 2);
const part = partAround([a, b], 1.2, 1.2, 2, rest);
expect(part.slotStart).toBeCloseTo(1.2);
expect(part.clamped).toBe(false);
expect(part.at(a)).toBeCloseTo(3.2);
expect(part.at(b)).toBeCloseTo(5.2);
});
test("a resident whose midpoint sits before the pointer holds, and clamps the slot after it", () => {
const a = item("a", 0, 2);
const part = partAround([a], 1.5, 0.5, 2, rest);
expect(part.slotStart).toBeCloseTo(2);
expect(part.clamped).toBe(true);
expect(part.at(a)).toBe(0);
});
});
describe("commitRow on the video stack", () => {
test("the row past the top opens a new highest track", () => {
const spine = vclip({ track: 0 });
const layer = vclip({ track: 1 });
const mover = vclip({ track: 0, start: 2 });
useEditor.setState({ clips: [spine, layer, mover] });
commitRow("video", mover.id, -1);
expect(clipById(mover.id).track).toBe(2);
expect(clipById(layer.id).track).toBe(1);
expect(clipById(spine.id).track).toBe(0);
});
test("the row past the bottom makes the clip the new track 0 and renumbers the stack", () => {
const spine = vclip({ track: 0 });
const mover = vclip({ track: 1 });
const anchor = vclip({ track: 1, start: 3 });
useEditor.setState({ clips: [spine, mover, anchor] });
commitRow("video", mover.id, 2);
expect(clipById(mover.id).track).toBe(0);
expect(clipById(spine.id).track).toBe(1);
expect(clipById(anchor.id).track).toBe(2);
});
test("a row in the stack is the track drawn there", () => {
const spine = vclip({ track: 0 });
const layer = vclip({ track: 1 });
const mover = vclip({ track: 0, start: 2 });
useEditor.setState({ clips: [spine, layer, mover] });
commitRow("video", mover.id, 0);
expect(clipById(mover.id).track).toBe(1);
});
test("the only track-0 clip moving up grounds the stack", () => {
const mover = vclip({ track: 0 });
const resident = vclip({ track: 1, start: 3 });
useEditor.setState({ clips: [mover, resident] });
commitRow("video", mover.id, 0);
expect(clipById(mover.id).track).toBe(0);
expect(clipById(resident.id).track).toBe(0);
});
});
describe("landOnRow on the audio lanes", () => {
// What the store's add does with the lane it is handed: 0 is stored as none.
const land = (_row: number, lane: number) => {
const c = aclip({ start: 5, ...(lane > 0 ? { lane } : {}) });
useEditor.setState({ audioClips: [...s().audioClips, c] });
return c.id;
};
test("a row in use lands on that lane", () => {
const bed = aclip({});
const fx = aclip({ lane: 1 });
useEditor.setState({ audioClips: [bed, fx] });
let id = "";
landOnRow("audio", 1, (lane) => (id = land(1, lane)));
expect(audioById(id).lane).toBe(1);
expect(audioById(bed.id).lane).toBeUndefined();
});
test("the row past the top opens a new first lane, the way a drag does", () => {
const bed = aclip({});
const fx = aclip({ lane: 1 });
useEditor.setState({ audioClips: [bed, fx] });
let id = "";
landOnRow("audio", -1, (lane) => (id = land(-1, lane)));
expect(audioById(id).lane).toBeUndefined();
expect(audioById(bed.id).lane).toBe(1);
expect(audioById(fx.id).lane).toBe(2);
});
test("the row past the bottom opens a new last lane", () => {
const bed = aclip({});
useEditor.setState({ audioClips: [bed] });
let id = "";
landOnRow("audio", 1, (lane) => (id = land(1, lane)));
expect(audioById(id).lane).toBe(1);
expect(audioById(bed.id).lane).toBeUndefined();
});
test("an empty band lands on lane 0", () => {
useEditor.setState({ audioClips: [] });
let id = "";
landOnRow("audio", 0, (lane) => (id = land(0, lane)));
expect(audioById(id).lane).toBeUndefined();
});
});
describe("commitRow on the audio lanes", () => {
test("the row past the top opens a new first lane", () => {
const bed = aclip({});
const fx = aclip({ lane: 1 });
useEditor.setState({ audioClips: [bed, fx] });
commitRow("audio", fx.id, -1);
expect(audioById(fx.id).lane).toBeUndefined();
expect(audioById(bed.id).lane).toBe(1);
});
test("the row past the bottom opens a new last lane", () => {
const bed = aclip({});
const fx = aclip({ start: 3 });
useEditor.setState({ audioClips: [bed, fx] });
commitRow("audio", fx.id, 1);
expect(audioById(fx.id).lane).toBe(1);
expect(audioById(bed.id).lane).toBeUndefined();
});
test("an emptied lane collapses", () => {
const a = aclip({});
const b = aclip({ lane: 1 });
const c = aclip({ lane: 2 });
useEditor.setState({ audioClips: [a, b, c] });
commitRow("audio", b.id, 0);
expect(audioById(b.id).lane).toBeUndefined();
expect(audioById(c.id).lane).toBe(1);
});
});
describe("element rows use the shared drop and move placement", () => {
const add = {
text: (lane: number) => s().addOverlay({ at: 4, lane }),
shape: (lane: number) => s().addShape("rect", { at: 4, lane }),
sticker: (lane: number) => s().addSticker({ assetId: "sticker", at: 4, lane }),
effect: (lane: number) => s().addEffect("blur", { at: 4, lane }),
};
for (const [kind, place] of Object.entries(add)) {
for (const row of [-1, 0, 0.5, 1, 2]) {
test(`${kind} drops on row ${row} and undoes as one edit`, () => {
s().addOverlay({ at: 0, lane: 0 });
s().addShape("rect", { at: 0, lane: 1 });
const before = s().overlays;
landOnRow("overlay", row, place);
const landed = s().overlays.find((o) => o.id === s().selection?.id)!;
expect(landed.lane).toBe(Math.max(0, Math.ceil(row)));
expect(landed.start).toBe(4);
expect(s().overlays.find((o) => o.id === before[0].id)?.lane).toBe(row < 0 ? 1 : 0);
expect(s().overlays.find((o) => o.id === before[1].id)?.lane).toBe(row < 0 || row === 0.5 ? 2 : 1);
s().undo();
expect(s().overlays).toEqual(before);
});
}
test(`${kind} opens the first row of an empty band`, () => {
landOnRow("overlay", 0, place);
expect(s().overlays).toHaveLength(1);
expect(s().overlays[0].lane).toBe(0);
});
test(`${kind} can move between occupied rows and undo`, () => {
s().addOverlay({ at: 0, lane: 0 });
const a = s().selection!.id;
s().addShape("rect", { at: 0, lane: 1 });
const b = s().selection!.id;
place(0);
const id = s().selection!.id;
const before = s().overlays;
s().pushHistory();
commitRow("overlay", id, 0.5);
expect(s().overlays.find((o) => o.id === id)?.lane).toBe(1);
expect(s().overlays.find((o) => o.id === a)?.lane).toBe(0);
expect(s().overlays.find((o) => o.id === b)?.lane).toBe(2);
s().undo();
expect(s().overlays).toEqual(before);
});
test(`${kind} can move to a fresh row at either end`, () => {
s().addOverlay({ at: 0, lane: 0 });
place(0);
const id = s().selection!.id;
s().pushHistory();
commitRow("overlay", id, -1);
expect(s().overlays.find((o) => o.id === id)?.lane).toBe(0);
expect(s().overlays.find((o) => o.id !== id)?.lane).toBe(1);
commitRow("overlay", id, 2);
expect(s().overlays.find((o) => o.id === id)?.lane).toBe(1);
expect(s().overlays.find((o) => o.id !== id)?.lane).toBe(0);
});
}
});
describe("a cross-row move keeps the lane sound", () => {
test("landing among residents parts them and the commit takes the row", () => {
const mover = vclip({ track: 0, start: 0, out: 2 });
const anchor = vclip({ track: 0, start: 2, out: 2 });
const resident = vclip({ track: 1, start: 1, out: 2 });
useEditor.setState({ clips: [mover, anchor, resident] });
const st = s();
const view = (c: VideoClip) => ({ view: { id: c.id, start: c.start, len: clipLen(c), lane: c.track } });
const part = partAround([view(resident)], 1.2, 1.2, 2, (x) => x.view.start);
st.updateClipsTransient([
{ id: mover.id, patch: { start: part.slotStart } },
{ id: resident.id, patch: { start: part.at(view(resident)) } },
]);
commitRow("video", mover.id, 0);
expect(clipById(mover.id).track).toBe(1);
expect(clipById(mover.id).start).toBeCloseTo(1.2);
expect(clipById(resident.id).start).toBeCloseTo(3.2);
});
});
describe("groupTrimEdges", () => {
// Three right edges at 10, 12 and 8, each with its own room.
const edges = [
{ at: 10, lo: 6, hi: 14 },
{ at: 12, lo: 9, hi: 13 },
{ at: 8, lo: 4, hi: 20 },
];
test("every edge travels with the grabbed one", () => {
expect(groupTrimEdges(edges, 0, 2)).toEqual({ travel: 2, at: [12, 13, 10] });
});
test("the grabbed edge's own room holds the whole travel", () => {
expect(groupTrimEdges(edges, 0, 9)).toEqual({ travel: 4, at: [14, 13, 12] });
expect(groupTrimEdges(edges, 0, -9)).toEqual({ travel: -4, at: [6, 9, 4] });
});
test("a member out of room stops at its wall while the rest keep going", () => {
expect(groupTrimEdges(edges, 2, 5)).toEqual({ travel: 5, at: [14, 13, 13] });
});
});
for (const kind of ["video", "audio"] as const) {
test(`${kind} inserts between occupied rows through the shared resolver`, () => {
const a = kind === "video" ? vclip({ track: 0 }) : aclip({ lane: 0 });
const b = kind === "video" ? vclip({ track: 1 }) : aclip({ lane: 1 });
const mover = kind === "video" ? vclip({ track: 0, start: 3 }) : aclip({ lane: 0, start: 3 });
useEditor.setState(kind === "video" ? { clips: [a, b, mover] as VideoClip[] } : { audioClips: [a, b, mover] as AudioClip[] });
const row = resolveRow([{ top: 0, h: 40 }, { top: 40, h: 40 }], 40, null, { top: true, bottom: true });
commitRow(kind, mover.id, row);
const lane = (id: string) => kind === "video" ? clipById(id).track : audioById(id).lane ?? 0;
expect(lane(mover.id)).toBe(1);
expect(lane(a.id)).toBe(0);
expect(lane(b.id)).toBe(2);
});
}
test("chat inserts an element row through the drag coordinator and undoes it", async () => {
s().addOverlay({ at: 0, lane: 0 });
s().addShape("rect", { at: 0, lane: 1 });
s().addOverlay({ at: 5, lane: 0 });
const id = s().selection!.id;
const before = s().overlays;
await runAiTool("insert_item_row", { kind: "overlay", id, before_row: 1 });
expect(s().overlays.map((o) => o.lane)).toEqual([0, 2, 1]);
expect(s().overlays[2].start).toBe(5);
s().undo();
expect(s().overlays).toEqual(before);
});