Chrome DevTools for coding agents
fix: handle screencast file extensions case-insensitively and clean up temp dir on failure (#2207)
Nebrass Lamouchiba80096521da
diff --git a/src/tools/screencast.ts b/src/tools/screencast.tsindex cc26e6cd..2e611146 100644--- a/src/tools/screencast.ts+++ b/src/tools/screencast.ts@@ -48,17 +48,29 @@ export const startScreencast = definePageTool(args => ({return;}- const filePath = request.params.filePath ?? (await generateTempFilePath());- let enforcedExtension = '.mp4' as `.${string}`;- let format: VideoFormat = 'mp4';+ const requestedFilePath = request.params.filePath;+ const filePath = requestedFilePath ?? (await generateTempFilePath());- for (const supportedExtension of supportedExtensions) {- if (filePath.endsWith(supportedExtension)) {- enforcedExtension = supportedExtension;- format = supportedExtension.substring(1) as VideoFormat;- break;- }+ // Match the extension case-insensitively so e.g. `.WEBM` is recognized as+ // WebM. An explicitly requested but unsupported extension is rejected+ // rather than being silently rewritten to `.mp4` (which would change both+ // the format and the output path from what was requested). A missing+ // extension falls back to `.mp4`. The matched extension is normalized to+ // lower case.+ const requestedExtension = path.extname(filePath);+ const matchedExtension = supportedExtensions.find(+ supportedExtension =>+ supportedExtension === requestedExtension.toLowerCase(),+ );+ if (!matchedExtension && requestedExtension !== '') {+ throw new Error(+ `Unsupported screencast file extension "${requestedExtension}". ` ++ `Supported formats: ${supportedExtensions.join(', ')} (case-insensitive).`,+ );}+ const enforcedExtension: `.${string}` = matchedExtension ?? '.mp4';+ const format: VideoFormat = (matchedExtension?.substring(1) ??+ 'mp4') as VideoFormat;const resolvedPath = ensureExtension(path.resolve(filePath),@@ -75,6 +87,18 @@ export const startScreencast = definePageTool(args => ({ffmpegPath: args?.experimentalFfmpegPath,});} catch (err) {+ // If we generated a temporary directory for this recording, remove it so+ // a failed start (e.g. ffmpeg missing) does not leak an empty directory.+ if (requestedFilePath === undefined) {+ try {+ await fs.rm(path.dirname(resolvedPath), {+ recursive: true,+ force: true,+ });+ } catch {+ // no-op+ }+ }const message = err instanceof Error ? err.message : String(err);if (message.includes('ENOENT') && message.includes('ffmpeg')) {throw new Error(diff --git a/tests/tools/screencast.test.ts b/tests/tools/screencast.test.tsindex 4708f12c..2eb43412 100644--- a/tests/tools/screencast.test.ts+++ b/tests/tools/screencast.test.ts@@ -5,6 +5,8 @@*/import assert from 'node:assert';+import fs from 'node:fs/promises';+import path from 'node:path';import {describe, it, afterEach} from 'node:test';import sinon from 'sinon';@@ -56,6 +58,53 @@ describe('screencast', () => {});});+ it('records WebM for an uppercase extension (case-insensitive)', async () => {+ await withMcpContext(async (response, context) => {+ const mockRecorder = createMockRecorder();+ const selectedPage = context.getSelectedPptrPage();+ const screencastStub = sinon+ .stub(selectedPage, 'screencast')+ .resolves(mockRecorder as never);++ await startScreencast().handler(+ {+ params: {filePath: '/tmp/test-recording.WEBM'},+ page: context.getSelectedMcpPage(),+ },+ response,+ context,+ );++ sinon.assert.calledOnce(screencastStub);+ const callArgs = screencastStub.firstCall.args[0];+ assert.ok(callArgs);+ assert.strictEqual(callArgs.format, 'webm');+ assert.ok(callArgs.path?.endsWith('.webm'));+ });+ });++ it('rejects an unsupported extension instead of silently using mp4', async () => {+ await withMcpContext(async (response, context) => {+ const selectedPage = context.getSelectedPptrPage();+ const screencastStub = sinon.stub(selectedPage, 'screencast');++ await assert.rejects(+ startScreencast().handler(+ {+ params: {filePath: '/tmp/recording.avi'},+ page: context.getSelectedMcpPage(),+ },+ response,+ context,+ ),+ /Unsupported screencast file extension/,+ );++ sinon.assert.notCalled(screencastStub);+ assert.strictEqual(context.getScreenRecorder(), null);+ });+ });+it('starts a screencast recording with temp file when no filePath', async () => {await withMcpContext(async (response, context) => {const mockRecorder = createMockRecorder();@@ -126,6 +175,30 @@ describe('screencast', () => {});});+ it('cleans up the generated temp directory if recording fails to start', async () => {+ await withMcpContext(async (response, context) => {+ const selectedPage = context.getSelectedPptrPage();+ const screencastStub = sinon+ .stub(selectedPage, 'screencast')+ .rejects(new Error('spawn ffmpeg ENOENT'));++ await assert.rejects(+ startScreencast().handler(+ {params: {}, page: context.getSelectedMcpPage()},+ response,+ context,+ ),+ /ffmpeg is required for screencast recording/,+ );++ // The temp directory generateTempFilePath() created must be removed.+ const tempPath = screencastStub.firstCall.args[0]?.path as string;+ assert.ok(tempPath);+ await assert.rejects(fs.access(path.dirname(tempPath)));+ assert.strictEqual(context.getScreenRecorder(), null);+ });+ });+it('passes ffmpegPath from args to puppeteer', async () => {await withMcpContext(async (response, context) => {const mockRecorder = createMockRecorder();