diff --git a/src/util/inputHistory.test.ts b/src/util/inputHistory.test.ts new file mode 100644 index 000000000..d5b4c571e --- /dev/null +++ b/src/util/inputHistory.test.ts @@ -0,0 +1,88 @@ +import { vi } from 'vitest'; + +import { InputHistory } from './inputHistory.js'; + +// Mock fs to avoid creating actual files during tests +vi.mock('fs', () => ({ + default: { + existsSync: vi.fn(), + readFileSync: vi.fn(), + writeFileSync: vi.fn(), + mkdirSync: vi.fn() + }, + existsSync: vi.fn(), + readFileSync: vi.fn(), + writeFileSync: vi.fn(), + mkdirSync: vi.fn() +})); + +describe('InputHistory', () => { + let inputHistory: InputHistory; + + beforeEach(async () => { + // Reset mocks and set up default behavior + vi.clearAllMocks(); + const fs = await import('fs'); + vi.mocked(fs.existsSync).mockReturnValue(false); + vi.mocked(fs.readFileSync).mockReturnValue('[]'); + + inputHistory = new InputHistory(); + }); + + describe('navigation with typed input preservation', () => { + beforeEach(() => { + // Add some history entries + inputHistory.addEntry('first command'); + inputHistory.addEntry('second command'); + }); + + it('should preserve typed input when navigating history', () => { + const typedInput = 'user typed this'; + + // User types something then presses up arrow + const historyEntry1 = inputHistory.navigateUp(typedInput); + expect(historyEntry1).toBe('second command'); + + // Then presses down arrow - should get back their typed input + const returnedInput = inputHistory.navigateDown(); + expect(returnedInput).toBe('user typed this'); + }); + + it('should handle multiple up/down navigation cycles', () => { + const typedInput = 'hello world'; + + // Navigate up twice, then back down twice + inputHistory.navigateUp(typedInput); + inputHistory.navigateUp(typedInput); + inputHistory.navigateDown(); // Go back one in history + const result = inputHistory.navigateDown(); // Go back to original + + expect(result).toBe('hello world'); + }); + }); + + describe('basic navigation functionality', () => { + beforeEach(() => { + inputHistory.addEntry('command1'); + inputHistory.addEntry('command2'); + inputHistory.addEntry('command3'); + }); + + it('should navigate up through history in reverse order', () => { + const result1 = inputHistory.navigateUp('current'); + expect(result1).toBe('command3'); // Most recent first + + const result2 = inputHistory.navigateUp('current'); + expect(result2).toBe('command2'); + + const result3 = inputHistory.navigateUp('current'); + expect(result3).toBe('command1'); + }); + + it('should handle navigation with no history', () => { + const emptyHistory = new InputHistory(); + const result = emptyHistory.navigateUp('test'); + expect(result).toBe(null); + }); + }); +}); \ No newline at end of file diff --git a/src/util/inputHistory.ts b/src/util/inputHistory.ts index 7c076bf1f..32cc2a9f0 100644 --- a/src/util/inputHistory.ts +++ b/src/util/inputHistory.ts @@ -93,8 +93,9 @@ export class InputHistory { return this.history[this.currentIndex]?.text || null; } else { // Return to original input + const originalInput = this.originalInput; this.resetNavigation(); - return this.originalInput; + return originalInput; } }