Elffuss BraidHub

kikocis/continue public

main · 5.5 KiBRawPermalink
1import { render } from "ink-testing-library";
2import React from "react";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
5import { UserInput } from "../UserInput.js";
6
7describe("TUIChat - Interruption UI (Minimal Test)", () => {
8 beforeEach(() => {
9 vi.useFakeTimers();
10 });
11
12 afterEach(() => {
13 vi.clearAllTimers();
14 vi.useRealTimers();
15 });
16
17 it("shows interruption message when wasInterrupted is true", () => {
18 const mockOnSubmit = vi.fn();
19 const mockOnInterrupt = vi.fn();
20
21 const { lastFrame, rerender, unmount } = render(
22 React.createElement(UserInput, {
23 onSubmit: mockOnSubmit,
24 isWaitingForResponse: false,
25 inputMode: true,
26 onInterrupt: mockOnInterrupt,
27 wasInterrupted: false, // Initially false
28 }),
29 );
30
31 try {
32 // Initial render - no interruption message
33 let frame = lastFrame();
34 expect(frame).toBeDefined();
35 expect(frame).not.toContain("Interrupted by user");
36 expect(frame).not.toContain("Press enter to continue");
37
38 // Re-render with wasInterrupted: true
39 rerender(
40 React.createElement(UserInput, {
41 onSubmit: mockOnSubmit,
42 isWaitingForResponse: false,
43 inputMode: true,
44 onInterrupt: mockOnInterrupt,
45 wasInterrupted: true, // Now true
46 }),
47 );
48
49 // Should show interruption message
50 frame = lastFrame();
51 expect(frame).toBeDefined();
52 expect(frame).toContain("Interrupted by user");
53 expect(frame).toContain("Press enter to continue");
54 } finally {
55 unmount();
56 }
57 });
58
59 it("hides interruption message when wasInterrupted is false", () => {
60 const mockOnSubmit = vi.fn();
61 const mockOnInterrupt = vi.fn();
62
63 const { lastFrame, rerender, unmount } = render(
64 React.createElement(UserInput, {
65 onSubmit: mockOnSubmit,
66 isWaitingForResponse: false,
67 inputMode: true,
68 onInterrupt: mockOnInterrupt,
69 wasInterrupted: true, // Initially true
70 }),
71 );
72
73 try {
74 // Initial render - should show interruption message
75 let frame = lastFrame();
76 expect(frame).toBeDefined();
77 expect(frame).toContain("Interrupted by user");
78 expect(frame).toContain("Press enter to continue");
79
80 // Re-render with wasInterrupted: false
81 rerender(
82 React.createElement(UserInput, {
83 onSubmit: mockOnSubmit,
84 isWaitingForResponse: false,
85 inputMode: true,
86 onInterrupt: mockOnInterrupt,
87 wasInterrupted: false, // Now false
88 }),
89 );
90
91 // Should not show interruption message
92 frame = lastFrame();
93 expect(frame).toBeDefined();
94 expect(frame).not.toContain("Interrupted by user");
95 expect(frame).not.toContain("Press enter to continue");
96 } finally {
97 unmount();
98 }
99 });
100
101 it("calls onSubmit with empty string when Enter is pressed while interrupted", () => {
102 const mockOnSubmit = vi.fn();
103 const mockOnInterrupt = vi.fn();
104
105 const { stdin, unmount } = render(
106 React.createElement(UserInput, {
107 onSubmit: mockOnSubmit,
108 isWaitingForResponse: false,
109 inputMode: true,
110 onInterrupt: mockOnInterrupt,
111 wasInterrupted: true, // Interrupted state
112 }),
113 );
114
115 try {
116 // Press Enter while in interrupted state
117 stdin.write("\r");
118
119 // Should call onSubmit with empty string (for resume)
120 expect(mockOnSubmit).toHaveBeenCalledWith("", expect.any(Map));
121 } finally {
122 unmount();
123 }
124 });
125
126 it("calls onSubmit with typed content when typing new message after interruption", () => {
127 const mockOnSubmit = vi.fn();
128 const mockOnInterrupt = vi.fn();
129
130 const { stdin, unmount } = render(
131 React.createElement(UserInput, {
132 onSubmit: mockOnSubmit,
133 isWaitingForResponse: false,
134 inputMode: true,
135 onInterrupt: mockOnInterrupt,
136 wasInterrupted: true, // Interrupted state
137 }),
138 );
139
140 try {
141 // Type a new message
142 stdin.write("New message after interruption");
143 stdin.write("\r");
144
145 // Should call onSubmit with the typed content
146 expect(mockOnSubmit).toHaveBeenCalledWith(
147 "New message after interruption",
148 expect.any(Map),
149 );
150 } finally {
151 unmount();
152 }
153 });
154
155 it("shows interruption message above input box in correct position", () => {
156 const mockOnSubmit = vi.fn();
157 const mockOnInterrupt = vi.fn();
158
159 const { lastFrame, unmount } = render(
160 React.createElement(UserInput, {
161 onSubmit: mockOnSubmit,
162 isWaitingForResponse: false,
163 inputMode: true,
164 onInterrupt: mockOnInterrupt,
165 wasInterrupted: true,
166 }),
167 );
168
169 try {
170 const frame = lastFrame();
171 expect(frame).toBeDefined();
172
173 // The interruption message should appear before the input box
174 const frameLines = frame?.split("\n") || [];
175
176 let foundInterruptionLine = -1;
177 let foundInputBoxLine = -1;
178
179 frameLines.forEach((line, index) => {
180 if (line.includes("Interrupted by user")) {
181 foundInterruptionLine = index;
182 }
183 if (line.includes("●") || line.includes("▋")) {
184 foundInputBoxLine = index;
185 }
186 });
187
188 // Interruption message should come before the input box
189 expect(foundInterruptionLine).toBeGreaterThan(-1);
190 expect(foundInputBoxLine).toBeGreaterThan(-1);
191 expect(foundInterruptionLine).toBeLessThan(foundInputBoxLine);
192 } finally {
193 unmount();
194 }
195 });
196});