Chrome DevTools for coding agents
test: migrate screencast tests off withMcpContext to unit tests (#2683)
Mahesh Singh02c5911a44e5
diff --git a/src/third_party/index.ts b/src/third_party/index.tsindex d7b49695..157d3df8 100644--- a/src/third_party/index.ts+++ b/src/third_party/index.ts@@ -40,6 +40,7 @@ export {PredefinedNetworkConditions,KnownDevices,CDPSessionEvent,+ ScreenRecorder,} from 'puppeteer-core';export {default as puppeteer} from 'puppeteer-core';export type * from 'puppeteer-core';diff --git a/tests/mocks.ts b/tests/mocks.tsindex 8bf2c9e6..e34ce93f 100644--- a/tests/mocks.ts+++ b/tests/mocks.ts@@ -31,7 +31,9 @@ import {McpResponse} from '../src/McpResponse.js';import {CdpPage} from '../src/third_party/index.js';import type {Page} from '../src/third_party/index.js';-export type MockMcpPage = sinon.SinonStubbedInstance<McpPage>;+export type MockMcpPage = sinon.SinonStubbedInstance<McpPage> & {+ pptrPage: sinon.SinonStubbedInstance<Page>;+};export type MockMcpContext = sinon.SinonStubbedInstance<McpContext>;export type MockMcpResponse = sinon.SinonStubbedInstance<McpResponse>;@@ -96,8 +98,12 @@ export function createMockPuppeteerPage(): sinon.SinonStubbedInstance<Page> {return page;}-export function createMockMcpPage(): MockMcpPage {- return sinon.createStubInstance(McpPage);+export function createMockMcpPage(+ options: {pptrPage?: sinon.SinonStubbedInstance<Page>} = {},+): MockMcpPage {+ const page = sinon.createStubInstance(McpPage);+ const pptrPage = options.pptrPage ?? createMockPuppeteerPage();+ return Object.assign(page, {pptrPage});}export function createMockMcpContext(@@ -106,6 +112,7 @@ export function createMockMcpContext(const context = sinon.createStubInstance(McpContext);const page = options.selectedPage ?? createMockMcpPage();context.getSelectedMcpPage.returns(page satisfies McpPage);+return context;}diff --git a/tests/tools/screencast.test.ts b/tests/tools/screencast.test.tsindex 5dc3008b..a5a7e4af 100644--- a/tests/tools/screencast.test.ts+++ b/tests/tools/screencast.test.ts@@ -8,18 +8,25 @@ import assert from 'node:assert';import fs from 'node:fs/promises';import os from 'node:os';import path from 'node:path';-import {describe, it, afterEach} from 'node:test';+import {afterEach, describe, it} from 'node:test';import sinon from 'sinon';import {parseArguments} from '../../src/config/mcp-options.js';+import {ScreenRecorder} from '../../src/third_party/index.js';import {startScreencast, stopScreencast} from '../../src/tools/screencast.js';-import {withMcpContext} from '../utils.js';+import {createHandlerMocks} from '../mocks.js';-function createMockRecorder() {- return {- stop: sinon.stub().resolves(),- };+function createMockScreenRecorder(): sinon.SinonStubbedInstance<ScreenRecorder> {+ return sinon.createStubInstance(ScreenRecorder);+}++function createScreencastMocks() {+ const {page, context, response} = createHandlerMocks();+ const mockRecorder = createMockScreenRecorder();+ page.pptrPage.screencast.resolves(mockRecorder);+ context.getScreenRecorder.returns(null);+ return {page, context, response, mockRecorder};}describe('screencast', () => {@@ -29,293 +36,301 @@ describe('screencast', () => {describe('screencast_start', () => {it('starts a screencast recording with filePath', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- const selectedPage = context.getSelectedMcpPage().pptrPage;- const screencastStub = sinon- .stub(selectedPage, 'screencast')- .resolves(mockRecorder as never);-- await startScreencast().handler(- {- params: {filePath: path.join(os.tmpdir(), 'test-recording.mp4')},- page: context.getSelectedMcpPage(),- },- response,- context,- );-- sinon.assert.calledOnce(screencastStub);- const callArgs = screencastStub.firstCall.args[0];- assert.ok(callArgs);- assert.ok(callArgs.path?.endsWith('test-recording.mp4'));-- assert.ok(context.getScreenRecorder() !== null);- assert.ok(- response.responseLines- .join('\n')- .includes('Screencast recording started'),- );+ const {page, context, response, mockRecorder} = createScreencastMocks();++ const filePath: `${string}.mp4` = `${path.join(+ os.tmpdir(),+ 'test-recording',+ )}.mp4`;+ context.ensureExtension.resolves(filePath);++ await startScreencast().handler(+ {+ params: {filePath},+ page,+ },+ response,+ context,+ );++ sinon.assert.calledOnceWithExactly(+ context.ensureExtension,+ filePath,+ '.mp4',+ );+ sinon.assert.calledOnceWithExactly(page.pptrPage.screencast, {+ path: filePath,+ format: 'mp4',+ ffmpegPath: undefined,+ fps: undefined,+ });+ sinon.assert.calledOnceWithExactly(context.setScreenRecorder, {+ recorder: mockRecorder,+ filePath,});+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ `Screencast recording started. The recording will be saved to ${filePath}. Use screencast_stop to stop recording.`,+ );});it('records WebM for an uppercase extension (case-insensitive)', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- const selectedPage = context.getSelectedMcpPage().pptrPage;- const screencastStub = sinon- .stub(selectedPage, 'screencast')- .resolves(mockRecorder as never);-- await startScreencast().handler(- {- params: {filePath: path.join(os.tmpdir(), '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'));+ const {page, context, response, mockRecorder} = createScreencastMocks();++ const requestedPath = `${path.join(os.tmpdir(), 'test-recording')}.WEBM`;+ const expectedPath: `${string}.webm` = `${path.join(+ os.tmpdir(),+ 'test-recording',+ )}.webm`;+ context.ensureExtension.resolves(expectedPath);++ await startScreencast().handler(+ {+ params: {filePath: requestedPath},+ page,+ },+ response,+ context,+ );++ sinon.assert.calledOnceWithExactly(+ context.ensureExtension,+ requestedPath,+ '.webm',+ );+ sinon.assert.calledOnceWithExactly(page.pptrPage.screencast, {+ path: expectedPath,+ format: 'webm',+ ffmpegPath: undefined,+ fps: undefined,+ });+ sinon.assert.calledOnceWithExactly(context.setScreenRecorder, {+ recorder: mockRecorder,+ filePath: expectedPath,});});it('rejects an unsupported extension instead of silently using mp4', async () => {- await withMcpContext(async (response, context) => {- const selectedPage = context.getSelectedMcpPage().pptrPage;- const screencastStub = sinon.stub(selectedPage, 'screencast');-- await assert.rejects(- startScreencast().handler(- {- params: {filePath: path.join(os.tmpdir(), 'recording.avi')},- page: context.getSelectedMcpPage(),- },- response,- context,- ),- /Unsupported screencast file extension/,- );-- sinon.assert.notCalled(screencastStub);- assert.strictEqual(context.getScreenRecorder(), null);- });- });+ const {page, context, response} = createScreencastMocks();+ const filePath = `${path.join(os.tmpdir(), 'recording')}.avi`;- it('starts a screencast recording with temp file when no filePath', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- const selectedPage = context.getSelectedMcpPage().pptrPage;- const screencastStub = sinon- .stub(selectedPage, 'screencast')- .resolves(mockRecorder as never);-- await startScreencast().handler(- {params: {}, page: context.getSelectedMcpPage()},+ await assert.rejects(+ startScreencast().handler(+ {+ params: {filePath},+ page,+ },response,context,- );+ ),+ /Unsupported screencast file extension/,+ );++ sinon.assert.notCalled(context.ensureExtension);+ sinon.assert.notCalled(page.pptrPage.screencast);+ sinon.assert.notCalled(context.setScreenRecorder);+ });- sinon.assert.calledOnce(screencastStub);- const callArgs = screencastStub.firstCall.args[0];- assert.ok(callArgs);- assert.ok(callArgs.path?.endsWith('.mp4'));- assert.ok(context.getScreenRecorder() !== null);+ it('starts a screencast recording with temp file when no filePath', async () => {+ const {page, context, response, mockRecorder} = createScreencastMocks();+ const expectedPath: `${string}.mp4` = `${path.join(+ os.tmpdir(),+ 'temp-screencast',+ )}.mp4`;+ context.ensureExtension.resolves(expectedPath);++ await startScreencast().handler({params: {}, page}, response, context);++ sinon.assert.calledOnce(context.ensureExtension);+ assert.ok(context.ensureExtension.firstCall.args[0].endsWith('.mp4'));+ assert.strictEqual(context.ensureExtension.firstCall.args[1], '.mp4');+ sinon.assert.calledOnceWithExactly(page.pptrPage.screencast, {+ path: expectedPath,+ format: 'mp4',+ ffmpegPath: undefined,+ fps: undefined,+ });+ sinon.assert.calledOnceWithExactly(context.setScreenRecorder, {+ recorder: mockRecorder,+ filePath: expectedPath,});});it('errors if a recording is already active', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- context.setScreenRecorder({- recorder: mockRecorder as never,- filePath: path.join(os.tmpdir(), 'existing.mp4'),- });-- const selectedPage = context.getSelectedMcpPage().pptrPage;- const screencastStub = sinon.stub(selectedPage, 'screencast');-- await startScreencast().handler(- {params: {}, page: context.getSelectedMcpPage()},- response,- context,- );-- sinon.assert.notCalled(screencastStub);- assert.ok(- response.responseLines- .join('\n')- .includes('a screencast recording is already in progress'),- );+ const {page, context, response, mockRecorder} = createScreencastMocks();+ context.getScreenRecorder.returns({+ recorder: mockRecorder,+ filePath: `${path.join(os.tmpdir(), 'existing')}.mp4`,});++ await startScreencast().handler({params: {}, page}, response, context);++ sinon.assert.notCalled(context.ensureExtension);+ sinon.assert.notCalled(page.pptrPage.screencast);+ sinon.assert.notCalled(context.setScreenRecorder);+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ 'Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.',+ );});it('provides a clear error when ffmpeg is not found', async () => {- await withMcpContext(async (response, context) => {- const selectedPage = context.getSelectedMcpPage().pptrPage;- const error = new Error('spawn ffmpeg ENOENT');- sinon.stub(selectedPage, 'screencast').rejects(error);-- await assert.rejects(- startScreencast().handler(- {- params: {filePath: path.join(os.tmpdir(), 'test.mp4')},- page: context.getSelectedMcpPage(),- },- response,- context,- ),- /ffmpeg is required for screencast recording/,- );-- assert.strictEqual(context.getScreenRecorder(), null);- });+ const {page, context, response} = createScreencastMocks();+ const filePath: `${string}.mp4` = `${path.join(os.tmpdir(), 'test')}.mp4`;+ context.ensureExtension.resolves(filePath);+ page.pptrPage.screencast.rejects(new Error('spawn ffmpeg ENOENT'));++ await assert.rejects(+ startScreencast().handler(+ {+ params: {filePath},+ page,+ },+ response,+ context,+ ),+ /ffmpeg is required for screencast recording/,+ );++ sinon.assert.calledOnceWithExactly(+ context.ensureExtension,+ filePath,+ '.mp4',+ );+ sinon.assert.notCalled(context.setScreenRecorder);});it('cleans up the generated temp directory if recording fails to start', async () => {- await withMcpContext(async (response, context) => {- const selectedPage = context.getSelectedMcpPage().pptrPage;- 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);- });+ const {page, context, response} = createScreencastMocks();+ context.ensureExtension.callsFake(async filePath => `${filePath}.mp4`);+ page.pptrPage.screencast.rejects(new Error('spawn ffmpeg ENOENT'));++ await assert.rejects(+ startScreencast().handler({params: {}, page}, response, context),+ /ffmpeg is required for screencast recording/,+ );++ const tempPath = page.pptrPage.screencast.firstCall.args[0]?.path;+ assert.ok(tempPath);+ await assert.rejects(fs.access(path.dirname(tempPath)));+ sinon.assert.notCalled(context.setScreenRecorder);});it('passes ffmpegPath from args to puppeteer', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- const selectedPage = context.getSelectedMcpPage().pptrPage;- const screencastStub = sinon- .stub(selectedPage, 'screencast')- .resolves(mockRecorder as never);-- const experimentalFfmpegPath = '/custom/path/to/ffmpeg';- const args = parseArguments('test', [- 'node',- 'test',- '--experimental-screencast',- `--experimental-ffmpeg-path=${experimentalFfmpegPath}`,- ]);- await startScreencast(args).handler(- {params: {}, page: context.getSelectedMcpPage()},- response,- context,- );-- sinon.assert.calledOnce(screencastStub);- const callArgs = screencastStub.firstCall.args[0];- assert.strictEqual(callArgs?.ffmpegPath, experimentalFfmpegPath);+ const {page, context, response, mockRecorder} = createScreencastMocks();+ const filePath: `${string}.mp4` = `${path.join(os.tmpdir(), 'test')}.mp4`;+ context.ensureExtension.resolves(filePath);++ const experimentalFfmpegPath = '/custom/path/to/ffmpeg';+ const args = parseArguments('test', [+ 'node',+ 'test',+ '--experimental-screencast',+ `--experimental-ffmpeg-path=${experimentalFfmpegPath}`,+ ]);+ await startScreencast(args).handler(+ {params: {filePath}, page},+ response,+ context,+ );++ sinon.assert.calledOnceWithExactly(+ context.ensureExtension,+ filePath,+ '.mp4',+ );+ sinon.assert.calledOnceWithExactly(page.pptrPage.screencast, {+ path: filePath,+ format: 'mp4',+ ffmpegPath: experimentalFfmpegPath,+ fps: undefined,+ });+ sinon.assert.calledOnceWithExactly(context.setScreenRecorder, {+ recorder: mockRecorder,+ filePath,});});it('passes screencast fps from args to puppeteer', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- const selectedPage = context.getSelectedMcpPage().pptrPage;- const screencastStub = sinon- .stub(selectedPage, 'screencast')- .resolves(mockRecorder as never);-- const args = parseArguments('test', [- 'node',- 'test',- '--experimental-screencast',- '--experimental-screencast-fps=10',- ]);- await startScreencast(args).handler(- {params: {}, page: context.getSelectedMcpPage()},- response,- context,- );-- sinon.assert.calledOnce(screencastStub);- const callArgs = screencastStub.firstCall.args[0];- assert.strictEqual(callArgs?.fps, 10);+ const {page, context, response, mockRecorder} = createScreencastMocks();+ const filePath: `${string}.mp4` = `${path.join(os.tmpdir(), 'test')}.mp4`;+ context.ensureExtension.resolves(filePath);++ const args = parseArguments('test', [+ 'node',+ 'test',+ '--experimental-screencast',+ '--experimental-screencast-fps=10',+ ]);+ await startScreencast(args).handler(+ {params: {filePath}, page},+ response,+ context,+ );++ sinon.assert.calledOnceWithExactly(+ context.ensureExtension,+ filePath,+ '.mp4',+ );+ sinon.assert.calledOnceWithExactly(page.pptrPage.screencast, {+ path: filePath,+ format: 'mp4',+ ffmpegPath: undefined,+ fps: 10,+ });+ sinon.assert.calledOnceWithExactly(context.setScreenRecorder, {+ recorder: mockRecorder,+ filePath,});});});describe('screencast_stop', () => {it('returns an error message if no recording is active', async () => {- await withMcpContext(async (response, context) => {- assert.strictEqual(context.getScreenRecorder(), null);- await stopScreencast.handler(- {params: {}, page: context.getSelectedMcpPage()},- response,- context,- );- assert.ok(- response.responseLines- .join('\n')- .includes('no active screencast recording to stop'),- );- });+ const {page, context, response} = createScreencastMocks();+ await stopScreencast.handler({params: {}, page}, response, context);+ sinon.assert.notCalled(context.setScreenRecorder);+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ 'Error: no active screencast recording to stop.',+ );});it('stops an active recording and reports the file path', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- const filePath = path.join(os.tmpdir(), 'test-recording.mp4');- context.setScreenRecorder({- recorder: mockRecorder as never,- filePath,- });-- await stopScreencast.handler(- {params: {}, page: context.getSelectedMcpPage()},- response,- context,- );-- sinon.assert.calledOnce(mockRecorder.stop);- assert.strictEqual(context.getScreenRecorder(), null);- assert.ok(- response.responseLines- .join('\n')- .includes(`stopped and saved to ${filePath}`),- );+ const {page, context, response, mockRecorder} = createScreencastMocks();+ const filePath = `${path.join(os.tmpdir(), 'test-recording')}.mp4`;+ context.getScreenRecorder.returns({+ recorder: mockRecorder,+ filePath,});++ await stopScreencast.handler({params: {}, page}, response, context);++ sinon.assert.calledOnce(mockRecorder.stop);+ sinon.assert.calledOnceWithExactly(context.setScreenRecorder, null);+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ `The screencast recording has been stopped and saved to ${filePath}.`,+ );});it('clears the recorder even if stop() throws', async () => {- await withMcpContext(async (response, context) => {- const mockRecorder = createMockRecorder();- mockRecorder.stop.rejects(new Error('ffmpeg process error'));- context.setScreenRecorder({- recorder: mockRecorder as never,- filePath: path.join(os.tmpdir(), 'test.mp4'),- });-- await assert.rejects(- stopScreencast.handler(- {params: {}, page: context.getSelectedMcpPage()},- response,- context,- ),- /ffmpeg process error/,- );-- assert.strictEqual(context.getScreenRecorder(), null);+ const {page, context, response, mockRecorder} = createScreencastMocks();+ mockRecorder.stop.rejects(new Error('ffmpeg process error'));+ const filePath = `${path.join(os.tmpdir(), 'test')}.mp4`;+ context.getScreenRecorder.returns({+ recorder: mockRecorder,+ filePath,});++ await assert.rejects(+ stopScreencast.handler({params: {}, page}, response, context),+ /ffmpeg process error/,+ );++ sinon.assert.calledOnce(mockRecorder.stop);+ sinon.assert.calledOnceWithExactly(context.setScreenRecorder, null);});});});