agent-core-tui 0.6.0

TUI frontend for agent-core - ratatui-based terminal interface
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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
//! Permission panel widget for AskForPermissions tool
//!
//! A reusable Ratatui panel that displays permission requests from the LLM
//! and collects user responses (Grant Once / Grant Session / Deny).
//!
//! # Navigation
//! - Up/Down: Move between options
//! - Enter/Space: Select option
//! - Esc: Cancel (deny)

use crate::controller::{PermissionPanelResponse, TurnId};
use crate::permissions::{Grant, GrantTarget, PermissionLevel, PermissionRequest};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
    layout::Rect,
    style::Modifier,
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph},
    Frame,
};

use crate::themes::Theme;

/// Default configuration values for PermissionPanel
pub mod defaults {
    /// Maximum percentage of screen height the panel can use
    pub const MAX_PANEL_PERCENT: u16 = 50;
    /// Selection indicator for focused items
    pub const SELECTION_INDICATOR: &str = " \u{203A} ";
    /// Blank space for non-focused items (same width as indicator)
    pub const NO_INDICATOR: &str = "   ";
    /// Panel title
    pub const TITLE: &str = " Permission Request ";
    /// Help text
    pub const HELP_TEXT: &str =
        " Up/Down: Navigate \u{00B7} Enter/Space: Select \u{00B7} Esc: Cancel";
    /// Target type icons
    pub const ICON_PATH: &str = "\u{1F4C4}"; // Document (for paths)
    pub const ICON_DOMAIN: &str = "\u{2194}"; // Network arrow (for domains)
    pub const ICON_COMMAND: &str = "\u{2295}"; // Command/terminal (for commands)
    pub const ICON_OTHER: &str = "\u{25CB}"; // Default fallback
}

/// Configuration for PermissionPanel widget
#[derive(Clone)]
pub struct PermissionPanelConfig {
    /// Maximum percentage of screen height the panel can use
    pub max_panel_percent: u16,
    /// Selection indicator for focused items
    pub selection_indicator: String,
    /// Blank space for non-focused items
    pub no_indicator: String,
    /// Panel title
    pub title: String,
    /// Help text
    pub help_text: String,
    /// Icon for path targets
    pub icon_path: String,
    /// Icon for domain targets
    pub icon_domain: String,
    /// Icon for command targets
    pub icon_command: String,
    /// Icon for other/unknown targets
    pub icon_other: String,
}

impl Default for PermissionPanelConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl PermissionPanelConfig {
    /// Create a new PermissionPanelConfig with default values
    pub fn new() -> Self {
        Self {
            max_panel_percent: defaults::MAX_PANEL_PERCENT,
            selection_indicator: defaults::SELECTION_INDICATOR.to_string(),
            no_indicator: defaults::NO_INDICATOR.to_string(),
            title: defaults::TITLE.to_string(),
            help_text: defaults::HELP_TEXT.to_string(),
            icon_path: defaults::ICON_PATH.to_string(),
            icon_domain: defaults::ICON_DOMAIN.to_string(),
            icon_command: defaults::ICON_COMMAND.to_string(),
            icon_other: defaults::ICON_OTHER.to_string(),
        }
    }

    /// Set the maximum panel height percentage
    pub fn with_max_panel_percent(mut self, percent: u16) -> Self {
        self.max_panel_percent = percent;
        self
    }

    /// Set the selection indicator
    pub fn with_selection_indicator(mut self, indicator: impl Into<String>) -> Self {
        self.selection_indicator = indicator.into();
        self
    }

    /// Set the panel title
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Set the help text
    pub fn with_help_text(mut self, text: impl Into<String>) -> Self {
        self.help_text = text.into();
        self
    }

    /// Set target type icons
    pub fn with_target_icons(
        mut self,
        path: impl Into<String>,
        domain: impl Into<String>,
        command: impl Into<String>,
        other: impl Into<String>,
    ) -> Self {
        self.icon_path = path.into();
        self.icon_domain = domain.into();
        self.icon_command = command.into();
        self.icon_other = other.into();
        self
    }
}

/// Options available for the user to select
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PermissionOption {
    /// Grant permission for this request only (no persistent grant)
    GrantOnce,
    /// Grant permission for the remainder of the session (this specific resource)
    GrantSession,
    /// Grant permission for ALL operations of this type for the session
    GrantAllSession,
    /// Deny the permission request
    Deny,
}

