Chrome DevTools for coding agents
test: migrate slim handler validation tests to mocks (#2751)
lisa0314655031baeca2
diff --git a/tests/tools/slim/tools.test.js.snapshot b/tests/tools/slim/tools.test.js.snapshotindex 7e1d12b1..657bffdc 100644--- a/tests/tools/slim/tools.test.js.snapshot+++ b/tests/tools/slim/tools.test.js.snapshot@@ -1,11 +1,3 @@-exports[`slim > evaluates 1`] = `-10-`;--exports[`slim > handles errors 1`] = `-test error-`;-exports[`slim > navigates to correct page 1`] = `Navigated to data:text/html,<div>Hello MCP</div>.`;diff --git a/tests/tools/slim/tools.test.ts b/tests/tools/slim/tools.test.tsindex c6c5bb26..3d9543e3 100644--- a/tests/tools/slim/tools.test.ts+++ b/tests/tools/slim/tools.test.ts@@ -7,44 +7,44 @@import assert from 'node:assert';import fs from 'node:fs';import path from 'node:path';-import {describe, it} from 'node:test';+import {afterEach, describe, it} from 'node:test';++import sinon from 'sinon';import {parseArguments} from '../../../src/config/mcp-options.js';import {evaluate, navigate, screenshot} from '../../../src/tools/slim/tools.js';+import {createHandlerMocks} from '../../mocks.js';import {screenshots} from '../../snapshot.js';import {withMcpContext} from '../../utils.js';describe('slim', () => {- it('evaluates', async t => {- await withMcpContext(async (response, context) => {- await evaluate.handler(- {- params: {- script: `2 * 5`,- },- page: context.getSelectedMcpPage(),- },- response,- context,- );- t.assert.snapshot(response.responseLines.join('\n'));- });+ afterEach(() => {+ sinon.restore();});- it('handles errors', async t => {- await withMcpContext(async (response, context) => {- await evaluate.handler(- {- params: {- script: `throw new Error('test error')`,- },- page: context.getSelectedMcpPage(),- },- response,- context,- );- t.assert.snapshot(response.responseLines.join('\n'));- });+ it('evaluates', async () => {+ const {page, context, response} = createHandlerMocks();+ const script = '2 * 5';+ page.pptrPage.evaluate.resolves(10);++ await evaluate.handler({params: {script}, page}, response, context);++ sinon.assert.calledOnceWithExactly(page.pptrPage.evaluate, script);+ sinon.assert.calledOnceWithExactly(response.appendResponseLine, '10');+ });++ it('handles errors', async () => {+ const {page, context, response} = createHandlerMocks();+ const script = "throw new Error('test error')";+ page.pptrPage.evaluate.rejects(new Error('test error'));++ await evaluate.handler({params: {script}, page}, response, context);++ sinon.assert.calledOnceWithExactly(page.pptrPage.evaluate, script);+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ 'test error',+ );});it('navigates to correct page', async t => {@@ -68,119 +68,105 @@ describe('slim', () => {});it('throws when URL does not parse with new URL', async () => {- await withMcpContext(async (response, context) => {- await assert.rejects(- async () => {- await navigate().handler(- {- params: {url: 'not a valid url'},- page: context.getSelectedMcpPage(),- },- response,- context,- );- },- {- message:- 'Invalid URL: "not a valid url". URLs must be valid according to the URL standard.',- },- );- });+ const {page, context, response} = createHandlerMocks();++ await assert.rejects(+ async () => {+ await navigate().handler(+ {params: {url: 'not a valid url'}, page},+ response,+ context,+ );+ },+ {+ message:+ 'Invalid URL: "not a valid url". URLs must be valid according to the URL standard.',+ },+ );++ sinon.assert.notCalled(page.pptrPage.goto);});it('disallows javascript, data, and vbscript URLs when javascriptEvaluation is false', async () => {- await withMcpContext(async (response, context) => {- const disabledArgs = parseArguments(- '1.0.0',- ['node', 'script.js', '--slim', '--no-javascript-evaluation'],- {CHROME_DEVTOOLS_MCP_NO_USAGE_STATISTICS: 'true'},- );- const tool = navigate(disabledArgs);- await assert.rejects(- async () => {- await tool.handler(- {- params: {url: 'javascript:alert(1)'},- page: context.getSelectedMcpPage(),- },- response,- context,- );- },- {- message:- 'Navigating to javascript: URLs is not allowed when JavaScript evaluation is disabled.',- },- );- await assert.rejects(- async () => {- await tool.handler(- {- params: {url: 'data:text/html,<div>test</div>'},- page: context.getSelectedMcpPage(),- },- response,- context,- );- },- {- message:- 'Navigating to data: URLs is not allowed when JavaScript evaluation is disabled.',- },- );- await assert.rejects(- async () => {- await tool.handler(- {- params: {url: 'vbscript:msgbox(1)'},- page: context.getSelectedMcpPage(),- },- response,- context,- );- },- {- message:- 'Navigating to vbscript: URLs is not allowed when JavaScript evaluation is disabled.',- },- );- });+ const {page, context, response} = createHandlerMocks();+ const disabledArgs = parseArguments(+ '1.0.0',+ ['node', 'script.js', '--slim', '--no-javascript-evaluation'],+ {CHROME_DEVTOOLS_MCP_NO_USAGE_STATISTICS: 'true'},+ );+ const tool = navigate(disabledArgs);+ await assert.rejects(+ async () => {+ await tool.handler(+ {params: {url: 'javascript:alert(1)'}, page},+ response,+ context,+ );+ },+ {+ message:+ 'Navigating to javascript: URLs is not allowed when JavaScript evaluation is disabled.',+ },+ );+ await assert.rejects(+ async () => {+ await tool.handler(+ {params: {url: 'data:text/html,<div>test</div>'}, page},+ response,+ context,+ );+ },+ {+ message:+ 'Navigating to data: URLs is not allowed when JavaScript evaluation is disabled.',+ },+ );+ await assert.rejects(+ async () => {+ await tool.handler(+ {params: {url: 'vbscript:msgbox(1)'}, page},+ response,+ context,+ );+ },+ {+ message:+ 'Navigating to vbscript: URLs is not allowed when JavaScript evaluation is disabled.',+ },+ );++ sinon.assert.notCalled(page.pptrPage.goto);});it('rejects chrome: and chrome-untrusted: URLs', async () => {- await withMcpContext(async (response, context) => {- const tool = navigate();- await assert.rejects(- async () => {- await tool.handler(- {- params: {url: 'chrome://settings'},- page: context.getSelectedMcpPage(),- },- response,- context,- );- },- {- message: 'Navigating to chrome: URLs is not allowed.',- },- );- await assert.rejects(- async () => {- await tool.handler(- {- params: {url: 'chrome-untrusted://terminal'},- page: context.getSelectedMcpPage(),- },- response,- context,- );- },- {- message: 'Navigating to chrome-untrusted: URLs is not allowed.',- },- );- });+ const {page, context, response} = createHandlerMocks();+ const tool = navigate();+ await assert.rejects(+ async () => {+ await tool.handler(+ {params: {url: 'chrome://settings'}, page},+ response,+ context,+ );+ },+ {+ message: 'Navigating to chrome: URLs is not allowed.',+ },+ );+ await assert.rejects(+ async () => {+ await tool.handler(+ {params: {url: 'chrome-untrusted://terminal'}, page},+ response,+ context,+ );+ },+ {+ message: 'Navigating to chrome-untrusted: URLs is not allowed.',+ },+ );++ sinon.assert.notCalled(page.pptrPage.goto);});it('with default options', async () => {