|
| 1 | +import type { LogEntry } from '../LogPanel.js'; |
| 2 | +import { LogPanel } from '../LogPanel.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 UP_ARROW = '\x1B[A'; |
| 8 | +const DOWN_ARROW = '\x1B[B'; |
| 9 | + |
| 10 | +afterEach(() => vi.restoreAllMocks()); |
| 11 | + |
| 12 | +const makeLogs = (count: number, level: LogEntry['level'] = 'system'): LogEntry[] => |
| 13 | + Array.from({ length: count }, (_, i) => ({ |
| 14 | + level, |
| 15 | + message: `Log message ${i + 1}`, |
| 16 | + })); |
| 17 | + |
| 18 | +describe('LogPanel', () => { |
| 19 | + describe('empty state', () => { |
| 20 | + it('renders "No output yet" with no other content', () => { |
| 21 | + const { lastFrame } = render(<LogPanel logs={[]} />); |
| 22 | + expect(lastFrame()).toBe('No output yet'); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('rendering', () => { |
| 27 | + it('renders system log messages without level label', () => { |
| 28 | + const logs: LogEntry[] = [{ level: 'system', message: 'Agent started' }]; |
| 29 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 30 | + const frame = lastFrame()!; |
| 31 | + expect(frame).toContain('Agent started'); |
| 32 | + // System logs don't show the level label prefix |
| 33 | + expect(frame).not.toContain('SYSTEM'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renders response logs with "Response" separator and message', () => { |
| 37 | + const logs: LogEntry[] = [{ level: 'response', message: 'Hello from agent' }]; |
| 38 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 39 | + const frame = lastFrame()!; |
| 40 | + expect(frame).toContain('─── Response ───'); |
| 41 | + expect(frame).toContain('Hello from agent'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('renders error logs with ERROR level prefix', () => { |
| 45 | + const logs: LogEntry[] = [{ level: 'error', message: 'Something broke' }]; |
| 46 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 47 | + const frame = lastFrame()!; |
| 48 | + // ERROR label is padded to 6 chars |
| 49 | + expect(frame).toMatch(/ERROR\s+Something broke/); |
| 50 | + }); |
| 51 | + |
| 52 | + it('renders warn logs with WARN level prefix', () => { |
| 53 | + const logs: LogEntry[] = [{ level: 'warn', message: 'Slow response' }]; |
| 54 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 55 | + expect(lastFrame()).toMatch(/WARN\s+Slow response/); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('minimal filtering', () => { |
| 60 | + it('hides info-level logs in minimal mode (default)', () => { |
| 61 | + const logs: LogEntry[] = [ |
| 62 | + { level: 'info', message: 'Debug info' }, |
| 63 | + { level: 'system', message: 'Visible system log' }, |
| 64 | + ]; |
| 65 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 66 | + expect(lastFrame()).not.toContain('Debug info'); |
| 67 | + expect(lastFrame()).toContain('Visible system log'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('hides logs containing JSON debug markers like "timestamp" or "level"', () => { |
| 71 | + const logs: LogEntry[] = [ |
| 72 | + { level: 'error', message: '{"timestamp": "2024-01-01", "level": "ERROR"}' }, |
| 73 | + { level: 'system', message: 'Visible log' }, |
| 74 | + ]; |
| 75 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 76 | + expect(lastFrame()).not.toContain('timestamp'); |
| 77 | + expect(lastFrame()).toContain('Visible log'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('hides warn/error logs starting with [ or { as JSON debug', () => { |
| 81 | + const logs: LogEntry[] = [ |
| 82 | + { level: 'warn', message: '[{"key": "value"}]' }, |
| 83 | + { level: 'error', message: '{"error": "details"}' }, |
| 84 | + { level: 'system', message: 'Keep this' }, |
| 85 | + ]; |
| 86 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 87 | + expect(lastFrame()).not.toContain('key'); |
| 88 | + expect(lastFrame()).not.toContain('details'); |
| 89 | + expect(lastFrame()).toContain('Keep this'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('always shows response and system logs even with JSON-like content', () => { |
| 93 | + const logs: LogEntry[] = [ |
| 94 | + { level: 'response', message: '{"data": "json response"}' }, |
| 95 | + { level: 'system', message: '{"internal": true}' }, |
| 96 | + ]; |
| 97 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 98 | + expect(lastFrame()).toContain('json response'); |
| 99 | + expect(lastFrame()).toContain('internal'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('shows plain error/warn messages that are not JSON', () => { |
| 103 | + const logs: LogEntry[] = [ |
| 104 | + { level: 'error', message: 'Connection timeout' }, |
| 105 | + { level: 'warn', message: 'Retrying in 5s' }, |
| 106 | + ]; |
| 107 | + const { lastFrame } = render(<LogPanel logs={logs} />); |
| 108 | + expect(lastFrame()).toContain('Connection timeout'); |
| 109 | + expect(lastFrame()).toContain('Retrying in 5s'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('shows all logs including info when minimal is false', () => { |
| 113 | + const logs: LogEntry[] = [ |
| 114 | + { level: 'info', message: 'Debug info visible' }, |
| 115 | + { level: 'system', message: 'System log' }, |
| 116 | + ]; |
| 117 | + const { lastFrame } = render(<LogPanel logs={logs} minimal={false} />); |
| 118 | + expect(lastFrame()).toContain('Debug info visible'); |
| 119 | + expect(lastFrame()).toContain('System log'); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('scrolling', () => { |
| 124 | + it('shows "↑↓ scroll" indicator when logs exceed maxLines', () => { |
| 125 | + const logs = makeLogs(20); |
| 126 | + const { lastFrame } = render(<LogPanel logs={logs} maxLines={5} minimal={false} />); |
| 127 | + expect(lastFrame()).toContain('↑↓ scroll'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('does not show scroll indicator when all logs fit in maxLines', () => { |
| 131 | + const logs = makeLogs(3); |
| 132 | + const { lastFrame } = render(<LogPanel logs={logs} maxLines={10} minimal={false} />); |
| 133 | + expect(lastFrame()).not.toContain('↑↓ scroll'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('auto-scrolls to bottom showing latest logs', () => { |
| 137 | + const logs = makeLogs(20); |
| 138 | + const { lastFrame } = render(<LogPanel logs={logs} maxLines={5} minimal={false} />); |
| 139 | + const frame = lastFrame()!; |
| 140 | + // Should show the last 5 logs (16-20) and "more above" |
| 141 | + expect(frame).toContain('Log message 20'); |
| 142 | + expect(frame).toContain('Log message 16'); |
| 143 | + // 'Log message 1' would match 'Log message 16' etc, so use regex for exact match |
| 144 | + expect(frame).not.toMatch(/Log message 1\b/); |
| 145 | + expect(frame).toContain('more above'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('switches to manual scroll on up arrow, showing earliest logs', async () => { |
| 149 | + const logs = makeLogs(20); |
| 150 | + const { lastFrame, stdin } = render(<LogPanel logs={logs} maxLines={5} minimal={false} />); |
| 151 | + |
| 152 | + // Initially auto-scrolled to bottom |
| 153 | + expect(lastFrame()).toContain('Log message 20'); |
| 154 | + |
| 155 | + // Up arrow sets userScrolled=true and scrollOffset stays at 0 (initial state), |
| 156 | + // so we jump to the top of the log showing messages 1-5 |
| 157 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 158 | + stdin.write(UP_ARROW); |
| 159 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 160 | + |
| 161 | + const frame = lastFrame()!; |
| 162 | + expect(frame).toContain('Log message 1'); |
| 163 | + expect(frame).not.toContain('Log message 20'); |
| 164 | + expect(frame).toContain('more below'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('scrolls back down to bottom after scrolling up', async () => { |
| 168 | + const logs = makeLogs(20); |
| 169 | + const { lastFrame, stdin } = render(<LogPanel logs={logs} maxLines={5} minimal={false} />); |
| 170 | + |
| 171 | + // Scroll up to top |
| 172 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 173 | + stdin.write(UP_ARROW); |
| 174 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 175 | + expect(lastFrame()).toContain('Log message 1'); |
| 176 | + |
| 177 | + // Scroll down past maxScroll (15) to reach the bottom |
| 178 | + for (let i = 0; i < 15; i++) { |
| 179 | + await new Promise(resolve => setTimeout(resolve, 20)); |
| 180 | + stdin.write(DOWN_ARROW); |
| 181 | + } |
| 182 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 183 | + |
| 184 | + expect(lastFrame()).toContain('Log message 20'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('supports vim-style j/k keys for scrolling', async () => { |
| 188 | + const logs = makeLogs(20); |
| 189 | + const { lastFrame, stdin } = render(<LogPanel logs={logs} maxLines={5} minimal={false} />); |
| 190 | + |
| 191 | + // k scrolls up (same as up arrow) |
| 192 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 193 | + stdin.write('k'); |
| 194 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 195 | + |
| 196 | + expect(lastFrame()).toContain('Log message 1'); |
| 197 | + |
| 198 | + // j scrolls down |
| 199 | + stdin.write('j'); |
| 200 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 201 | + |
| 202 | + expect(lastFrame()).toContain('Log message 2'); |
| 203 | + }); |
| 204 | + }); |
| 205 | +}); |
0 commit comments