Elffuss BraidHub

kikocis/continue public

main · 8.2 KiBRawPermalink
1import { beforeEach, afterEach, describe, expect, vi, test } from "vitest";
2
3import { ModeService } from "./ModeService.js";
4
5describe("ModeService", () => {
6 let modeService: ModeService;
7
8 beforeEach(() => {
9 // Reset singleton instance for each test
10 (ModeService as any).instance = undefined;
11 modeService = ModeService.getInstance();
12 });
13
14 afterEach(() => {
15 // Clean up singleton instance
16 (ModeService as any).instance = undefined;
17 });
18
19 describe("State Management", () => {
20 test("should initialize with normal mode by default", async () => {
21 const state = await modeService.initialize({});
22
23 expect(state).toEqual({
24 mode: "normal",
25 toolPermissionService: expect.any(Object),
26 });
27 expect(modeService.getCurrentMode()).toBe("normal");
28 });
29
30 test("should convert readonly flag to plan mode", async () => {
31 const state = await modeService.initialize({ readonly: true });
32
33 expect(state.mode).toBe("plan");
34 expect(modeService.getCurrentMode()).toBe("plan");
35 });
36
37 test("should convert auto flag to auto mode", async () => {
38 const state = await modeService.initialize({ auto: true });
39
40 expect(state.mode).toBe("auto");
41 expect(modeService.getCurrentMode()).toBe("auto");
42 });
43
44 test("should initialize tool permission service with flags", async () => {
45 const state = await modeService.initialize({
46 allow: ["testTool"],
47 exclude: ["badTool"],
48 });
49
50 const toolPermissionService = state.toolPermissionService;
51 expect(toolPermissionService).toBeDefined();
52 expect(toolPermissionService.getState()).toBeDefined();
53 expect(toolPermissionService.getCurrentMode()).toBe("normal");
54 });
55 });
56
57 describe("switchMode()", () => {
58 beforeEach(async () => {
59 await modeService.initialize({});
60 });
61
62 test("should switch from normal to plan mode", () => {
63 expect(modeService.getCurrentMode()).toBe("normal");
64
65 modeService.switchMode("plan");
66
67 expect(modeService.getCurrentMode()).toBe("plan");
68 expect(modeService.getState().mode).toBe("plan");
69 });
70
71 test("should switch from plan to auto mode", () => {
72 modeService.switchMode("plan");
73 expect(modeService.getCurrentMode()).toBe("plan");
74
75 modeService.switchMode("auto");
76
77 expect(modeService.getCurrentMode()).toBe("auto");
78 expect(modeService.getState().mode).toBe("auto");
79 });
80
81 test("should switch back to normal mode", () => {
82 modeService.switchMode("plan");
83 modeService.switchMode("normal");
84
85 expect(modeService.getCurrentMode()).toBe("normal");
86 expect(modeService.getState().mode).toBe("normal");
87 });
88
89 test("should update tool permission service when switching modes", () => {
90 const toolPermissionService = modeService.getToolPermissionService();
91 const initialState = toolPermissionService.getState();
92
93 modeService.switchMode("auto");
94 const newState = toolPermissionService.getState();
95
96 expect(newState.currentMode).toBe("auto");
97 expect(newState.permissions.policies.length).toBeGreaterThan(0);
98 expect(newState.permissions.policies[0]).toEqual({
99 tool: "*",
100 permission: "allow",
101 });
102 });
103
104 test("should handle multiple mode switches correctly", () => {
105 // Start in normal
106 expect(modeService.getCurrentMode()).toBe("normal");
107
108 // Switch to plan
109 modeService.switchMode("plan");
110 const planState = modeService.getToolPermissionService().getState();
111 expect(planState.permissions.policies).toContainEqual({
112 tool: "Write",
113 permission: "exclude",
114 });
115
116 // Switch to auto
117 modeService.switchMode("auto");
118 const autoState = modeService.getToolPermissionService().getState();
119 expect(autoState.permissions.policies).toHaveLength(1);
120 expect(autoState.permissions.policies[0]).toEqual({
121 tool: "*",
122 permission: "allow",
123 });
124
125 // Back to normal
126 modeService.switchMode("normal");
127 const normalState = modeService.getToolPermissionService().getState();
128 expect(normalState.currentMode).toBe("normal");
129 });
130 });
131
132 describe("getAvailableModes()", () => {
133 beforeEach(async () => {
134 await modeService.initialize({});
135 });
136
137 test("should return all available modes with descriptions", () => {
138 const modes = modeService.getAvailableModes();
139 expect(modes).toHaveLength(3);
140
141 const modeNames = modes.map((m) => m.mode);
142 expect(modeNames).toContain("normal");
143 expect(modeNames).toContain("plan");
144 expect(modeNames).toContain("auto");
145
146 // Check that all modes have descriptions
147 modes.forEach((mode) => {
148 expect(mode.description).toBeDefined();
149 expect(mode.description.length).toBeGreaterThan(0);
150 });
151 });
152 });
153
154 describe("Singleton Behavior", () => {
155 test("should return the same instance", () => {
156 const instance1 = ModeService.getInstance();
157 const instance2 = ModeService.getInstance();
158 expect(instance1).toBe(instance2);
159 });
160
161 test("should maintain state across getInstance calls", async () => {
162 const instance1 = ModeService.getInstance();
163 await instance1.initialize({});
164 instance1.switchMode("plan");
165
166 const instance2 = ModeService.getInstance();
167 expect(instance2.getCurrentMode()).toBe("plan");
168 });
169 });
170
171 describe("isReady()", () => {
172 test("should always return true for ModeService", () => {
173 // ModeService is always ready since it's a singleton
174 expect(modeService.isReady()).toBe(true);
175 });
176 });
177
178 describe("Event Emission", () => {
179 let modeChangeListener: ReturnType<typeof vi.fn>;
180 let stateChangeListener: ReturnType<typeof vi.fn>;
181
182 beforeEach(async () => {
183 await modeService.initialize({});
184 modeChangeListener = vi.fn();
185 stateChangeListener = vi.fn();
186 modeService.on("modeChanged", modeChangeListener);
187 modeService.on("stateChanged", stateChangeListener);
188 });
189
190 afterEach(() => {
191 modeService.off("modeChanged", modeChangeListener);
192 modeService.off("stateChanged", stateChangeListener);
193 });
194
195 test("should emit modeChanged event when mode switches", () => {
196 modeService.switchMode("plan");
197
198 expect(modeChangeListener).toHaveBeenCalledWith("plan", "normal");
199 expect(modeChangeListener).toHaveBeenCalledTimes(1);
200 });
201
202 test("should emit stateChanged event when mode switches", () => {
203 modeService.switchMode("plan");
204
205 expect(stateChangeListener).toHaveBeenCalledWith(
206 expect.objectContaining({ mode: "plan" }),
207 expect.objectContaining({ mode: "normal" }),
208 );
209 });
210
211 test("should emit event with correct previous and new modes", () => {
212 modeService.switchMode("plan");
213 modeChangeListener.mockClear();
214 stateChangeListener.mockClear();
215
216 modeService.switchMode("auto");
217
218 expect(modeChangeListener).toHaveBeenCalledWith("auto", "plan");
219 expect(modeChangeListener).toHaveBeenCalledTimes(1);
220 expect(stateChangeListener).toHaveBeenCalledTimes(1);
221 });
222
223 test("should not emit event when switching to same mode", () => {
224 modeService.switchMode("plan");
225 modeChangeListener.mockClear();
226 stateChangeListener.mockClear();
227
228 modeService.switchMode("plan");
229
230 expect(modeChangeListener).not.toHaveBeenCalled();
231 // stateChanged might still be called due to setState, but modeChanged should not
232 });
233 });
234
235 describe("Integration with ToolPermissionService", () => {
236 beforeEach(async () => {
237 await modeService.initialize({
238 allow: ["Read"],
239 exclude: ["dangerous_tool"],
240 });
241 });
242
243 test("should pass initialization args to ToolPermissionService", () => {
244 const toolService = modeService.getToolPermissionService();
245 const state = toolService.getState();
246
247 // The state should have processed the allow/exclude rules
248 expect(state.permissions.policies).toBeDefined();
249 expect(state.currentMode).toBe("normal");
250 });
251
252 test("should maintain ToolPermissionService reference", () => {
253 const toolService1 = modeService.getToolPermissionService();
254 const toolService2 = modeService.getToolPermissionService();
255
256 expect(toolService1).toBe(toolService2);
257 });
258 });
259});