@@ -11,8 +11,10 @@ import {
1111} from './browserAgentFactory.js' ;
1212import { injectAutomationOverlay } from './automationOverlay.js' ;
1313import { makeFakeConfig } from '../../test-utils/config.js' ;
14+ import { PolicyDecision , PRIORITY_SUBAGENT_TOOL } from '../../policy/types.js' ;
1415import type { Config } from '../../config/config.js' ;
1516import type { MessageBus } from '../../confirmation-bus/message-bus.js' ;
17+ import type { PolicyEngine } from '../../policy/policy-engine.js' ;
1618import type { BrowserManager } from './browserManager.js' ;
1719
1820// Create mock browser manager
@@ -300,6 +302,116 @@ describe('browserAgentFactory', () => {
300302 } ) ;
301303 } ) ;
302304
305+ describe ( 'Policy Registration' , ( ) => {
306+ let mockPolicyEngine : {
307+ addRule : ReturnType < typeof vi . fn > ;
308+ hasRuleForTool : ReturnType < typeof vi . fn > ;
309+ removeRulesForTool : ReturnType < typeof vi . fn > ;
310+ getRules : ReturnType < typeof vi . fn > ;
311+ } ;
312+
313+ beforeEach ( ( ) => {
314+ mockPolicyEngine = {
315+ addRule : vi . fn ( ) ,
316+ hasRuleForTool : vi . fn ( ) . mockReturnValue ( false ) ,
317+ removeRulesForTool : vi . fn ( ) ,
318+ getRules : vi . fn ( ) . mockReturnValue ( [ ] ) ,
319+ } ;
320+ vi . spyOn ( mockConfig , 'getPolicyEngine' ) . mockReturnValue (
321+ mockPolicyEngine as unknown as PolicyEngine ,
322+ ) ;
323+ } ) ;
324+
325+ it ( 'should register sensitive action rules' , async ( ) => {
326+ mockConfig = makeFakeConfig ( {
327+ agents : {
328+ browser : {
329+ confirmSensitiveActions : true ,
330+ } ,
331+ } ,
332+ } ) ;
333+ vi . spyOn ( mockConfig , 'getPolicyEngine' ) . mockReturnValue (
334+ mockPolicyEngine as unknown as PolicyEngine ,
335+ ) ;
336+
337+ await createBrowserAgentDefinition ( mockConfig , mockMessageBus ) ;
338+
339+ expect ( mockPolicyEngine . addRule ) . toHaveBeenCalledWith (
340+ expect . objectContaining ( {
341+ toolName : 'mcp_browser_agent_fill' ,
342+ decision : PolicyDecision . ASK_USER ,
343+ priority : 999 ,
344+ } ) ,
345+ ) ;
346+
347+ expect ( mockPolicyEngine . addRule ) . toHaveBeenCalledWith (
348+ expect . objectContaining ( {
349+ toolName : 'mcp_browser_agent_upload_file' ,
350+ decision : PolicyDecision . ASK_USER ,
351+ priority : 999 ,
352+ } ) ,
353+ ) ;
354+
355+ expect ( mockPolicyEngine . addRule ) . toHaveBeenCalledWith (
356+ expect . objectContaining ( {
357+ toolName : 'mcp_browser_agent_evaluate_script' ,
358+ decision : PolicyDecision . ASK_USER ,
359+ priority : 999 ,
360+ } ) ,
361+ ) ;
362+ } ) ;
363+
364+ it ( 'should register fill rule even when confirmSensitiveActions is disabled' , async ( ) => {
365+ await createBrowserAgentDefinition ( mockConfig , mockMessageBus ) ;
366+
367+ expect ( mockPolicyEngine . addRule ) . toHaveBeenCalledWith (
368+ expect . objectContaining ( {
369+ toolName : 'mcp_browser_agent_fill' ,
370+ } ) ,
371+ ) ;
372+
373+ expect ( mockPolicyEngine . addRule ) . not . toHaveBeenCalledWith (
374+ expect . objectContaining ( {
375+ toolName : 'mcp_browser_agent_upload_file' ,
376+ } ) ,
377+ ) ;
378+ } ) ;
379+
380+ it ( 'should register ALLOW rules for read-only tools' , async ( ) => {
381+ mockBrowserManager . getDiscoveredTools . mockResolvedValue ( [
382+ { name : 'take_snapshot' , description : 'Take snapshot' } ,
383+ { name : 'take_screenshot' , description : 'Take screenshot' } ,
384+ { name : 'list_pages' , description : 'list all pages' } ,
385+ ] ) ;
386+
387+ await createBrowserAgentDefinition ( mockConfig , mockMessageBus ) ;
388+
389+ expect ( mockPolicyEngine . addRule ) . toHaveBeenCalledWith (
390+ expect . objectContaining ( {
391+ toolName : 'mcp_browser_agent_take_snapshot' ,
392+ decision : PolicyDecision . ALLOW ,
393+ priority : PRIORITY_SUBAGENT_TOOL ,
394+ } ) ,
395+ ) ;
396+
397+ expect ( mockPolicyEngine . addRule ) . toHaveBeenCalledWith (
398+ expect . objectContaining ( {
399+ toolName : 'mcp_browser_agent_take_screenshot' ,
400+ decision : PolicyDecision . ALLOW ,
401+ priority : PRIORITY_SUBAGENT_TOOL ,
402+ } ) ,
403+ ) ;
404+
405+ expect ( mockPolicyEngine . addRule ) . toHaveBeenCalledWith (
406+ expect . objectContaining ( {
407+ toolName : 'mcp_browser_agent_list_pages' ,
408+ decision : PolicyDecision . ALLOW ,
409+ priority : PRIORITY_SUBAGENT_TOOL ,
410+ } ) ,
411+ ) ;
412+ } ) ;
413+ } ) ;
414+
303415 describe ( 'cleanupBrowserAgent' , ( ) => {
304416 it ( 'should call close on browser manager' , async ( ) => {
305417 await cleanupBrowserAgent (
0 commit comments