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