ghostscope-ui 0.1.1

Terminal user interface that streams GhostScope traces with async input handling.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use anyhow::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ghostscope_protocol::{streaming_parser::ParsedInstruction, ParsedTraceEvent};
use ghostscope_ui::events::{RuntimeCommand, RuntimeStatus};
use ratatui::{backend::TestBackend, Terminal};
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex};

/// Mock App wrapper for testing
pub struct TestableApp {
    /// Mock terminal for rendering
    terminal: Arc<Mutex<Terminal<TestBackend>>>,

    /// Channels for mocking runtime communication
    _command_receiver: Arc<Mutex<mpsc::UnboundedReceiver<RuntimeCommand>>>,
    _trace_sender: mpsc::UnboundedSender<ParsedTraceEvent>,
    _status_sender: mpsc::UnboundedSender<RuntimeStatus>,

    /// Internal app state (simplified for testing)
    state: Arc<Mutex<AppTestState>>,
}

#[derive(Debug)]
struct AppTestState {
    current_panel: PanelType,
    command_input: String,
    trace_events: Vec<ParsedTraceEvent>,
    runtime_status: Option<RuntimeStatus>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum PanelType {
    Command,
    Source,
    EbpfOutput,
}

impl TestableApp {
    /// Create a new testable app instance
    pub fn new(width: u16, height: u16) -> Self {
        // Create mock channels
        let (_command_sender, command_receiver) = mpsc::unbounded_channel();
        let (trace_sender, _trace_receiver) = mpsc::unbounded_channel();
        let (status_sender, _status_receiver) = mpsc::unbounded_channel();

        // Create test terminal
        let backend = TestBackend::new(width, height);
        let terminal = Terminal::new(backend).unwrap();

        // Initialize app state
        let state = AppTestState {
            current_panel: PanelType::Command,
            command_input: String::new(),
            trace_events: Vec::new(),
            runtime_status: None,
        };

        Self {
            terminal: Arc::new(Mutex::new(terminal)),
            _command_receiver: Arc::new(Mutex::new(command_receiver)),
            _trace_sender: trace_sender,
            _status_sender: status_sender,
            state: Arc::new(Mutex::new(state)),
        }
    }

    /// Simulate handling a key event
    pub async fn handle_key(&self, key: KeyEvent) -> Result<()> {
        let mut state = self.state.lock().await;

        match key.code {
            KeyCode::Tab => {
                // Switch panels
                state.current_panel = match state.current_panel {
                    PanelType::Command => PanelType::Source,
                    PanelType::Source => PanelType::EbpfOutput,
                    PanelType::EbpfOutput => PanelType::Command,
                };
            }
            KeyCode::Char(c) => {
                if state.current_panel == PanelType::Command {
                    state.command_input.push(c);
                }
            }
            KeyCode::Backspace => {
                if state.current_panel == PanelType::Command {
                    state.command_input.pop();
                }
            }
            KeyCode::Enter => {
                if state.current_panel == PanelType::Command && !state.command_input.is_empty() {
                    // Process command
                    let _cmd = state.command_input.clone();
                    state.command_input.clear();

                    // Send as RuntimeCommand (mock)
                    // In real implementation, this would parse and send the actual command
                }
            }
            _ => {}
        }

        Ok(())
    }

    /// Simulate receiving a trace event
    pub async fn receive_trace_event(&self, event: ParsedTraceEvent) -> Result<()> {
        let mut state = self.state.lock().await;
        state.trace_events.push(event);
        Ok(())
    }

    /// Simulate receiving runtime status
    pub async fn receive_runtime_status(&self, status: RuntimeStatus) -> Result<()> {
        let mut state = self.state.lock().await;
        state.runtime_status = Some(status);
        Ok(())
    }

