From 2583fd7dc01aeae1e288f65621e0f79d34088b47 Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 13:08:00 +0900 Subject: [PATCH 1/9] fix(electron): keep no empty video when ffmpeg fails before it writes one Recorder.stop() swallowed the encoder's failure whenever the output existed, to keep what a failed or killed ffmpeg had already written. ffmpeg opens its output before it reads a frame, so one that failed before writing any video left an empty .webm that was attached as the recording. Keep only a file with content; remove an empty one and fail with ENGINE_FAILURE. Co-Authored-By: Claude Opus 5.5 --- packages/electron/src/video.ts | 9 +++++---- packages/electron/tests/integration/launch.test.ts | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/electron/src/video.ts b/packages/electron/src/video.ts index fa48f58c0b..1dcba6c017 100644 --- a/packages/electron/src/video.ts +++ b/packages/electron/src/video.ts @@ -6,7 +6,7 @@ */ import { spawn, type ChildProcess } from 'node:child_process'; -import { existsSync, mkdirSync, rmSync } from 'node:fs'; +import { existsSync, mkdirSync, rmSync, statSync } from 'node:fs'; import path from 'node:path'; import { EngineError, type VideoFile } from '@e2e-dev/core/engine'; import { answerWithin } from './budget.ts'; @@ -120,9 +120,10 @@ export class Recorder { } finally { this.source = undefined; await encoder.close().catch((cause: unknown) => { - if (!existsSync(this.absolute)) { - throw new EngineError('ENGINE_FAILURE', `the video ${this.relative} could not be finalized: ${cause instanceof Error ? cause.message : String(cause)}`, { retryable: false, cause }); - } + // What ffmpeg wrote before it failed still plays; an empty file is no video. + if ((statSync(this.absolute, { throwIfNoEntry: false })?.size ?? 0) > 0) return; + rmSync(this.absolute, { force: true }); + throw new EngineError('ENGINE_FAILURE', `the video ${this.relative} could not be finalized: ${cause instanceof Error ? cause.message : String(cause)}`, { retryable: false, cause }); }); } return existsSync(this.absolute) ? [{ path: this.relative, startedAt }] : []; diff --git a/packages/electron/tests/integration/launch.test.ts b/packages/electron/tests/integration/launch.test.ts index 88f397ee2e..ffe766b40c 100644 --- a/packages/electron/tests/integration/launch.test.ts +++ b/packages/electron/tests/integration/launch.test.ts @@ -610,6 +610,18 @@ describe('video', () => { expect(frames.some(([red, , blue]) => red < 80 && blue > 180)).toBe(true); }); + it.skipIf(process.platform === 'win32')('fails, and keeps no file, when the encoder fails before it writes any video', async () => { + const { windows, directory } = await twoWindows(); + // An encoder that opens its output, as ffmpeg does before it reads a frame, and then fails. + const failing = path.join(directory, 'failing-ffmpeg.sh'); + writeFileSync(failing, '#!/bin/sh\nfor output; do :; done\n: > "$output"\ncat > /dev/null\nexit 23\n', { mode: 0o755 }); + const recorder = new Recorder(failing, directory, 'video/failed.webm', () => true); + recorder.follow(windows.open()[0]!.page); + await new Promise((resolve) => setTimeout(resolve, 300)); + expect(await recorder.stop().then(() => undefined, (error: unknown) => error)).toMatchObject({ code: 'ENGINE_FAILURE', message: expect.stringContaining('exited with 23') as string }); + expect(existsSync(path.join(directory, 'video', 'failed.webm'))).toBe(false); + }); + it('keeps no frame of a window it may not show, and writes no file for a recording with none', async () => { const { windows, directory, ffmpeg } = await twoWindows(); const [one, two] = windows.open(); From 7438fa080d262a55cb8af6d21fc31977fd372bb2 Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 14:11:21 +0900 Subject: [PATCH 2/9] fix(electron): drop a video snapshot of a window that loaded another document as it was captured Recorder.snap() checked the file bound before Page.captureScreenshot and not after, so a window that went to a local file outside the app while it was captured (up to two seconds) could put a frame of that file in the video. Check the document generation and the bound again on the capture, as the screenshot artifact already does. Co-Authored-By: Claude Opus 5.5 --- packages/electron/src/video.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/electron/src/video.ts b/packages/electron/src/video.ts index 1dcba6c017..c49168b699 100644 --- a/packages/electron/src/video.ts +++ b/packages/electron/src/video.ts @@ -137,8 +137,11 @@ export class Recorder { async snap(): Promise { const { source, encoder } = this; if (source === undefined || encoder === undefined || source.page.isClosed() || !this.allowed(source.page)) return; + const { generation } = source.page; const screen = await answerWithin(source.page.session.send('Page.captureScreenshot', { format: 'jpeg', quality: JPEG_QUALITY }), LAST_SCREEN_MS); - if (screen !== undefined) await encoder.frame(Buffer.from(screen.data, 'base64')); + // A window that loaded another document while it was captured may show what no frame may: the capture is dropped. + if (screen === undefined || source.page.generation !== generation || !this.allowed(source.page)) return; + await encoder.frame(Buffer.from(screen.data, 'base64')); } /** Ends a recording nobody collects: the file it wrote is removed. */ From 85ae0b726fa7967256f25f8654eb675caad86376 Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 17:40:12 +0900 Subject: [PATCH 3/9] fix(electron): end a recording in its close time when the encoder has stopped reading Recorder.stop() waited for the window switch and the last snapshot before encoder.close() started its kill timer, and both wait on a frame's write. An ffmpeg that stopped reading its input held that write, so stop() never reached the timer. Start the timer before stop() or abandon() waits on anything, and close the encoder's input when it is killed, so a waiting write returns. Co-Authored-By: Claude Opus 5.5 --- packages/electron/src/video.ts | 64 +++++++++++++------ .../electron/tests/integration/launch.test.ts | 16 +++++ 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/packages/electron/src/video.ts b/packages/electron/src/video.ts index c49168b699..cc93ffed06 100644 --- a/packages/electron/src/video.ts +++ b/packages/electron/src/video.ts @@ -110,23 +110,40 @@ export class Recorder { * ever shown. */ async stop(): Promise { - await this.switching; - this.stopped = true; - const { source, encoder, startedAt } = this; - if (encoder === undefined || startedAt === undefined) return []; + return await this.withinCloseTime(async () => { + await this.switching; + this.stopped = true; + const { source, encoder, startedAt } = this; + if (encoder === undefined || startedAt === undefined) return []; + try { + await this.snap(); + await source?.stop(); + } finally { + this.source = undefined; + await encoder.close().catch((cause: unknown) => { + // What ffmpeg wrote before it failed still plays; an empty file is no video. + if ((statSync(this.absolute, { throwIfNoEntry: false })?.size ?? 0) > 0) return; + rmSync(this.absolute, { force: true }); + throw new EngineError('ENGINE_FAILURE', `the video ${this.relative} could not be finalized: ${cause instanceof Error ? cause.message : String(cause)}`, { retryable: false, cause }); + }); + } + return existsSync(this.absolute) ? [{ path: this.relative, startedAt }] : []; + }); + } + + /** + * Runs the end of a recording with the encoder killed once it has had + * `CLOSE_TIMEOUT_MS`: an ffmpeg that stops reading holds a frame's write, + * and the window switch and the last snapshot that wait on it, before its + * own close would start a timer. + */ + private async withinCloseTime(end: () => Promise): Promise { + const stuck = setTimeout(() => this.encoder?.kill(), CLOSE_TIMEOUT_MS); try { - await this.snap(); - await source?.stop(); + return await end(); } finally { - this.source = undefined; - await encoder.close().catch((cause: unknown) => { - // What ffmpeg wrote before it failed still plays; an empty file is no video. - if ((statSync(this.absolute, { throwIfNoEntry: false })?.size ?? 0) > 0) return; - rmSync(this.absolute, { force: true }); - throw new EngineError('ENGINE_FAILURE', `the video ${this.relative} could not be finalized: ${cause instanceof Error ? cause.message : String(cause)}`, { retryable: false, cause }); - }); + clearTimeout(stuck); } - return existsSync(this.absolute) ? [{ path: this.relative, startedAt }] : []; } /** @@ -146,11 +163,13 @@ export class Recorder { /** Ends a recording nobody collects: the file it wrote is removed. */ async abandon(): Promise { - await this.switching; - this.stopped = true; - await this.source?.stop(); - this.source = undefined; - await this.encoder?.abort(); + await this.withinCloseTime(async () => { + await this.switching; + this.stopped = true; + await this.source?.stop(); + this.source = undefined; + await this.encoder?.abort(); + }); } } @@ -231,6 +250,13 @@ class FrameEncoder { if (code !== 0) throw new Error(`ffmpeg ${signal === null ? `exited with ${String(code)}` : `was killed (${signal})`}${this.stderr === '' ? '' : `: ${this.stderr.trim()}`}`); } + /** Kills the encoder: the frames it holds are dropped, and a write waiting on it returns. */ + kill(): void { + this.process.kill('SIGKILL'); + // Its input closes on this side too, so a write waiting for room in it returns whatever still holds the pipe. + this.process.stdin?.destroy(); + } + /** Kills the encoder and removes what it wrote. */ async abort(): Promise { this.process.kill('SIGKILL'); diff --git a/packages/electron/tests/integration/launch.test.ts b/packages/electron/tests/integration/launch.test.ts index ffe766b40c..a250835bdf 100644 --- a/packages/electron/tests/integration/launch.test.ts +++ b/packages/electron/tests/integration/launch.test.ts @@ -622,6 +622,22 @@ describe('video', () => { expect(existsSync(path.join(directory, 'video', 'failed.webm'))).toBe(false); }); + it.skipIf(process.platform === 'win32')('ends, in its close time, a recording whose encoder stopped reading', async () => { + // Noise, redrawn every frame: each JPEG is large, so the encoder's input pipe fills in a moment. + const { windows, launch, directory } = appWith(`const noise = ''; + app.whenReady().then(() => new BrowserWindow({ show: ${SHOWN}, width: 600, height: 400 }).loadURL('data:text/html,' + encodeURIComponent(noise)));`); + await launch(); + // An encoder that never reads a frame: its input pipe fills, and a write waits on it for good. + const stuck = path.join(directory, 'stuck-ffmpeg.sh'); + writeFileSync(stuck, '#!/bin/sh\nexec sleep 60\n', { mode: 0o755 }); + const recorder = new Recorder(stuck, directory, 'video/stuck.webm', () => true); + recorder.follow(windows.require().page); + await new Promise((resolve) => setTimeout(resolve, 2000)); + const started = Date.now(); + await expect(recorder.stop()).rejects.toMatchObject({ code: 'ENGINE_FAILURE' }); + expect(Date.now() - started).toBeLessThan(14_000); + }); + it('keeps no frame of a window it may not show, and writes no file for a recording with none', async () => { const { windows, directory, ffmpeg } = await twoWindows(); const [one, two] = windows.open(); From effdb37de5485188bb4ff47dd79bcf04ca4dbaef Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 19:24:46 +0900 Subject: [PATCH 4/9] fix(electron): end a recording whose window never answers the start of its screencast Review: stop() and abandon() waited on the window switch in progress with no bound, so a window whose renderer never answered Page.getLayoutMetrics or Page.startScreencast held the end of the recording, and the app's quit after it, for good. They now wait at most two seconds, and a switch answered after that backs out instead of starting an encoder or a screencast. Co-Authored-By: Claude Opus 5.5 --- packages/electron/src/video.ts | 26 ++++++++++++++++--- .../electron/tests/integration/launch.test.ts | 14 ++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/electron/src/video.ts b/packages/electron/src/video.ts index cc93ffed06..706c2484ac 100644 --- a/packages/electron/src/video.ts +++ b/packages/electron/src/video.ts @@ -23,6 +23,8 @@ const CLOSE_TIMEOUT_MS = 10_000; const STDERR_TAIL = 2_000; /** How long a window gets to hand over its last screen when the recording stops. */ const LAST_SCREEN_MS = 2_000; +/** How long the end of a recording waits for a window switch a window that does not answer holds up. */ +const SWITCH_WAIT_MS = 2_000; interface Size { readonly width: number; @@ -71,6 +73,8 @@ export class Recorder { const { session } = page; if (this.encoder === undefined) { const { cssVisualViewport } = await session.send('Page.getLayoutMetrics'); + // The recording ended while the window was asked: it gets no encoder. + if (this.stopped) return; // Even sides: the encoder's pixel format halves them. const size = { width: Math.max(2, Math.floor(cssVisualViewport.clientWidth / 2) * 2), height: Math.max(2, Math.floor(cssVisualViewport.clientHeight / 2) * 2) }; mkdirSync(path.dirname(this.absolute), { recursive: true }); @@ -92,6 +96,7 @@ export class Recorder { await session.send('Page.stopScreencast').catch(() => undefined); }, }; + const source = this.source; try { await session.send('Page.startScreencast', { format: 'jpeg', quality: JPEG_QUALITY, maxWidth: encoder.size.width, maxHeight: encoder.size.height }); } catch { @@ -101,6 +106,11 @@ export class Recorder { this.source = undefined; return; } + // The recording ended while the screencast started: it sends no frame to an encoder that closed. + if (this.stopped && this.source !== source) { + await source.stop(); + return; + } // A window shown again repaints nothing: the video would hold the window before it. await this.snap(); } @@ -111,8 +121,7 @@ export class Recorder { */ async stop(): Promise { return await this.withinCloseTime(async () => { - await this.switching; - this.stopped = true; + await this.settleSwitching(); const { source, encoder, startedAt } = this; if (encoder === undefined || startedAt === undefined) return []; try { @@ -131,6 +140,16 @@ export class Recorder { }); } + /** + * Waits for the window switch in progress, for at most `SWITCH_WAIT_MS`: a + * window that does not answer the switch's DevTools calls holds it up for + * good. A switch that answers later backs out (`stopped`). + */ + private async settleSwitching(): Promise { + await answerWithin(this.switching, SWITCH_WAIT_MS); + this.stopped = true; + } + /** * Runs the end of a recording with the encoder killed once it has had * `CLOSE_TIMEOUT_MS`: an ffmpeg that stops reading holds a frame's write, @@ -164,8 +183,7 @@ export class Recorder { /** Ends a recording nobody collects: the file it wrote is removed. */ async abandon(): Promise { await this.withinCloseTime(async () => { - await this.switching; - this.stopped = true; + await this.settleSwitching(); await this.source?.stop(); this.source = undefined; await this.encoder?.abort(); diff --git a/packages/electron/tests/integration/launch.test.ts b/packages/electron/tests/integration/launch.test.ts index a250835bdf..35349f802a 100644 --- a/packages/electron/tests/integration/launch.test.ts +++ b/packages/electron/tests/integration/launch.test.ts @@ -638,6 +638,20 @@ describe('video', () => { expect(Date.now() - started).toBeLessThan(14_000); }); + it('ends a recording whose window never answers the start of its screencast', async () => { + const { windows, directory, ffmpeg } = await twoWindows(); + const busy = windows.open()[0]!.page; + // A page that never yields: the window answers no DevTools call its renderer handles. + void busy.session.send('Runtime.evaluate', { expression: 'while (true) {}' }).catch(() => undefined); + const recorder = new Recorder(ffmpeg, directory, 'video/busy.webm', () => true); + recorder.follow(busy); + await new Promise((resolve) => setTimeout(resolve, 300)); + const started = Date.now(); + expect(await recorder.stop()).toEqual([]); + expect(Date.now() - started).toBeLessThan(5000); + expect(existsSync(path.join(directory, 'video', 'busy.webm'))).toBe(false); + }); + it('keeps no frame of a window it may not show, and writes no file for a recording with none', async () => { const { windows, directory, ffmpeg } = await twoWindows(); const [one, two] = windows.open(); From 18b07de7eefa991f7e1fa344d2cb80e5b59d6ed8 Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 19:24:47 +0900 Subject: [PATCH 5/9] chore(electron): add a changeset for the video recording fixes Co-Authored-By: Claude Opus 5.5 --- .changeset/electron-video-close.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/electron-video-close.md diff --git a/.changeset/electron-video-close.md b/.changeset/electron-video-close.md new file mode 100644 index 0000000000..242919e904 --- /dev/null +++ b/.changeset/electron-video-close.md @@ -0,0 +1,5 @@ +--- +'@e2e-dev/electron': patch +--- + +A video recording ends in its close time when ffmpeg stops reading frames or a window never answers the start of its screencast; either used to hold the end of the test for good. A recording whose ffmpeg fails before it writes any video fails with `ENGINE_FAILURE` and leaves no empty file, and a capture of a window that loaded a page outside the app's directory while it was taken is dropped. From 7c920cc978b2b87267db0dd9d98cb588364d1175 Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 23:14:04 +0900 Subject: [PATCH 6/9] fix(electron): bound the end of a screencast when the recording stops Review: Recorder.stop() and abandon() awaited Page.stopScreencast with no bound. The browser's DevTools handlers run on the Electron main process, so an app whose main process is busy answered no stop, and stop() waited past its close time; the close timer then killed ffmpeg before it had written the file, and the recording was lost. Wait at most two seconds for the answer, as the last snapshot and the window switch already do. Co-Authored-By: Claude Opus 5.5 --- packages/electron/src/video.ts | 4 +++- .../electron/tests/integration/launch.test.ts | 22 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/electron/src/video.ts b/packages/electron/src/video.ts index 706c2484ac..3acf3d93d8 100644 --- a/packages/electron/src/video.ts +++ b/packages/electron/src/video.ts @@ -25,6 +25,8 @@ const STDERR_TAIL = 2_000; const LAST_SCREEN_MS = 2_000; /** How long the end of a recording waits for a window switch a window that does not answer holds up. */ const SWITCH_WAIT_MS = 2_000; +/** How long a window gets to answer the end of its screencast: a busy main process answers no DevTools call. */ +const STOP_SCREENCAST_MS = 2_000; interface Size { readonly width: number; @@ -93,7 +95,7 @@ export class Recorder { stop: async () => { off(); page.recording = false; - await session.send('Page.stopScreencast').catch(() => undefined); + await answerWithin(session.send('Page.stopScreencast'), STOP_SCREENCAST_MS); }, }; const source = this.source; diff --git a/packages/electron/tests/integration/launch.test.ts b/packages/electron/tests/integration/launch.test.ts index 35349f802a..99490b6551 100644 --- a/packages/electron/tests/integration/launch.test.ts +++ b/packages/electron/tests/integration/launch.test.ts @@ -566,11 +566,11 @@ describe('video', () => { return frames; } - async function twoWindows(): Promise<{ readonly windows: Windows; readonly directory: string; readonly ffmpeg: string }> { - const { windows, launch, directory } = appWith(TWO_WINDOWS); + async function twoWindows(): Promise<{ readonly app: AppProcess; readonly windows: Windows; readonly directory: string; readonly ffmpeg: string }> { + const { app, windows, launch, directory } = appWith(TWO_WINDOWS); await launch(); await expect.poll(() => windows.open().length).toBe(2); - return { windows, directory, ffmpeg: await ensureFfmpeg(process.env, new AbortController().signal) }; + return { app, windows, directory, ffmpeg: await ensureFfmpeg(process.env, new AbortController().signal) }; } it('records the shown window, and the next one shown, into one file', async () => { @@ -652,6 +652,22 @@ describe('video', () => { expect(existsSync(path.join(directory, 'video', 'busy.webm'))).toBe(false); }); + it('ends a recording, in its close time, while the app\'s main process answers nothing', async () => { + const { app, windows, directory, ffmpeg } = await twoWindows(); + const recorder = new Recorder(ffmpeg, directory, 'video/blocked.webm', () => true); + recorder.follow(windows.open()[0]!.page); + await new Promise((resolve) => setTimeout(resolve, 500)); + // The main process runs the browser's DevTools handlers: while it is busy, no window answers the end of its screencast. + void app.evaluate(() => { + const end = Date.now() + 15_000; + while (Date.now() < end); + }, undefined).catch(() => undefined); + await new Promise((resolve) => setTimeout(resolve, 200)); + const started = Date.now(); + expect(await recorder.stop()).toEqual([{ path: 'video/blocked.webm', startedAt: expect.any(String) as string }]); + expect(Date.now() - started).toBeLessThan(8000); + }); + it('keeps no frame of a window it may not show, and writes no file for a recording with none', async () => { const { windows, directory, ffmpeg } = await twoWindows(); const [one, two] = windows.open(); From a55cb4fde1cfb8bf7665d8674b8e4a9f9bfd7b46 Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 23:15:20 +0900 Subject: [PATCH 7/9] fix(electron): start no screencast for a window switch the end of the recording left waiting Review: when stop() or abandon() gave up waiting for a window switch, the switch could still be waiting on the stop of the window before. Once that answered, it started a screencast on the next window after the recording had ended: its frame listener stayed on and the page stayed marked as recording. The switch now backs out when the recording ended while it waited. Co-Authored-By: Claude Opus 5.5 --- packages/electron/src/video.ts | 2 + packages/electron/tests/unit/video.test.ts | 94 ++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 packages/electron/tests/unit/video.test.ts diff --git a/packages/electron/src/video.ts b/packages/electron/src/video.ts index 3acf3d93d8..0e80ea20ce 100644 --- a/packages/electron/src/video.ts +++ b/packages/electron/src/video.ts @@ -70,6 +70,8 @@ export class Recorder { private async switchTo(page: PageTarget | undefined): Promise { if (this.stopped || this.source?.page === page) return; await this.source?.stop(); + // The recording ended while the window before was stopped: the window shown next gets no screencast. + if (this.stopped) return; this.source = undefined; if (page === undefined || page.isClosed()) return; const { session } = page; diff --git a/packages/electron/tests/unit/video.test.ts b/packages/electron/tests/unit/video.test.ts new file mode 100644 index 0000000000..3dd9f44de9 --- /dev/null +++ b/packages/electron/tests/unit/video.test.ts @@ -0,0 +1,94 @@ +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { afterEach, describe, expect, it } from 'vitest'; +import type { PageTarget } from '../../src/page.ts'; +import { Recorder } from '../../src/video.ts'; + +/** A call the test answers when it chooses. */ +interface Held { + readonly answer: () => void; +} + +/** + * The part of a page target a recording drives: its DevTools session, which + * answers every call at once unless the test holds that method, and whether + * a screencast runs on it. + */ +class FakePage { + recording = false; + readonly generation = 0; + readonly sent: string[] = []; + listeners = 0; + private readonly holds = new Map(); + + readonly session = { + send: (method: string): Promise => { + this.sent.push(method); + const answer = method === 'Page.getLayoutMetrics' ? { cssVisualViewport: { clientWidth: 100, clientHeight: 100 } } : method === 'Page.captureScreenshot' ? { data: 'AAAA' } : {}; + const held = this.holds.get(method)?.shift(); + if (held === undefined) return Promise.resolve(answer); + return new Promise((resolve) => { + (held as { answer: () => void }).answer = () => resolve(answer); + }); + }, + on: (): (() => void) => { + this.listeners += 1; + let on = true; + return () => { + if (on) this.listeners -= 1; + on = false; + }; + }, + }; + + /** Holds the next call of `method` until the returned `answer` is called. */ + hold(method: string): Held { + const held: { answer: () => void } = { answer: () => undefined }; + this.holds.set(method, [...(this.holds.get(method) ?? []), held]); + return { answer: () => held.answer() }; + } + + isClosed(): boolean { + return false; + } + + asTarget(): PageTarget { + return this as unknown as PageTarget; + } +} + +const directories: string[] = []; + +afterEach(() => { + for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }); +}); + +const wait = (ms: number): Promise => new Promise((resolve) => setTimeout(resolve, ms)); + +describe.skipIf(process.platform === 'win32')('Recorder', () => { + it('starts no screencast for a window switch that the end of the recording left waiting', async () => { + const directory = mkdtempSync(path.join(tmpdir(), 'e2e-electron-video-')); + directories.push(directory); + // An encoder that takes every frame and writes nothing. + const encoder = path.join(directory, 'ffmpeg.sh'); + writeFileSync(encoder, '#!/bin/sh\ncat > /dev/null\n', { mode: 0o755 }); + const one = new FakePage(); + const two = new FakePage(); + const startOne = one.hold('Page.startScreencast'); + const recorder = new Recorder(encoder, directory, 'video.webm', () => true); + recorder.follow(one.asTarget()); + recorder.follow(two.asTarget()); + const stopping = recorder.stop(); + // The switch to the first window ends a second into the two the recording's end waits; the switch to the second then waits on the first's stop past them. + const stopOne = one.hold('Page.stopScreencast'); + await wait(1000); + startOne.answer(); + await stopping; + stopOne.answer(); + await wait(100); + expect(two.sent).not.toContain('Page.startScreencast'); + expect(two.recording).toBe(false); + expect(two.listeners).toBe(0); + }); +}); From d20f0bae54167c55c318564d6e052ba3a5d49e45 Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Sun, 29 Nov 2026 23:16:35 +0900 Subject: [PATCH 8/9] chore(electron): name a busy main process in the video changeset The end of a recording is now also bounded when the app's main process answers no DevTools call, so the release note says so. Co-Authored-By: Claude Opus 5.5 --- .changeset/electron-video-close.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/electron-video-close.md b/.changeset/electron-video-close.md index 242919e904..eef76d470b 100644 --- a/.changeset/electron-video-close.md +++ b/.changeset/electron-video-close.md @@ -2,4 +2,4 @@ '@e2e-dev/electron': patch --- -A video recording ends in its close time when ffmpeg stops reading frames or a window never answers the start of its screencast; either used to hold the end of the test for good. A recording whose ffmpeg fails before it writes any video fails with `ENGINE_FAILURE` and leaves no empty file, and a capture of a window that loaded a page outside the app's directory while it was taken is dropped. +A video recording ends in its close time when ffmpeg stops reading frames, a window never answers the start of its screencast, or the app's main process is too busy to answer the end of it; each used to hold the end of the test for good. A recording whose ffmpeg fails before it writes any video fails with `ENGINE_FAILURE` and leaves no empty file, and a capture of a window that loaded a page outside the app's directory while it was taken is dropped. From 518aaafa0d7ba5cb75a1f04ff0e72421f3f7c0fc Mon Sep 17 00:00:00 2001 From: DongYun Kang Date: Mon, 30 Nov 2026 01:12:27 +0900 Subject: [PATCH 9/9] docs(electron): reword the comment on the window switch wait Review: the comment on SWITCH_WAIT_MS did not parse. Co-Authored-By: Claude Opus 5.5 --- packages/electron/src/video.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/electron/src/video.ts b/packages/electron/src/video.ts index 0e80ea20ce..c2b2c2b4a2 100644 --- a/packages/electron/src/video.ts +++ b/packages/electron/src/video.ts @@ -23,7 +23,7 @@ const CLOSE_TIMEOUT_MS = 10_000; const STDERR_TAIL = 2_000; /** How long a window gets to hand over its last screen when the recording stops. */ const LAST_SCREEN_MS = 2_000; -/** How long the end of a recording waits for a window switch a window that does not answer holds up. */ +/** How long the end of a recording waits for a window switch, which a window that does not answer can hold up. */ const SWITCH_WAIT_MS = 2_000; /** How long a window gets to answer the end of its screencast: a busy main process answers no DevTools call. */ const STOP_SCREENCAST_MS = 2_000;