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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
use ratatui::{
    backend::TestBackend,
    layout::{Constraint, Direction, Layout},
    widgets::{Block, Borders, List, ListItem, Paragraph},
    Terminal,
};
use std::collections::VecDeque;

/// Complex scenario: Multi-line command editing with history
mod multiline_editing_tests {

    struct MultilineEditor {
        lines: Vec<String>,
        cursor_line: usize,
        cursor_col: usize,
        viewport_offset: usize,
        viewport_height: usize,
    }

    impl MultilineEditor {
        fn new(viewport_height: usize) -> Self {
            Self {
                lines: vec![String::new()],
                cursor_line: 0,
                cursor_col: 0,
                viewport_offset: 0,
                viewport_height,
            }
        }

        fn insert_char(&mut self, ch: char) {
            if ch == '\n' {
                self.split_line_at_cursor();
            } else {
                self.lines[self.cursor_line].insert(self.cursor_col, ch);
                self.cursor_col += 1;
            }
            self.update_viewport();
        }

        fn split_line_at_cursor(&mut self) {
            let current_line = &mut self.lines[self.cursor_line];
            let new_line = current_line.split_off(self.cursor_col);
            self.cursor_line += 1;
            self.cursor_col = 0;
            self.lines.insert(self.cursor_line, new_line);
        }

        fn move_cursor_up(&mut self) {
            if self.cursor_line > 0 {
                self.cursor_line -= 1;
                self.cursor_col = self.cursor_col.min(self.lines[self.cursor_line].len());
                self.update_viewport();
            }
        }

        fn move_cursor_down(&mut self) {
            if self.cursor_line < self.lines.len() - 1 {
                self.cursor_line += 1;
                self.cursor_col = self.cursor_col.min(self.lines[self.cursor_line].len());
                self.update_viewport();
            }
        }

        fn update_viewport(&mut self) {
            if self.cursor_line < self.viewport_offset {
                self.viewport_offset = self.cursor_line;
            } else if self.cursor_line >= self.viewport_offset + self.viewport_height {
                self.viewport_offset = self.cursor_line - self.viewport_height + 1;
            }
        }

        fn get_visible_lines(&self) -> Vec<(usize, &str)> {
            self.lines
                .iter()
                .enumerate()
                .skip(self.viewport_offset)
                .take(self.viewport_height)
                .map(|(i, line)| (i, line.as_str()))
                .collect()
        }
    }

    #[test]
    fn test_multiline_command_editing() {
        let mut editor = MultilineEditor::new(5);

        // Type a multi-line script
        let script = "trace main --script '\nprint x\nprint y\nreturn\n'";
        for ch in script.chars() {
            editor.insert_char(ch);
        }

        assert_eq!(editor.lines.len(), 5);
        assert_eq!(editor.lines[0], "trace main --script '");
        assert_eq!(editor.lines[1], "print x");
        assert_eq!(editor.lines[2], "print y");
        assert_eq!(editor.lines[3], "return");
        assert_eq!(editor.lines[4], "'");
    }

    #[test]
    fn test_viewport_scrolling_with_cursor() {
        let mut editor = MultilineEditor::new(3); // Small viewport

        // Add more lines than viewport can show
        for i in 0..10 {
            if i > 0 {
                editor.insert_char('\n');
            }
            for ch in format!("Line {i}").chars() {
                editor.insert_char(ch);
            }
        }

        // Move cursor up beyond viewport
        for _ in 0..10 {
            editor.move_cursor_up();
        }

        assert_eq!(editor.cursor_line, 0);
        assert_eq!(editor.viewport_offset, 0);

        // Move cursor down to trigger scroll
        for _ in 0..5 {
            editor.move_cursor_down();
        }

        assert_eq!(editor.cursor_line, 5);
        assert!(editor.viewport_offset > 0);

        let visible = editor.get_visible_lines();
        assert_eq!(visible.len(), 3);
    }
}

/// Complex scenario: Command execution with real-time output
mod execution_output_tests {
    use super::*;

    struct ExecutionContext {
        command_history: VecDeque<String>,
        output_buffer: Vec<String>,
        is_executing: bool,
        execution_progress: f32,
    }

    impl ExecutionContext {
        fn new() -> Self {
            Self {
                command_history: VecDeque::new(),
                output_buffer: Vec::new(),
                is_executing: false,
                execution_progress: 0.0,
            }
        }

        fn execute_command(&mut self, command: String) {
            self.command_history.push_back(command.clone());
            if self.command_history.len() > 100 {
                self.command_history.pop_front();
            }

            self.is_executing = true;
            self.execution_progress = 0.0;
            self.output_buffer.clear();

            // Simulate command execution
            self.output_buffer.push(format!("> {command}"));
            self.output_buffer.push("Starting execution...".to_string());
        }

