Elffuss BraidHub

kikocis/continue public

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