| 1 | //! Whole mounted transcript acceptance. |
| 2 | use super::*; |
| 3 | use crate::tui::selection::TranscriptSelectionPoint; |
| 4 | use codewhale_runtime::config::Config; |
| 5 | use std::path::PathBuf; |
| 6 | |
| 7 | fn app(locale: Locale, case: usize) -> App { |
| 8 | let mut app = App::new( |
| 9 | crate::tui::test_support::test_tui_options(PathBuf::from(".")), |
| 10 | &Config::default(), |
| 11 | ); |
| 12 | app.launch.visible = false; |
| 13 | app.ui_locale = locale; |
| 14 | app.theme_id = palette::ThemeId::Terminal; |
| 15 | app.ui_theme = palette::TERMINAL_UI_THEME; |
| 16 | app.low_motion = true; |
| 17 | app.fancy_animations = false; |
| 18 | if case == 0 { |
| 19 | return app; |
| 20 | } |
| 21 | app.push_history_cell(HistoryCell::User { |
| 22 | content: "Review this source and its exact source copy".into(), |
| 23 | }); |
| 24 | app.push_history_cell(HistoryCell::Assistant { |
| 25 | content: "A linked [guide](https://example.test/private-target) with 鲸鱼 cafe\u{0301} 1\u{20e3}① and 👩\u{200d}💻.\n\n```rust\nlet source = exact_source;\n```".into(), streaming: case == 2, |
| 26 | }); |
| 27 | if case >= 3 { |
| 28 | app.use_mouse_capture = true; |
| 29 | for n in 0..8 { |
| 30 | app.push_history_cell(HistoryCell::User { |
| 31 | content: format!("User turn {n}"), |
| 32 | }); |
| 33 | app.push_history_cell(HistoryCell::Assistant { |
| 34 | content: format!("Receipt {n} with enough text to wrap in a narrow transcript."), |
| 35 | streaming: false, |
| 36 | }); |
| 37 | } |
| 38 | app.viewport.transcript_scroll = |
| 39 | codewhale_tui_kit::scrolling::TranscriptScroll::at_line(if case == 4 { 3 } else { 0 }); |
| 40 | } |
| 41 | if case == 4 { |
| 42 | app.viewport.transcript_selection.anchor = Some(TranscriptSelectionPoint { |
| 43 | line_index: 3, |
| 44 | column: 1, |
| 45 | }); |
| 46 | app.viewport.transcript_selection.head = Some(TranscriptSelectionPoint { |
| 47 | line_index: 5, |
| 48 | column: 9, |
| 49 | }); |
| 50 | } |
| 51 | if case == 5 { |
| 52 | // A user theme keeps independent source colors across native chrome. |
| 53 | app.ui_theme.surface_bg = Color::Rgb(13, 29, 47); |
| 54 | app.ui_theme.border = Color::Rgb(61, 79, 101); |
| 55 | app.ui_theme.status_working = Color::Rgb(127, 149, 173); |
| 56 | app.ui_theme.selection_bg = Color::Rgb(181, 197, 211); |
| 57 | app.push_history_cell(HistoryCell::Tool(ToolCell::Generic(GenericToolCell { |
| 58 | name: "read_file".into(), |
| 59 | status: ToolStatus::Success, |
| 60 | input_summary: Some("src/main.rs".into()), |
| 61 | output: Some("kept source and warning details".into()), |
| 62 | prompts: None, |
| 63 | spillover_path: None, |
| 64 | output_summary: None, |
| 65 | is_diff: false, |
| 66 | }))); |
| 67 | } |
| 68 | app |
| 69 | } |
| 70 | fn guarded(area: Rect) -> Buffer { |
| 71 | let mut buf = Buffer::empty(Rect::new(2, 3, area.width + 12, area.height + 8)); |
| 72 | for cell in &mut buf.content { |
| 73 | cell.set_symbol("~") |
| 74 | .set_style(Style::default().bg(Color::Rgb(11, 23, 37))); |
| 75 | } |
| 76 | buf |
| 77 | } |
| 78 | #[test] |
| 79 | fn mounted_transcript_stream_resize_and_scroll_lock_preserve_cache_source() { |
| 80 | let mut current = app(Locale::En, 2); |
| 81 | for (chunk, width, height, top) in [ |
| 82 | ("First source", 80, 12, None), |
| 83 | ("First source plus 鲸鱼\n```rust\nlet x", 20, 5, None), |
| 84 | ( |
| 85 | "First source plus 鲸鱼\n```rust\nlet x = 1;\n```\n[guide](https://example.test/source)", |
| 86 | 40, |
| 87 | 3, |
| 88 | Some(0), |
| 89 | ), |
| 90 | ("Final source and receipt", 80, 20, Some(0)), |
| 91 | ] { |
| 92 | current.history[1] = HistoryCell::Assistant { |
| 93 | content: chunk.into(), |
| 94 | streaming: chunk != "Final source and receipt", |
| 95 | }; |
| 96 | current.bump_history_cell(1); |
| 97 | if let Some(top) = top { |
| 98 | current.viewport.transcript_scroll = |
| 99 | codewhale_tui_kit::scrolling::TranscriptScroll::at_line(top); |
| 100 | current.user_scrolled_during_stream = true; |
| 101 | } |
| 102 | let area = Rect::new(7, 5, width, height); |
| 103 | let mut actual = guarded(area); |
| 104 | ChatWidget::new_with_ocean_elapsed(&mut current, area, 0).render(area, &mut actual); |
| 105 | assert!(current.viewport.last_transcript_area.is_some()); |
| 106 | match ¤t.history[1] { |
| 107 | HistoryCell::Assistant { content, .. } => assert_eq!(content, chunk), |
| 108 | _ => panic!("source authority changed"), |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | #[test] |
| 113 | fn mounted_transcript_links_never_hyperlink_jump_chrome_or_paint_targets() { |
| 114 | let mut app = app(Locale::En, 3); |
| 115 | let area = Rect::new(7, 5, 40, 8); |
| 116 | let mut widget = ChatWidget::new_with_ocean_elapsed(&mut app, area, 0); |
| 117 | widget.lines = vec![Line::from("Read this visible guide"); 8]; |
| 118 | widget.transcript_area = area; |
| 119 | widget.scrollbar = Some(codewhale_ratatui::TranscriptScrollFacts { |
| 120 | top: 0, |
| 121 | visible: 8, |
| 122 | total: 20, |
| 123 | }); |
| 124 | widget.jump_to_latest_button = codewhale_ratatui::transcript_jump_rect(area, true); |
| 125 | widget.line_links = vec![ |
| 126 | vec![codewhale_tui_kit::osc8::LineLink { |
| 127 | col_start: 0, |
| 128 | col_end: 100, |
| 129 | target: "https://example.test/opaque-target".into(), |
| 130 | line: None, |
| 131 | }]; |
| 132 | 8 |
| 133 | ]; |
| 134 | let mut buf = guarded(area); |
| 135 | widget.render(area, &mut buf); |
| 136 | let plan = widget.viewport().plan(area); |
| 137 | let button = plan.jump.unwrap(); |
| 138 | let regions = codewhale_tui_kit::osc8::FRAME_LINKS.with(|regions| regions.borrow().clone()); |
| 139 | assert!(!regions.is_empty()); |
| 140 | for region in regions { |
| 141 | let rect = Rect::new( |
| 142 | region.col_start, |
| 143 | region.row, |
| 144 | region.col_end - region.col_start + 1, |
| 145 | 1, |
| 146 | ); |
| 147 | assert_eq!(rect.intersection(plan.link_area), rect); |
| 148 | assert!(rect.intersection(button).is_empty()); |
| 149 | } |
| 150 | let shown: String = buf.content.iter().map(|cell| cell.symbol()).collect(); |
| 151 | assert!(!shown.contains("opaque-target")); |
| 152 | assert_eq!(buf[(button.x + 1, button.y + 1)].symbol(), "↓"); |
| 153 | } |
| 154 |