        fn update_progress(&mut self, delta: f32) {
            if self.is_executing {
                self.execution_progress = (self.execution_progress + delta).min(1.0);

                // Generate output based on progress
                if self.execution_progress > 0.25 && self.output_buffer.len() < 3 {
                    self.output_buffer.push("25% complete...".to_string());
                }
                if self.execution_progress > 0.5 && self.output_buffer.len() < 4 {
                    self.output_buffer.push("50% complete...".to_string());
                }
                if self.execution_progress > 0.75 && self.output_buffer.len() < 5 {
                    self.output_buffer.push("75% complete...".to_string());
                }
                if self.execution_progress >= 1.0 {
                    self.output_buffer.push("Execution completed!".to_string());
                    self.is_executing = false;
                }
            }
        }
    }

    #[test]
    fn test_command_execution_flow() {
        let mut ctx = ExecutionContext::new();

        ctx.execute_command("trace main --verbose".to_string());
        assert!(ctx.is_executing);
        assert_eq!(ctx.output_buffer[0], "> trace main --verbose");

        // Simulate progress updates
        ctx.update_progress(0.3);
        assert!(ctx.output_buffer.contains(&"25% complete...".to_string()));

        ctx.update_progress(0.3);
        assert!(ctx.output_buffer.contains(&"50% complete...".to_string()));

        ctx.update_progress(0.5);
        assert!(!ctx.is_executing);
        assert!(ctx
            .output_buffer
            .contains(&"Execution completed!".to_string()));
    }

    #[test]
    fn test_output_buffer_rendering() {
        let backend = TestBackend::new(60, 15);
        let mut terminal = Terminal::new(backend).unwrap();

        let mut ctx = ExecutionContext::new();
        ctx.execute_command("print variable_data".to_string());
        ctx.update_progress(0.6);

        terminal
            .draw(|f| {
                let chunks = Layout::default()
                    .direction(Direction::Vertical)
                    .constraints([
                        Constraint::Length(3), // Command input
                        Constraint::Min(0),    // Output
                    ])
                    .split(f.area());

                // Render command input area
                let input = Paragraph::new("> ")
                    .block(Block::default().title("Command").borders(Borders::ALL));
                f.render_widget(input, chunks[0]);

                // Render output with progress
                let mut output_lines: Vec<ListItem> = ctx
                    .output_buffer
                    .iter()
                    .map(|line| ListItem::new(line.as_str()))
                    .collect();

                if ctx.is_executing {
                    let progress_bar = format!(
                        "Progress: [{}{}] {:.0}%",
                        "=".repeat((ctx.execution_progress * 20.0) as usize),
                        " ".repeat((20.0 - ctx.execution_progress * 20.0) as usize),
                        ctx.execution_progress * 100.0
                    );
                    output_lines.push(ListItem::new(progress_bar));
                }

                let output_list = List::new(output_lines)
                    .block(Block::default().title("Output").borders(Borders::ALL));
                f.render_widget(output_list, chunks[1]);
            })
            .unwrap();

        // Verify output is rendered
        let buffer = terminal.backend().buffer();
        let mut found_progress = false;

        for y in 0..15 {
            let mut line = String::new();
            for x in 0..60 {
                let cell = &buffer[(x, y)];
                line.push_str(cell.symbol());
            }
            if line.contains("50% complete") {
                found_progress = true;
                break;
            }
        }

        assert!(found_progress, "Progress output should be visible");
    }
}

/// Complex scenario: Interactive debugging session
mod interactive_debugging_tests {
    use super::*;

    #[derive(Debug, PartialEq)]
    enum DebuggerState {
        Idle,
        Running,
        Paused { line: usize },
        SteppingOver,
        SteppingInto,
    }

    struct DebugSession {
        state: DebuggerState,
        breakpoints: Vec<usize>,
        call_stack: Vec<String>,
        variables: Vec<(String, String)>,
        current_line: usize,
    }

    impl DebugSession {
        fn new() -> Self {
            Self {
                state: DebuggerState::Idle,
                breakpoints: Vec::new(),
                call_stack: Vec::new(),
                variables: Vec::new(),
                current_line: 0,
            }
        }

        fn set_breakpoint(&mut self, line: usize) {
            if !self.breakpoints.contains(&line) {
                self.breakpoints.push(line);
                self.breakpoints.sort();
            }
        }