    /// Render the UI and return the buffer content
    pub async fn render(&self) -> Result<Vec<String>> {
        let mut terminal = self.terminal.lock().await;
        let state = self.state.lock().await;

        terminal.draw(|f| {
            use ratatui::{
                layout::{Constraint, Direction, Layout},
                style::{Color, Style},
                widgets::{Block, Borders, List, ListItem, Paragraph},
            };

            // Create layout
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Percentage(30), // Command panel
                    Constraint::Percentage(40), // Source panel
                    Constraint::Percentage(30), // eBPF output panel
                ])
                .split(f.area());

            // Render command panel
            let command_block = Block::default()
                .title(format!(
                    " Command Panel {} ",
                    if state.current_panel == PanelType::Command {
                        "[FOCUSED]"
                    } else {
                        ""
                    }
                ))
                .borders(Borders::ALL)
                .border_style(if state.current_panel == PanelType::Command {
                    Style::default().fg(Color::Yellow)
                } else {
                    Style::default()
                });

            let command_text = format!("> {}", state.command_input);
            let command_widget = Paragraph::new(command_text).block(command_block);
            f.render_widget(command_widget, chunks[0]);

            // Render source panel
            let source_block = Block::default()
                .title(format!(
                    " Source Panel {} ",
                    if state.current_panel == PanelType::Source {
                        "[FOCUSED]"
                    } else {
                        ""
                    }
                ))
                .borders(Borders::ALL)
                .border_style(if state.current_panel == PanelType::Source {
                    Style::default().fg(Color::Yellow)
                } else {
                    Style::default()
                });

            let source_content = match &state.runtime_status {
                Some(RuntimeStatus::DwarfLoadingCompleted { symbols_count }) => {
                    format!("DWARF symbols loaded: {symbols_count}")
                }
                _ => "Not connected".to_string(),
            };

            let source_widget = Paragraph::new(source_content).block(source_block);
            f.render_widget(source_widget, chunks[1]);

            // Render eBPF output panel
            let ebpf_block = Block::default()
                .title(format!(
                    " eBPF Output {} ",
                    if state.current_panel == PanelType::EbpfOutput {
                        "[FOCUSED]"
                    } else {
                        ""
                    }
                ))
                .borders(Borders::ALL)
                .border_style(if state.current_panel == PanelType::EbpfOutput {
                    Style::default().fg(Color::Yellow)
                } else {
                    Style::default()
                });

            // Create list of trace events
            let items: Vec<ListItem> = state
                .trace_events
                .iter()
                .map(|event| {
                    let content =
                        format!("[{}] PID:{} TID:{}", event.timestamp, event.pid, event.tid);
                    ListItem::new(content)
                })
                .collect();

            let events_list = List::new(items).block(ebpf_block);
            f.render_widget(events_list, chunks[2]);
        })?;

        // Extract buffer content
        let buffer = terminal.backend().buffer();
        let mut lines = Vec::new();

        for y in 0..buffer.area.height {
            let mut line = String::new();
            for x in 0..buffer.area.width {
                let cell = &buffer[(x, y)];
                line.push_str(cell.symbol());
            }
            lines.push(line.trim_end().to_string());
        }

