mockforge-tui 0.3.134

Terminal UI dashboard for MockForge
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//! Chaos control panel — toggle chaos engineering, select presets, view settings.

use std::time::Instant;

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    layout::{Constraint, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph},
    Frame,
};
use tokio::sync::mpsc;

use crate::api::client::MockForgeClient;
use crate::event::Event;
use crate::screens::Screen;
use crate::theme::Theme;
use crate::widgets::confirm::ConfirmDialog;

const FETCH_INTERVAL: u64 = 5;

const PRESETS: &[(&str, &str)] = &[
    ("network_degradation", "Simulate degraded network conditions"),
    ("service_instability", "Intermittent service failures"),
    ("cascading_failure", "Chain reaction of failures"),
    ("peak_traffic", "High traffic load simulation"),
    ("slow_backend", "Backend latency injection"),
];

enum PendingAction {
    Toggle,
    StartScenario(String),
    StopScenario(String),
}

pub struct ChaosScreen {
    data: Option<serde_json::Value>,
    /// Chaos fault counter snapshot (issue-#79 follow-up). Fetched alongside
    /// status; renders into a third panel beneath Settings.
    stats: Option<serde_json::Value>,
    error: Option<String>,
    last_fetch: Option<Instant>,
    pending_action: Option<PendingAction>,
    confirm: ConfirmDialog,
    /// Whether the preset picker overlay is showing.
    preset_picker: bool,
    selected_preset: usize,
    /// Whether the settings detail pane is focused.
    detail_focus: bool,
    detail_scroll: usize,
}

impl ChaosScreen {
    pub fn new() -> Self {
        Self {
            data: None,
            stats: None,
            error: None,
            last_fetch: None,
            pending_action: None,
            confirm: ConfirmDialog::new(),
            preset_picker: false,
            selected_preset: 0,
            detail_focus: false,
            detail_scroll: 0,
        }
    }

    fn is_enabled(&self) -> bool {
        self.data
            .as_ref()
            .and_then(|d| d.get("enabled"))
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
    }

    fn active_scenario(&self) -> Option<&str> {
        self.data
            .as_ref()
            .and_then(|d| d.get("active_scenario"))
            .and_then(|v| v.as_str())
            .filter(|s| !s.is_empty() && *s != "none")
    }

    fn active_scenarios(&self) -> Vec<String> {
        self.data
            .as_ref()
            .and_then(|d| d.get("active_scenarios"))
            .and_then(|v| v.as_array())
            .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
            .unwrap_or_default()
    }
}

impl Screen for ChaosScreen {
    fn title(&self) -> &str {
        "Chaos"
    }

