Skip to content

Commit 7176efa

Browse files
committed
Add tests for reasons
1 parent e01944b commit 7176efa

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/vs/workbench/contrib/terminalContrib/chatAgentTools/test/browser/commandLineAutoApprover.test.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { workbenchInstantiationService } from '../../../../../test/browser/workb
1111
import { TerminalChatAgentToolsSettingId } from '../../common/terminalChatAgentToolsConfiguration.js';
1212
import { CommandLineAutoApprover } from '../../browser/commandLineAutoApprover.js';
1313
import { ConfigurationTarget } from '../../../../../../platform/configuration/common/configuration.js';
14-
import { ok } from 'assert';
14+
import { ok, strictEqual } from 'assert';
1515

1616
suite('CommandLineAutoApprover', () => {
1717
const store = ensureNoDisposablesAreLeakedInTestSuite();
@@ -38,6 +38,10 @@ suite('CommandLineAutoApprover', () => {
3838
setConfig(TerminalChatAgentToolsSettingId.AutoApprove, value);
3939
}
4040

41+
function setAutoApproveWithCommandLine(value: { [key: string]: { approve: boolean; matchCommandLine?: boolean } | boolean }) {
42+
setConfig(TerminalChatAgentToolsSettingId.AutoApprove, value);
43+
}
44+
4145
function setConfig(key: string, value: unknown) {
4246
configurationService.setUserConfiguration(key, value);
4347
configurationService.onDidChangeConfigurationEmitter.fire({
@@ -331,10 +335,6 @@ suite('CommandLineAutoApprover', () => {
331335
});
332336

333337
suite('isCommandLineAutoApproved - matchCommandLine functionality', () => {
334-
function setAutoApproveWithCommandLine(value: { [key: string]: { approve: boolean; matchCommandLine: boolean } | boolean }) {
335-
setConfig(TerminalChatAgentToolsSettingId.AutoApprove, value);
336-
}
337-
338338
test('should auto-approve command line patterns with matchCommandLine: true', () => {
339339
setAutoApproveWithCommandLine({
340340
"echo": { approve: true, matchCommandLine: true }
@@ -440,4 +440,44 @@ suite('CommandLineAutoApprover', () => {
440440
ok(!isCommandLineAutoApproved('powershell -File script.ps1 -ExecutionPolicy Bypass'));
441441
});
442442
});
443+
444+
suite('reasons', () => {
445+
function getCommandReason(command: string): string {
446+
return commandLineAutoApprover.isCommandAutoApproved(command, shell, os).reason;
447+
}
448+
449+
function getCommandLineReason(commandLine: string): string {
450+
return commandLineAutoApprover.isCommandLineAutoApproved(commandLine).reason;
451+
}
452+
453+
suite('command', () => {
454+
test('approved', () => {
455+
setAutoApprove({ echo: true });
456+
strictEqual(getCommandReason('echo hello'), `Command 'echo hello' is approved by allow list rule: echo`);
457+
});
458+
test('not approved', () => {
459+
setAutoApprove({ echo: false });
460+
strictEqual(getCommandReason('echo hello'), `Command 'echo hello' is denied by deny list rule: echo`);
461+
});
462+
test('no match', () => {
463+
setAutoApprove({});
464+
strictEqual(getCommandReason('echo hello'), `Command 'echo hello' has no matching auto approve entries`);
465+
});
466+
});
467+
468+
suite('command line', () => {
469+
test('approved', () => {
470+
setAutoApproveWithCommandLine({ echo: { approve: true, matchCommandLine: true } });
471+
strictEqual(getCommandLineReason('echo hello'), `Command line 'echo hello' is approved by allow list rule: echo`);
472+
});
473+
test('not approved', () => {
474+
setAutoApproveWithCommandLine({ echo: { approve: false, matchCommandLine: true } });
475+
strictEqual(getCommandLineReason('echo hello'), `Command line 'echo hello' is denied by deny list rule: echo`);
476+
});
477+
test('no match', () => {
478+
setAutoApproveWithCommandLine({});
479+
strictEqual(getCommandLineReason('echo hello'), `Command line 'echo hello' has no matching auto approve entries`);
480+
});
481+
});
482+
});
443483
});

0 commit comments

Comments
 (0)