Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript
13.6 KB
Raw Blame
/**
 * Real-Electron crash recovery + long-form soak (Playwright _electron, the BUILT app, SIGKILL).
 *
 * Needs a build made with the e2e hook:  VITE_OPENREEL_E2E=1 pnpm --filter @openreel/desktop build
 * Soak sources: node apps/web/e2e/soak/gen-media.mjs <dir>   (ProRes included).
 * Opt in with OPENREEL_DESKTOP_SOAK=1 (skipped otherwise):  pnpm --filter @openreel/desktop test:soak-e2e
 *
 *  - the app process is SIGKILLed mid-edit: after relaunch on the same profile the newest auto-save
 *    restores the timeline byte-identically;
 *  - SIGKILL mid-export (native ffmpeg sidecar): the queue comes back with the job `interrupted`,
 *    paused, and Retry re-runs it from its persisted snapshot to a valid file;
 *  - SIGKILL while a project file is being saved: the file on disk is the old or the new project,
 *    never a truncated one (temp + fsync + rename);
 *  - a ProRes 422 Proxy source imports and plays on the timeline.
 */
import { createRequire } from "node:module";
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";

const root = path.resolve(__dirname, "../../..");
const FFMPEG = process.env.FFMPEG ?? "/opt/homebrew/bin/ffmpeg";
const FFPROBE = process.env.FFPROBE ?? "/opt/homebrew/bin/ffprobe";
const MEDIA = process.env.SOAK_MEDIA_DIR ?? path.join(os.tmpdir(), "openreel-soak-media");
const enabled =
  process.env.OPENREEL_DESKTOP_SOAK === "1" &&
  fs.existsSync(FFMPEG) &&
  fs.existsSync(path.join(MEDIA, "manifest.json")) &&
  fs.existsSync(path.join(root, "apps/desktop/dist/main/index.js")) &&
  fs.existsSync(path.join(root, "apps/web/dist/index.html"));
const d = enabled ? describe : describe.skip;

const require = createRequire(path.join(root, "package.json"));
function loadPlaywright(): { _electron: any } {
  const pnpmDir = path.join(root, "node_modules/.pnpm");
  const dir = fs.readdirSync(pnpmDir).find((name) => name.startsWith("playwright@"));
  return require(path.join(pnpmDir, dir!, "node_modules/playwright"));
}
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

let work = "";
const sessions: Array<{ app: any; page: any }> = [];
let launches = 0;

async function launch(profile: string): Promise<{ app: any; page: any }> {
  const { _electron } = loadPlaywright();
  const app = await _electron.launch({
    args: [path.join(root, "apps/desktop"), `--user-data-dir=${profile}`],
    env: { ...process.env, OPENREEL_FFMPEG_PATH: FFMPEG, OPENREEL_SKIP_ONBOARDING: "1" },
    timeout: 120_000,
  });
  const page = await app.firstWindow();
  await page.addInitScript(() => {
    try {
      localStorage.setItem("openreel-onboarding-complete", "true");
      localStorage.setItem("openreel-mograph-tour-complete", "true");
    } catch {
      /* storage unavailable */
    }
  });
  await page.reload();
  launches++;
  const session = { app, page };
  sessions.push(session);
  return session;
}

/** The hard kill: no quit handlers, no beforeunload, no flush. */
async function sigkill(session: { app: any }): Promise<void> {
  const proc = session.app.process();
  proc.kill("SIGKILL");
  await new Promise((r) => proc.once("exit", r));
}

async function openEditor(page: any): Promise<void> {
  await page.getByRole("button", { name: /Horizontal/ }).click({ timeout: 60_000 });
  await page.locator('canvas[aria-label^="Program frame"]').waitFor({ timeout: 60_000 });
  await page.waitForFunction(() => Boolean((window as any).__openreelE2E?.autoSave), undefined, { timeout: 30_000 });
}

const canonical = (page: any): Promise<string> =>
  page.evaluate(() => {
    const p = (window as any).__openreelE2E.project.getState().getFullProject();
    return JSON.stringify({ timeline: p.timeline, textClips: p.textClips ?? [] });
  });

async function waitSavedAfter(page: any, since: number): Promise<void> {
  const deadline = Date.now() + 60_000;
  while (Date.now() < deadline) {
    const newest = await page.evaluate(async () => {
      const saves = await (window as any).__openreelE2E.autoSave.checkForRecovery();
      return saves[0]?.timestamp ?? 0;
    });
    if (newest > since) return;
    await sleep(300);
  }
  throw new Error("no auto-save appeared");
}