    fn handle_key(&mut self, key: KeyEvent) -> bool {
        // Confirm dialog takes priority when visible.
        if self.confirm.visible {
            if let Some(confirmed) = self.confirm.handle_key(key) {
                if confirmed {
                    if let Some(action) = self.pending_action.take() {
                        self.pending_action = Some(action);
                    }
                } else {
                    self.pending_action = None;
                }
                return true;
            }
            return true;
        }

        // Preset picker overlay.
        if self.preset_picker {
            match key.code {
                KeyCode::Char('j') | KeyCode::Down => {
                    if self.selected_preset + 1 < PRESETS.len() {
                        self.selected_preset += 1;
                    }
                    return true;
                }
                KeyCode::Char('k') | KeyCode::Up => {
                    self.selected_preset = self.selected_preset.saturating_sub(1);
                    return true;
                }
                KeyCode::Enter => {
                    let (name, _) = PRESETS[self.selected_preset];
                    let active = self.active_scenarios();
                    if active.contains(&name.to_string()) {
                        // Already running — stop it.
                        self.pending_action = Some(PendingAction::StopScenario(name.to_string()));
                        self.confirm.show("Stop Scenario", format!("Stop scenario '{name}'?"));
                    } else {
                        self.pending_action = Some(PendingAction::StartScenario(name.to_string()));
                        self.confirm.show("Start Scenario", format!("Start scenario '{name}'?"));
                    }
                    self.preset_picker = false;
                    return true;
                }
                KeyCode::Esc | KeyCode::Char('p') => {
                    self.preset_picker = false;
                    return true;
                }
                _ => return true,
            }
        }

        // Detail focus for scrolling settings.
        if self.detail_focus {
            match key.code {
                KeyCode::Char('j') | KeyCode::Down => {
                    self.detail_scroll += 1;
                    return true;
                }
                KeyCode::Char('k') | KeyCode::Up => {
                    self.detail_scroll = self.detail_scroll.saturating_sub(1);
                    return true;
                }
                KeyCode::Esc | KeyCode::Char('e') => {
                    self.detail_focus = false;
                    return true;
                }
                _ => {}
            }
        }

        match key.code {
            KeyCode::Char('t') => {
                let action = if self.is_enabled() {
                    "disable"
                } else {
                    "enable"
                };
                self.pending_action = Some(PendingAction::Toggle);
                self.confirm
                    .show("Toggle Chaos", format!("Are you sure you want to {action} chaos?"));
                true
            }
            KeyCode::Char('p') => {
                self.preset_picker = true;
                self.selected_preset = 0;
                true
            }
            KeyCode::Char('e') => {
                self.detail_focus = true;
                self.detail_scroll = 0;
                true
            }
            KeyCode::Char('r') => {
                self.last_fetch = None;
                true
            }
            _ => false,
        }
    }

    fn render(&self, frame: &mut Frame, area: Rect) {
        let Some(ref data) = self.data else {
            let loading = Paragraph::new("Loading chaos status...").style(Theme::dim()).block(
                Block::default()
                    .title(" Chaos ")
                    .borders(Borders::ALL)
                    .border_style(Theme::dim()),
            );
            frame.render_widget(loading, area);
            return;
        };

        let cols = Layout::horizontal([Constraint::Percentage(40), Constraint::Percentage(60)])
            .split(area);

        // Right column: Settings on top, Stats below (if available).
        let right_rows = if self.stats.is_some() {
            Layout::vertical([Constraint::Percentage(60), Constraint::Percentage(40)])
                .split(cols[1])
        } else {
            // No stats yet — settings takes the whole right column.
            Layout::vertical([Constraint::Percentage(100)]).split(cols[1])
        };

        self.render_status(frame, cols[0], data);
        self.render_settings(frame, right_rows[0], data);
        if let Some(ref stats) = self.stats {
            self.render_stats(frame, right_rows[1], stats);
        }

        // Preset picker overlay.
        if self.preset_picker {
            self.render_preset_picker(frame, area);
        }

        self.confirm.render(frame);
    }

