|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 8 | +import * as fs from 'node:fs/promises'; |
| 9 | +import { createPolicyUpdater } from './config.js'; |
| 10 | +import { PolicyEngine } from './policy-engine.js'; |
| 11 | +import { MessageBus } from '../confirmation-bus/message-bus.js'; |
| 12 | +import { MessageBusType } from '../confirmation-bus/types.js'; |
| 13 | +import { Storage } from '../config/storage.js'; |
| 14 | +import toml from '@iarna/toml'; |
| 15 | +import { ShellToolInvocation } from '../tools/shell.js'; |
| 16 | +import { type Config } from '../config/config.js'; |
| 17 | +import { |
| 18 | + ToolConfirmationOutcome, |
| 19 | + type PolicyUpdateOptions, |
| 20 | +} from '../tools/tools.js'; |
| 21 | +import * as shellUtils from '../utils/shell-utils.js'; |
| 22 | + |
| 23 | +vi.mock('node:fs/promises'); |
| 24 | +vi.mock('../config/storage.js'); |
| 25 | +vi.mock('../utils/shell-utils.js', () => ({ |
| 26 | + getCommandRoots: vi.fn(), |
| 27 | + stripShellWrapper: vi.fn(), |
| 28 | +})); |
| 29 | +interface ParsedPolicy { |
| 30 | + rule?: Array<{ |
| 31 | + commandPrefix?: string | string[]; |
| 32 | + }>; |
| 33 | +} |
| 34 | + |
| 35 | +interface TestableShellToolInvocation { |
| 36 | + getPolicyUpdateOptions( |
| 37 | + outcome: ToolConfirmationOutcome, |
| 38 | + ): PolicyUpdateOptions | undefined; |
| 39 | +} |
| 40 | + |
| 41 | +describe('createPolicyUpdater', () => { |
| 42 | + let policyEngine: PolicyEngine; |
| 43 | + let messageBus: MessageBus; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + vi.resetAllMocks(); |
| 47 | + policyEngine = new PolicyEngine({}); |
| 48 | + vi.spyOn(policyEngine, 'addRule'); |
| 49 | + |
| 50 | + messageBus = new MessageBus(policyEngine); |
| 51 | + vi.spyOn(Storage, 'getUserPoliciesDir').mockReturnValue( |
| 52 | + '/mock/user/policies', |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + vi.restoreAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should add multiple rules when commandPrefix is an array', async () => { |
| 61 | + createPolicyUpdater(policyEngine, messageBus); |
| 62 | + |
| 63 | + await messageBus.publish({ |
| 64 | + type: MessageBusType.UPDATE_POLICY, |
| 65 | + toolName: 'run_shell_command', |
| 66 | + commandPrefix: ['echo', 'ls'], |
| 67 | + persist: false, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(policyEngine.addRule).toHaveBeenCalledTimes(2); |
| 71 | + expect(policyEngine.addRule).toHaveBeenNthCalledWith( |
| 72 | + 1, |
| 73 | + expect.objectContaining({ |
| 74 | + toolName: 'run_shell_command', |
| 75 | + argsPattern: new RegExp('"command":"echo(?:[\\s"]|$)'), |
| 76 | + }), |
| 77 | + ); |
| 78 | + expect(policyEngine.addRule).toHaveBeenNthCalledWith( |
| 79 | + 2, |
| 80 | + expect.objectContaining({ |
| 81 | + toolName: 'run_shell_command', |
| 82 | + argsPattern: new RegExp('"command":"ls(?:[\\s"]|$)'), |
| 83 | + }), |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should add a single rule when commandPrefix is a string', async () => { |
| 88 | + createPolicyUpdater(policyEngine, messageBus); |
| 89 | + |
| 90 | + await messageBus.publish({ |
| 91 | + type: MessageBusType.UPDATE_POLICY, |
| 92 | + toolName: 'run_shell_command', |
| 93 | + commandPrefix: 'git', |
| 94 | + persist: false, |
| 95 | + }); |
| 96 | + |
| 97 | + expect(policyEngine.addRule).toHaveBeenCalledTimes(1); |
| 98 | + expect(policyEngine.addRule).toHaveBeenCalledWith( |
| 99 | + expect.objectContaining({ |
| 100 | + toolName: 'run_shell_command', |
| 101 | + argsPattern: new RegExp('"command":"git(?:[\\s"]|$)'), |
| 102 | + }), |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should persist multiple rules correctly to TOML', async () => { |
| 107 | + createPolicyUpdater(policyEngine, messageBus); |
| 108 | + vi.mocked(fs.readFile).mockRejectedValue({ code: 'ENOENT' }); |
| 109 | + vi.mocked(fs.mkdir).mockResolvedValue(undefined); |
| 110 | + vi.mocked(fs.writeFile).mockResolvedValue(undefined); |
| 111 | + vi.mocked(fs.rename).mockResolvedValue(undefined); |
| 112 | + |
| 113 | + await messageBus.publish({ |
| 114 | + type: MessageBusType.UPDATE_POLICY, |
| 115 | + toolName: 'run_shell_command', |
| 116 | + commandPrefix: ['echo', 'ls'], |
| 117 | + persist: true, |
| 118 | + }); |
| 119 | + |
| 120 | + // Wait for the async listener to complete |
| 121 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 122 | + |
| 123 | + expect(fs.writeFile).toHaveBeenCalled(); |
| 124 | + const [_path, content] = vi.mocked(fs.writeFile).mock.calls[0] as [ |
| 125 | + string, |
| 126 | + string, |
| 127 | + ]; |
| 128 | + const parsed = toml.parse(content) as unknown as ParsedPolicy; |
| 129 | + |
| 130 | + expect(parsed.rule).toHaveLength(1); |
| 131 | + expect(parsed.rule![0].commandPrefix).toEqual(['echo', 'ls']); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('ShellToolInvocation Policy Update', () => { |
| 136 | + let mockConfig: Config; |
| 137 | + let mockMessageBus: MessageBus; |
| 138 | + |
| 139 | + beforeEach(() => { |
| 140 | + vi.resetAllMocks(); |
| 141 | + mockConfig = {} as Config; |
| 142 | + mockMessageBus = {} as MessageBus; |
| 143 | + |
| 144 | + vi.mocked(shellUtils.stripShellWrapper).mockImplementation( |
| 145 | + (c: string) => c, |
| 146 | + ); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should extract multiple root commands for chained commands', () => { |
| 150 | + vi.mocked(shellUtils.getCommandRoots).mockReturnValue(['git', 'npm']); |
| 151 | + |
| 152 | + const invocation = new ShellToolInvocation( |
| 153 | + mockConfig, |
| 154 | + { command: 'git status && npm test' }, |
| 155 | + new Set(), |
| 156 | + mockMessageBus, |
| 157 | + 'run_shell_command', |
| 158 | + 'Shell', |
| 159 | + ); |
| 160 | + |
| 161 | + // Accessing protected method for testing |
| 162 | + const options = ( |
| 163 | + invocation as unknown as TestableShellToolInvocation |
| 164 | + ).getPolicyUpdateOptions(ToolConfirmationOutcome.ProceedAlways); |
| 165 | + expect(options!.commandPrefix).toEqual(['git', 'npm']); |
| 166 | + expect(shellUtils.getCommandRoots).toHaveBeenCalledWith( |
| 167 | + 'git status && npm test', |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should extract a single root command', () => { |
| 172 | + vi.mocked(shellUtils.getCommandRoots).mockReturnValue(['ls']); |
| 173 | + |
| 174 | + const invocation = new ShellToolInvocation( |
| 175 | + mockConfig, |
| 176 | + { command: 'ls -la /tmp' }, |
| 177 | + new Set(), |
| 178 | + mockMessageBus, |
| 179 | + 'run_shell_command', |
| 180 | + 'Shell', |
| 181 | + ); |
| 182 | + |
| 183 | + // Accessing protected method for testing |
| 184 | + const options = ( |
| 185 | + invocation as unknown as TestableShellToolInvocation |
| 186 | + ).getPolicyUpdateOptions(ToolConfirmationOutcome.ProceedAlways); |
| 187 | + expect(options!.commandPrefix).toEqual(['ls']); |
| 188 | + expect(shellUtils.getCommandRoots).toHaveBeenCalledWith('ls -la /tmp'); |
| 189 | + }); |
| 190 | +}); |
0 commit comments