Open source CapCut alternative.
TypeScript
import { afterEach, expect, mock, test } from "bun:test";
import { Prisma } from "@/generated/prisma/client";
import { stubModule } from "@/lib/testing/stubModule";
// The account the charge point weighs every object against: free tier, no Pro.
const prisma = {
cutRenderJob: { findMany: mock(async (args: unknown): Promise<QueueRow[]> => { void args; return []; }) },
// No overrides: the plan terms read their defaults.
settingOverride: { findUnique: mock(async () => ({ value: { maxAttempts: 3 } })), findMany: mock(async () => []) },
user: { findUnique: mock(async () => ({ superUser: false })) },
proSubscription: { findUnique: mock(async () => null) },
$transaction: mock(async (
run: (tx: Prisma.TransactionClient) => Promise<unknown>,
options: { isolationLevel: string },
): Promise<unknown> => {
void run; void options;
throw new Error("Unexpected transaction");
}) };
await stubModule<typeof import("@/lib/prisma")>("@/lib/prisma", import.meta.url, { prisma: prisma as never });
const { claimNextJob, registerObject, unregisterObjects } = await import("./db");
const { FREE_STORAGE_BYTES } = await import("../server/cloud/limits");
const { STORAGE_FULL } = await import("../lib/operationFailure");
const conflict = () => new Prisma.PrismaClientKnownRequestError("Write conflict", {
code: "P2034", clientVersion: "test",
});
const object = {
id: "object", userId: "user", projectId: "project", r2Key: "cut/user/file.mp4",
fileName: "file.mp4", mime: "video/mp4", bytes: BigInt(40), kind: "export",
uploadState: "complete", quotaExempt: false,
};
const input = { ...object, bytes: 100 };
const restores: (() => void)[] = [];
afterEach(() => { for (const restore of restores.splice(0).reverse()) restore(); });
function harness(failures = 1, error: Error = conflict()) {
let stored: typeof object | null = { ...object };
let usage = BigInt(140); // Includes another 100 bytes belonging to the account.
let attempts = 0;
const transaction = prisma.$transaction.mockImplementation((async (
run: (tx: Prisma.TransactionClient) => Promise<unknown>,
) => {
attempts++;
let pending = stored ? { ...stored } : null;
let pendingUsage = usage;
const tx = {
cutMediaObject: {
findUnique: async () => pending,
findMany: async () => pending ? [pending] : [],
upsert: async ({ update }: { update: Partial<typeof object> }) => {
pending = { ...object, ...pending, ...update };
return pending;
},
deleteMany: async () => { pending = null; return { count: 1 }; },
},
cutStorageUsage: {
findUnique: async () => ({ userId: "user", bytes: pendingUsage }),
upsert: async () => ({ userId: "user", bytes: pendingUsage }),
updateMany: async ({ where, data }: {
where: { bytes: bigint }; data: { bytes: { increment: bigint } };
}) => {
if (where.bytes !== pendingUsage) return { count: 0 };
pendingUsage += data.bytes.increment;
return { count: 1 };
},
},
};
const result = await run(tx as unknown as Prisma.TransactionClient);
if (attempts <= failures) {
// The conflicting writer commits a different object size. The retry
// must reread it and recompute the delta after this attempt rolls back.
if (stored) { stored = { ...stored, bytes: stored.bytes + BigInt(10) }; usage += BigInt(10); }
throw error;
}
stored = pending;
usage = pendingUsage;
return result;
}));
restores.push(() => transaction.mockRestore());
return { transaction, state: () => ({ stored, usage, attempts }) };
}
test("registration retries the whole transaction and charges the fresh size delta once", async () => {
const h = harness();
expect(await registerObject(input)).toBe("object");
expect(h.state()).toMatchObject({ attempts: 2, usage: BigInt(200), stored: { bytes: BigInt(100) } });
for (const call of h.transaction.mock.calls) expect(call[1]).toEqual({ isolationLevel: "Serializable" });
});
test("unregistration retries and refunds the freshly read bytes once", async () => {
const h = harness();
await unregisterObjects("user", [object.r2Key]);
expect(h.state()).toEqual({ attempts: 2, usage: BigInt(100), stored: null });
});
test("conflicts stop at the configured attempt limit", async () => {
const error = conflict();
const h = harness(10, error);
await expect(registerObject(input)).rejects.toBe(error);
expect(h.state().attempts).toBe(3);
});
test("other failures propagate without retrying", async () => {
const error = new Error("Database unavailable");
const h = harness(1, error);
await expect(registerObject(input)).rejects.toBe(error);
expect(h.state().attempts).toBe(1);
});
test("an object that would break the account's storage ceiling is refused, uncharged", async () => {
const h = harness(0);
await expect(registerObject({ ...input, bytes: FREE_STORAGE_BYTES * 2 })).rejects.toThrow(STORAGE_FULL);
expect(h.state()).toMatchObject({ usage: BigInt(140), stored: { bytes: BigInt(40) } });
});
test("an export renders into the margin past the account's quota", async () => {
const h = harness(0);
expect(await registerObject({ ...input, bytes: Math.floor(FREE_STORAGE_BYTES * 1.1) })).toBe("object");
expect(h.state().stored).toMatchObject({ bytes: BigInt(Math.floor(FREE_STORAGE_BYTES * 1.1)) });
});
test("the margin is the export's alone: the same bytes coming in meet the quota", async () => {
const h = harness(0);
await expect(
registerObject({ ...input, kind: "media", bytes: Math.floor(FREE_STORAGE_BYTES * 1.1) })
).rejects.toThrow(STORAGE_FULL);
expect(h.state()).toMatchObject({ usage: BigInt(140), stored: { bytes: BigInt(40) } });
});
test("a re-registration that shrinks the object is charged its delta", async () => {
const h = harness(0);
expect(await registerObject({ ...input, bytes: 10 })).toBe("object");
expect(h.state()).toMatchObject({ usage: BigInt(110), stored: { bytes: BigInt(10) } });
});
type QueueRow = {
id: string; userId: string; projectId: string; kind: string; state: string;
spec: null; outName: null;
};
function queueHarness(kinds: string[]) {
const rows: QueueRow[] = kinds.map((kind, i) => ({
id: `job-${i}`, userId: "user", projectId: "project", kind, state: "queued", spec: null, outName: null,
}));
const findMany = prisma.cutRenderJob.findMany.mockImplementation(async (args: unknown) => {
const { where, take } = args as { where: { state: string; kind: { in?: string[]; notIn?: string[] } }; take: number };
return rows.filter((row) => row.state === where.state
&& (!where.kind.in || where.kind.in.includes(row.kind))
&& (!where.kind.notIn || !where.kind.notIn.includes(row.kind))).slice(0, take);
});
const transaction = prisma.$transaction.mockImplementation(async (run: (tx: Prisma.TransactionClient) => Promise<unknown>) => {
const tx = { cutRenderJob: {
findFirst: async ({ where }: { where: { kind: string; state: string } }) => rows.find((row) => row.kind === where.kind && row.state === where.state) ?? null,
updateMany: async ({ where, data }: { where: { id: string; state: string }; data: { state: string } }) => {
const row = rows.find((row) => row.id === where.id && row.state === where.state);
if (!row) return { count: 0 };
row.state = data.state;
return { count: 1 };
},
findUnique: async ({ where }: { where: { id: string } }) => rows.find((row) => row.id === where.id) ?? null,
} };
return run(tx as unknown as Prisma.TransactionClient);
});
restores.push(() => { findMany.mockRestore(); transaction.mockRestore(); });
return rows;
}
test("the worker leaves editor command batches queued, even when they fill the oldest page", async () => {
const rows = queueHarness([...Array<string>(12).fill("commands"), "preview", "card", "hls"]);
for (const kind of ["preview", "card", "hls"]) expect((await claimNextJob())?.kind).toBe(kind);
expect(await claimNextJob()).toBeNull();
expect(rows.filter((row) => row.kind === "commands").every((row) => row.state === "queued")).toBe(true);
});
test("watched jobs precede background work and unsupported jobs stay queued", async () => {
const rows = queueHarness(["future_kind", "preview", "commands", "export", "import_url", "convert", "agent_turn"]);
for (const kind of ["export", "import_url", "convert", "agent_turn", "preview"]) {
expect((await claimNextJob())?.kind).toBe(kind);
}
expect(await claimNextJob()).toBeNull();
expect(rows.slice(0, 3).filter((row) => row.kind !== "preview").every((row) => row.state === "queued")).toBe(true);
});
test("a running project preview prevents a second preview claim while other work proceeds", async () => {
const rows = queueHarness(["preview", "preview", "card"]);
expect((await claimNextJob())?.id).toBe(rows[0].id);
expect((await claimNextJob())?.kind).toBe("card");
expect(await claimNextJob()).toBeNull();
expect(rows[1].state).toBe("queued");
});
test("an export waits behind no proxy: proxies take the tier after the watched work", async () => {
// Opening a cloud project queues a proxy for every heavy clip; an export
// asked for a moment later used to wait behind all of them.
queueHarness([...Array<string>(12).fill("proxy"), "export", "preview"]);
expect((await claimNextJob())?.kind).toBe("export");
expect((await claimNextJob())?.kind).toBe("proxy");
});