-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add MP4 Electron export E2E #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yusufm
wants to merge
2
commits into
siddharthvaddem:main
Choose a base branch
from
yusufm:codex/mp4-electron-e2e
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import type { ElectronApplication } from "@playwright/test"; | ||
|
|
||
| export async function waitForProcessExit( | ||
| child: ReturnType<ElectronApplication["process"]>, | ||
| timeoutMs: number, | ||
| ) { | ||
| if (child.exitCode !== null || child.killed) return; | ||
|
|
||
| await Promise.race([ | ||
| new Promise<void>((resolve) => child.once("exit", () => resolve())), | ||
| new Promise<void>((resolve) => setTimeout(resolve, timeoutMs)), | ||
| ]); | ||
| } | ||
|
|
||
| export async function closeElectronApp(app: ElectronApplication) { | ||
| const child = app.process(); | ||
| await app | ||
| .evaluate(({ app: electronApp }) => { | ||
| electronApp.exit(0); | ||
| }) | ||
| .catch(() => { | ||
| // App may already be closing. | ||
| }); | ||
| await waitForProcessExit(child, 2_000); | ||
| if (child.exitCode === null && !child.killed) { | ||
| child.kill("SIGKILL"); | ||
| await waitForProcessExit(child, 2_000); | ||
| } | ||
| } | ||
|
|
||
| export async function interceptExportSave(app: ElectronApplication) { | ||
| await app.evaluate(({ ipcMain }) => { | ||
| ipcMain.removeHandler("save-exported-video"); | ||
| ipcMain.handle( | ||
| "save-exported-video", | ||
| (_event: Electron.IpcMainInvokeEvent, buffer: ArrayBuffer) => { | ||
| (globalThis as Record<string, unknown>)["__testExportData"] = | ||
| Buffer.from(buffer).toString("base64"); | ||
| return { success: true, path: "pending" }; | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| export async function copyFixtureToRecordings( | ||
| app: ElectronApplication, | ||
| fixturePath: string, | ||
| fileName: string, | ||
| ) { | ||
| const userDataDir = await app.evaluate(({ app: electronApp }) => { | ||
| return electronApp.getPath("userData"); | ||
| }); | ||
| const recordingsDir = path.join(userDataDir, "recordings"); | ||
| const targetPath = path.join(recordingsDir, fileName); | ||
| fs.mkdirSync(recordingsDir, { recursive: true }); | ||
| fs.copyFileSync(fixturePath, targetPath); | ||
| return targetPath; | ||
| } | ||
|
|
||
| export async function readCapturedExportBuffer(app: ElectronApplication) { | ||
| const base64 = await app.evaluate( | ||
| () => (globalThis as Record<string, unknown>)["__testExportData"] as string, | ||
| ); | ||
| if (typeof base64 !== "string" || base64.length === 0) { | ||
| throw new Error("__testExportData was not set or is invalid"); | ||
| } | ||
| return Buffer.from(base64, "base64"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { _electron as electron, expect, test } from "@playwright/test"; | ||
| import { | ||
| closeElectronApp, | ||
| copyFixtureToRecordings, | ||
| interceptExportSave, | ||
| readCapturedExportBuffer, | ||
| } from "./helpers"; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const ROOT = path.join(__dirname, "../.."); | ||
| const MAIN_JS = path.join(ROOT, "dist-electron/main.js"); | ||
| const TEST_VIDEO = path.join(__dirname, "../fixtures/sample.webm"); | ||
|
|
||
| test("exports an MP4 from a loaded video", async () => { | ||
| const outputPath = path.join(os.tmpdir(), `test-mp4-export-${Date.now()}.mp4`); | ||
| let testVideoInRecordings = ""; | ||
|
|
||
| const app = await electron.launch({ | ||
| args: [MAIN_JS, "--no-sandbox", "--enable-unsafe-swiftshader"], | ||
| env: { | ||
| ...process.env, | ||
| HEADLESS: process.env["HEADLESS"] ?? "true", | ||
| }, | ||
| }); | ||
|
|
||
| app.process().stdout?.on("data", (d) => process.stdout.write(`[electron] ${d}`)); | ||
| app.process().stderr?.on("data", (d) => process.stderr.write(`[electron] ${d}`)); | ||
|
|
||
| try { | ||
| const hudWindow = await app.firstWindow({ timeout: 60_000 }); | ||
| await hudWindow.waitForLoadState("domcontentloaded"); | ||
| await interceptExportSave(app); | ||
|
|
||
| testVideoInRecordings = await copyFixtureToRecordings(app, TEST_VIDEO, "test-sample-mp4.webm"); | ||
|
|
||
| try { | ||
| await hudWindow.evaluate(async (videoPath: string) => { | ||
| await window.electronAPI.setCurrentVideoPath(videoPath); | ||
| await window.electronAPI.switchToEditor(); | ||
| }, testVideoInRecordings); | ||
| } catch { | ||
| // Expected: switchToEditor closes the HUD window. | ||
| } | ||
|
|
||
| const editorWindow = await app.waitForEvent("window", { | ||
| predicate: (w) => w.url().includes("windowType=editor"), | ||
| timeout: 15_000, | ||
| }); | ||
|
|
||
| await editorWindow.reload(); | ||
| await editorWindow.waitForLoadState("domcontentloaded"); | ||
| await expect(editorWindow.getByText("Loading video...")).not.toBeVisible({ | ||
| timeout: 15_000, | ||
| }); | ||
|
|
||
| await editorWindow.getByTestId("testId-mp4-format-button").click(); | ||
| await editorWindow.getByTestId("testId-export-button").click(); | ||
|
|
||
| await expect(editorWindow.getByText("Video exported successfully")).toBeVisible({ | ||
| timeout: 90_000, | ||
| }); | ||
|
|
||
| fs.writeFileSync(outputPath, await readCapturedExportBuffer(app)); | ||
| expect(fs.existsSync(outputPath), `MP4 not found at ${outputPath}`).toBe(true); | ||
|
|
||
| const header = Buffer.alloc(12); | ||
| const fd = fs.openSync(outputPath, "r"); | ||
| fs.readSync(fd, header, 0, 12, 0); | ||
| fs.closeSync(fd); | ||
|
|
||
| expect(header.subarray(4, 8).toString("ascii")).toBe("ftyp"); | ||
| expect(fs.statSync(outputPath).size).toBeGreaterThan(1024); | ||
| } finally { | ||
| await closeElectronApp(app); | ||
| if (fs.existsSync(outputPath)) { | ||
| fs.unlinkSync(outputPath); | ||
| } | ||
| if (testVideoInRecordings && fs.existsSync(testVideoInRecordings)) { | ||
| fs.unlinkSync(testVideoInRecordings); | ||
| } | ||
| } | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.