    fn tick(&mut self, client: &MockForgeClient, tx: &mpsc::UnboundedSender<Event>) {
        // Handle pending action.
        if let Some(action) = self.pending_action.take() {
            let client = client.clone();
            let tx = tx.clone();
            tokio::spawn(async move {
                let result = match action {
                    PendingAction::Toggle => {
                        // Check current state and toggle.
                        let enabled = client
                            .get_chaos_status()
                            .await
                            .ok()
                            .and_then(|d| d.get("enabled").and_then(|v| v.as_bool()))
                            .unwrap_or(false);
                        client.toggle_chaos(!enabled).await.map(|_| ())
                    }
                    PendingAction::StartScenario(name) => {
                        client.start_chaos_scenario(&name).await.map(|_| ())
                    }
                    PendingAction::StopScenario(name) => {
                        client.stop_chaos_scenario(&name).await.map(|_| ())
                    }
                };
                match result {
                    Ok(()) => {
                        // Refetch status.
                        match client.get_chaos_status().await {
                            Ok(data) => {
                                let json = serde_json::to_string(&data).unwrap_or_default();
                                let _ = tx.send(Event::Data {
                                    screen: "chaos",
                                    payload: json,
                                });
                            }
                            Err(e) => {
                                let _ = tx.send(Event::ApiError {
                                    screen: "chaos",
                                    message: e.to_string(),
                                });
                            }
                        }
                    }
                    Err(e) => {
                        let _ = tx.send(Event::ApiError {
                            screen: "chaos",
                            message: format!("Action failed: {e}"),
                        });
                    }
                }
            });
            return;
        }

        let should_fetch =
            self.last_fetch.map_or(true, |t| t.elapsed().as_secs() >= FETCH_INTERVAL);
        if !should_fetch {
            return;
        }
        self.last_fetch = Some(Instant::now());

        let client = client.clone();
        let tx = tx.clone();
        tokio::spawn(async move {
            // Status is required; stats are best-effort. If stats fail (e.g. older
            // server without /__mockforge/chaos/stats), we still render the
            // existing Status + Settings panels with stats.is_none().
            let status = match client.get_chaos_status().await {
                Ok(data) => data,
                Err(e) => {
                    let _ = tx.send(Event::ApiError {
                        screen: "chaos",
                        message: e.to_string(),
                    });
                    return;
                }
            };
            let stats = client.get_chaos_stats().await.ok();
            let payload = serde_json::json!({
                "status": status,
                "stats": stats,
            });
            let _ = tx.send(Event::Data {
                screen: "chaos",
                payload: serde_json::to_string(&payload).unwrap_or_default(),
            });
        });
    }

    fn on_data(&mut self, payload: &str) {
        match serde_json::from_str::<serde_json::Value>(payload) {
            Ok(combined) => {
                // tick() now packs {status, stats}; back-compat: if the payload
                // looks like the old shape (no "status" key), treat the whole
                // thing as status data and leave stats unchanged.
                if let Some(status) = combined.get("status").cloned() {
                    self.data = Some(status);
                    if let Some(stats) = combined.get("stats").cloned() {
                        // The endpoint envelope is {success, data, error, ...};
                        // unwrap to the inner snapshot for rendering.
                        let snapshot = stats.get("data").cloned().unwrap_or(stats);
                        self.stats = if snapshot.is_null() {
                            None
                        } else {
                            Some(snapshot)
                        };
                    }
                } else {
                    self.data = Some(combined);
                }
                self.error = None;
            }
            Err(e) => {
                self.error = Some(format!("Parse error: {e}"));
            }
        }
    }

    fn on_error(&mut self, message: &str) {
        self.error = Some(message.to_string());
    }

    fn error(&self) -> Option<&str> {
        self.error.as_deref()
    }

    fn force_refresh(&mut self) {
        self.last_fetch = None;
    }

    fn status_hint(&self) -> &str {
        if self.preset_picker {
            "Enter:select  j/k:navigate  Esc:close"
        } else if self.detail_focus {
            "j/k:scroll  Esc:back"
        } else {
            "t:toggle  p:presets  e:details  r:refresh"
        }
    }
}

