|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('conf', () => ({ |
| 4 | + default: vi.fn(() => ({ |
| 5 | + get: vi.fn((key) => { |
| 6 | + if (key === 'cwd') return '/mockuser'; |
| 7 | + return null; |
| 8 | + }), |
| 9 | + })), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('node:child_process'); |
| 13 | + |
| 14 | +vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 15 | +vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 16 | + |
| 17 | +let executor; |
| 18 | +let execMock; |
| 19 | + |
| 20 | +beforeEach(async () => { |
| 21 | + vi.resetModules(); |
| 22 | + vi.clearAllMocks(); |
| 23 | + |
| 24 | + const childProcess = await import('node:child_process'); |
| 25 | + execMock = vi.mocked(childProcess.exec); |
| 26 | + |
| 27 | + executor = await import('../src/executor.js'); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('getPrompt', () => { |
| 31 | + it('should contain puter@<cwd name>', () => { |
| 32 | + const prompt = executor.getPrompt(); |
| 33 | + |
| 34 | + expect(prompt).toContain('puter@mockuser'); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('showHelp', () => { |
| 39 | + it('should display available commands', async () => { |
| 40 | + await executor.execCommand('help'); |
| 41 | + |
| 42 | + expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Available commands')); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('host command (!)', () => { |
| 47 | + it('should call exec with the command after !', async () => { |
| 48 | + await executor.execCommand('!ls -la'); |
| 49 | + |
| 50 | + expect(execMock).toHaveBeenCalledWith('ls -la', expect.any(Function)); |
| 51 | + }); |
| 52 | +}); |
0 commit comments