77import assert from 'node:assert' ;
88import { describe , it } from 'node:test' ;
99
10- import sinon from 'sinon' ;
11-
1210import type { ParsedArguments } from '../../src/bin/chrome-devtools-mcp-cli-options.js' ;
13- import type { ToolGroup } from '../../src/tools/inPage.js' ;
1411import { listInPageTools } from '../../src/tools/inPage.js' ;
1512import { withMcpContext } from '../utils.js' ;
1613
@@ -20,60 +17,111 @@ describe('inPage', () => {
2017 await withMcpContext (
2118 async ( response , context ) => {
2219 const page = await context . newPage ( ) ;
23- const toolGroup : ToolGroup = {
24- name : 'test-group' ,
25- description : 'test description' ,
26- tools : [
27- {
28- name : 'test-tool' ,
29- description : 'test tool description' ,
30- inputSchema : {
31- type : 'object' ,
32- properties : {
33- arg : { type : 'string' } ,
20+
21+ await page . pptrPage . evaluate ( ( ) => {
22+ window . __dtmcp = {
23+ toolGroup : {
24+ name : 'test-group' ,
25+ description : 'test description' ,
26+ tools : [
27+ {
28+ name : 'test-tool' ,
29+ description : 'test tool description' ,
30+ inputSchema : {
31+ type : 'object' ,
32+ properties : {
33+ arg : { type : 'string' } ,
34+ } ,
35+ } ,
36+ execute : ( ) => 'result' ,
3437 } ,
35- } ,
36- execute : ( ) => 'result' ,
38+ ] ,
3739 } ,
38- ] ,
39- } ;
40+ } ;
41+ window . addEventListener ( 'devtoolstooldiscovery' , ( e : Event ) => {
42+ // @ts -expect-error Event has `respondWith`
43+ e . respondWith ( window . __dtmcp ?. toolGroup ) ;
44+ } ) ;
45+ } ) ;
4046
47+ await listInPageTools . handler ( { params : { } , page} , response , context ) ;
48+
49+ const result = await response . handle ( 'list_in_page_tools' , context ) ;
50+ // @ts -expect-error `structuredContent` has `inPageTools`
51+ const actualGroup = result . structuredContent . inPageTools ;
52+ assert . strictEqual ( actualGroup . name , 'test-group' ) ;
53+ assert . strictEqual ( actualGroup . description , 'test description' ) ;
54+ assert . strictEqual ( actualGroup . tools . length , 1 ) ;
55+ assert . strictEqual ( actualGroup . tools [ 0 ] . name , 'test-tool' ) ;
56+ assert . strictEqual (
57+ actualGroup . tools [ 0 ] . description ,
58+ 'test tool description' ,
59+ ) ;
60+ assert . deepEqual ( actualGroup . tools [ 0 ] . inputSchema , {
61+ type : 'object' ,
62+ properties : {
63+ arg : { type : 'string' } ,
64+ } ,
65+ } ) ;
66+ } ,
67+ undefined ,
68+ { categoryInPageTools : true } as ParsedArguments ,
69+ ) ;
70+ } ) ;
71+
72+ it ( 'handles empty response' , async ( ) => {
73+ await withMcpContext (
74+ async ( response , context ) => {
75+ const page = await context . newPage ( ) ;
4176 await page . pptrPage . evaluate ( ( ) => {
42- window . addEventListener ( 'devtoolstooldiscovery' , ( ) => {
43- // No-op
77+ window . addEventListener ( 'devtoolstooldiscovery' , ( e : Event ) => {
78+ // @ts -expect-error Event has `respondWith`
79+ e . respondWith ( { } ) ;
4480 } ) ;
4581 } ) ;
4682
47- const evaluateStub = sinon . stub ( page . pptrPage , 'evaluate' ) ;
48- evaluateStub . resolves ( toolGroup ) ;
49-
5083 await listInPageTools . handler ( { params : { } , page} , response , context ) ;
5184
5285 const result = await response . handle ( 'list_in_page_tools' , context ) ;
5386 assert . ok ( 'inPageTools' in result . structuredContent ) ;
5487 assert . deepEqual (
55- ( result . structuredContent as { inPageTools : ToolGroup } ) . inPageTools ,
56- toolGroup ,
88+ ( result . structuredContent as { inPageTools : undefined } ) . inPageTools ,
89+ { } ,
5790 ) ;
5891 } ,
5992 undefined ,
6093 { categoryInPageTools : true } as ParsedArguments ,
6194 ) ;
6295 } ) ;
6396
64- it ( 'handles no tools ' , async ( ) => {
97+ it ( 'handles no response ' , async ( ) => {
6598 await withMcpContext (
6699 async ( response , context ) => {
67100 const page = await context . newPage ( ) ;
68101 await page . pptrPage . evaluate ( ( ) => {
69102 window . addEventListener ( 'devtoolstooldiscovery' , ( ) => {
70- // No-op
103+ // do nothing
71104 } ) ;
72105 } ) ;
73106
74- const evaluateStub = sinon . stub ( page . pptrPage , 'evaluate' ) ;
75- evaluateStub . resolves ( undefined ) ;
107+ await listInPageTools . handler ( { params : { } , page} , response , context ) ;
76108
109+ const result = await response . handle ( 'list_in_page_tools' , context ) ;
110+ assert . ok ( 'inPageTools' in result . structuredContent ) ;
111+ assert . strictEqual (
112+ ( result . structuredContent as { inPageTools : undefined } ) . inPageTools ,
113+ undefined ,
114+ ) ;
115+ } ,
116+ undefined ,
117+ { categoryInPageTools : true } as ParsedArguments ,
118+ ) ;
119+ } ) ;
120+
121+ it ( 'handles no eventListener' , async ( ) => {
122+ await withMcpContext (
123+ async ( response , context ) => {
124+ const page = await context . newPage ( ) ;
77125 await listInPageTools . handler ( { params : { } , page} , response , context ) ;
78126
79127 const result = await response . handle ( 'list_in_page_tools' , context ) ;
0 commit comments