Elffuss BraidHub

kikocis/continue public

4e263ad457a7 · 6.0 KiBRawPermalink
1import { render } from "ink-testing-library";
2import React from "react";
3import { afterEach, beforeEach, describe, expect, it } from "vitest";
4
5import { SERVICE_NAMES, serviceContainer } from "../../services/index.js";
6import { modeService } from "../../services/ModeService.js";
7import { getAllTools } from "../../streamChatResponse.js";
8import { createUITestContext } from "../../test-helpers/ui-test-context.js";
9import { AppRoot } from "../AppRoot.js";
10
11describe("TUIChat - Plan Mode Bug Reproduction", () => {
12 let context: any;
13
14 beforeEach(async () => {
15 context = createUITestContext({
16 allServicesReady: true,
17 serviceState: "ready",
18 serviceValue: { some: "data" },
19 });
20
21 // Ensure mode service is properly initialized in normal mode
22 await modeService.initialize({});
23 });
24
25 afterEach(() => {
26 context.cleanup();
27 });
28
29 it("a) start up b) shift+tab to plan c) [plan] visible d) send message", async () => {
30 const { lastFrame, stdin, unmount } = render(React.createElement(AppRoot));
31
32 try {
33 // a) Start up
34 let frame = lastFrame();
35 expect(frame).toBeDefined();
36 expect(frame).toContain("Ask anything");
37
38 // b) Shift+Tab to switch into plan mode
39 stdin.write("\x1b[Z");
40 await new Promise((resolve) => setTimeout(resolve, 200));
41
42 frame = lastFrame();
43
44 // c) Make sure the string "[plan]" is on the screen
45 expect(frame!).toContain("[plan]");
46
47 // d) Successfully send a message without error
48 const testMessage = "Can you help me analyze this code?";
49 stdin.write(testMessage);
50 stdin.write("\r");
51
52 // Wait for message processing
53 await new Promise((resolve) => setTimeout(resolve, 300));
54
55 frame = lastFrame();
56
57 // Verify no crash occurred and UI is still functional
58 expect(frame).toBeDefined();
59 expect(frame!.length).toBeGreaterThan(0);
60
61 // Should still be in plan mode after sending message
62 expect(frame!).toContain("[plan]");
63
64 // Should still show the input prompt
65 expect(frame!).toContain("Ask anything");
66 } finally {
67 unmount();
68 }
69 });
70
71 it("tests mode switching back and forth works correctly", async () => {
72 const { lastFrame, stdin, unmount } = render(React.createElement(AppRoot));
73
74 try {
75 // Start in normal mode
76 let frame = lastFrame();
77 expect(frame!).not.toContain("[plan]");
78 expect(frame!).not.toContain("[auto]");
79
80 // Switch to plan mode
81 stdin.write("\x1b[Z");
82 await new Promise((resolve) => setTimeout(resolve, 100));
83
84 frame = lastFrame();
85 expect(frame!).toContain("[plan]");
86
87 // Switch to auto mode
88 stdin.write("\x1b[Z");
89 await new Promise((resolve) => setTimeout(resolve, 100));
90
91 frame = lastFrame();
92 expect(frame!).toContain("[auto]");
93 expect(frame!).not.toContain("[plan]");
94
95 // Switch back to normal mode
96 stdin.write("\x1b[Z");
97 await new Promise((resolve) => setTimeout(resolve, 100));
98
99 frame = lastFrame();
100 expect(frame!).not.toContain("[plan]");
101 expect(frame!).not.toContain("[auto]");
102 } finally {
103 unmount();
104 }
105 });
106
107 it("tests that you can switch modes after being in plan mode", async () => {
108 const { lastFrame, stdin, unmount } = render(React.createElement(AppRoot));
109
110 try {
111 // Switch to plan mode
112 stdin.write("\x1b[Z");
113 await new Promise((resolve) => setTimeout(resolve, 100));
114
115 let frame = lastFrame();
116 expect(frame!).toContain("[plan]");
117
118 // Send a message while in plan mode
119 stdin.write("Test message");
120 stdin.write("\r");
121 await new Promise((resolve) => setTimeout(resolve, 200));
122
123 frame = lastFrame();
124
125 // Try to switch to another mode - this is where the bug might occur
126 stdin.write("\x1b[Z]"); // Should go to auto mode
127 await new Promise((resolve) => setTimeout(resolve, 200));
128
129 frame = lastFrame();
130
131 // This should work - if it fails, we've reproduced the "can't switch back" bug
132 expect(frame!).toContain("[auto]");
133 expect(frame!).not.toContain("[plan]");
134 } finally {
135 unmount();
136 }
137 });
138
139 it.skip("reproduces the 'ToolPermissionService not initialized' error when switching modes", async () => {
140 // This test specifically tests the service container interaction
141 // that causes the actual bug in production
142
143 // First, set up a realistic service state (not mocked)
144 await modeService.initialize({ isHeadless: false });
145
146 // Register the tool permissions service like the real app does
147 const initialState = modeService.getToolPermissionService().getState();
148 serviceContainer.registerValue(
149 SERVICE_NAMES.TOOL_PERMISSIONS,
150 initialState,
151 );
152
153 // Verify initial state works
154 expect(() => getAllTools()).not.toThrow();
155
156 // Switch modes WITHOUT updating the service container (this simulates the bug)
157 modeService.switchMode("plan");
158
159 // The service container still has the old state, so getAllTools() should not find
160 // the updated state with plan mode permissions. However, since the ToolPermissionService
161 // overrides isReady() to always return true, getAllTools() will actually work.
162 // The real bug would manifest when the service container has an out-of-sync state.
163
164 // To reproduce the ACTUAL bug, we need to simulate the case where the service
165 // container has a stale state that doesn't match the ToolPermissionService
166 const staleState = { ...initialState, currentMode: "normal" as const };
167 serviceContainer.set(SERVICE_NAMES.TOOL_PERMISSIONS, staleState);
168
169 // Now getAllTools() should work because it gets the service state from the container
170 // and the container has a valid state (even if it's stale)
171 const tools = getAllTools();
172 expect(tools).toBeDefined();
173 expect(Array.isArray(tools)).toBe(true);
174
175 // But the tools returned will be for "normal" mode, not "plan" mode
176 // In plan mode, many tools should be excluded, but with stale normal state, all tools are available
177 const planModeState = modeService.getToolPermissionService().getState();
178 });
179});