Elffuss BraidHub

kikocis/continue public

main · 5.5 KiBRawPermalink
1import { describe, expect, test } from "vitest";
2
3import {
4 getServiceSync,
5 initializeServices,
6 SERVICE_NAMES,
7 serviceContainer,
8} from "../services/index.js";
9import type { ToolPermissionServiceState } from "../services/ToolPermissionService.js";
10
11import { getAllTools } from "./streamChatResponse.js";
12
13describe("getAllTools - Tool Filtering", () => {
14 beforeEach(() => {
15 // Clean up service container state before each test
16 const services = [
17 SERVICE_NAMES.TOOL_PERMISSIONS,
18 SERVICE_NAMES.AUTH,
19 SERVICE_NAMES.API_CLIENT,
20 SERVICE_NAMES.CONFIG,
21 SERVICE_NAMES.MODEL,
22 SERVICE_NAMES.MCP,
23 ];
24
25 services.forEach((service) => {
26 // Reset service state
27 (serviceContainer as any).services.delete(service);
28 (serviceContainer as any).factories.delete(service);
29 (serviceContainer as any).dependencies.delete(service);
30 });
31 });
32
33 test("should exclude Bash tool in plan mode after service initialization", async () => {
34 // Initialize services in plan mode (simulating `cn -p`)
35 await initializeServices({
36 headless: true,
37 toolPermissionOverrides: {
38 mode: "plan",
39 },
40 });
41
42 // Verify service is ready
43 const serviceResult = getServiceSync<ToolPermissionServiceState>(
44 SERVICE_NAMES.TOOL_PERMISSIONS,
45 );
46 expect(serviceResult.state).toBe("ready");
47 expect(serviceResult.value?.currentMode).toBe("plan");
48
49 // Get available tools - this should include Bash in plan mode
50 const tools = await getAllTools();
51 const toolNames = tools.map((t) => t.function.name);
52
53 // Bash should be allowed in plan mode
54 expect(toolNames).toContain("Bash");
55
56 // Read-only tools should still be available
57 expect(toolNames).toContain("Read");
58 expect(toolNames).toContain("List");
59 expect(toolNames).toContain("Search");
60 expect(toolNames).toContain("Fetch");
61 expect(toolNames).toContain("Checklist");
62
63 // Write tools should be excluded
64 expect(toolNames).not.toContain("Write");
65 expect(toolNames).not.toContain("Edit");
66 });
67
68 test("should include Bash tool in normal mode", async () => {
69 // Initialize services in normal mode
70 await initializeServices({
71 headless: true,
72 toolPermissionOverrides: {
73 mode: "normal",
74 },
75 });
76
77 // Verify service is ready
78 const serviceResult = getServiceSync<ToolPermissionServiceState>(
79 SERVICE_NAMES.TOOL_PERMISSIONS,
80 );
81 expect(serviceResult.state).toBe("ready");
82 expect(serviceResult.value?.currentMode).toBe("normal");
83
84 // Get available tools - Bash should be available in normal mode
85 const tools = await getAllTools();
86 const toolNames = tools.map((t) => t.function.name);
87
88 // All tools should be available in normal mode
89 expect(toolNames).toContain("Bash");
90 expect(toolNames).toContain("Read");
91 expect(toolNames).toContain("Write");
92 expect(toolNames).toContain("Edit");
93 });
94
95 test("should include all tools in auto mode", async () => {
96 // Initialize services in auto mode
97 await initializeServices({
98 headless: true,
99 toolPermissionOverrides: {
100 mode: "auto",
101 },
102 });
103
104 // Verify service is ready
105 const serviceResult = getServiceSync<ToolPermissionServiceState>(
106 SERVICE_NAMES.TOOL_PERMISSIONS,
107 );
108 expect(serviceResult.state).toBe("ready");
109 expect(serviceResult.value?.currentMode).toBe("auto");
110
111 // Get available tools - all tools should be available in auto mode
112 const tools = await getAllTools();
113 const toolNames = tools.map((t) => t.function.name);
114
115 // All tools should be available in auto mode
116 expect(toolNames).toContain("Bash");
117 expect(toolNames).toContain("Read");
118 expect(toolNames).toContain("Write");
119 expect(toolNames).toContain("Edit");
120 });
121
122 test("should respect explicit exclude in normal mode", async () => {
123 // Initialize services in normal mode with Read tool explicitly excluded
124 await initializeServices({
125 headless: true,
126 toolPermissionOverrides: {
127 mode: "normal",
128 exclude: ["Read"],
129 },
130 });
131
132 const tools = await getAllTools();
133 const toolNames = tools.map((t) => t.function.name);
134
135 // Read should be excluded due to explicit exclude
136 expect(toolNames).not.toContain("Read");
137
138 // Other tools should still be available in normal mode
139 expect(toolNames).toContain("Bash");
140 expect(toolNames).toContain("Write");
141 expect(toolNames).toContain("List");
142 expect(toolNames).toContain("Search");
143 });
144
145 test("plan mode should override allow flags (regression test for GitHub Actions issue)", async () => {
146 // This test specifically addresses the original issue where plan mode
147 // wasn't properly excluding tools despite being in plan mode
148 await initializeServices({
149 headless: true,
150 toolPermissionOverrides: {
151 mode: "plan",
152 allow: ["Write", "Edit"], // These should be ignored in plan mode
153 },
154 });
155
156 const tools = await getAllTools();
157 const toolNames = tools.map((t) => t.function.name);
158
159 // Plan mode should still exclude write tools despite --allow flags
160 // This tests that plan mode policies have absolute precedence
161 expect(toolNames).not.toContain("Write");
162 expect(toolNames).not.toContain("Edit");
163
164 // Bash should be available in plan mode
165 expect(toolNames).toContain("Bash");
166
167 // Read-only tools should be available
168 expect(toolNames).toContain("Read");
169 expect(toolNames).toContain("List");
170 expect(toolNames).toContain("Search");
171 });
172});