impl PermissionOption {
    /// Get all options in display order
    pub fn all() -> &'static [PermissionOption] {
        &[
            PermissionOption::GrantOnce,
            PermissionOption::GrantSession,
            PermissionOption::GrantAllSession,
            PermissionOption::Deny,
        ]
    }

    /// Get the display label for this option
    pub fn label(&self) -> &'static str {
        match self {
            PermissionOption::GrantOnce => "Grant Once",
            PermissionOption::GrantSession => "Grant for Session",
            PermissionOption::GrantAllSession => "Allow All Similar",
            PermissionOption::Deny => "Deny",
        }
    }

    /// Get the description for this option based on request context
    pub fn description(&self, request: &PermissionRequest) -> String {
        match self {
            PermissionOption::GrantOnce => "Allow only this request".to_string(),
            PermissionOption::GrantSession => match (&request.target, request.required_level) {
                (GrantTarget::Path { .. }, PermissionLevel::Read) => {
                    "Allow reading this file".to_string()
                }
                (GrantTarget::Path { .. }, PermissionLevel::Write) => {
                    "Allow writing this file".to_string()
                }
                (GrantTarget::Command { .. }, _) => "Allow this command".to_string(),
                (GrantTarget::Domain { .. }, _) => "Allow this domain".to_string(),
                _ => "Allow for the session".to_string(),
            },
            PermissionOption::GrantAllSession => match (&request.target, request.required_level) {
                (GrantTarget::Path { .. }, PermissionLevel::Read) => {
                    "Allow reading any file".to_string()
                }
                (GrantTarget::Path { .. }, PermissionLevel::Write) => {
                    "Allow writing any file".to_string()
                }
                (GrantTarget::Command { .. }, _) => "Allow all commands".to_string(),
                (GrantTarget::Domain { .. }, _) => "Allow all domains".to_string(),
                _ => "Allow all similar actions".to_string(),
            },
            PermissionOption::Deny => "Deny this request".to_string(),
        }
    }

    /// Returns true if this is a positive (grant) option
    pub fn is_positive(&self) -> bool {
        !matches!(self, PermissionOption::Deny)
    }

    /// Convert to a response with optional grant based on the request
    pub fn to_response(&self, request: &PermissionRequest) -> PermissionPanelResponse {
        match self {
            PermissionOption::GrantOnce => PermissionPanelResponse {
                granted: true,
                grant: None, // No persistent grant
                message: None,
            },
            PermissionOption::GrantSession => {
                // Create a grant matching the exact request
                let grant = Grant::new(request.target.clone(), request.required_level);
                PermissionPanelResponse {
                    granted: true,
                    grant: Some(grant),
                    message: None,
                }
            }
            PermissionOption::GrantAllSession => {
                // Create a broader grant based on target type, using the requested level
                let grant = match &request.target {
                    GrantTarget::Path { .. } => {
                        Grant::new(GrantTarget::path("/", true), request.required_level)
                    }
                    GrantTarget::Domain { .. } => Grant::domain("*", request.required_level),
                    GrantTarget::Command { .. } => Grant::command("*", request.required_level),
                };
                PermissionPanelResponse {
                    granted: true,
                    grant: Some(grant),
                    message: None,
                }
            }
            PermissionOption::Deny => PermissionPanelResponse {
                granted: false,
                grant: None,
                message: None,
            },
        }
    }
}

/// Result of handling a key event
#[derive(Debug, Clone)]
pub enum KeyAction {
    /// No action taken
    None,
    /// User selected an option (includes tool_use_id and response)
    Selected(String, PermissionPanelResponse),
    /// User cancelled (pressed Escape)
    Cancelled(String),
}

/// State for the permission panel
pub struct PermissionPanel {
    /// Whether the panel is active/visible
    active: bool,
    /// Tool use ID for this permission request
    tool_use_id: String,
    /// Session ID
    session_id: i64,
    /// The permission request to display
    request: PermissionRequest,
    /// Turn ID for context
    turn_id: Option<TurnId>,
    /// Currently selected option index
    selected_idx: usize,
    /// Configuration for display customization
    config: PermissionPanelConfig,
}

impl PermissionPanel {
    /// Create a new inactive permission panel
    pub fn new() -> Self {
        Self::with_config(PermissionPanelConfig::new())
    }