async function importFiles(page: any, files: string[]): Promise<Record<string, string>> {
  await page.locator('[data-tour="assets"] input[type="file"]').setInputFiles(files);
  const names = files.map((f) => path.basename(f));
  const deadline = Date.now() + 10 * 60_000;
  for (;;) {
    const ids = await page.evaluate((expected: string[]) => {
      const items = (window as any).__openreelE2E.project.getState().project.mediaLibrary.items as any[];
      const out: Record<string, string> = {};
      for (const n of expected) {
        const m = items.find((i) => i.name === n);
        if (m && m.metadata?.duration > 0) out[n] = m.id;
      }
      return out;
    }, names);
    if (Object.keys(ids).length === names.length) return ids;
    if (Date.now() > deadline) throw new Error("import timed out");
    await sleep(500);
  }
}

/** A modest multi-track timeline from two sources through the same `clip/add` action the UI uses. */
async function buildTimeline(page: any, ids: Record<string, string>, seconds: Record<string, number>): Promise<number> {
  return page.evaluate(
    async ({ ids, seconds }: { ids: Record<string, string>; seconds: Record<string, number> }) => {
      const st = () => (window as any).__openreelE2E.project.getState();
      await st().addTrack("video", undefined, { name: "V-soak" });
      await st().addTrack("audio", undefined, { name: "A-soak" });
      const tracks = st().project.timeline.tracks;
      const v = tracks.find((t: any) => t.name === "V-soak").id as string;
      const a = tracks.find((t: any) => t.name === "A-soak").id as string;
      const names = Object.keys(ids);
      let n = 0;
      for (let i = 0; i < 60; i++) {
        const name = names[i % names.length];
        const dur = 6 + (i % 5);
        const inPoint = (i * 13.7) % Math.max(1, seconds[name] - dur - 1);
        for (const trackId of [v, a]) {
          const r = await st().executeAction({ type: "clip/add", params: { trackId, mediaId: ids[name], startTime: i * (dur + 0.5), clipId: crypto.randomUUID(), duration: dur, inPoint, outPoint: inPoint + dur } });
          if (r.success) n++;
        }
      }
      return n;
    },
    { ids, seconds },
  );
}

async function edits(page: any, n: number, salt: number): Promise<void> {
  await page.evaluate(
    async ({ n, salt }: { n: number; salt: number }) => {
      const st = (window as any).__openreelE2E.project.getState();
      const clips = st.project.timeline.tracks.filter((t: any) => t.type === "video").flatMap((t: any) => t.clips);
      for (let i = 0; i < n; i++) await st.addVideoEffect(clips[(salt * 7 + i * 13) % clips.length].id, "gaussian-blur", { radius: 1 + ((salt + i) % 9) });
    },
    { n, salt },
  );
}

