Chrome DevTools for coding agents
test: migrate click_at handler tests to mocks (#3190)
Twot382f4418ac0c
diff --git a/src/third_party/index.ts b/src/third_party/index.tsindex 1fa97406..29103c48 100644--- a/src/third_party/index.ts+++ b/src/third_party/index.ts@@ -71,6 +71,7 @@ export {CdpFrame} from 'puppeteer-core/internal/cdp/Frame.js';export {CdpPage} from 'puppeteer-core/internal/cdp/Page.js';export {CdpScreenRecording} from 'puppeteer-core/internal/cdp/ScreenRecording.js';export {CdpExtension} from 'puppeteer-core/internal/cdp/Extension.js';+export {CdpMouse} from 'puppeteer-core/internal/cdp/Input.js';export type {CdpWebWorker} from 'puppeteer-core/internal/cdp/WebWorker.js';export type {Realm} from 'puppeteer-core/internal/api/Realm.js';export {FrameEvent} from 'puppeteer-core/internal/api/Frame.js';diff --git a/tests/mocks.ts b/tests/mocks.tsindex bd167c28..ff9bc4fd 100644--- a/tests/mocks.ts+++ b/tests/mocks.ts@@ -24,7 +24,7 @@import './setupDevTools.js';-import type {Frame} from 'puppeteer-core';+import type {Frame, Mouse} from 'puppeteer-core';import sinon from 'sinon';import {@@ -49,6 +49,7 @@ import {CdpBrowser,CdpExtension,CdpFrame,+ CdpMouse,CdpPage,CDPSession,Connection,@@ -74,8 +75,14 @@ import type {RunnerResult,} from '../src/third_party/index.js';+export type MockPuppeteerPage = sinon.SinonStubbedInstance<Page> & {+ webmcp: sinon.SinonStubbedInstance<WebMCP>;+ tracing: sinon.SinonStubbedInstance<Tracing>;+ mouse: sinon.SinonStubbedInstance<Mouse>;+};+export type MockMcpPage = sinon.SinonStubbedInstance<McpPage> & {- pptrPage: sinon.SinonStubbedInstance<Page>;+ pptrPage: MockPuppeteerPage;target: sinon.SinonStubbedInstance<MockTarget>;};export type MockMcpContext = sinon.SinonStubbedInstance<McpContext>;@@ -219,10 +226,7 @@ export function createMockCDPSession(): sinon.SinonStubbedInstance<MockCDPSessioreturn session;}-export function createMockPuppeteerPage(): sinon.SinonStubbedInstance<Page> & {- webmcp: sinon.SinonStubbedInstance<WebMCP>;- tracing: sinon.SinonStubbedInstance<Tracing>;-} {+export function createMockPuppeteerPage(): MockPuppeteerPage {const page = sinon.createStubInstance(CdpPage,) as unknown as sinon.SinonStubbedInstance<Page>;@@ -295,10 +299,19 @@ export function createMockPuppeteerPage(): sinon.SinonStubbedInstance<Page> & {configurable: true,});+ const mouse = sinon.createStubInstance(+ CdpMouse,+ ) as unknown as sinon.SinonStubbedInstance<Mouse>;+ Object.defineProperty(page, 'mouse', {+ value: mouse,+ writable: true,+ configurable: true,+ });+page.emulateFocusedPage.resolves();page.evaluateHandle.resolves(createMockElementHandle().handle);- return Object.assign(page, {webmcp, tracing});+ return Object.assign(page, {webmcp, tracing, mouse});}export function createMockWebMCPTool(options: {@@ -370,7 +383,7 @@ export function createMockPuppeteerTarget(export function createMockMcpPage(options: {- pptrPage?: sinon.SinonStubbedInstance<Page>;+ pptrPage?: MockPuppeteerPage;target?: sinon.SinonStubbedInstance<MockTarget>;} = {},): MockMcpPage {diff --git a/tests/tools/input.test.ts b/tests/tools/input.test.tsindex 0b82680b..8ebf2829 100644--- a/tests/tools/input.test.ts+++ b/tests/tools/input.test.ts@@ -551,68 +551,70 @@ describe('input', () => {describe('click_at', () => {it('clicks at coordinates', async () => {- await withMcpContext(async (response, context, args) => {- const page = context.getSelectedMcpPage().pptrPage;- await page.setContent(- html`<div- style="width: 100px; height: 100px; background: red;"- onclick="this.innerText = 'clicked'"- ></div>`,- );- context.getSelectedMcpPage().textSnapshot = await TextSnapshot.create(- context.getSelectedMcpPage(),- );- await clickAt(args).handler(- {- params: {- x: 50,- y: 50,- },- page: context.getSelectedMcpPage(),+ const {page, context, response, args} = createHandlerMocks();+ await clickAt(args).handler(+ {+ params: {+ x: 50,+ y: 50,},- response,- context,- );- assert.strictEqual(- response.responseLines[0],- 'Successfully clicked at the coordinates',- );- assert.ok(response.includeSnapshot);- assert.ok(await page.$('text/clicked'));+ page,+ },+ response,+ context,+ );+ sinon.assert.calledOnceWithExactly(page.pptrPage.mouse.click, 50, 50, {+ count: 1,});+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ 'Successfully clicked at the coordinates',+ );+ sinon.assert.notCalled(response.includeSnapshot);});it('double clicks at coordinates', async () => {- await withMcpContext(async (response, context, args) => {- const page = context.getSelectedMcpPage().pptrPage;- await page.setContent(- html`<div- style="width: 100px; height: 100px; background: red;"- ondblclick="this.innerText = 'dblclicked'"- ></div>`,- );- context.getSelectedMcpPage().textSnapshot = await TextSnapshot.create(- context.getSelectedMcpPage(),- );- await clickAt(args).handler(- {- params: {- x: 50,- y: 50,- dblClick: true,- },- page: context.getSelectedMcpPage(),+ const {page, context, response, args} = createHandlerMocks();+ await clickAt(args).handler(+ {+ params: {+ x: 50,+ y: 50,+ dblClick: true,},- response,- context,- );- assert.strictEqual(- response.responseLines[0],- 'Successfully double clicked at the coordinates',- );- assert.ok(response.includeSnapshot);- assert.ok(await page.$('text/dblclicked'));+ page,+ },+ response,+ context,+ );+ sinon.assert.calledOnceWithExactly(page.pptrPage.mouse.click, 50, 50, {+ count: 2,+ });+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ 'Successfully double clicked at the coordinates',+ );+ sinon.assert.notCalled(response.includeSnapshot);+ });++ it('includes snapshot if includeSnapshot is true', async () => {+ const {page, context, response, args} = createHandlerMocks();+ await clickAt(args).handler(+ {+ params: {+ x: 50,+ y: 50,+ includeSnapshot: true,+ },+ page,+ },+ response,+ context,+ );+ sinon.assert.calledOnceWithExactly(page.pptrPage.mouse.click, 50, 50, {+ count: 1,});+ sinon.assert.calledOnce(response.includeSnapshot);});});