|
| 1 | +import type { LogEntry } from '../LogPanel.js'; |
| 2 | +import { FullScreenLogView } from '../FullScreenLogView.js'; |
| 3 | +import { render } from 'ink-testing-library'; |
| 4 | +import React from 'react'; |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +const ESCAPE = '\x1B'; |
| 8 | +const UP = '\x1B[A'; |
| 9 | + |
| 10 | +function delay(ms = 50) { |
| 11 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 12 | +} |
| 13 | + |
| 14 | +afterEach(() => vi.restoreAllMocks()); |
| 15 | + |
| 16 | +function makeLogs(count: number): LogEntry[] { |
| 17 | + return Array.from({ length: count }, (_, i) => ({ |
| 18 | + level: 'info' as const, |
| 19 | + message: `Log message ${i + 1}`, |
| 20 | + timestamp: new Date(2024, 0, 1, 0, 0, i), |
| 21 | + })); |
| 22 | +} |
| 23 | + |
| 24 | +describe('FullScreenLogView', () => { |
| 25 | + it('renders log entries', () => { |
| 26 | + const logs: LogEntry[] = [ |
| 27 | + { level: 'info', message: 'Starting deploy', timestamp: new Date() }, |
| 28 | + { level: 'error', message: 'Deploy failed', timestamp: new Date() }, |
| 29 | + ]; |
| 30 | + const { lastFrame } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 31 | + const frame = lastFrame()!; |
| 32 | + |
| 33 | + expect(frame).toContain('Starting deploy'); |
| 34 | + expect(frame).toContain('Deploy failed'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('renders header with entry count', () => { |
| 38 | + const logs = makeLogs(5); |
| 39 | + const { lastFrame } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 40 | + |
| 41 | + expect(lastFrame()).toContain('5 entries'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('renders log file path when provided', () => { |
| 45 | + const logs = makeLogs(2); |
| 46 | + const { lastFrame } = render( |
| 47 | + <FullScreenLogView logs={logs} logFilePath="/tmp/deploy.log" onExit={vi.fn()} /> |
| 48 | + ); |
| 49 | + |
| 50 | + expect(lastFrame()).toContain('/tmp/deploy.log'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('shows "No logs yet" when empty', () => { |
| 54 | + const { lastFrame } = render(<FullScreenLogView logs={[]} onExit={vi.fn()} />); |
| 55 | + |
| 56 | + expect(lastFrame()).toContain('No logs yet'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('calls onExit on Escape key', () => { |
| 60 | + const onExit = vi.fn(); |
| 61 | + const { stdin } = render(<FullScreenLogView logs={makeLogs(3)} onExit={onExit} />); |
| 62 | + |
| 63 | + stdin.write(ESCAPE); |
| 64 | + |
| 65 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 66 | + }); |
| 67 | + |
| 68 | + it('calls onExit on q key', () => { |
| 69 | + const onExit = vi.fn(); |
| 70 | + const { stdin } = render(<FullScreenLogView logs={makeLogs(3)} onExit={onExit} />); |
| 71 | + |
| 72 | + stdin.write('\x11'); // Ctrl+Q |
| 73 | + |
| 74 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it('calls onExit on l key', () => { |
| 78 | + const onExit = vi.fn(); |
| 79 | + const { stdin } = render(<FullScreenLogView logs={makeLogs(3)} onExit={onExit} />); |
| 80 | + |
| 81 | + stdin.write('l'); |
| 82 | + |
| 83 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it('renders error log with level label', () => { |
| 87 | + const logs: LogEntry[] = [{ level: 'error', message: 'Something broke', timestamp: new Date() }]; |
| 88 | + const { lastFrame } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 89 | + const frame = lastFrame()!; |
| 90 | + |
| 91 | + expect(frame).toContain('ERROR'); |
| 92 | + expect(frame).toContain('Something broke'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('renders response log with special formatting', () => { |
| 96 | + const logs: LogEntry[] = [{ level: 'response', message: 'Agent response text', timestamp: new Date() }]; |
| 97 | + const { lastFrame } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 98 | + const frame = lastFrame()!; |
| 99 | + |
| 100 | + expect(frame).toContain('Response'); |
| 101 | + expect(frame).toContain('Agent response text'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('renders footer with navigation hints', () => { |
| 105 | + const { lastFrame } = render(<FullScreenLogView logs={makeLogs(3)} onExit={vi.fn()} />); |
| 106 | + |
| 107 | + expect(lastFrame()).toContain('Esc/q/l exit'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('shows scroll percentage', () => { |
| 111 | + const logs = makeLogs(3); |
| 112 | + const { lastFrame } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 113 | + |
| 114 | + // Should show some percentage |
| 115 | + expect(lastFrame()).toMatch(/\d+%/); |
| 116 | + }); |
| 117 | + |
| 118 | + it('scrolls with arrow keys', async () => { |
| 119 | + // Create enough logs to require scrolling |
| 120 | + const logs = makeLogs(50); |
| 121 | + const { lastFrame, stdin } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 122 | + |
| 123 | + await delay(); |
| 124 | + stdin.write(UP); |
| 125 | + await delay(); |
| 126 | + |
| 127 | + // After scrolling up, the frame should change |
| 128 | + const frame = lastFrame()!; |
| 129 | + expect(frame).toMatch(/\d+%/); |
| 130 | + }); |
| 131 | + |
| 132 | + it('supports vim-style navigation with j/k', async () => { |
| 133 | + const logs = makeLogs(50); |
| 134 | + const { stdin } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 135 | + |
| 136 | + // These should not throw |
| 137 | + await delay(); |
| 138 | + stdin.write('k'); // scroll up |
| 139 | + stdin.write('j'); // scroll down |
| 140 | + await delay(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('supports g/G for top/bottom navigation', async () => { |
| 144 | + const logs = makeLogs(50); |
| 145 | + const { lastFrame, stdin } = render(<FullScreenLogView logs={logs} onExit={vi.fn()} />); |
| 146 | + |
| 147 | + await delay(); |
| 148 | + stdin.write('g'); // go to top |
| 149 | + await delay(); |
| 150 | + |
| 151 | + // At top, should show first log |
| 152 | + expect(lastFrame()).toContain('Log message 1'); |
| 153 | + |
| 154 | + stdin.write('G'); // go to bottom |
| 155 | + await delay(); |
| 156 | + |
| 157 | + // At bottom, should show last log |
| 158 | + expect(lastFrame()).toContain('Log message 50'); |
| 159 | + }); |
| 160 | +}); |
0 commit comments