|
| 1 | +import { render } from "ink-testing-library"; |
| 2 | +import React from "react"; |
| 3 | +import { vi, expect, describe, it, beforeEach, afterEach } from "vitest"; |
| 4 | + |
| 5 | +import { UserInput } from "../UserInput.js"; |
| 6 | + |
| 7 | +describe("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 resume"); |
| 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 resume"); |
| 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 resume"); |
| 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 resume"); |
| 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(""); |
| 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 | + ); |
| 149 | + } finally { |
| 150 | + unmount(); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + it("shows interruption message above input box in correct position", () => { |
| 155 | + const mockOnSubmit = vi.fn(); |
| 156 | + const mockOnInterrupt = vi.fn(); |
| 157 | + |
| 158 | + const { lastFrame, unmount } = render( |
| 159 | + React.createElement(UserInput, { |
| 160 | + onSubmit: mockOnSubmit, |
| 161 | + isWaitingForResponse: false, |
| 162 | + inputMode: true, |
| 163 | + onInterrupt: mockOnInterrupt, |
| 164 | + wasInterrupted: true, |
| 165 | + }), |
| 166 | + ); |
| 167 | + |
| 168 | + try { |
| 169 | + const frame = lastFrame(); |
| 170 | + expect(frame).toBeDefined(); |
| 171 | + |
| 172 | + // The interruption message should appear before the input box |
| 173 | + const frameLines = frame?.split("\n") || []; |
| 174 | + |
| 175 | + let foundInterruptionLine = -1; |
| 176 | + let foundInputBoxLine = -1; |
| 177 | + |
| 178 | + frameLines.forEach((line, index) => { |
| 179 | + if (line.includes("⚠ Interrupted by user")) { |
| 180 | + foundInterruptionLine = index; |
| 181 | + } |
| 182 | + if (line.includes("●") && line.includes("▋")) { |
| 183 | + foundInputBoxLine = index; |
| 184 | + } |
| 185 | + }); |
| 186 | + |
| 187 | + // Interruption message should come before the input box |
| 188 | + expect(foundInterruptionLine).toBeGreaterThan(-1); |
| 189 | + expect(foundInputBoxLine).toBeGreaterThan(-1); |
| 190 | + expect(foundInterruptionLine).toBeLessThan(foundInputBoxLine); |
| 191 | + } finally { |
| 192 | + unmount(); |
| 193 | + } |
| 194 | + }); |
| 195 | +}); |
0 commit comments