        fn run(&mut self) {
            self.state = DebuggerState::Running;
            self.call_stack.push("main()".to_string());

            // Simulate hitting a breakpoint
            if let Some(&bp_line) = self.breakpoints.first() {
                self.state = DebuggerState::Paused { line: bp_line };
                self.current_line = bp_line;
                self.update_variables();
            }
        }

        fn step_over(&mut self) {
            if matches!(self.state, DebuggerState::Paused { .. }) {
                self.state = DebuggerState::SteppingOver;
                self.current_line += 1;

                // Check if we hit another breakpoint
                if self.breakpoints.contains(&self.current_line) {
                    self.state = DebuggerState::Paused {
                        line: self.current_line,
                    };
                } else {
                    self.state = DebuggerState::Running;
                }
                self.update_variables();
            }
        }

        fn step_into(&mut self) {
            if matches!(self.state, DebuggerState::Paused { .. }) {
                self.state = DebuggerState::SteppingInto;
                let line = self.current_line;
                self.call_stack.push(format!("function_at_line_{line}"));
                self.current_line += 1;
                self.state = DebuggerState::Paused {
                    line: self.current_line,
                };
                self.update_variables();
            }
        }

        fn update_variables(&mut self) {
            // Simulate variable changes based on line
            self.variables.clear();
            self.variables
                .push(("x".to_string(), (self.current_line * 10).to_string()));
            self.variables
                .push(("y".to_string(), (self.current_line * 5).to_string()));
            let state = &self.state;
            self.variables
                .push(("state".to_string(), format!("{state:?}")));
        }
    }

    #[test]
    fn test_debugging_session_flow() {
        let mut session = DebugSession::new();

        // Set breakpoints
        session.set_breakpoint(10);
        session.set_breakpoint(20);

        // Start debugging
        session.run();
        assert_eq!(session.state, DebuggerState::Paused { line: 10 });
        assert_eq!(session.current_line, 10);

        // Step over
        session.step_over();
        assert_eq!(session.current_line, 11);

        // Step into
        session.state = DebuggerState::Paused { line: 11 };
        session.step_into();
        assert_eq!(session.call_stack.len(), 2);
        assert!(session.call_stack[1].contains("function_at_line"));
    }

    #[test]
    fn test_debug_view_rendering() {
        let backend = TestBackend::new(80, 25);
        let mut terminal = Terminal::new(backend).unwrap();

        let mut session = DebugSession::new();
        session.set_breakpoint(10);
        session.run();

        terminal
            .draw(|f| {
                let main_chunks = Layout::default()
                    .direction(Direction::Horizontal)
                    .constraints([
                        Constraint::Percentage(60), // Code view
                        Constraint::Percentage(40), // Debug info
                    ])
                    .split(f.area());

                // Code panel with breakpoint indicators
                let code_lines: Vec<ListItem> = (1..=30)
                    .map(|i| {
                        let mut line = format!("{i:3} ");

                        // Add breakpoint indicator
                        if session.breakpoints.contains(&i) {
                            line.push_str("");
                        } else {
                            line.push_str("  ");
                        }

                        // Add current line indicator
                        if i == session.current_line {
                            line.push_str("");
                        } else {
                            line.push_str("  ");
                        }

                        line.push_str(&format!("code_line_{i}"));
                        ListItem::new(line)
                    })
                    .collect();

                let code_view = List::new(code_lines)
                    .block(Block::default().title("Source Code").borders(Borders::ALL));
                f.render_widget(code_view, main_chunks[0]);

                // Debug info panel
                let debug_chunks = Layout::default()
                    .direction(Direction::Vertical)
                    .constraints([
                        Constraint::Percentage(50), // Call stack
                        Constraint::Percentage(50), // Variables
                    ])
                    .split(main_chunks[1]);

                // Call stack
                let stack_items: Vec<ListItem> = session
                    .call_stack
                    .iter()
                    .rev()
                    .enumerate()
                    .map(|(i, func)| ListItem::new(format!("{i}: {func}")))
                    .collect();

                let call_stack = List::new(stack_items)
                    .block(Block::default().title("Call Stack").borders(Borders::ALL));
                f.render_widget(call_stack, debug_chunks[0]);

                // Variables
                let var_items: Vec<ListItem> = session
                    .variables
                    .iter()
                    .map(|(name, value)| ListItem::new(format!("{name} = {value}")))
                    .collect();

                let variables = List::new(var_items)
                    .block(Block::default().title("Variables").borders(Borders::ALL));
                f.render_widget(variables, debug_chunks[1]);
            })
            .unwrap();

        // Verify debug view is rendered
        let buffer = terminal.backend().buffer();
        let mut found_breakpoint = false;
        let mut found_current_line = false;
        let mut found_variable = false;

        for y in 0..25 {
            let mut line = String::new();
            for x in 0..80 {
                let cell = &buffer[(x, y)];
                line.push_str(cell.symbol());
            }

            if line.contains("") {
                found_breakpoint = true;
            }
            if line.contains("") {
                found_current_line = true;
            }
            if line.contains("x = ") {
                found_variable = true;
            }
        }

        assert!(found_breakpoint, "Breakpoint indicator should be visible");
        assert!(
            found_current_line,
            "Current line indicator should be visible"
        );
        assert!(found_variable, "Variables should be visible");
    }
}

