Skip to content

Open source CapCut alternative.

TypeScript
11.3 KB
Raw Blame
import { describe, expect, test } from "bun:test";
import { CLIP_EFFECT_IDS, codeScale, EFFECT_IDS, effectFilterLines, effectPreviewState, glitchHitAt } from "./effects";

// The bundled engine ffmpeg is LGPL: a recipe reaching for a GPL-only filter
// renders on a dev machine (Homebrew ffmpeg) and fails in the shipped app.
// `eq` is NOT here: it is GPL-licensed and absent from the shipped binary,
// which a dev machine's Homebrew ffmpeg (a GPL build) hides.
const LGPL_FILTERS = new Set([
  "noise",
  "rgbashift",
  "gblur",
  "vignette",
  "scale",
  "crop",
  "split",
  "overlay",
  // The graded looks are effects too, and their chains reach further.
  "format",
  "blend",
  "lutyuv",
  "colorchannelmixer",
  "colorbalance",
  "curves",
  "hue",
  "huesaturation",
  "vibrance",
  "unsharp",
  "colortemperature",
  "geq",
]);

const filterNamesOf = (lines: string[]): string[] => {
  const names: string[] = [];
  for (const line of lines) {
    // Strip labels and quoted expressions, then read each chain step's name.
    const bare = line.replace(/\[[^\]]*\]/g, "").replace(/'[^']*'/g, "''");
    for (const step of bare.split(",")) {
      const name = step.split("=")[0].trim();
      if (name) names.push(name);
    }
  }
  return names;
};