d("desktop crash recovery (SIGKILL)", () => {
  const manifest: Array<{ name: string; file: string; seconds: number; codec: string }> = enabled ? JSON.parse(fs.readFileSync(path.join(MEDIA, "manifest.json"), "utf8")) : [];
  const src = (name: string) => manifest.find((m) => m.name === name)!;

  beforeAll(() => {
    work = fs.mkdtempSync(path.join(os.tmpdir(), "openreel-desktop-soak-"));
  });
  afterAll(async () => {
    for (const s of sessions) {
      try {
        s.app.process().kill("SIGKILL");
      } catch {
        /* gone */
      }
    }
  });

  it("SIGKILL mid-edit: the newest auto-save restores the timeline byte-identically", async () => {
    const profile = path.join(work, "profile-edit");
    const first = await launch(profile);
    await openEditor(first.page);
    const a = src("hd-h264-longgop");
    const b = src("odd-h264");
    const ids = await importFiles(first.page, [a.file, b.file]);
    const placed = await buildTimeline(first.page, ids, { [path.basename(a.file)]: a.seconds, [path.basename(b.file)]: b.seconds });
    expect(placed).toBeGreaterThan(100);
    const t0 = Date.now();
    await edits(first.page, 5, 1);
    await waitSavedAfter(first.page, t0);
    const before = await canonical(first.page);
    await sigkill(first);

    const second = await launch(profile);
    await second.page.getByRole("button", { name: /Horizontal/ }).waitFor({ timeout: 60_000 });
    await second.page.waitForFunction(() => Boolean((window as any).__openreelE2E?.autoSave), undefined, { timeout: 30_000 });
    const rec = await second.page.evaluate(async () => {
      const E = (window as any).__openreelE2E;
      await E.autoSave.initialize();
      const saves = await E.autoSave.checkForRecovery();
      const t = performance.now();
      const ok = saves.length > 0 && (await E.project.getState().recoverFromAutoSave(saves[0].id));
      return { ok, ms: performance.now() - t, saves: saves.length };
    });
    console.log(`[desktop recovery] saves ${rec.saves}, restored in ${rec.ms.toFixed(0)} ms`);
    expect(rec.ok).toBe(true);
    expect(await canonical(second.page)).toBe(before);
    await sigkill(second);
  }, 15 * 60_000);

  it("SIGKILL mid-export: the job is interrupted after relaunch and Retry finishes it", async () => {
    const profile = path.join(work, "profile-export");
    const first = await launch(profile);
    await openEditor(first.page);
    const a = src("hd-h264-longgop");
    const ids = await importFiles(first.page, [a.file]);
    await buildTimeline(first.page, ids, { [path.basename(a.file)]: a.seconds });
    const jobId = await first.page.evaluate(() => {
      const q = (window as any).__openreelE2E.exportQueue.getState();
      const job = q.enqueue({ settings: { width: 1920, height: 1080, frameRate: 30, format: "mp4", codec: "h264", bitrate: 8000, range: { startTime: 0, endTime: 120 } }, presetLabel: "Kill me" });
      q.start();
      return job.id as string;
    });
    const deadline = Date.now() + 5 * 60_000;
    for (;;) {
      const p = await first.page.evaluate((id: string) => (window as any).__openreelE2E.exportQueue.getState().queue.jobs.find((j: any) => j.id === id)?.progress ?? 0, jobId);
      if (p > 0.1) break;
      if (Date.now() > deadline) throw new Error("export never progressed");
      await sleep(400);
    }
    await sleep(1200);
    await sigkill(first);

    const second = await launch(profile);
    await second.page.waitForFunction(() => Boolean((window as any).__openreelE2E?.exportQueue), undefined, { timeout: 60_000 });
    await second.page.evaluate(() => (window as any).__openreelE2E.exportQueue.getState().hydrate());
    const state = await second.page.evaluate((id: string) => {
      const q = (window as any).__openreelE2E.exportQueue.getState().queue;
      return { status: q.jobs.find((j: any) => j.id === id)?.status as string, paused: q.paused as boolean };
    }, jobId);
    console.log(`[desktop recovery] export after SIGKILL: ${JSON.stringify(state)}`);
    expect(state).toEqual({ status: "interrupted", paused: true });
    await second.page.evaluate((id: string) => {
      const q = (window as any).__openreelE2E.exportQueue.getState();
      q.retry(id);
      q.start();
    }, jobId);
    let outPath = "";
    const end = Date.now() + 10 * 60_000;
    for (;;) {
      const j = await second.page.evaluate((id: string) => {
        const x = (window as any).__openreelE2E.exportQueue.getState().queue.jobs.find((y: any) => y.id === id);
        return { status: x?.status as string, path: x?.output?.path as string | undefined, error: x?.error as string | undefined };
      }, jobId);
      if (j.status === "completed") {
        outPath = j.path ?? "";
        break;
      }
      if (j.status === "failed") throw new Error(`retry failed: ${j.error}`);
      if (Date.now() > end) throw new Error("retry did not finish");
      await sleep(500);
    }
    const info = JSON.parse(execFileSync(FFPROBE, ["-v", "error", "-show_entries", "format=duration", "-of", "json", outPath], { encoding: "utf8" }));
    expect(Number(info.format.duration)).toBeGreaterThan(115);
    await sigkill(second);
  }, 25 * 60_000);

  it("a ProRes 422 Proxy source imports and plays on the timeline", async () => {
    const prores = manifest.find((m) => m.codec === "prores");
    if (!prores) return;
    const s = await launch(path.join(work, "profile-prores"));
    await openEditor(s.page);
    const ids = await importFiles(s.page, [prores.file]);
    const id = Object.values(ids)[0];
    const result = await s.page.evaluate(async (mediaId: string) => {
      const E = (window as any).__openreelE2E;
      const st = E.project.getState();
      await st.addTrack("video", undefined, { name: "ProRes" });
      const track = E.project.getState().project.timeline.tracks.filter((t: any) => t.type === "video").at(-1).id;
      const r = await st.executeAction({ type: "clip/add", params: { trackId: track, mediaId, startTime: 0, clipId: crypto.randomUUID(), duration: 20, inPoint: 0, outPoint: 20 } });
      E.timeline.getState().seekTo(1);
      const t0 = performance.now();
      const p0 = E.timeline.getState().playheadPosition;
      E.timeline.getState().play();
      await new Promise((res) => setTimeout(res, 5000));
      E.timeline.getState().pause();
      return { ok: r.success, advanced: E.timeline.getState().playheadPosition - p0, wall: (performance.now() - t0) / 1000 };
    }, id);
    console.log(`[desktop prores] ${JSON.stringify(result)}`);
    expect(result.ok).toBe(true);
    // Chromium has no ProRes decoder: playback of the original goes through the native sidecar and was
    // measured at ~0.64x real time at 1080p (docs/soak-report.md); proxies are the intended path. Guard against a collapse.
    expect(result.advanced).toBeGreaterThan(result.wall * 0.4);
    await sigkill(s);
  }, 10 * 60_000);
});