    /// Create a new inactive permission panel with custom configuration
    pub fn with_config(config: PermissionPanelConfig) -> Self {
        Self {
            active: false,
            tool_use_id: String::new(),
            session_id: 0,
            request: PermissionRequest::new(
                "",
                GrantTarget::path("/", false),
                PermissionLevel::None,
                "",
            ),
            turn_id: None,
            selected_idx: 0,
            config,
        }
    }

    /// Get the current configuration
    pub fn config(&self) -> &PermissionPanelConfig {
        &self.config
    }

    /// Set a new configuration
    pub fn set_config(&mut self, config: PermissionPanelConfig) {
        self.config = config;
    }

    /// Activate the panel with a permission request
    pub fn activate(
        &mut self,
        tool_use_id: String,
        session_id: i64,
        request: PermissionRequest,
        turn_id: Option<TurnId>,
    ) {
        self.active = true;
        self.tool_use_id = tool_use_id;
        self.session_id = session_id;
        self.request = request;
        self.turn_id = turn_id;
        self.selected_idx = 0; // Default to first option (Grant Once)
    }

    /// Deactivate the panel
    pub fn deactivate(&mut self) {
        self.active = false;
        self.tool_use_id.clear();
        self.request = PermissionRequest::new(
            "",
            GrantTarget::path("/", false),
            PermissionLevel::None,
            "",
        );
        self.turn_id = None;
        self.selected_idx = 0;
    }

    /// Check if the panel is active
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Get the current tool use ID
    pub fn tool_use_id(&self) -> &str {
        &self.tool_use_id
    }

    /// Get the session ID
    pub fn session_id(&self) -> i64 {
        self.session_id
    }

    /// Get the current request
    pub fn request(&self) -> &PermissionRequest {
        &self.request
    }

    /// Get the turn ID
    pub fn turn_id(&self) -> Option<&TurnId> {
        self.turn_id.as_ref()
    }

    /// Get available options based on request type.
    ///
    /// For commands, "Allow All Similar" is not shown (too risky).
    /// For file reads/writes, all 4 options are shown.
    fn available_options(&self) -> Vec<PermissionOption> {
        match &self.request.target {
            GrantTarget::Command { .. } => {
                // No "Allow All" for commands - too risky
                vec![
                    PermissionOption::GrantOnce,
                    PermissionOption::GrantSession,
                    PermissionOption::Deny,
                ]
            }
            _ => {
                // File reads/writes and domains show all options
                vec![
                    PermissionOption::GrantOnce,
                    PermissionOption::GrantSession,
                    PermissionOption::GrantAllSession,
                    PermissionOption::Deny,
                ]
            }
        }
    }

    /// Get the currently selected option
    pub fn selected_option(&self) -> PermissionOption {
        let options = self.available_options();
        options[self.selected_idx.min(options.len() - 1)]
    }

    /// Move selection to the next option
    pub fn select_next(&mut self) {
        let options = self.available_options();
        self.selected_idx = (self.selected_idx + 1) % options.len();
    }

    /// Move selection to the previous option
    pub fn select_prev(&mut self) {
        let options = self.available_options();
        if self.selected_idx == 0 {
            self.selected_idx = options.len() - 1;
        } else {
            self.selected_idx -= 1;
        }
    }

    /// Handle a key event
    pub fn process_key(&mut self, key: KeyEvent) -> KeyAction {
        if !self.active {
            return KeyAction::None;
        }

        match key.code {
            // Navigation
            KeyCode::Up | KeyCode::Char('k') => {
                self.select_prev();
                KeyAction::None
            }
            KeyCode::Down | KeyCode::Char('j') => {
                self.select_next();
                KeyAction::None
            }
            KeyCode::Char('p') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.select_prev();
                KeyAction::None
            }
            KeyCode::Char('n') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.select_next();
                KeyAction::None
            }

            // Selection
            KeyCode::Enter | KeyCode::Char(' ') => {
                let option = self.selected_option();
                let response = option.to_response(&self.request);
                let tool_use_id = self.tool_use_id.clone();
                KeyAction::Selected(tool_use_id, response)
            }

            // Cancel
            KeyCode::Esc => {
                let tool_use_id = self.tool_use_id.clone();
                KeyAction::Cancelled(tool_use_id)
            }

