Elffuss BraidHub

kikocis/continue public

main · 4.0 KiBRawPermalink
1import { render } from "ink-testing-library";
2import React from "react";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
5import { createUITestContext } from "../../test-helpers/ui-test-context.js";
6import { AppRoot } from "../AppRoot.js";
7
8// Mock the useChat hook to control state
9const mockUseChat = vi.fn();
10const mockHandleInterrupt = vi.fn();
11
12vi.mock("../hooks/useChat.js", () => ({
13 useChat: () => mockUseChat(),
14}));
15
16describe("TUIChat - Compaction Cancellation", () => {
17 let context: any;
18
19 beforeEach(() => {
20 context = createUITestContext({
21 allServicesReady: true,
22 serviceState: "ready",
23 });
24
25 // Reset the mock
26 mockHandleInterrupt.mockClear();
27 });
28
29 afterEach(() => {
30 context.cleanup();
31 vi.clearAllMocks();
32 });
33
34 it("calls handleInterrupt when ESC is pressed during compaction", () => {
35 const compactionStartTime = Date.now();
36
37 mockUseChat.mockReturnValue({
38 chatHistory: [],
39 setChatHistory: vi.fn(),
40 isWaitingForResponse: false,
41 responseStartTime: null,
42 isCompacting: true,
43 compactionStartTime,
44 inputMode: true, // Changed to true so ESC handling is active
45 attachedFiles: [],
46 activePermissionRequest: null,
47 wasInterrupted: false,
48 handleUserMessage: vi.fn(),
49 handleInterrupt: mockHandleInterrupt,
50 handleFileAttached: vi.fn(),
51 resetChatHistory: vi.fn(),
52 handleToolPermissionResponse: vi.fn(),
53 });
54
55 const { stdin, unmount } = render(React.createElement(AppRoot, {}));
56
57 try {
58 // Give some time for the component to render and set up input handling
59 setTimeout(() => {
60 // Simulate ESC key press during compaction
61 stdin.write("\x1b"); // ESC key
62
63 // The handleInterrupt should be called
64 expect(mockHandleInterrupt).toHaveBeenCalledTimes(1);
65 }, 100);
66 } finally {
67 unmount();
68 }
69 });
70
71 it("shows compaction status with esc to interrupt message", () => {
72 const compactionStartTime = Date.now();
73
74 mockUseChat.mockReturnValue({
75 chatHistory: [],
76 setChatHistory: vi.fn(),
77 isWaitingForResponse: false,
78 responseStartTime: null,
79 isCompacting: true,
80 compactionStartTime,
81 inputMode: false,
82 attachedFiles: [],
83 activePermissionRequest: null,
84 wasInterrupted: false,
85 handleUserMessage: vi.fn(),
86 handleInterrupt: mockHandleInterrupt,
87 handleFileAttached: vi.fn(),
88 resetChatHistory: vi.fn(),
89 handleToolPermissionResponse: vi.fn(),
90 });
91
92 const { lastFrame, unmount } = render(React.createElement(AppRoot, {}));
93
94 try {
95 const frame = lastFrame();
96 expect(frame).toBeDefined();
97
98 // Should show compaction message with interrupt option
99 expect(frame).toContain("Compacting history");
100 expect(frame).toContain("esc to interrupt");
101 } finally {
102 unmount();
103 }
104 });
105
106 it("prioritizes compaction cancellation over response cancellation", () => {
107 // This tests the case where both might be true (theoretical edge case)
108 const compactionStartTime = Date.now();
109 const responseStartTime = Date.now();
110
111 mockUseChat.mockReturnValue({
112 chatHistory: [],
113 setChatHistory: vi.fn(),
114 isWaitingForResponse: true,
115 responseStartTime,
116 isCompacting: true,
117 compactionStartTime,
118 inputMode: false,
119 attachedFiles: [],
120 activePermissionRequest: null,
121 wasInterrupted: false,
122 handleUserMessage: vi.fn(),
123 handleInterrupt: mockHandleInterrupt,
124 handleFileAttached: vi.fn(),
125 resetChatHistory: vi.fn(),
126 handleToolPermissionResponse: vi.fn(),
127 });
128
129 const { stdin, unmount } = render(React.createElement(AppRoot, {}));
130
131 try {
132 // Simulate ESC key press when both states are active
133 stdin.write("\x1b"); // ESC key
134
135 // Should still call handleInterrupt (which will prioritize compaction)
136 expect(mockHandleInterrupt).toHaveBeenCalledTimes(1);
137 } finally {
138 unmount();
139 }
140 });
141});