        Ok(lines)
    }

    /// Get current panel
    pub async fn get_current_panel(&self) -> PanelType {
        self.state.lock().await.current_panel.clone()
    }

    /// Get command input
    pub async fn get_command_input(&self) -> String {
        self.state.lock().await.command_input.clone()
    }

    /// Get trace events
    pub async fn get_trace_events(&self) -> Vec<ParsedTraceEvent> {
        self.state.lock().await.trace_events.clone()
    }

    /// Assert text appears in rendered output
    pub async fn assert_text_in_output(&self, text: &str) -> bool {
        let lines = self.render().await.unwrap();
        lines.iter().any(|line| line.contains(text))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_panel_navigation() {
        let app = TestableApp::new(120, 40);

        // Initial panel should be Command
        assert_eq!(app.get_current_panel().await, PanelType::Command);

        // Press Tab to switch to Source
        app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::empty()))
            .await
            .unwrap();
        assert_eq!(app.get_current_panel().await, PanelType::Source);

        // Press Tab to switch to EbpfOutput
        app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::empty()))
            .await
            .unwrap();
        assert_eq!(app.get_current_panel().await, PanelType::EbpfOutput);

        // Press Tab to cycle back to Command
        app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::empty()))
            .await
            .unwrap();
        assert_eq!(app.get_current_panel().await, PanelType::Command);
    }

    #[tokio::test]
    async fn test_command_input() {
        let app = TestableApp::new(120, 40);

        // Type "print x"
        let command = "print x";
        for c in command.chars() {
            app.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::empty()))
                .await
                .unwrap();
        }

        assert_eq!(app.get_command_input().await, "print x");

        // Test backspace
        app.handle_key(KeyEvent::new(KeyCode::Backspace, KeyModifiers::empty()))
            .await
            .unwrap();
        assert_eq!(app.get_command_input().await, "print ");

        // Test clear on Enter
        app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::empty()))
            .await
            .unwrap();
        assert_eq!(app.get_command_input().await, "");
    }

    #[tokio::test]
    async fn test_trace_event_display() {
        let app = TestableApp::new(120, 40);

        // Add trace events
        let event1 = ParsedTraceEvent {
            timestamp: 1000,
            trace_id: 1,
            pid: 12345,
            tid: 67890,
            instructions: vec![ParsedInstruction::PrintString {
                content: "Entering main function".to_string(),
            }],
        };

        let event2 = ParsedTraceEvent {
            timestamp: 2000,
            trace_id: 2,
            pid: 12345,
            tid: 67890,
            instructions: vec![ParsedInstruction::PrintString {
                content: "Processing data: x=42".to_string(),
            }],
        };

        app.receive_trace_event(event1).await.unwrap();
        app.receive_trace_event(event2).await.unwrap();

        // Verify events are stored
        let events = app.get_trace_events().await;
        assert_eq!(events.len(), 2);
        assert_eq!(events[0].trace_id, 1);
        assert_eq!(events[1].trace_id, 2);

        // Verify rendering
        assert!(app.assert_text_in_output("12345").await);
        assert!(app.assert_text_in_output("67890").await);
    }

    #[tokio::test]
    async fn test_runtime_status_display() {
        let app = TestableApp::new(120, 40);

        // Set runtime status
        let status = RuntimeStatus::DwarfLoadingCompleted {
            symbols_count: 1500,
        };

        app.receive_runtime_status(status).await.unwrap();

        // Verify status appears in render
        assert!(app.assert_text_in_output("DWARF symbols loaded").await);
        assert!(app.assert_text_in_output("1500").await);
    }

    #[tokio::test]
    async fn test_panel_focus_indication() {
        let app = TestableApp::new(120, 40);

        // Command panel should be focused initially
        assert!(app.assert_text_in_output("Command Panel [FOCUSED]").await);

        // Switch to Source panel
        app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::empty()))
            .await
            .unwrap();
        assert!(app.assert_text_in_output("Source Panel [FOCUSED]").await);

        // Switch to eBPF Output panel
        app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::empty()))
            .await
            .unwrap();
        assert!(app.assert_text_in_output("eBPF Output [FOCUSED]").await);
    }

    #[tokio::test]
    async fn test_complex_interaction_flow() {
        let app = TestableApp::new(120, 40);

        // 1. Connect to runtime
        let status = RuntimeStatus::DwarfLoadingCompleted {
            symbols_count: 2000,
        };
        app.receive_runtime_status(status).await.unwrap();

        // 2. Type and execute a command
        for c in "trace main".chars() {
            app.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::empty()))
                .await
                .unwrap();
        }
        assert_eq!(app.get_command_input().await, "trace main");

        // 3. Receive trace events
        for i in 0u64..5 {
            let event = ParsedTraceEvent {
                timestamp: 1000 + i * 100,
                trace_id: i,
                pid: 99999,
                tid: 11111,
                instructions: vec![ParsedInstruction::PrintString {
                    content: format!("Event {i}"),
                }],
            };
            app.receive_trace_event(event).await.unwrap();
        }

        // 4. Verify everything is displayed
        assert!(app.assert_text_in_output("2000").await);
        assert!(app.assert_text_in_output("trace main").await);
        assert!(app.assert_text_in_output("99999").await);
        assert!(app.assert_text_in_output("11111").await);
    }
}