            _ => KeyAction::None,
        }
    }

    /// Calculate the height needed for the panel
    pub fn panel_height(&self, max_height: u16) -> u16 {
        // Help text: 1
        // Blank after help: 1
        // Permission line: 1
        // Blank (separator): 1
        // Options: 3 or 4 (depending on request type)
        // Bottom padding: 1
        // Borders: 2

        let mut lines = 6u16; // Help (1) + blank (1) + permission (1) + separator (1) + padding (1) + 1
        lines += self.available_options().len() as u16;
        lines += 2; // Borders

        let max_from_percent = (max_height * self.config.max_panel_percent) / 100;
        lines.min(max_from_percent).min(max_height.saturating_sub(6))
    }

    /// Render the panel
    pub fn render_panel(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
        if !self.active {
            return;
        }

        // Clear the area first
        frame.render_widget(Clear, area);

        let inner_width = area.width.saturating_sub(4) as usize;
        let mut lines: Vec<Line> = Vec::new();

        // Help text at top
        lines.push(Line::from(Span::styled(
            truncate_text(&self.config.help_text, inner_width),
            theme.help_text(),
        )));
        lines.push(Line::from("")); // Blank line

        // Single permission line: icon + level + target
        let (icon, level_str, target_desc) = match &self.request.target {
            GrantTarget::Path { path, recursive } => {
                let rec_suffix = if *recursive { " (recursive)" } else { "" };
                let level = format_level(self.request.required_level);
                (
                    &self.config.icon_path,
                    level,
                    format!("{}{}", path.display(), rec_suffix),
                )
            }
            GrantTarget::Domain { pattern } => (
                &self.config.icon_domain,
                "Access Domain",
                pattern.clone(),
            ),
            GrantTarget::Command { pattern } => (
                &self.config.icon_command,
                "Execute",
                pattern.clone(),
            ),
        };

        lines.push(Line::from(vec![
            Span::styled("   ", theme.muted_text()),
            Span::styled(format!("{} ", icon), theme.category()),
            Span::styled(format!("{}: ", level_str), theme.muted_text()),
            Span::styled(
                truncate_text(&target_desc, inner_width.saturating_sub(20)),
                theme.resource(),
            ),
        ]));

        // Separator line
        lines.push(Line::from(""));

        // Options
        let options = self.available_options();
        for (idx, option) in options.iter().enumerate() {
            let is_selected = idx == self.selected_idx;
            let prefix = if is_selected {
                &self.config.selection_indicator
            } else {
                &self.config.no_indicator
            };

            let description = option.description(&self.request);

            let (label_style, desc_style) = if is_selected {
                if option.is_positive() {
                    (theme.button_confirm_focused(), theme.focused_text())
                } else {
                    (theme.button_cancel_focused(), theme.focused_text())
                }
            } else if option.is_positive() {
                (theme.button_confirm(), theme.muted_text())
            } else {
                (theme.button_cancel(), theme.muted_text())
            };

            let indicator_style = if is_selected {
                theme.focus_indicator()
            } else {
                theme.muted_text()
            };

            lines.push(Line::from(vec![
                Span::styled(prefix.clone(), indicator_style),
                Span::styled(option.label(), label_style),
                Span::styled(" - ", theme.muted_text()),
                Span::styled(description, desc_style),
            ]));
        }

        // Bottom padding
        lines.push(Line::from(""));

        // Build the block
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(theme.warning())
            .title(Span::styled(
                self.config.title.clone(),
                theme.warning().add_modifier(Modifier::BOLD),
            ));

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

impl Default for PermissionPanel {
    fn default() -> Self {
        Self::new()
    }
}

/// Format a permission level for display
fn format_level(level: PermissionLevel) -> &'static str {
    match level {
        PermissionLevel::None => "None",
        PermissionLevel::Read => "Read File",
        PermissionLevel::Write => "Write File",
        PermissionLevel::Execute => "Execute",
        PermissionLevel::Admin => "Admin",
    }
}

/// Truncate text to fit within a maximum width
fn truncate_text(text: &str, max_width: usize) -> String {
    if text.chars().count() <= max_width {
        text.to_string()
    } else {
        let truncated: String = text.chars().take(max_width.saturating_sub(3)).collect();
        format!("{}...", truncated)
    }
}

// --- Widget trait implementation ---

use super::{widget_ids, Widget, WidgetAction, WidgetKeyContext, WidgetKeyResult};
use std::any::Any;

