| 1 | import { render } from "ink-testing-library"; |
| 2 | import React from "react"; |
| 3 | import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 4 | |
| 5 | import { UserInput } from "../UserInput.js"; |
| 6 | |
| 7 | describe("Ctrl+C input clearing", () => { |
| 8 | let mockOnSubmit: ReturnType<typeof vi.fn>; |
| 9 | let mockProcessKill: any; |
| 10 | |
| 11 | beforeEach(() => { |
| 12 | mockOnSubmit = vi.fn(); |
| 13 | |
| 14 | // Mock process.kill using vi.spyOn |
| 15 | mockProcessKill = vi.spyOn(process, "kill").mockImplementation(() => { |
| 16 | return true; |
| 17 | }); |
| 18 | }); |
| 19 | |
| 20 | afterEach(() => { |
| 21 | vi.clearAllMocks(); |
| 22 | vi.restoreAllMocks(); |
| 23 | }); |
| 24 | |
| 25 | it("sends SIGINT when Ctrl+C is pressed (input clearing tested implicitly)", () => { |
| 26 | const { stdin } = render( |
| 27 | <UserInput |
| 28 | onSubmit={mockOnSubmit} |
| 29 | inputMode={true} |
| 30 | disabled={false} |
| 31 | isWaitingForResponse={false} |
| 32 | />, |
| 33 | ); |
| 34 | |
| 35 | // Type some text first |
| 36 | stdin.write("hello world"); |
| 37 | |
| 38 | // Simulate Ctrl+C using raw character code (like other tests in codebase) |
| 39 | stdin.write("\u0003"); |
| 40 | |
| 41 | // Should send SIGINT to process (which handles the two-stage exit) |
| 42 | expect(mockProcessKill).toHaveBeenCalledWith(process.pid, "SIGINT"); |
| 43 | }); |
| 44 | |
| 45 | it("sends SIGINT to main process for two-stage exit handling", () => { |
| 46 | const { stdin } = render( |
| 47 | <UserInput |
| 48 | onSubmit={mockOnSubmit} |
| 49 | inputMode={true} |
| 50 | disabled={false} |
| 51 | isWaitingForResponse={false} |
| 52 | />, |
| 53 | ); |
| 54 | |
| 55 | // Simulate Ctrl+C using raw character code (like other tests in codebase) |
| 56 | stdin.write("\u0003"); |
| 57 | |
| 58 | // Should delegate to main process SIGINT handler |
| 59 | expect(mockProcessKill).toHaveBeenCalledWith(process.pid, "SIGINT"); |
| 60 | }); |
| 61 | }); |