| 1 | import * as readline from "readline"; |
| 2 | |
| 3 | import { |
| 4 | describe, |
| 5 | expect, |
| 6 | it, |
| 7 | vi, |
| 8 | beforeEach, |
| 9 | afterEach, |
| 10 | MockInstance, |
| 11 | } from "vitest"; |
| 12 | |
| 13 | // Mock readline module |
| 14 | vi.mock("readline", () => ({ |
| 15 | createInterface: vi.fn(), |
| 16 | })); |
| 17 | |
| 18 | import { question, questionWithChoices } from "./prompt.js"; |
| 19 | |
| 20 | describe("prompt utilities", () => { |
| 21 | let mockInterface: any; |
| 22 | let consoleLogSpy: MockInstance<typeof console.log>; |
| 23 | let processExitSpy: MockInstance<typeof process.exit>; |
| 24 | |
| 25 | beforeEach(() => { |
| 26 | // Create a mock readline interface |
| 27 | mockInterface = { |
| 28 | question: vi.fn(), |
| 29 | close: vi.fn(), |
| 30 | on: vi.fn(), |
| 31 | }; |
| 32 | |
| 33 | // Mock readline.createInterface to return our mock interface |
| 34 | (readline.createInterface as any).mockReturnValue(mockInterface); |
| 35 | |
| 36 | // Mock console.log for limit messages |
| 37 | consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 38 | |
| 39 | // Mock process.exit |
| 40 | processExitSpy = vi.spyOn(process, "exit").mockImplementation((() => { |
| 41 | throw new Error("Process exit"); |
| 42 | }) as any); |
| 43 | }); |
| 44 | |
| 45 | afterEach(() => { |
| 46 | vi.clearAllMocks(); |
| 47 | consoleLogSpy.mockRestore(); |
| 48 | processExitSpy.mockRestore(); |
| 49 | }); |
| 50 | |
| 51 | describe("question", () => { |
| 52 | it("should return user input", async () => { |
| 53 | const promptText = "What is your name? "; |
| 54 | const userInput = "John Doe"; |
| 55 | |
| 56 | // Setup mock to call the callback with user input |
| 57 | mockInterface.question.mockImplementation( |
| 58 | (prompt: string, callback: (answer: string) => void) => { |
| 59 | setTimeout(() => callback(userInput), 0); |
| 60 | }, |
| 61 | ); |
| 62 | |
| 63 | const result = await question(promptText); |
| 64 | |
| 65 | expect(mockInterface.question).toHaveBeenCalledWith( |
| 66 | promptText, |
| 67 | expect.any(Function), |
| 68 | ); |
| 69 | expect(mockInterface.close).toHaveBeenCalled(); |
| 70 | expect(result).toBe(userInput); |
| 71 | }); |
| 72 | |
| 73 | it("should handle empty input", async () => { |
| 74 | const promptText = "Enter something: "; |
| 75 | |
| 76 | mockInterface.question.mockImplementation( |
| 77 | (prompt: string, callback: (answer: string) => void) => { |
| 78 | setTimeout(() => callback(""), 0); |
| 79 | }, |
| 80 | ); |
| 81 | |
| 82 | const result = await question(promptText); |
| 83 | |
| 84 | expect(result).toBe(""); |
| 85 | }); |
| 86 | |
| 87 | it("should handle special characters in input", async () => { |
| 88 | const promptText = "Enter special chars: "; |
| 89 | const userInput = "!@#$%^&*()_+-=[]{}|;':\",./<>?"; |
| 90 | |
| 91 | mockInterface.question.mockImplementation( |
| 92 | (prompt: string, callback: (answer: string) => void) => { |
| 93 | setTimeout(() => callback(userInput), 0); |
| 94 | }, |
| 95 | ); |
| 96 | |
| 97 | const result = await question(promptText); |
| 98 | |
| 99 | expect(result).toBe(userInput); |
| 100 | }); |
| 101 | |
| 102 | it.skip("should handle SIGINT (Ctrl+C) by exiting process", async () => { |
| 103 | const promptText = "Enter something: "; |
| 104 | let sigintHandler: (() => void) | null = null; |
| 105 | |
| 106 | // Setup mock to capture SIGINT handler |
| 107 | mockInterface.on.mockImplementation( |
| 108 | (event: string, handler: () => void) => { |
| 109 | if (event === "SIGINT") { |
| 110 | sigintHandler = handler; |
| 111 | } |
| 112 | return mockInterface; |
| 113 | }, |
| 114 | ); |
| 115 | |
| 116 | // Setup question mock that doesn't resolve immediately |
| 117 | mockInterface.question.mockImplementation(() => { |
| 118 | // Trigger SIGINT after question is called |
| 119 | setTimeout(() => { |
| 120 | if (sigintHandler) { |
| 121 | sigintHandler(); |
| 122 | } |
| 123 | }, 10); |
| 124 | }); |
| 125 | |
| 126 | // The promise should reject when SIGINT is triggered |
| 127 | await expect(question(promptText)).rejects.toThrow("Process exit"); |
| 128 | expect(processExitSpy).toHaveBeenCalledWith(0); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | describe("questionWithChoices", () => { |
| 133 | it("should accept valid choice", async () => { |
| 134 | const promptText = "Choose (1): "; |
| 135 | const choices = ["1", "2", "3"]; |
| 136 | const userChoice = "2"; |
| 137 | |
| 138 | mockInterface.question.mockImplementationOnce( |
| 139 | (prompt: string, callback: (answer: string) => void) => { |
| 140 | setTimeout(() => callback(userChoice), 0); |
| 141 | }, |
| 142 | ); |
| 143 | |
| 144 | const result = await questionWithChoices(promptText, choices); |
| 145 | expect(result).toBe(userChoice); |
| 146 | }); |
| 147 | |
| 148 | it("should use default choice when empty input is provided", async () => { |
| 149 | const promptText = "Choose (1): "; |
| 150 | const choices = ["1", "2", "3"]; |
| 151 | const defaultChoice = "1"; |
| 152 | |
| 153 | mockInterface.question.mockImplementationOnce( |
| 154 | (prompt: string, callback: (answer: string) => void) => { |
| 155 | setTimeout(() => callback(""), 0); |
| 156 | }, |
| 157 | ); |
| 158 | |
| 159 | const result = await questionWithChoices( |
| 160 | promptText, |
| 161 | choices, |
| 162 | defaultChoice, |
| 163 | ); |
| 164 | expect(result).toBe(defaultChoice); |
| 165 | }); |
| 166 | |
| 167 | it("should reject invalid choice and ask again", async () => { |
| 168 | const promptText = "Choose (1 or 2): "; |
| 169 | const choices = ["1", "2"]; |
| 170 | const limitMessage = "Please enter 1 or 2"; |
| 171 | |
| 172 | let callCount = 0; |
| 173 | mockInterface.question.mockImplementation( |
| 174 | (prompt: string, callback: (answer: string) => void) => { |
| 175 | setTimeout(() => { |
| 176 | if (callCount === 0) { |
| 177 | callCount++; |
| 178 | callback("3"); // Invalid choice |
| 179 | } else { |
| 180 | callback("1"); // Valid choice |
| 181 | } |
| 182 | }, 0); |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | const result = await questionWithChoices( |
| 187 | promptText, |
| 188 | choices, |
| 189 | undefined, |
| 190 | limitMessage, |
| 191 | ); |
| 192 | expect(result).toBe("1"); |
| 193 | expect(consoleLogSpy).toHaveBeenCalledWith(limitMessage); |
| 194 | }); |
| 195 | |
| 196 | it("should handle multiple invalid attempts before valid choice", async () => { |
| 197 | const promptText = "Choose: "; |
| 198 | const choices = ["yes", "no"]; |
| 199 | const limitMessage = "Invalid choice"; |
| 200 | |
| 201 | let callCount = 0; |
| 202 | const invalidAnswers = ["maybe", "perhaps"]; |
| 203 | mockInterface.question.mockImplementation( |
| 204 | (prompt: string, callback: (answer: string) => void) => { |
| 205 | setTimeout(() => { |
| 206 | if (callCount < invalidAnswers.length) { |
| 207 | callback(invalidAnswers[callCount++]); |
| 208 | } else { |
| 209 | callback("yes"); |
| 210 | } |
| 211 | }, 0); |
| 212 | }, |
| 213 | ); |
| 214 | |
| 215 | const result = await questionWithChoices( |
| 216 | promptText, |
| 217 | choices, |
| 218 | undefined, |
| 219 | limitMessage, |
| 220 | ); |
| 221 | expect(result).toBe("yes"); |
| 222 | expect(consoleLogSpy).toHaveBeenCalledTimes(2); |
| 223 | }); |
| 224 | |
| 225 | it("should not show limit message when none provided", async () => { |
| 226 | const promptText = "Choose: "; |
| 227 | const choices = ["a", "b"]; |
| 228 | |
| 229 | let callCount = 0; |
| 230 | mockInterface.question.mockImplementation( |
| 231 | (prompt: string, callback: (answer: string) => void) => { |
| 232 | setTimeout(() => { |
| 233 | if (callCount === 0) { |
| 234 | callCount++; |
| 235 | callback("c"); // Invalid |
| 236 | } else { |
| 237 | callback("a"); // Valid |
| 238 | } |
| 239 | }, 0); |
| 240 | }, |
| 241 | ); |
| 242 | |
| 243 | const result = await questionWithChoices(promptText, choices); |
| 244 | expect(result).toBe("a"); |
| 245 | expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 246 | }); |
| 247 | |
| 248 | it("should handle empty string as valid choice", async () => { |
| 249 | const promptText = "Press Enter to continue or type 'cancel': "; |
| 250 | const choices = ["", "cancel"]; |
| 251 | |
| 252 | mockInterface.question.mockImplementationOnce( |
| 253 | (prompt: string, callback: (answer: string) => void) => { |
| 254 | setTimeout(() => callback(""), 0); |
| 255 | }, |
| 256 | ); |
| 257 | |
| 258 | const result = await questionWithChoices(promptText, choices); |
| 259 | expect(result).toBe(""); |
| 260 | }); |
| 261 | |
| 262 | it("should handle choice with spaces", async () => { |
| 263 | const promptText = "Choose an option: "; |
| 264 | const choices = ["option one", "option two", "option three"]; |
| 265 | const userChoice = "option two"; |
| 266 | |
| 267 | mockInterface.question.mockImplementationOnce( |
| 268 | (prompt: string, callback: (answer: string) => void) => { |
| 269 | setTimeout(() => callback(userChoice), 0); |
| 270 | }, |
| 271 | ); |
| 272 | |
| 273 | const result = await questionWithChoices(promptText, choices); |
| 274 | expect(result).toBe(userChoice); |
| 275 | }); |
| 276 | }); |
| 277 | }); |