| 1 | import { render } from "ink-testing-library"; |
| 2 | import React from "react"; |
| 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | |
| 5 | import { createUITestContext } from "../../test-helpers/ui-test-context.js"; |
| 6 | import { AppRoot } from "../AppRoot.js"; |
| 7 | |
| 8 | // Mock the useChat hook to control state |
| 9 | const mockUseChat = vi.fn(); |
| 10 | |
| 11 | vi.mock("../hooks/useChat.js", () => ({ |
| 12 | useChat: () => mockUseChat(), |
| 13 | })); |
| 14 | |
| 15 | describe("TUIChat - ActionStatus", () => { |
| 16 | let context: any; |
| 17 | |
| 18 | beforeEach(() => { |
| 19 | context = createUITestContext({ |
| 20 | allServicesReady: true, |
| 21 | serviceState: "ready", |
| 22 | }); |
| 23 | |
| 24 | // Default mock return value |
| 25 | mockUseChat.mockReturnValue({ |
| 26 | chatHistory: [], |
| 27 | setChatHistory: vi.fn(), |
| 28 | isWaitingForResponse: false, |
| 29 | responseStartTime: null, |
| 30 | isCompacting: false, |
| 31 | compactionStartTime: null, |
| 32 | inputMode: true, |
| 33 | attachedFiles: [], |
| 34 | activePermissionRequest: null, |
| 35 | wasInterrupted: false, |
| 36 | handleUserMessage: vi.fn(), |
| 37 | handleInterrupt: vi.fn(), |
| 38 | handleFileAttached: vi.fn(), |
| 39 | resetChatHistory: vi.fn(), |
| 40 | handleToolPermissionResponse: vi.fn(), |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | afterEach(() => { |
| 45 | context.cleanup(); |
| 46 | vi.clearAllMocks(); |
| 47 | }); |
| 48 | |
| 49 | it("shows only response status when waiting for response", () => { |
| 50 | const responseStartTime = Date.now(); |
| 51 | |
| 52 | mockUseChat.mockReturnValue({ |
| 53 | chatHistory: [], |
| 54 | setChatHistory: vi.fn(), |
| 55 | isWaitingForResponse: true, |
| 56 | responseStartTime, |
| 57 | isCompacting: false, |
| 58 | compactionStartTime: null, |
| 59 | inputMode: false, |
| 60 | attachedFiles: [], |
| 61 | activePermissionRequest: null, |
| 62 | wasInterrupted: false, |
| 63 | handleUserMessage: vi.fn(), |
| 64 | handleInterrupt: vi.fn(), |
| 65 | handleFileAttached: vi.fn(), |
| 66 | resetChatHistory: vi.fn(), |
| 67 | handleToolPermissionResponse: vi.fn(), |
| 68 | }); |
| 69 | |
| 70 | const { lastFrame, unmount } = render(React.createElement(AppRoot, {})); |
| 71 | |
| 72 | try { |
| 73 | const frame = lastFrame(); |
| 74 | expect(frame).toBeDefined(); |
| 75 | |
| 76 | // Should show spinner/response indicator but not compaction message |
| 77 | expect(frame).toContain("esc to interrupt"); |
| 78 | expect(frame).not.toContain("Compacting history"); |
| 79 | } finally { |
| 80 | unmount(); |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | it("shows only compaction status when compacting", () => { |
| 85 | const compactionStartTime = Date.now(); |
| 86 | |
| 87 | mockUseChat.mockReturnValue({ |
| 88 | chatHistory: [], |
| 89 | setChatHistory: vi.fn(), |
| 90 | isWaitingForResponse: false, |
| 91 | responseStartTime: null, |
| 92 | isCompacting: true, |
| 93 | compactionStartTime, |
| 94 | inputMode: true, |
| 95 | attachedFiles: [], |
| 96 | activePermissionRequest: null, |
| 97 | wasInterrupted: false, |
| 98 | handleUserMessage: vi.fn(), |
| 99 | handleInterrupt: vi.fn(), |
| 100 | handleFileAttached: vi.fn(), |
| 101 | resetChatHistory: vi.fn(), |
| 102 | handleToolPermissionResponse: vi.fn(), |
| 103 | }); |
| 104 | |
| 105 | const { lastFrame, unmount } = render(React.createElement(AppRoot, {})); |
| 106 | |
| 107 | try { |
| 108 | const frame = lastFrame(); |
| 109 | expect(frame).toBeDefined(); |
| 110 | |
| 111 | // Should show compaction message but not spinner |
| 112 | expect(frame).toContain("Compacting history"); |
| 113 | expect(frame).toContain("esc to interrupt"); |
| 114 | } finally { |
| 115 | unmount(); |
| 116 | } |
| 117 | }); |
| 118 | |
| 119 | it("shows neither status when both states are false", () => { |
| 120 | mockUseChat.mockReturnValue({ |
| 121 | chatHistory: [], |
| 122 | setChatHistory: vi.fn(), |
| 123 | isWaitingForResponse: false, |
| 124 | responseStartTime: null, |
| 125 | isCompacting: false, |
| 126 | compactionStartTime: null, |
| 127 | inputMode: true, |
| 128 | attachedFiles: [], |
| 129 | activePermissionRequest: null, |
| 130 | wasInterrupted: false, |
| 131 | handleUserMessage: vi.fn(), |
| 132 | handleInterrupt: vi.fn(), |
| 133 | handleFileAttached: vi.fn(), |
| 134 | resetChatHistory: vi.fn(), |
| 135 | handleToolPermissionResponse: vi.fn(), |
| 136 | }); |
| 137 | |
| 138 | const { lastFrame, unmount } = render(React.createElement(AppRoot, {})); |
| 139 | |
| 140 | try { |
| 141 | const frame = lastFrame(); |
| 142 | expect(frame).toBeDefined(); |
| 143 | |
| 144 | // Should not show any status messages |
| 145 | expect(frame).not.toContain("Compacting history"); |
| 146 | // The basic UI should still be there |
| 147 | expect(frame).toContain("Ask anything"); |
| 148 | } finally { |
| 149 | unmount(); |
| 150 | } |
| 151 | }); |
| 152 | |
| 153 | it("handles state transitions correctly", () => { |
| 154 | const { lastFrame, rerender, unmount } = render( |
| 155 | React.createElement(AppRoot, {}), |
| 156 | ); |
| 157 | |
| 158 | // Start with compaction |
| 159 | const compactionStartTime = Date.now(); |
| 160 | mockUseChat.mockReturnValue({ |
| 161 | chatHistory: [], |
| 162 | setChatHistory: vi.fn(), |
| 163 | isWaitingForResponse: false, |
| 164 | responseStartTime: null, |
| 165 | isCompacting: true, |
| 166 | compactionStartTime, |
| 167 | inputMode: true, |
| 168 | attachedFiles: [], |
| 169 | activePermissionRequest: null, |
| 170 | wasInterrupted: false, |
| 171 | handleUserMessage: vi.fn(), |
| 172 | handleInterrupt: vi.fn(), |
| 173 | handleFileAttached: vi.fn(), |
| 174 | resetChatHistory: vi.fn(), |
| 175 | handleToolPermissionResponse: vi.fn(), |
| 176 | }); |
| 177 | |
| 178 | try { |
| 179 | rerender(React.createElement(AppRoot, {})); |
| 180 | |
| 181 | let frame = lastFrame(); |
| 182 | expect(frame).toContain("Compacting history"); |
| 183 | expect(frame).not.toContain("⠀⠁⠃⠇⠏⠟⠿⣿"); // No spinner chars when only compacting |
| 184 | |
| 185 | // Transition to response waiting |
| 186 | const responseStartTime = Date.now(); |
| 187 | mockUseChat.mockReturnValue({ |
| 188 | chatHistory: [], |
| 189 | setChatHistory: vi.fn(), |
| 190 | isWaitingForResponse: true, |
| 191 | responseStartTime, |
| 192 | isCompacting: false, |
| 193 | compactionStartTime: null, |
| 194 | inputMode: false, |
| 195 | attachedFiles: [], |
| 196 | activePermissionRequest: null, |
| 197 | wasInterrupted: false, |
| 198 | handleUserMessage: vi.fn(), |
| 199 | handleInterrupt: vi.fn(), |
| 200 | handleFileAttached: vi.fn(), |
| 201 | resetChatHistory: vi.fn(), |
| 202 | handleToolPermissionResponse: vi.fn(), |
| 203 | }); |
| 204 | |
| 205 | rerender(React.createElement(AppRoot, {})); |
| 206 | |
| 207 | frame = lastFrame(); |
| 208 | expect(frame).not.toContain("Compacting history"); |
| 209 | expect(frame).toContain("esc to interrupt"); |
| 210 | } finally { |
| 211 | unmount(); |
| 212 | } |
| 213 | }); |
| 214 | }); |