impl Widget for PermissionPanel {
    fn id(&self) -> &'static str {
        widget_ids::PERMISSION_PANEL
    }

    fn priority(&self) -> u8 {
        200 // High priority - modal panel
    }

    fn is_active(&self) -> bool {
        self.active
    }

    fn handle_key(&mut self, key: KeyEvent, ctx: &WidgetKeyContext) -> WidgetKeyResult {
        if !self.active {
            return WidgetKeyResult::NotHandled;
        }

        // Use NavigationHelper for navigation keys
        if ctx.nav.is_move_up(&key) {
            self.select_prev();
            return WidgetKeyResult::Handled;
        }
        if ctx.nav.is_move_down(&key) {
            self.select_next();
            return WidgetKeyResult::Handled;
        }

        // Selection using nav helper
        if ctx.nav.is_select(&key) {
            let option = self.selected_option();
            let response = option.to_response(&self.request);
            let tool_use_id = self.tool_use_id.clone();
            return WidgetKeyResult::Action(WidgetAction::SubmitPermission {
                tool_use_id,
                response,
            });
        }

        // Cancel using nav helper
        if ctx.nav.is_cancel(&key) {
            let tool_use_id = self.tool_use_id.clone();
            return WidgetKeyResult::Action(WidgetAction::CancelPermission { tool_use_id });
        }

        // j/k vim-style navigation (kept for consistency)
        match key.code {
            KeyCode::Char('k') => {
                self.select_prev();
                WidgetKeyResult::Handled
            }
            KeyCode::Char('j') => {
                self.select_next();
                WidgetKeyResult::Handled
            }
            _ => WidgetKeyResult::NotHandled,
        }
    }

    fn render(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) {
        self.render_panel(frame, area, theme);
    }

    fn required_height(&self, max_height: u16) -> u16 {
        if self.active {
            self.panel_height(max_height)
        } else {
            0
        }
    }

    fn blocks_input(&self) -> bool {
        self.active
    }

    fn is_overlay(&self) -> bool {
        false
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

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

    fn create_test_request() -> PermissionRequest {
        PermissionRequest::file_write("test-1", "/tmp/foo.txt")
    }

    #[test]
    fn test_permission_option_all() {
        let options = PermissionOption::all();
        assert_eq!(options.len(), 4);
        assert_eq!(options[0], PermissionOption::GrantOnce);
        assert_eq!(options[1], PermissionOption::GrantSession);
        assert_eq!(options[2], PermissionOption::GrantAllSession);
        assert_eq!(options[3], PermissionOption::Deny);
    }

    #[test]
    fn test_permission_option_to_response() {
        let request = create_test_request();

        let once = PermissionOption::GrantOnce.to_response(&request);
        assert!(once.granted);
        assert!(once.grant.is_none()); // Grant once doesn't persist

        let session = PermissionOption::GrantSession.to_response(&request);
        assert!(session.granted);
        assert!(session.grant.is_some()); // Session grant persists

        let all_session = PermissionOption::GrantAllSession.to_response(&request);
        assert!(all_session.granted);
        assert!(all_session.grant.is_some()); // All session grant persists

        let deny = PermissionOption::Deny.to_response(&request);
        assert!(!deny.granted);
        assert!(deny.grant.is_none());
    }

    #[test]
    fn test_panel_activation() {
        let mut panel = PermissionPanel::new();
        assert!(!panel.is_active());

        let request = PermissionRequest::file_write("test-1", "/tmp/foo.txt");

        panel.activate("tool_123".to_string(), 1, request, None);
        assert!(panel.is_active());
        assert_eq!(panel.tool_use_id(), "tool_123");
        assert_eq!(panel.session_id(), 1);

        panel.deactivate();
        assert!(!panel.is_active());
    }

    #[test]
    fn test_navigation() {
        let mut panel = PermissionPanel::new();
        let request = create_test_request();
        panel.activate("tool_1".to_string(), 1, request, None);

        // Default is first option
        assert_eq!(panel.selected_option(), PermissionOption::GrantOnce);

        // Move down
        panel.select_next();
        assert_eq!(panel.selected_option(), PermissionOption::GrantSession);

        panel.select_next();
        assert_eq!(panel.selected_option(), PermissionOption::GrantAllSession);

        panel.select_next();
        assert_eq!(panel.selected_option(), PermissionOption::Deny);

        // Wrap around
        panel.select_next();
        assert_eq!(panel.selected_option(), PermissionOption::GrantOnce);

        // Move up (wrap)
        panel.select_prev();
        assert_eq!(panel.selected_option(), PermissionOption::Deny);
    }

    #[test]
    fn test_handle_key_navigation() {
        let mut panel = PermissionPanel::new();
        let request = create_test_request();
        panel.activate("tool_1".to_string(), 1, request, None);

        // Down key
        let action = panel.process_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
        matches!(action, KeyAction::None);
        assert_eq!(panel.selected_option(), PermissionOption::GrantSession);

        // Up key
        let action = panel.process_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE));
        matches!(action, KeyAction::None);
        assert_eq!(panel.selected_option(), PermissionOption::GrantOnce);
    }

    #[test]
    fn test_handle_key_selection() {
        let mut panel = PermissionPanel::new();
        let request = create_test_request();
        panel.activate("tool_1".to_string(), 1, request, None);

        // Enter to select
        let action = panel.process_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        match action {
            KeyAction::Selected(tool_use_id, response) => {
                assert_eq!(tool_use_id, "tool_1");
                assert!(response.granted);
                assert!(response.grant.is_none()); // GrantOnce doesn't persist
            }
            _ => panic!("Expected Selected action"),
        }
        panel.deactivate();
        assert!(!panel.is_active());
    }

    #[test]
    fn test_handle_key_cancel() {
        let mut panel = PermissionPanel::new();
        let request = create_test_request();
        panel.activate("tool_1".to_string(), 1, request, None);

        // Escape to cancel
        let action = panel.process_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        match action {
            KeyAction::Cancelled(tool_use_id) => {
                assert_eq!(tool_use_id, "tool_1");
            }
            _ => panic!("Expected Cancelled action"),
        }
        panel.deactivate();
        assert!(!panel.is_active());
    }

    #[test]
    fn test_option_descriptions() {
        let read_request = PermissionRequest::file_read("1", "/tmp/foo.txt");
        let write_request = PermissionRequest::file_write("2", "/tmp/bar.txt");
        let cmd_request = PermissionRequest::command_execute("3", "git status");

        // GrantOnce is always the same
        assert_eq!(
            PermissionOption::GrantOnce.description(&read_request),
            "Allow only this request"
        );

        // GrantSession varies by type
        assert_eq!(
            PermissionOption::GrantSession.description(&read_request),
            "Allow reading this file"
        );
        assert_eq!(
            PermissionOption::GrantSession.description(&write_request),
            "Allow writing this file"
        );
        assert_eq!(
            PermissionOption::GrantSession.description(&cmd_request),
            "Allow this command"
        );

        // GrantAllSession varies by type
        assert_eq!(
            PermissionOption::GrantAllSession.description(&read_request),
            "Allow reading any file"
        );
        assert_eq!(
            PermissionOption::GrantAllSession.description(&write_request),
            "Allow writing any file"
        );
        assert_eq!(
            PermissionOption::GrantAllSession.description(&cmd_request),
            "Allow all commands"
        );

        // Deny is always the same
        assert_eq!(
            PermissionOption::Deny.description(&read_request),
            "Deny this request"
        );
    }

    #[test]
    fn test_truncate_text() {
        assert_eq!(truncate_text("short", 10), "short");
        assert_eq!(truncate_text("this is a longer text", 10), "this is...");
        assert_eq!(truncate_text("exact", 5), "exact");
    }

    #[test]
    fn test_command_hides_allow_all() {
        let mut panel = PermissionPanel::new();

        // File read: should have 4 options including GrantAllSession
        let read_request = PermissionRequest::file_read("1", "/tmp/foo.txt");
        panel.activate("tool_1".to_string(), 1, read_request, None);
        let options = panel.available_options();
        assert_eq!(options.len(), 4);
        assert!(options.contains(&PermissionOption::GrantAllSession));
        panel.deactivate();

        // Command: should have 3 options, NO GrantAllSession
        let cmd_request = PermissionRequest::command_execute("2", "git status");
        panel.activate("tool_2".to_string(), 1, cmd_request, None);
        let options = panel.available_options();
        assert_eq!(options.len(), 3);
        assert!(!options.contains(&PermissionOption::GrantAllSession));
    }
}