impl ChaosScreen {
    fn render_status(&self, frame: &mut Frame, area: Rect, _data: &serde_json::Value) {
        let block = Block::default()
            .title(" Chaos Status ")
            .title_style(Theme::title())
            .borders(Borders::ALL)
            .border_style(Theme::dim())
            .style(Theme::surface());

        let enabled = self.is_enabled();
        let status_icon = if enabled { "ON " } else { "OFF" };
        let status_color = if enabled {
            Theme::STATUS_UP
        } else {
            Theme::STATUS_DOWN
        };

        let scenario = self.active_scenario().unwrap_or("none");
        let active = self.active_scenarios();

        let mut lines = vec![
            Line::from(vec![
                Span::styled("  Status:      ", Theme::dim()),
                Span::styled(
                    status_icon,
                    Style::default().fg(status_color).add_modifier(Modifier::BOLD),
                ),
            ]),
            Line::from(vec![
                Span::styled("  Scenario:    ", Theme::dim()),
                Span::styled(scenario.to_string(), Style::default().fg(Theme::FG)),
            ]),
            Line::from(""),
        ];

        // Show active scenarios.
        if !active.is_empty() {
            lines.push(Line::from(Span::styled("  Active Scenarios:", Theme::dim())));
            for name in &active {
                lines.push(Line::from(vec![
                    Span::styled("", Style::default().fg(Theme::STATUS_UP)),
                    Span::styled(name.clone(), Style::default().fg(Theme::FG)),
                ]));
            }
            lines.push(Line::from(""));
        }

        // Preset quick list.
        lines.push(Line::from(Span::styled("  Available Presets:", Theme::dim())));
        for (name, desc) in PRESETS {
            let is_active = active.iter().any(|a| a == name);
            let icon = if is_active { "" } else { "" };
            let color = if is_active {
                Theme::STATUS_UP
            } else {
                Theme::FG
            };
            lines.push(Line::from(vec![
                Span::styled(format!("    {icon} "), Style::default().fg(color)),
                Span::styled(format!("{name:<25}"), Style::default().fg(color)),
                Span::styled((*desc).to_string(), Theme::dim()),
            ]));
        }

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(paragraph, area);
    }

    fn render_settings(&self, frame: &mut Frame, area: Rect, data: &serde_json::Value) {
        let focus_indicator = if self.detail_focus { " [FOCUS]" } else { "" };
        let border_style = if self.detail_focus {
            Style::default().fg(Theme::BLUE)
        } else {
            Theme::dim()
        };

        let block = Block::default()
            .title(format!(" Settings{focus_indicator} "))
            .title_style(Theme::title())
            .borders(Borders::ALL)
            .border_style(border_style)
            .style(Theme::surface());

        let mut lines = Vec::new();

        if let Some(settings) = data.get("settings").and_then(|v| v.as_object()) {
            for (key, value) in settings {
                lines.push(Line::from(vec![
                    Span::styled(format!("  {key:<24}"), Theme::dim()),
                    Span::styled(format!("{value}"), Style::default().fg(Theme::FG)),
                ]));
            }
        }

        // Also show config fields if present.
        for section in [
            "latency",
            "fault_injection",
            "rate_limit",
            "traffic_shaping",
        ] {
            if let Some(config) = data.get(section).and_then(|v| v.as_object()) {
                lines.push(Line::from(""));
                lines.push(Line::from(Span::styled(
                    format!("  {section}:"),
                    Style::default().fg(Theme::BLUE).add_modifier(Modifier::BOLD),
                )));
                for (key, value) in config {
                    lines.push(Line::from(vec![
                        Span::styled(format!("    {key:<22}"), Theme::dim()),
                        Span::styled(format!("{value}"), Style::default().fg(Theme::FG)),
                    ]));
                }
            }
        }

        if lines.is_empty() {
            lines.push(Line::from(Span::styled("  No settings data available", Theme::dim())));
        }

        // Apply scroll offset.
        let visible_lines: Vec<Line> = lines.into_iter().skip(self.detail_scroll).collect();

        let paragraph = Paragraph::new(visible_lines).block(block);
        frame.render_widget(paragraph, area);
    }