/// Complex scenario: Live tracing with filtering
mod live_tracing_tests {
    use super::*;
    use std::time::{Duration, Instant};

    struct TraceFilter {
        function_pattern: Option<String>,
        min_duration: Option<Duration>,
        max_events: usize,
    }

    struct LiveTraceSession {
        events: VecDeque<TraceEvent>,
        filter: TraceFilter,
        is_active: bool,
        start_time: Instant,
    }

    #[derive(Clone)]
    struct TraceEvent {
        _timestamp: Instant,
        function: String,
        _event_type: String,
        duration: Option<Duration>,
        _variables: Vec<(String, String)>,
    }

    impl LiveTraceSession {
        fn new(max_events: usize) -> Self {
            Self {
                events: VecDeque::new(),
                filter: TraceFilter {
                    function_pattern: None,
                    min_duration: None,
                    max_events,
                },
                is_active: false,
                start_time: Instant::now(),
            }
        }

        fn start(&mut self) {
            self.is_active = true;
            self.start_time = Instant::now();
            self.events.clear();
        }

        fn add_event(&mut self, event: TraceEvent) {
            if !self.is_active {
                return;
            }

            // Apply filters
            if let Some(ref pattern) = self.filter.function_pattern {
                if !event.function.contains(pattern) {
                    return;
                }
            }

            if let Some(min_dur) = self.filter.min_duration {
                if let Some(dur) = event.duration {
                    if dur < min_dur {
                        return;
                    }
                }
            }

            self.events.push_back(event);

            // Limit buffer size
            while self.events.len() > self.filter.max_events {
                self.events.pop_front();
            }
        }

        fn get_filtered_events(&self) -> Vec<&TraceEvent> {
            self.events.iter().collect()
        }

        fn set_function_filter(&mut self, pattern: String) {
            self.filter.function_pattern = Some(pattern);
        }

        fn _clear_filters(&mut self) {
            self.filter.function_pattern = None;
            self.filter.min_duration = None;
        }
    }

    #[test]
    fn test_live_trace_filtering() {
        let mut session = LiveTraceSession::new(100);
        session.start();

        // Add various events
        session.add_event(TraceEvent {
            _timestamp: Instant::now(),
            function: "main".to_string(),
            _event_type: "entry".to_string(),
            duration: None,
            _variables: vec![],
        });

        session.add_event(TraceEvent {
            _timestamp: Instant::now(),
            function: "process_data".to_string(),
            _event_type: "entry".to_string(),
            duration: Some(Duration::from_millis(50)),
            _variables: vec![("x".to_string(), "42".to_string())],
        });

        session.add_event(TraceEvent {
            _timestamp: Instant::now(),
            function: "helper".to_string(),
            _event_type: "exit".to_string(),
            duration: Some(Duration::from_millis(5)),
            _variables: vec![],
        });

        // Apply function filter
        session.set_function_filter("process".to_string());
        let _filtered = session.get_filtered_events();

        // Note: Filter only applies to new events after it's set
        // Add new event after filter
        session.add_event(TraceEvent {
            _timestamp: Instant::now(),
            function: "process_result".to_string(),
            _event_type: "entry".to_string(),
            duration: None,
            _variables: vec![],
        });

        assert_eq!(session.events.len(), 4); // All events still in buffer
    }

    #[test]
    fn test_trace_buffer_overflow() {
        let mut session = LiveTraceSession::new(5); // Small buffer
        session.start();

        // Add more events than buffer can hold
        for i in 0..10 {
            session.add_event(TraceEvent {
                _timestamp: Instant::now(),
                function: format!("func_{i}"),
                _event_type: "entry".to_string(),
                duration: None,
                _variables: vec![],
            });
        }

        assert_eq!(session.events.len(), 5);
        assert_eq!(session.events.front().unwrap().function, "func_5");
        assert_eq!(session.events.back().unwrap().function, "func_9");
    }
}