describe("effect recipes", () => {
  test("every clip effect is a picture effect with both recipes", () => {
    for (const id of CLIP_EFFECT_IDS) {
      expect((EFFECT_IDS as string[]).includes(id)).toBe(true);
      expect(effectFilterLines("in", "out", id, 0.5, 0, 2, 540, 960, "t")).not.toBe(null);
    }
  });

  test("a negative flips every color whatever the amount", () => {
    expect(effectPreviewState("negative", 0.1, 3).cssFilter).toBe("invert(1)");
    expect(effectPreviewState("negative", 1, 0).cssFilter).toBe("invert(1)");
  });

  test("a negative with a rate strobes: flipped for the first half of each pulse", () => {
    // Rate 15 at 30 fps: every other frame flipped, the picture between.
    const at = (frame: number) => effectPreviewState("negative", 0.5, frame / 30, undefined, undefined, 2, { rate: 15 }).cssFilter;
    expect([0, 1, 2, 3, 4].map(at)).toEqual(["invert(1)", "", "invert(1)", "", "invert(1)"]);
    // The export gates its flip by the same pulse.
    const lines = effectFilterLines("in", "out", "negative", 0.5, 1, 2, 540, 960, "t", undefined, undefined, undefined, { rate: 15 })!;
    expect(lines.join(";")).toMatch(/enable='[^']*lt\(mod\(\(t-1\)\*15\+/);
  });

  test("a color cycle turns the hue at the amount's pace, from its own start", () => {
    // Amount 0.5 is 0.8 turns a second: a quarter second in is 72 degrees.
    expect(effectPreviewState("huecycle", 0.5, 0).cssFilter).toBe("hue-rotate(0deg)");
    expect(effectPreviewState("huecycle", 0.5, 0.25).cssFilter).toBe("hue-rotate(72deg)");
    // A faster amount turns further over the same time.
    const deg = (k: number) => Number(effectPreviewState("huecycle", k, 0.1).cssFilter.match(/[\d.]+/)![0]);
    expect(deg(1)).toBeGreaterThan(deg(0.2));
    // The export turns on the same clock, measured from the element's start.
    expect(effectFilterLines("in", "out", "huecycle", 0.5, 2, 5, 1080, 1920, "t")![0]).toContain("hue=H='2*PI*0.8*(t-2)'");
  });

  test("every effect exports through the LGPL-safe filter set", () => {
    for (const id of EFFECT_IDS) {
      const lines = effectFilterLines("in", "out", id, 0.7, 1, 4, 1080, 1920, "t");
      expect(lines).not.toBe(null);
      for (const name of filterNamesOf(lines!)) {
        expect(LGPL_FILTERS.has(name)).toBe(true);
      }
    }
  });

  test("fragments are time-gated to their window", () => {
    for (const id of EFFECT_IDS) {
      const lines = effectFilterLines("in", "out", id, 0.5, 2.5, 6, 1080, 1920, "t")!;
      const joined = lines.join("\n");
      expect(joined.includes("gte(t,2.500000)") || joined.includes("sin(t*")).toBe(true);
    }
  });

  test("the 8-bit code values in geq scale to a 10-bit composite", () => {
    expect(codeScale(8)).toBe(1);
    expect(codeScale(undefined)).toBe(1);
    expect(codeScale(10)).toBeCloseTo(1023 / 255, 9);
    const flash8 = effectFilterLines("in", "out", "flash", 1, 0, 1, 1920, 1080, "t")![0];
    const flash10 = effectFilterLines("in", "out", "flash", 1, 0, 1, 1920, 1080, "t", undefined, undefined, {
      pixFmt: "yuv420p10le",
      overlay: ":format=yuv420p10",
      depth: 10,
    })![0];
    expect(flash8).toContain("min(235,");
    expect(flash8).toContain("+200*exp(");
    expect(flash10).toContain("min(942.765,");
    expect(flash10).toContain("+802.353*exp(");
    const leak10 = effectFilterLines("in", "out", "lightleak", 1, 0, 1, 1920, 1080, "t", undefined, undefined, {
      pixFmt: "yuv420p10le",
      overlay: ":format=yuv420p10",
      depth: 10,
    })![0];
    expect(leak10).toContain("(942.765-lum(X,Y))");
    expect(leak10).toContain("cb(X,Y)-160.471*");
    expect(leak10).toContain("cr(X,Y)+104.306*");
    // A look effect hands the depth on to its chain.
    const hal10 = effectFilterLines("in", "out", "halation", 1, 0, 1, 1920, 1080, "t", undefined, undefined, {
      pixFmt: "yuv420p10le",
      overlay: ":format=yuv420p10",
      depth: 10,
    })!.join(";");
    expect(hal10).toContain("clip((val-641.882)*3,0,1023)");
  });

  test("unknown ids return null instead of breaking the graph", () => {
    expect(effectFilterLines("in", "out", "nope", 1, 0, 1, 1080, 1920, "t")).toBe(null);
  });

  test("flash decays on the element clock, per frame", () => {
    // geq's `T` is the frame's own time, so the decay renders fresh each
    // frame; the element-local offset keeps the peak at the element start.
    const flash = effectFilterLines("in", "out", "flash", 1, 2.5, 6, 1920, 1080, "t")![0];
    expect(flash.includes("exp(")).toBe(true);
    expect(flash.includes("(T-2.5)")).toBe(true);
  });

  test("shake reads the same clock and travel as its canvas twin", () => {
    const start = 2.5;
    const lines = effectFilterLines("in", "out", "shake", 1, start, 6, 1920, 1080, "t")!;
    const crop = lines.find((l) => l.includes("crop="))!;
    // Element-local time, so the jitter lands on the frames the preview showed.
    expect(crop.includes(`sin((t-${start})*33)`)).toBe(true);
    expect(crop.includes("sin(t*33)")).toBe(false);
    // …and the same travel: 22 design px at a 1080 short side.
    const amp = Number(/\+([\d.]+)\*sin/.exec(crop)![1]);
    const peak = Math.max(
      ...Array.from({ length: 400 }, (_, i) => effectPreviewState("shake", 1, i / 400).dx ?? 0)
    );
    expect(Math.abs(amp - peak)).toBeLessThan(0.5);
  });

  test("a zoom lands the export on the point the preview holds", () => {
    const focus = { x: 0.25, y: 0.8 };
    const st = effectPreviewState("zoom", 1, 9, focus, 0);
    expect(st.origin).toEqual(focus);
    // Cut straight in: the copy is scaled once and cropped back to frame size.
    const lines = effectFilterLines("in", "out", "zoom", 1, 0, 4, 1920, 1080, "t", focus, 0)!;
    const line = lines.find((l) => l.includes("crop="))!;
    const [zw, zh] = /scale=(\d+):(\d+)/.exec(line)!.slice(1).map(Number);
    const [cx, cy] = /crop=1920:1080:(\d+):(\d+)/.exec(line)!.slice(1).map(Number);
    expect(zw / 1920).toBeCloseTo(st.zoom!, 2);
    // The crop sits the same fraction into the overscaled picture as the point
    // sits into the frame, which is what holds it still.
    expect(cx / (zw - 1920)).toBeCloseTo(focus.x, 2);
    expect(cy / (zh - 1080)).toBeCloseTo(focus.y, 2);
  });

  test("a ramped zoom holds its focus while the picture grows into it", () => {
    const focus = { x: 0.25, y: 0.8 };
    const lines = effectFilterLines("in", "out", "zoom", 1, 2.5, 6, 1920, 1080, "t", focus, 0.8)!;
    const over = lines.find((l) => l.includes("overlay="))!;
    // The copy is placed by its live size, offset by the focus fractions, so
    // the point stays put through the whole ramp.
    expect(over.includes("x='-(w-W)*0.25'")).toBe(true);
    expect(over.includes("y='-(h-H)*0.8'")).toBe(true);
    // Element-local clock, so the ramp starts where the element does.
    expect(lines.join("\n").includes("(t-2.5)/0.8")).toBe(true);
  });

  test("design px scale with the short side, so a vertical frame matches", () => {
    const wide = effectFilterLines("in", "out", "blur", 1, 0, 4, 1920, 1080, "t")![0];
    const tall = effectFilterLines("in", "out", "blur", 1, 0, 4, 1080, 1920, "t")![0];
    expect(/sigma=([\d.]+)/.exec(wide)![1]).toBe(/sigma=([\d.]+)/.exec(tall)![1]);
  });

  test("preview state is deterministic in local time", () => {
    for (const id of EFFECT_IDS) {
      const a = effectPreviewState(id, 0.6, 1.234);
      const b = effectPreviewState(id, 0.6, 1.234);
      expect(a).toEqual(b);
    }
  });

  test("a glitch breaks clean footage with runs of every kind, more at a higher amount", () => {
    const hits = (k: number) => Array.from({ length: 300 }, (_, i) => glitchHitAt(i, k));
    const count = (k: number) => hits(k).filter(Boolean).length;
    // 300 steps is 20 seconds: plenty of hits, with clean footage between.
    expect(count(0.1)).toBeGreaterThan(30);
    expect(count(1)).toBeGreaterThan(count(0.1));
    expect(count(1)).toBeLessThan(220);
    const kinds = new Set(hits(1).map((h) => h?.kind).filter(Boolean));
    expect([...kinds].sort()).toEqual(["blowout", "smear", "split", "tint"]);
  });

  test("a glitch's split holds its offset for the run while a smear moves its row", () => {
    const all = Array.from({ length: 600 }, (_, i) => glitchHitAt(i, 1));
    const held = all.findIndex((h, i) => h?.kind === "split" && all[i + 1]?.kind === "split");
    expect(held).toBeGreaterThanOrEqual(0);
    expect(all[held + 1]!.shift).toBe(all[held]!.shift);
    const smear = all.findIndex((h, i) => h?.kind === "smear" && all[i + 1]?.kind === "smear");
    expect(smear).toBeGreaterThanOrEqual(0);
    expect(all[smear + 1]!.row).not.toBe(all[smear]!.row);
  });

  test("a glitch's export hits the frames its preview does", () => {
    // Every frame of a 30fps window: the preview's state and the export's
    // gates must agree on which kind of hit plays, frame-edge times included.
    const start = 2.5;
    const lines = effectFilterLines("in", "out", "glitch", 0.6, start, 6.5, 1080, 1920, "t")!.join("\n");
    const kinds = new Set<string>();
    for (let n = 0; n < 120; n++) {
      const t = start + n / 30;
      const st = effectPreviewState("glitch", 0.6, t - start);
      const kind = st.stretch ? "smear" : st.ghost ? "split" : st.zoom ? "tint" : st.cssFilter ? "blowout" : null;
      const gate = (label: string) => {
        const m = new RegExp(`\\[${label}\\][^\\n]*?enable='([^']*)'`).exec(lines)!;
        return [...m[1].matchAll(/gte\(t,([\d.]+)\)\*lt\(t,([\d.]+)\)/g)]
          .slice(1)
          .some((w) => t >= Number(w[1]) && t < Number(w[2]));
      };
      const gated = gate("gsbt") ? "smear" : gate("gtbt") ? "tint" : gate("gpbt") ? "split" : gate("gpo2t") ? "blowout" : null;
      expect(gated).toBe(kind);
      if (kind) kinds.add(kind);
    }
    expect(kinds.size).toBeGreaterThanOrEqual(3);
  });

  test("flash decays from its start and shake stays bounded", () => {
    const early = effectPreviewState("flash", 1, 0.05).flash!;
    const late = effectPreviewState("flash", 1, 0.5).flash!;
    expect(early).toBeGreaterThan(late);
    const s = effectPreviewState("shake", 1, 0.37);
    expect(Math.abs(s.dx ?? 0)).toBeLessThanOrEqual(22);
    expect(Math.abs(s.dy ?? 0)).toBeLessThanOrEqual(22);
  });
});