    /// Issue-#79 follow-up: show per-fault-type counts from the chaos
    /// prometheus counters. Sourced from `/__mockforge/chaos/stats` →
    /// `mockforge_chaos::metrics::CHAOS_METRICS::snapshot()`.
    fn render_stats(&self, frame: &mut Frame, area: Rect, stats: &serde_json::Value) {
        let block = Block::default()
            .title(" Fault Stats ")
            .title_style(Theme::title())
            .borders(Borders::ALL)
            .border_style(Theme::dim())
            .style(Theme::surface());

        let mut lines: Vec<Line> = Vec::new();

        let grand_total = stats.get("faults_grand_total").and_then(|v| v.as_u64()).unwrap_or(0);
        lines.push(Line::from(vec![
            Span::styled("  Total faults injected: ", Theme::dim()),
            Span::styled(
                grand_total.to_string(),
                Style::default().fg(Theme::FG).add_modifier(Modifier::BOLD),
            ),
        ]));

        // Per-fault-type totals. Sort descending by count so the noisiest
        // fault floats to the top.
        let by_type = stats.get("faults_total_by_type").and_then(|v| v.as_object());
        if let Some(map) = by_type {
            let mut entries: Vec<(&String, u64)> =
                map.iter().filter_map(|(k, v)| v.as_u64().map(|n| (k, n))).collect();
            entries.sort_by(|a, b| b.1.cmp(&a.1));
            if entries.is_empty() {
                lines.push(Line::from(""));
                lines.push(Line::from(Span::styled("  No faults injected yet.", Theme::dim())));
            } else {
                lines.push(Line::from(""));
                lines.push(Line::from(Span::styled(
                    "  Per fault_type:",
                    Style::default().fg(Theme::BLUE).add_modifier(Modifier::BOLD),
                )));
                for (fault_type, count) in entries {
                    lines.push(Line::from(vec![
                        Span::styled(format!("    {fault_type:<22}"), Theme::dim()),
                        Span::styled(count.to_string(), Style::default().fg(Theme::FG)),
                    ]));
                }
            }
        }

        // Rate-limit violations summary.
        let rl_total =
            stats.get("rate_limit_violations_total").and_then(|v| v.as_u64()).unwrap_or(0);
        if rl_total > 0 {
            lines.push(Line::from(""));
            lines.push(Line::from(vec![
                Span::styled("  Rate-limit violations:  ", Theme::dim()),
                Span::styled(rl_total.to_string(), Style::default().fg(Theme::FG)),
            ]));
        }

        // Latency injection sample count + mean injected latency.
        if let Some(samples) = stats.get("latency_samples_by_endpoint").and_then(|v| v.as_object())
        {
            let total: u64 = samples.values().filter_map(|v| v.as_u64()).sum();
            if total > 0 {
                lines.push(Line::from(vec![
                    Span::styled("  Latency injections:     ", Theme::dim()),
                    Span::styled(total.to_string(), Style::default().fg(Theme::FG)),
                ]));
                let avg_mean = stats
                    .get("latency_avg_ms_by_endpoint")
                    .and_then(|v| v.as_object())
                    .map(|m| {
                        let vals: Vec<f64> = m.values().filter_map(|v| v.as_f64()).collect();
                        if vals.is_empty() {
                            0.0
                        } else {
                            vals.iter().sum::<f64>() / vals.len() as f64
                        }
                    })
                    .unwrap_or(0.0);
                if avg_mean > 0.0 {
                    lines.push(Line::from(vec![
                        Span::styled("    mean latency:         ", Theme::dim()),
                        Span::styled(format!("{avg_mean:.1}ms"), Style::default().fg(Theme::FG)),
                    ]));
                }
            }
        }

        // Jitter sample count + mean jitter offset.
        if let Some(samples) = stats.get("jitter_samples_by_endpoint").and_then(|v| v.as_object()) {
            let total: u64 = samples.values().filter_map(|v| v.as_u64()).sum();
            if total > 0 {
                lines.push(Line::from(vec![
                    Span::styled("  Jitter applications:    ", Theme::dim()),
                    Span::styled(total.to_string(), Style::default().fg(Theme::FG)),
                ]));
                let avg_mean = stats
                    .get("jitter_avg_ms_by_endpoint")
                    .and_then(|v| v.as_object())
                    .map(|m| {
                        let vals: Vec<f64> = m.values().filter_map(|v| v.as_f64()).collect();
                        if vals.is_empty() {
                            0.0
                        } else {
                            vals.iter().sum::<f64>() / vals.len() as f64
                        }
                    })
                    .unwrap_or(0.0);
                if avg_mean > 0.0 {
                    lines.push(Line::from(vec![
                        Span::styled("    mean offset:          ", Theme::dim()),
                        Span::styled(format!("{avg_mean:.1}ms"), Style::default().fg(Theme::FG)),
                    ]));
                }
            }
        }

        // Bandwidth throttle activity. Show per-direction sample counts and
        // the total artificial delay imposed so users can tell whether their
        // `bandwidth_limit_bps` config is actually biting.
        if let Some(samples) =
            stats.get("bandwidth_throttle_samples_by_direction").and_then(|v| v.as_object())
        {
            let total: u64 = samples.values().filter_map(|v| v.as_u64()).sum();
            if total > 0 {
                lines.push(Line::from(vec![
                    Span::styled("  Bandwidth throttles:    ", Theme::dim()),
                    Span::styled(total.to_string(), Style::default().fg(Theme::FG)),
                ]));
                for (dir, v) in samples {
                    if let Some(c) = v.as_u64() {
                        if c > 0 {
                            lines.push(Line::from(vec![
                                Span::styled(format!("    {dir:<20}"), Theme::dim()),
                                Span::styled(c.to_string(), Style::default().fg(Theme::FG)),
                            ]));
                        }
                    }
                }
                let total_ms =
                    stats.get("bandwidth_throttle_total_ms").and_then(|v| v.as_u64()).unwrap_or(0);
                if total_ms > 0 {
                    lines.push(Line::from(vec![
                        Span::styled("    total delay:          ", Theme::dim()),
                        Span::styled(format!("{total_ms}ms"), Style::default().fg(Theme::FG)),
                    ]));
                }
            }
        }

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(paragraph, area);
    }

