Skip to content

Open source CapCut alternative.

TypeScript
24.3 KB
Raw Blame
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdtemp, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { chromium } from "playwright";
import { widgetHtml } from "../src/clients/chatgpt/widget.generated";

const root = path.resolve(import.meta.dir, "..");
const scratch = await mkdtemp(path.join(os.tmpdir(), "donkey-chatgpt-widget-"));
const video = path.join(scratch, "preview.mp4");
execFileSync("ffmpeg", ["-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "color=c=blue:s=640x360:r=24", "-t", "2", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-movflags", "+faststart", video]);
const downloadBuild = await Bun.build({ entrypoints: [path.join(root, "src/cut/lib/download.ts")], target: "browser" });
assert.ok(downloadBuild.success, "download helper builds");
const downloadScript = await downloadBuild.outputs[0].text();
const sessionBuild = await Bun.build({ entrypoints: [path.join(root, "scripts/lib/chatgpt-session-fixture.tsx")], target: "browser" });
assert.ok(sessionBuild.success, "session recovery component builds");
const sessionScript = await sessionBuild.outputs[0].text();
const downloadRequests: { method: string; cookie: string | null; query: string }[] = [];
const mediaRequests: string[] = [];
const mediaServer = Bun.serve({ port: 0, fetch(request) {
  mediaRequests.push(request.method);
  return new Response(null, { headers: { "Access-Control-Allow-Origin": "*", "Content-Disposition": 'attachment; filename="export.mp4"' } });
} });
const signedDownload = `http://127.0.0.1:${mediaServer.port}/export.mp4?e=fixture&s=signed&d=export.mp4`;
const server = Bun.serve({ port: 0, fetch(request) {
  const url = new URL(request.url);
  if (url.pathname === "/widget") return new Response(widgetHtml(url.origin), { headers: { "Content-Type": "text/html" } });
  if (url.pathname === "/session.js") return new Response(sessionScript, { headers: { "Content-Type": "text/javascript" } });
  if (url.pathname === "/download.js") return new Response(downloadScript, { headers: { "Content-Type": "text/javascript" } });
  if (url.pathname.startsWith("/api/cut-cloud/")) {
    downloadRequests.push({ method: request.method, cookie: request.headers.get("cookie"), query: url.search });
    if (!request.headers.get("cookie")?.includes("editor-session=fixture")) return new Response(null, { status: 401 });
    if (url.pathname.endsWith("/missing.mp4")) return new Response(null, { status: 404 });
    return Response.redirect(signedDownload);
  }
  if (url.pathname === "/preview.mp4") return new Response(Bun.file(video));
  if (url.pathname === "/embed" && url.searchParams.get("code") === "signed-out") return new Response('<!doctype html><div id="session"></div><script type="module" src="/session.js"></script>', { headers: { "Content-Type": "text/html" } });
  if (url.pathname === "/embed") return new Response(`<!doctype html><title>editor</title><p id="editor">Editor for ${url.searchParams.get("project")} via ${url.searchParams.get("code")}</p><p id="inset"></p><p id="mode"></p><p id="theme"></p><button id="full" onclick="parent.postMessage({ type: 'donkeycut:fullscreen' }, '*')">Fullscreen</button><script>addEventListener('message', (e) => { if (e.source === parent && e.data.type === 'donkeycut:host') { document.getElementById('inset').textContent = String(e.data.insetBottom); document.getElementById('mode').textContent = e.data.displayMode; document.getElementById('theme').textContent = e.data.theme; } }); parent.postMessage({ type: 'donkeycut:host?' }, '*'); setTimeout(() => parent.postMessage({ type: 'donkeycut:ready' }, '*'), parent.parent.readyMs);</script><script type="module">import { downloadFromUrl, downloadFile } from '/download.js'; window.downloadFromUrl = downloadFromUrl; window.downloadFile = downloadFile;</script>`, { headers: { "Content-Type": "text/html", "Set-Cookie": "editor-session=fixture; Path=/api/cut-cloud/; HttpOnly; SameSite=Lax" } });
  if (url.pathname.startsWith("/clients/chatgpt/")) return new Response(Bun.file(path.join(root, "public/clients/chatgpt", path.basename(url.pathname))));
  return new Response(hostHtml, { headers: { "Content-Type": "text/html" } });
} });
const hostHtml = `<!doctype html><html><body><iframe title="Donkey Cut preview" src="/widget" style="width:700px;height:700px;border:0"></iframe><script>
const frame = document.querySelector('iframe');
const project = {id:'project',name:'Launch film',revision:'cloud:2',url:'https://donkeycut.com/app/p/project'};
let count = 0, renewals = 0;
window.calls = []; window.contexts = []; window.links = []; window.modes = []; window.editable = false; window.holdMs = 0; window.readyMs = 0; window.freshCard = false; window.signedOut = false; window.failOpen = false; window.missing = false; window.editorCodes = 0;
const view = (selected, preview = null) => ({view:selected?'project':'projects',projects:selected?[]:[project],nextCursor:null,project:selected?project:null,preview,canRender:true,canEdit:window.editable,export:null,job:null,results:[],changed:false,account:null,missing:null});
const missingView = () => ({...view(false),view:'missing',projects:[],missing:{id:project.id}});
const editorLink = () => ({url:location.origin+'/embed?embed=chatgpt&code=one-use-'+ ++window.editorCodes +'&project=project',expiresAt:Date.now()+60000});
// ChatGPT's sandbox drops null-valued keys before the widget sees a result.
const dropNulls = (value) => Array.isArray(value) ? value.map(dropNulls) : value && typeof value === 'object' ? Object.fromEntries(Object.entries(value).filter(([, v]) => v !== null).map(([k, v]) => [k, dropNulls(v)])) : value;
const result = (data, playback = null, editor = null) => dropNulls({content:[{type:'text',text:'Preview'}],structuredContent:data,_meta:{playback,editor,pollMs:1000}});
const reply = (id, result) => frame.contentWindow.postMessage({jsonrpc:'2.0',id,result},'*');
// A card ChatGPT shows again after a reload replays its own result: a stale
// preview with an expired playback link, and an editor link whose minute is
// spent unless the fixture says the card was just opened.
const cardResult = () => !window.editable ? result(view(true)) : result(view(true,{id:'job',status:'done',progress:1,revision:'cloud:1'}),{url:location.origin+'/preview.mp4?stale',expiresAt:Date.now()-1},window.freshCard ? {url:location.origin+'/embed?embed=chatgpt&code='+ (window.signedOut ? 'signed-out' : 'card') +'&project=project',expiresAt:Date.now()+60000} : {url:location.origin+'/embed?embed=chatgpt&code=spent&project=project',expiresAt:Date.now()-1});
// A tool result the host hands the card after it is open: the projects view.
window.showProjects = () => frame.contentWindow.postMessage({jsonrpc:'2.0',method:'ui/notifications/tool-result',params:result(view(false),null,window.editable ? {url:location.origin+'/embed?embed=chatgpt&code=one-use-'+ ++window.editorCodes,expiresAt:Date.now()+60000} : null)},'*');
window.addEventListener('message', ({source,data}) => {
 if(source !== frame.contentWindow || !data.method) return;
 if(data.method === 'ui/initialize') reply(data.id,{protocolVersion:data.params.protocolVersion,hostInfo:{name:'fixture',version:'1'},hostCapabilities:{serverTools:{},openLinks:{},updateModelContext:{text:{},structuredContent:{}}},hostContext:{theme:'light',displayMode:'inline',availableDisplayModes:['inline','fullscreen'],safeAreaInsets:{top:0,right:0,bottom:120,left:0}}});
 if(data.method === 'ui/notifications/initialized') frame.contentWindow.postMessage({jsonrpc:'2.0',method:'ui/notifications/tool-result',params:cardResult()},'*');
 if(data.method === 'tools/call') {
  const name = data.params.name; window.calls.push(name);
  if(name === 'list_projects') reply(data.id,result(view(false)));
  if(name === 'open_project') setTimeout(() => reply(data.id, window.failOpen ? {isError:true} : window.missing ? result(missingView()) : window.editable ? result(view(true),null,editorLink()) : result(view(true))), window.holdMs);
  if(name === 'create_project') reply(data.id, result(view(true),null,editorLink()));
  if(name === 'render_preview') { count=0; reply(data.id,result(view(true,{id:'job',status:'queued',progress:0,revision:'cloud:2'}))); }
  if(name === 'get_preview_status') {
   count++; const done = count >= 3;
   reply(data.id,result(view(true,{id:'job',status:done?'done':'running',progress:done?1:count/3,revision:'cloud:2'}),done?{url:location.origin+'/preview.mp4?v='+renewals++,expiresAt:Date.now()+3600000}:null));
  }
 }
 if(data.method === 'ui/update-model-context') { window.contexts.push(data.params); reply(data.id,{}); }
 if(data.method === 'ui/open-link') { window.links.push(data.params.url); reply(data.id,{}); }
 if(data.method === 'ui/request-display-mode') { window.modes.push(data.params.mode); reply(data.id,{mode:data.params.mode}); }
});
</script></body></html>`;
const browser = await chromium.launch({ headless: true });
try {
  const page = await browser.newPage();
  const errors: string[] = [], requests: string[] = [];
  page.on("pageerror", (error) => errors.push(error.message));
  page.on("request", (request) => requests.push(request.url()));
  await page.goto(`http://localhost:${server.port}`);
  const app = page.frameLocator("iframe");
  await app.getByRole("button", { name: "Render preview", exact: true }).click();
  await app.locator("video").waitFor();
  const frame = page.frames().find((f) => f.url().endsWith("/widget"))!;
  await frame.waitForFunction(() => (document.querySelector("video")?.readyState ?? 0) >= 2);
  const calls = await page.evaluate(() => (window as unknown as { calls: string[] }).calls);
  assert.equal(calls.filter((name) => name === "get_preview_status").length, 3, "poll through repeated running states");
  assert.equal(requests.some((url) => url.includes("/hls-")), false, "MP4 must not download HLS");
  await frame.evaluate(() => document.querySelector("video")!.dispatchEvent(new Event("error")));
  await frame.waitForFunction(() => document.querySelector("video")?.src.endsWith("v=1"));
  await frame.evaluate(async () => { const video = document.querySelector("video")!; video.muted = true; await video.play(); });
  await frame.evaluate(() => { Object.defineProperty(document, "hidden", { configurable: true, value: true }); document.dispatchEvent(new Event("visibilitychange")); });
  await frame.waitForFunction(() => document.querySelector("video")?.paused === true);
  await frame.evaluate(() => { Object.defineProperty(document, "hidden", { configurable: true, value: false }); document.dispatchEvent(new Event("visibilitychange")); });
  await app.getByRole("button", { name: "Open in Donkey Cut" }).click();
  await page.waitForFunction(() => (window as unknown as { links: string[] }).links.length === 1);
  assert.deepEqual(await page.evaluate(() => (window as unknown as { links: string[] }).links), ["https://donkeycut.com/app/p/project"]);
  await page.screenshot({ path: "/tmp/donkey-chatgpt-widget.png" });
  // A read-only connection has no projects home to frame.
  await page.evaluate(() => (window as unknown as { showProjects: () => void }).showProjects());
  await app.locator("video").waitFor({ state: "detached" });
  assert.equal(await app.locator("video").count(), 0, "the projects view releases the video");
  await app.getByRole("status").filter({ hasText: "Ask ChatGPT to open one of your projects" }).waitFor();
  assert.equal(await app.locator("iframe.editor").count(), 0, "a read-only projects card frames nothing");
  // An editing connection's projects view is the projects home, framed.
  await page.evaluate(() => { const host = window as unknown as { editable: boolean; showProjects: () => void }; host.editable = true; host.showProjects(); });
  await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "Editor for null" }).waitFor();
  await app.locator(".skeleton").waitFor({ state: "detached" });
  assert.equal(await app.locator("iframe.editor").getAttribute("aria-label"), "Your Donkey Cut projects");
  assert.deepEqual(await page.evaluate(() => (window as unknown as { modes: string[] }).modes), [], "an opened editor stays inline");
  // A project opened inside the frame reaches the model, which never saw that call.
  type Contexts = { contexts: { content?: unknown[]; structuredContent?: { projectId?: string; name?: string } }[] };
  assert.deepEqual(await page.evaluate(() => (window as unknown as Contexts).contexts.at(-1)?.content), [], "the projects home clears the card's context");
  const homeFrame = page.frames().find((f) => f.url().includes("/embed?"))!;
  await homeFrame.evaluate(() => parent.postMessage({ type: "donkeycut:page", projectId: "project", name: "Launch film" }, "*"));
  await page.waitForFunction(() => (window as unknown as Contexts).contexts.at(-1)?.structuredContent?.projectId === "project");
  assert.equal(await page.evaluate(() => (window as unknown as Contexts).contexts.at(-1)?.structuredContent?.name), "Launch film", "the model knows which project the frame shows");
  assert.equal(await app.locator("button").count(), 0, "the editor card has no controls of its own");
  assert.equal(await app.locator("main.editing").evaluate((m) => getComputedStyle(m).borderTopWidth), "0px", "an inline card keeps the host's own border");
  await app.frameLocator("iframe.editor").locator("#inset").filter({ hasText: /^0$/ }).waitFor();
  await app.frameLocator("iframe.editor").locator("#mode").filter({ hasText: /^inline$/ }).waitFor();
  await app.frameLocator("iframe.editor").getByRole("button", { name: "Fullscreen" }).click();
  await app.frameLocator("iframe.editor").locator("#mode").filter({ hasText: /^fullscreen$/ }).waitFor();
  await app.frameLocator("iframe.editor").locator("#inset").filter({ hasText: /^120$/ }).waitFor();
  assert.deepEqual(await page.evaluate(() => (window as unknown as { modes: string[] }).modes), ["fullscreen"], "the editor's button asks for the whole window");
  // The host's title sits above a fullscreen card; a hairline in the editor's
  // own border color parts it from the editor's top bar.
  assert.equal(await app.locator("main.editing").evaluate((m) => getComputedStyle(m).borderTopWidth), "1px", "a fullscreen card parts the host title from the editor");
  // A host in dark with a rail over the card's left edge: the card pads the
  // rail's band, wears dark, and hands the theme to the editor.
  await app.frameLocator("iframe.editor").locator("#theme").filter({ hasText: /^light$/ }).waitFor();
  await page.evaluate(() => document.querySelector("iframe")!.contentWindow!.postMessage({ jsonrpc: "2.0", method: "ui/notifications/host-context-changed", params: { theme: "dark", safeAreaInsets: { top: 0, right: 0, bottom: 120, left: 72 } } }, "*"));
  await app.frameLocator("iframe.editor").locator("#theme").filter({ hasText: /^dark$/ }).waitFor();
  assert.equal(await app.locator("main.editing").evaluate((m) => getComputedStyle(m).paddingLeft), "72px", "the card keeps the editor out of the rail's band");
  assert.equal(await app.locator("main.editing").evaluate((m) => getComputedStyle(m).backgroundColor), "rgb(10, 10, 10)", "the card wears the editor's dark surface");
  assert.equal(await app.locator("iframe.editor").evaluate((f) => getComputedStyle(f).colorScheme), "dark", "the editor frame first paints in the host's scheme");
  const editorFrame = page.frames().find((f) => f.url().includes("/embed?"))!;
  type Downloads = { downloadFromUrl: (url: string, name: string) => void; downloadFile: (url: string, name: string) => void };
  await editorFrame.waitForFunction(() => "downloadFromUrl" in window);
  await editorFrame.evaluate(() => (window as unknown as Downloads).downloadFromUrl("/api/cut-cloud/projects/project/exports/export.mp4?v=123", "export.mp4"));
  await page.waitForFunction(() => (window as unknown as { links: string[] }).links.length === 2);
  assert.equal(await page.evaluate(() => (window as unknown as { links: string[] }).links.at(-1)), signedDownload, "the host receives the signed URL");
  assert.deepEqual(downloadRequests, [{ method: "HEAD", cookie: "editor-session=fixture", query: "?v=123&download=1" }], "resolve in the authenticated frame");
  assert.deepEqual(mediaRequests, ["HEAD"], "resolving a download reads no media body");
  await editorFrame.evaluate(() => (window as unknown as Downloads).downloadFromUrl("/api/cut-cloud/projects/project/exports/missing.mp4", "missing.mp4"));
  await app.getByRole("alert").filter({ hasText: "Could not download missing.mp4" }).waitFor();
  assert.equal(await page.evaluate(() => (window as unknown as { links: string[] }).links.length), 2, "a failed resolution opens no broken link");
  await app.getByRole("button", { name: "Dismiss" }).click();
  await app.getByRole("alert").waitFor({ state: "detached" });
  await editorFrame.evaluate((url) => (window as unknown as Downloads).downloadFile(url, "export.mp4"), signedDownload);
  await page.waitForFunction(() => (window as unknown as { links: string[] }).links.length === 3);
  assert.equal(downloadRequests.length, 2, "already signed downloads open directly");
  await page.context().clearCookies();
  await editorFrame.evaluate(() => (window as unknown as Downloads).downloadFile("/api/cut-cloud/export/job/file", "export.mp4"));
  await app.getByRole("alert").filter({ hasText: "Could not download export.mp4" }).waitFor();
  assert.equal(await page.evaluate(() => (window as unknown as { links: string[] }).links.length), 3, "an expired session opens no link");
  await app.getByRole("button", { name: "Dismiss" }).click();

  const webPage = await browser.newPage();
  await webPage.goto(`http://localhost:${server.port}/embed`);
  await webPage.waitForFunction(() => "downloadFromUrl" in window);
  const [download] = await Promise.all([
    webPage.waitForEvent("download"),
    webPage.evaluate(() => (window as unknown as Downloads).downloadFromUrl("/api/cut-cloud/projects/project/exports/export.mp4?v=123", "export.mp4")),
  ]);
  assert.equal(download.suggestedFilename(), "export.mp4");
  assert.equal(downloadRequests.at(-1)?.method, "GET", "the standalone editor still downloads directly");
  assert.equal(mediaRequests.at(-1), "GET");
  await webPage.close();
  await page.screenshot({ path: "/tmp/donkey-chatgpt-widget-editor.png" });
  // A card rehydrated with a spent link holds the editor's space, inline and
  // without controls, while it mints a fresh link. The held reply outlasts the
  // stale playback's one-second renewal, so a poll would show up in the calls.
  type Host = { calls: string[]; modes: string[]; holdMs: number; readyMs: number; freshCard: boolean; signedOut: boolean; failOpen: boolean; missing: boolean; links: string[] };
  await page.evaluate(() => { const host = window as unknown as Host; host.calls = []; host.holdMs = 1500; });
  await frame.evaluate(() => { location.reload(); });
  await page.waitForFunction(() => (window as unknown as Host).calls.length === 1);
  await app.locator(".skeleton").waitFor();
  assert.equal(await app.locator("button").count(), 0, "a waking editor card shows no controls");
  assert.equal(await app.getByText("Render preview").count(), 0, "a waking editor card shows no preview card");
  await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "one-use" }).waitFor();
  await app.frameLocator("iframe.editor").locator("#mode").filter({ hasText: /^inline$/ }).waitFor();
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), ["open_project"], "the rehydrated card mints one fresh link and polls nothing");
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).modes), ["fullscreen"], "a card that wakes up stays inline");
  // A card whose own result carries a live link opens with it, inline, and
  // its skeleton covers the frame until the editor says it is on screen.
  await page.evaluate(() => { const host = window as unknown as Host; host.calls = []; host.holdMs = 0; host.readyMs = 1000; host.freshCard = true; });
  await frame.evaluate(() => { location.reload(); });
  await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "via card" }).waitFor();
  assert.equal(await app.locator(".skeleton").count(), 1, "the skeleton holds until the editor is ready");
  await page.screenshot({ path: "/tmp/donkey-chatgpt-widget-skeleton.png" });
  await app.locator(".skeleton").waitFor({ state: "detached" });
  await app.frameLocator("iframe.editor").locator("#mode").filter({ hasText: /^inline$/ }).waitFor();
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).modes), ["fullscreen"], "a newly opened card opens inline");
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), [], "a live link needs no second call");
  // Render the real signed-out control inside the editor frame.
  await page.evaluate(() => { const host = window as unknown as Host; host.calls = []; host.links = []; host.signedOut = true; host.readyMs = 0; host.holdMs = 300; });
  await frame.evaluate(() => { location.reload(); });
  const signedOutEditor = app.frameLocator("iframe.editor");
  await signedOutEditor.getByRole("button", { name: "Reconnect", exact: true }).waitFor();
  await app.locator(".skeleton").waitFor({ state: "detached" });
  // The host page has the wrong source: it cannot request a session renewal.
  await page.evaluate(() => document.querySelector("iframe")!.contentWindow!.postMessage({ type: "donkeycut:reconnect" }, "*"));
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), [], "foreign frames cannot renew the editor session");
  await signedOutEditor.getByRole("button", { name: "Reconnect", exact: true }).evaluate((button) => {
    for (let i = 0; i < 3; i++) (button as HTMLButtonElement).click();
  });
  await app.locator(".skeleton").waitFor();
  await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "one-use" }).waitFor();
  await app.locator(".skeleton").waitFor({ state: "detached" });
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), ["open_project"], "reconnect mints a fresh link once");
  assert.equal(await page.evaluate(() => (window as unknown as Host).links.length), 0, "reconnect stays inside ChatGPT");

  // Failure is visible and retry replaces the failed frame with a fresh link.
  await page.evaluate(() => { const host = window as unknown as Host; host.calls = []; host.failOpen = true; });
  await frame.evaluate(() => { location.reload(); });
  await signedOutEditor.getByRole("button", { name: "Reconnect", exact: true }).click();
  await app.getByRole("alert").filter({ hasText: "Donkey Cut could not complete this request" }).waitFor();
  assert.equal(await app.locator("iframe.editor").count(), 0, "failed recovery releases the signed-out frame");
  await page.evaluate(() => { (window as unknown as Host).failOpen = false; });
  await app.getByRole("button", { name: "Try again", exact: true }).click();
  await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "one-use" }).waitFor();
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), ["open_project", "open_project"], "manual retry recovers without a browser refresh");
  // A card for a project that has since been deleted says so, in the editor's
  // space, and makes a new project there in its place.
  await page.evaluate(() => { const host = window as unknown as Host; host.calls = []; host.freshCard = false; host.missing = true; });
  await frame.evaluate(() => { location.reload(); });
  await app.getByRole("status").filter({ hasText: "This project was deleted." }).waitFor();
  assert.equal(await app.locator("iframe.editor").count(), 0, "a deleted project shows no editor frame");
  await page.evaluate(() => { (window as unknown as Host).missing = false; });
  await app.getByRole("button", { name: "Create New Project", exact: true }).click();
  await app.frameLocator("iframe.editor").locator("#editor").filter({ hasText: "one-use" }).waitFor();
  await app.locator(".skeleton").waitFor({ state: "detached" });
  assert.deepEqual(await page.evaluate(() => (window as unknown as Host).calls), ["open_project", "create_project"], "a deleted project creates a new one in its place");
  assert.deepEqual(errors, []);
  console.log("PASS: project selection, repeated polling, native playback, URL recovery, hidden pause, Open in Donkey Cut, editor in the card, authenticated download redirects, download errors, skeleton until ready, waking editor card, session renewal and retry, deleted project, decoder teardown, lazy HLS.");
} finally {
  await browser.close(); server.stop(true); mediaServer.stop(true); await rm(scratch, { recursive: true, force: true });
}