Skip to content

Chrome DevTools for coding agents

test: migrate screenshot handler tests to mocks (#3183)

patrickson80b395fdeba4
diff --git a/tests/tools/screenshot.test.ts b/tests/tools/screenshot.test.ts
index 2800c64c..756fa6dc 100644
--- a/tests/tools/screenshot.test.ts
+++ b/tests/tools/screenshot.test.ts
@@ -188,57 +188,97 @@ describe('screenshot', () => {
});
});
it('ignores quality', async () => {
- await withMcpContext(async (response, context) => {
- const fixture = screenshots.basic;
- const page = context.getSelectedMcpPage().pptrPage;
- await page.setContent(fixture.html);
- await screenshotTool.handler(
- {
- params: {format: 'png', quality: 0},
- page: context.getSelectedMcpPage(),
- },
- response,
- context,
- );
+ const {page, context, response, args} = createHandlerMocks();
+ page.pptrPage.screenshot.resolves(new Uint8Array([1, 2, 3]));
- assert.equal(response.images.length, 1);
- assert.equal(response.images[0].mimeType, 'image/png');
- assert.equal(
- response.responseLines.at(0),
- "Took a screenshot of the current page's viewport.",
- );
+ await screenshot(args).handler(
+ {params: {format: 'png', quality: 0}, page},
+ response,
+ context,
+ );
+
+ sinon.assert.calledOnceWithExactly(page.pptrPage.screenshot, {
+ type: 'png',
+ fullPage: false,
+ quality: undefined,
+ optimizeForSpeed: true,
+ });
+ sinon.assert.calledOnceWithExactly(
+ response.appendResponseLine,
+ "Took a screenshot of the current page's viewport.",
+ );
+ sinon.assert.calledOnceWithExactly(response.attachImage, {
+ mimeType: 'image/png',
+ data: 'AQID',
});
});
it('with jpeg', async () => {
- await withMcpContext(async (response, context) => {
- await screenshotTool.handler(
- {params: {format: 'jpeg'}, page: context.getSelectedMcpPage()},
- response,
- context,
- );
+ const {page, context, response, args} = createHandlerMocks();
+ page.pptrPage.screenshot.resolves(new Uint8Array([1, 2, 3]));
- assert.equal(response.images.length, 1);
- assert.equal(response.images[0].mimeType, 'image/jpeg');
- assert.equal(
- response.responseLines.at(0),
- "Took a screenshot of the current page's viewport.",
- );
+ await screenshot(args).handler(
+ {params: {format: 'jpeg'}, page},
+ response,
+ context,
+ );
+
+ sinon.assert.calledOnceWithExactly(page.pptrPage.screenshot, {
+ type: 'jpeg',
+ fullPage: false,
+ quality: undefined,
+ optimizeForSpeed: true,
+ });
+ sinon.assert.calledOnceWithExactly(
+ response.appendResponseLine,
+ "Took a screenshot of the current page's viewport.",
+ );
+ sinon.assert.calledOnceWithExactly(response.attachImage, {
+ mimeType: 'image/jpeg',
+ data: 'AQID',
+ });
+ });
+ it('forwards the requested quality for jpeg', async () => {
+ const {page, context, response, args} = createHandlerMocks();
+ page.pptrPage.screenshot.resolves(new Uint8Array([1, 2, 3]));
+
+ await screenshot(args).handler(
+ {params: {format: 'jpeg', quality: 50}, page},
+ response,
+ context,
+ );
+
+ sinon.assert.calledOnceWithExactly(page.pptrPage.screenshot, {
+ type: 'jpeg',
+ fullPage: false,
+ quality: 50,
+ optimizeForSpeed: true,
});
});
it('with webp', async () => {
- await withMcpContext(async (response, context) => {
- await screenshotTool.handler(
- {params: {format: 'webp'}, page: context.getSelectedMcpPage()},
- response,
- context,
- );
+ const {page, context, response, args} = createHandlerMocks({
+ screenshotQuality: 80,
+ });
+ page.pptrPage.screenshot.resolves(new Uint8Array([1, 2, 3]));
- assert.equal(response.images.length, 1);
- assert.equal(response.images[0].mimeType, 'image/webp');
- assert.equal(
- response.responseLines.at(0),
- "Took a screenshot of the current page's viewport.",
- );
+ await screenshot(args).handler(
+ {params: {format: 'webp'}, page},
+ response,
+ context,
+ );
+
+ sinon.assert.calledOnceWithExactly(page.pptrPage.screenshot, {
+ type: 'webp',
+ fullPage: false,
+ quality: 80,
+ optimizeForSpeed: true,
+ });
+ sinon.assert.calledOnceWithExactly(
+ response.appendResponseLine,
+ "Took a screenshot of the current page's viewport.",
+ );
+ sinon.assert.calledOnceWithExactly(response.attachImage, {
+ mimeType: 'image/webp',
+ data: 'AQID',
});
});
it('with full page', async () => {
@@ -332,53 +372,45 @@ describe('screenshot', () => {
});
it('disposes the element handle after an element screenshot', async () => {
- await withMcpContext(async (response, context) => {
- const fixture = screenshots.button;
- const mcpPage = context.getSelectedMcpPage();
- await mcpPage.pptrPage.setContent(fixture.html);
- mcpPage.textSnapshot = await TextSnapshot.create(mcpPage);
- const handle = await mcpPage.getElementByUid('1_1');
- const disposeSpy = sinon.spy(handle, 'dispose');
- sinon.stub(mcpPage, 'getElementByUid').resolves(handle);
+ const {page, context, response, args} = createHandlerMocks();
+ const {handle} = createMockElementHandle();
+ page.getElementByUid.resolves(handle);
+ handle.screenshot.resolves(new Uint8Array());
- await screenshotTool.handler(
- {
- params: {format: 'png', uid: '1_1'},
- page: mcpPage,
- },
- response,
- context,
- );
+ await screenshot(args).handler(
+ {params: {format: 'png', uid: '1_1'}, page},
+ response,
+ context,
+ );
- sinon.assert.calledOnce(disposeSpy);
+ sinon.assert.calledOnceWithExactly(page.getElementByUid, '1_1');
+ sinon.assert.calledOnceWithExactly(handle.screenshot, {
+ type: 'png',
+ quality: undefined,
+ optimizeForSpeed: true,
});
+ sinon.assert.notCalled(page.pptrPage.screenshot);
+ sinon.assert.calledOnceWithExactly(handle.dispose);
});
it('disposes the element handle when the capture fails', async () => {
- await withMcpContext(async (response, context) => {
- const fixture = screenshots.button;
- const mcpPage = context.getSelectedMcpPage();
- await mcpPage.pptrPage.setContent(fixture.html);
- mcpPage.textSnapshot = await TextSnapshot.create(mcpPage);
- const handle = await mcpPage.getElementByUid('1_1');
- const disposeSpy = sinon.spy(handle, 'dispose');
- sinon.stub(handle, 'screenshot').rejects(new Error('Capture failed'));
- sinon.stub(mcpPage, 'getElementByUid').resolves(handle);
+ const {page, context, response, args} = createHandlerMocks();
+ const {handle} = createMockElementHandle();
+ page.getElementByUid.resolves(handle);
+ handle.screenshot.rejects(new Error('Capture failed'));
- await assert.rejects(
- screenshotTool.handler(
- {
- params: {format: 'png', uid: '1_1'},
- page: mcpPage,
- },
- response,
- context,
- ),
- /Capture failed/,
- );
+ await assert.rejects(
+ screenshot(args).handler(
+ {params: {format: 'png', uid: '1_1'}, page},
+ response,
+ context,
+ ),
+ /Capture failed/,
+ );
- sinon.assert.calledOnce(disposeSpy);
- });
+ sinon.assert.notCalled(page.pptrPage.screenshot);
+ sinon.assert.calledOnceWithExactly(handle.dispose);
+ sinon.assert.notCalled(response.attachImage);
});
it('with filePath', async () => {