    fn render_preset_picker(&self, frame: &mut Frame, area: Rect) {
        let width = 60u16.min(area.width.saturating_sub(4));
        let height = (u16::try_from(PRESETS.len()).unwrap_or(u16::MAX).saturating_add(2))
            .min(area.height.saturating_sub(4));
        let x = area.x + (area.width.saturating_sub(width)) / 2;
        let y = area.y + (area.height.saturating_sub(height)) / 2;
        let popup_area = Rect::new(x, y, width, height);

        let active = self.active_scenarios();

        let block = Block::default()
            .title(" Select Preset ")
            .title_style(Style::default().fg(Theme::BLUE).add_modifier(Modifier::BOLD))
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Theme::BLUE))
            .style(Theme::surface());

        let lines: Vec<Line> = PRESETS
            .iter()
            .enumerate()
            .map(|(i, (name, desc))| {
                let is_active = active.iter().any(|a| a == name);
                let selected = i == self.selected_preset;

                let icon = if is_active { "" } else { "" };
                let selector = if selected { "" } else { "  " };

                let style = if selected {
                    Style::default().fg(Theme::BG).bg(Theme::BLUE).add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(Theme::FG)
                };

                Line::from(Span::styled(format!("{selector}{icon} {name:<25} {desc}"), style))
            })
            .collect();

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(paragraph, popup_area);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent {
            code,
            modifiers: KeyModifiers::NONE,
            kind: KeyEventKind::Press,
            state: KeyEventState::NONE,
        }
    }

    fn sample_chaos_data() -> serde_json::Value {
        serde_json::json!({
            "enabled": true,
            "active_scenario": "network_degradation",
            "active_scenarios": ["network_degradation"],
            "settings": {
                "latency_ms": 200,
                "failure_rate": 0.1,
            }
        })
    }

    #[test]
    fn new_starts_clean() {
        let s = ChaosScreen::new();
        assert!(s.data.is_none());
        assert!(!s.preset_picker);
        assert!(!s.detail_focus);
    }

    #[test]
    fn toggle_shows_confirm() {
        let mut s = ChaosScreen::new();
        s.data = Some(sample_chaos_data());

        s.handle_key(key(KeyCode::Char('t')));
        assert!(s.confirm.visible);
        assert!(s.pending_action.is_some());
    }

    #[test]
    fn preset_picker_opens_and_closes() {
        let mut s = ChaosScreen::new();
        s.data = Some(sample_chaos_data());

        s.handle_key(key(KeyCode::Char('p')));
        assert!(s.preset_picker);
        assert_eq!(s.selected_preset, 0);

        s.handle_key(key(KeyCode::Esc));
        assert!(!s.preset_picker);
    }

    #[test]
    fn preset_picker_navigation() {
        let mut s = ChaosScreen::new();
        s.data = Some(sample_chaos_data());

        s.handle_key(key(KeyCode::Char('p')));

        s.handle_key(key(KeyCode::Char('j')));
        assert_eq!(s.selected_preset, 1);

        s.handle_key(key(KeyCode::Char('j')));
        assert_eq!(s.selected_preset, 2);

        s.handle_key(key(KeyCode::Char('k')));
        assert_eq!(s.selected_preset, 1);

        // Can't go below 0
        s.handle_key(key(KeyCode::Char('k')));
        s.handle_key(key(KeyCode::Char('k')));
        assert_eq!(s.selected_preset, 0);
    }

    #[test]
    fn preset_picker_enter_shows_confirm() {
        let mut s = ChaosScreen::new();
        s.data = Some(serde_json::json!({
            "enabled": true,
            "active_scenario": "none",
            "active_scenarios": [],
        }));

        s.handle_key(key(KeyCode::Char('p')));
        s.handle_key(key(KeyCode::Enter)); // select first preset

        assert!(!s.preset_picker);
        assert!(s.confirm.visible);
    }

    #[test]
    fn detail_focus() {
        let mut s = ChaosScreen::new();
        s.data = Some(sample_chaos_data());

        s.handle_key(key(KeyCode::Char('e')));
        assert!(s.detail_focus);

        s.handle_key(key(KeyCode::Char('j')));
        assert_eq!(s.detail_scroll, 1);

        s.handle_key(key(KeyCode::Esc));
        assert!(!s.detail_focus);
    }

    #[test]
    fn is_enabled_reads_data() {
        let mut s = ChaosScreen::new();
        assert!(!s.is_enabled());

        s.data = Some(serde_json::json!({ "enabled": true }));
        assert!(s.is_enabled());

        s.data = Some(serde_json::json!({ "enabled": false }));
        assert!(!s.is_enabled());
    }

    #[test]
    fn active_scenarios_reads_data() {
        let mut s = ChaosScreen::new();
        assert!(s.active_scenarios().is_empty());

        s.data = Some(serde_json::json!({
            "active_scenarios": ["network_degradation", "slow_backend"]
        }));
        assert_eq!(s.active_scenarios().len(), 2);
    }

    #[test]
    fn status_hints_change_with_mode() {
        let mut s = ChaosScreen::new();
        assert!(s.status_hint().contains("t:toggle"));

        s.preset_picker = true;
        assert!(s.status_hint().contains("Enter:select"));

        s.preset_picker = false;
        s.detail_focus = true;
        assert!(s.status_hint().contains("j/k:scroll"));
    }

    #[test]
    fn preset_picker_select_active_shows_stop() {
        let mut s = ChaosScreen::new();
        s.data = Some(serde_json::json!({
            "enabled": true,
            "active_scenario": "network_degradation",
            "active_scenarios": ["network_degradation"],
        }));

        s.handle_key(key(KeyCode::Char('p')));
        // First preset is "network_degradation" which is active.
        s.handle_key(key(KeyCode::Enter));

        // Should show "Stop" confirmation since it's already running.
        assert!(s.confirm.visible);
    }
}