| 1 | import { vi } from "vitest"; |
| 2 | |
| 3 | import { InputHistory } from "./inputHistory.js"; |
| 4 | |
| 5 | // Mock fs to avoid creating actual files during tests |
| 6 | vi.mock("fs", () => ({ |
| 7 | default: { |
| 8 | existsSync: vi.fn(), |
| 9 | readFileSync: vi.fn(), |
| 10 | writeFileSync: vi.fn(), |
| 11 | mkdirSync: vi.fn(), |
| 12 | }, |
| 13 | existsSync: vi.fn(), |
| 14 | readFileSync: vi.fn(), |
| 15 | writeFileSync: vi.fn(), |
| 16 | mkdirSync: vi.fn(), |
| 17 | })); |
| 18 | |
| 19 | describe("InputHistory", () => { |
| 20 | let inputHistory: InputHistory; |
| 21 | |
| 22 | beforeEach(async () => { |
| 23 | // Reset mocks and set up default behavior |
| 24 | vi.clearAllMocks(); |
| 25 | const fs = await import("fs"); |
| 26 | vi.mocked(fs.existsSync).mockReturnValue(false); |
| 27 | vi.mocked(fs.readFileSync).mockReturnValue("[]"); |
| 28 | |
| 29 | inputHistory = new InputHistory(); |
| 30 | }); |
| 31 | |
| 32 | describe("navigation with typed input preservation", () => { |
| 33 | beforeEach(() => { |
| 34 | // Add some history entries |
| 35 | inputHistory.addEntry("first command"); |
| 36 | inputHistory.addEntry("second command"); |
| 37 | }); |
| 38 | |
| 39 | it("should preserve typed input when navigating history", () => { |
| 40 | const typedInput = "user typed this"; |
| 41 | |
| 42 | // User types something then presses up arrow |
| 43 | const historyEntry1 = inputHistory.navigateUp(typedInput); |
| 44 | expect(historyEntry1).toBe("second command"); |
| 45 | |
| 46 | // Then presses down arrow - should get back their typed input |
| 47 | const returnedInput = inputHistory.navigateDown(); |
| 48 | expect(returnedInput).toBe("user typed this"); |
| 49 | }); |
| 50 | |
| 51 | it("should handle multiple up/down navigation cycles", () => { |
| 52 | const typedInput = "hello world"; |
| 53 | |
| 54 | // Navigate up twice, then back down twice |
| 55 | inputHistory.navigateUp(typedInput); |
| 56 | inputHistory.navigateUp(typedInput); |
| 57 | inputHistory.navigateDown(); // Go back one in history |
| 58 | const result = inputHistory.navigateDown(); // Go back to original |
| 59 | |
| 60 | expect(result).toBe("hello world"); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe("basic navigation functionality", () => { |
| 65 | beforeEach(() => { |
| 66 | inputHistory.addEntry("command1"); |
| 67 | inputHistory.addEntry("command2"); |
| 68 | inputHistory.addEntry("command3"); |
| 69 | }); |
| 70 | |
| 71 | it("should navigate up through history in reverse order", () => { |
| 72 | const result1 = inputHistory.navigateUp("current"); |
| 73 | expect(result1).toBe("command3"); // Most recent first |
| 74 | |
| 75 | const result2 = inputHistory.navigateUp("current"); |
| 76 | expect(result2).toBe("command2"); |
| 77 | |
| 78 | const result3 = inputHistory.navigateUp("current"); |
| 79 | expect(result3).toBe("command1"); |
| 80 | }); |
| 81 | |
| 82 | it("should handle navigation with no history", () => { |
| 83 | const emptyHistory = new InputHistory(); |
| 84 | const result = emptyHistory.navigateUp("test"); |
| 85 | expect(result).toBe(null); |
| 86 | }); |
| 87 | }); |
| 88 | }); |