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
3.6 KB
Raw Blame
import { expect, test, type Page } from "@playwright/test";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { openEditor, programMean, seek, settle } from "./helpers";

/**
 * Saved-project compatibility in real Chrome: the w4, w5 and w6 fixture files (generated by those
 * trees, see src/test/fixtures/compat/README.md) are loaded through the serializer into the live
 * editor with the real three-scene footage attached, and the program canvas must show the right
 * picture at 1 s (test pattern), 3 s (the red scene) and 5 s (colour bars). A re-save through the
 * serializer must describe the same timeline.
 */

interface Hook {
  __openreelE2E: {
    project: { getState(): any };
    ProjectSerializer: new (storage: unknown) => {
      importFromJson(json: string): any;
      exportToJson(project: unknown): string;
    };
  };
}

const FIXTURES = join(dirname(fileURLToPath(import.meta.url)), "..", "src", "test", "fixtures", "compat");

const loadFixture = (page: Page, json: string) =>
  page.evaluate((text) => {
    const e = (window as unknown as Hook).__openreelE2E;
    const live = e.project.getState().project.mediaLibrary.items[0];
    const serializer = new e.ProjectSerializer({});
    const imported = serializer.importFromJson(text);
    // The fixture's media is a placeholder (no blob): attach the real footage the editor just imported.
    const items = imported.mediaLibrary.items.map((item: any) => (item.id === "media-scenes" ? { ...live, id: "media-scenes", name: item.name } : item));
    e.project.getState().loadProject({ ...imported, mediaLibrary: { ...imported.mediaLibrary, items } });
    const state = e.project.getState().project;
    const resaved = JSON.parse(serializer.exportToJson(state));
    return {
      duration: state.timeline.duration as number,
      mainClips: state.timeline.tracks.find((t: any) => t.name === "Main").clips.map((c: any) => [c.startTime, c.duration]),
      resavedDuration: resaved.project.timeline.duration as number,
    };
  }, json);

for (const wave of ["w4", "w5", "w6"] as const) {
  test(`a project saved by the ${wave} tree loads, renders the right frames and re-saves the same timeline`, async ({ page }) => {
    test.setTimeout(150_000);
    await openEditor(page);
    const json = readFileSync(join(FIXTURES, `${wave}.openreel.json`), "utf8");
    const loaded = await loadFixture(page, json);
    expect(loaded.duration).toBeCloseTo(6, 2);
    expect(loaded.resavedDuration).toBeCloseTo(6, 2);
    expect(loaded.mainClips).toHaveLength(wave === "w6" ? 2 : 3);
    await settle(page, 1200);

    // 3 s: the solid red scene (w4/w5: a clip with a small brightness effect; w6: inside the nest).
    await seek(page, 3);
    await expect
      .poll(async () => (await programMean(page))[0], { timeout: 15_000, intervals: [250] })
      .toBeGreaterThan(150);
    const [r, g, b] = await programMean(page);
    expect(g).toBeLessThan(110);
    expect(b).toBeLessThan(110);

    // 5 s: colour bars are not red-dominant; 1 s: the test pattern differs from the red scene.
    await seek(page, 5);
    await expect
      .poll(async () => {
        const [pr, pg, pb] = await programMean(page);
        return pr - Math.max(pg, pb);
      }, { timeout: 15_000, intervals: [250] })
      .toBeLessThan(60);
    await seek(page, 1);
    await expect
      .poll(async () => {
        const [pr, pg, pb] = await programMean(page);
        return pr - Math.max(pg, pb);
      }, { timeout: 15_000, intervals: [250] })
      .toBeLessThan(60);
    expect(r).toBeGreaterThan(g);
  });
}