返回 CodeWhale
primary_tests.rs
根目录 / crates / tui / src / tui / mouse_ui / primary_tests.rs
1 use super::tests::create_test_app;
2 use super::*;
3 use crate::tui::clipboard::ClipboardHandler;
4 use crate::tui::history::HistoryCell;
5 use crate::tui::selection::TranscriptSelectionPoint;
6
7 fn composer() -> (App, Rect) {
8 let mut app = create_test_app();
9 app.launch.visible = false;
10 app.clipboard = ClipboardHandler::for_test(false, false);
11 app.clipboard.enable_primary_for_test();
12 app.clipboard.write_text("REGULAR_CLIPBOARD").unwrap();
13 app.viewport.last_composer_area = Some(Rect::new(0, 20, 80, 3));
14 let input = Rect::new(1, 21, 78, 1);
15 app.viewport.last_composer_content = Some(input);
16 let text = crate::tui::widgets::composer_content_geometry(input, false).text_area;
17 (app, text)
18 }
19
20 fn event(kind: MouseEventKind, column: u16, row: u16) -> MouseEvent {
21 MouseEvent {
22 kind,
23 column,
24 row,
25 modifiers: KeyModifiers::NONE,
26 }
27 }
28
29 #[test]
30 fn composer_selection_publishes_primary_quietly_even_when_released_outside() {
31 let (mut app, text) = composer();
32 app.input = "alpha beta".into();
33 handle_mouse_event(
34 &mut app,
35 event(MouseEventKind::Down(MouseButton::Left), text.x, text.y),
36 Instant::now(),
37 );
38 handle_mouse_event(
39 &mut app,
40 event(MouseEventKind::Drag(MouseButton::Left), text.x + 5, text.y),
41 Instant::now(),
42 );
43 let status = app.visible_notice_text();
44 handle_mouse_event(
45 &mut app,
46 event(MouseEventKind::Up(MouseButton::Left), 90, 25),
47 Instant::now(),
48 );
49 assert_eq!(app.clipboard.read_primary_text().as_deref(), Some("alpha"));
50 assert_eq!(app.selected_text(), "alpha");
51 assert_eq!(app.clipboard.last_written_text(), Some("REGULAR_CLIPBOARD"));
52 assert_eq!(app.visible_notice_text(), status);
53 copy_active_selection(&mut app);
54 assert_eq!(app.clipboard.last_written_text(), Some("alpha"));
55 assert_eq!(app.clipboard.read_primary_text().as_deref(), Some("alpha"));
56 }
57
58 #[test]
59 fn transcript_selection_uses_clean_text_and_preserves_regular_clipboard() {
60 let (mut app, _) = composer();
61 app.history = vec![HistoryCell::User {
62 content: "COPY_TRANSCRIPT".into(),
63 }];
64 app.resync_history_revisions();
65 app.viewport.transcript_cache.ensure(
66 &app.history,
67 &app.history_revisions,
68 80,
69 app.transcript_render_options(),
70 );
71 let last = app
72 .viewport
73 .transcript_cache
74 .lines()
75 .len()
76 .saturating_sub(1);
77 app.viewport.transcript_selection.anchor = Some(TranscriptSelectionPoint {
78 line_index: 0,
79 column: 0,
80 });
81 app.viewport.transcript_selection.head = Some(TranscriptSelectionPoint {
82 line_index: last,
83 column: 80,
84 });
85 app.viewport.transcript_selection.dragging = true;
86 let expected = selection_to_text(&app).unwrap();
87 let status = app.visible_notice_text();
88 handle_mouse_event(
89 &mut app,
90 event(MouseEventKind::Up(MouseButton::Left), 90, 10),
91 Instant::now(),
92 );
93 assert_eq!(app.clipboard.read_primary_text(), Some(expected));
94 assert_eq!(app.clipboard.last_written_text(), Some("REGULAR_CLIPBOARD"));
95 assert_eq!(app.visible_notice_text(), status);
96 assert!(!app.viewport.transcript_selection.dragging);
97 }
98
99 #[test]
100 fn middle_click_inserts_guarded_text_at_pointer_without_cutting_or_submitting() {
101 let (mut app, text) = composer();
102 app.input = "abc".into();
103 app.selection_anchor = Some(0);
104 app.cursor_position = 3;
105 app.clipboard.write_primary_text("X\r\n/quit\n").unwrap();
106 handle_mouse_event(
107 &mut app,
108 event(
109 MouseEventKind::Down(MouseButton::Middle),
110 text.x + 1,
111 text.y,
112 ),
113 Instant::now(),
114 );
115 assert_eq!(app.input, "aX\n/quit\nbc");
116 assert!(app.selection_anchor.is_none());
117 assert!(app.pending_composer_submit.is_none());
118 assert!(app.pending_launch_action.is_none());
119 assert_eq!(app.clipboard.last_written_text(), Some("REGULAR_CLIPBOARD"));
120 handle_mouse_event(
121 &mut app,
122 event(MouseEventKind::Up(MouseButton::Middle), text.x + 1, text.y),
123 Instant::now(),
124 );
125 assert_eq!(app.input, "aX\n/quit\nbc", "release must not paste twice");
126 }
127
128 #[test]
129 fn middle_click_respects_overlays_missing_composer_and_remote_clipboard() {
130 for mode in ["modal", "disabled", "ssh", "unavailable", "other-platform"] {
131 let (mut app, text) = composer();
132 app.input = "KEEP_DRAFT".into();
133 app.cursor_position = 4;
134 app.selection_anchor = Some(1);
135 app.clipboard
136 .write_primary_text("SHOULD_NOT_PASTE")
137 .unwrap();
138 match mode {
139 "modal" => {
140 super::open_context_menu(
141 &mut app,
142 event(MouseEventKind::Down(MouseButton::Right), text.x, text.y),
143 );
144 }
145 "disabled" => app.viewport.last_composer_area = None,
146 "ssh" => {
147 app.clipboard = ClipboardHandler::for_test(true, false);
148 app.clipboard.enable_primary_for_test();
149 }
150 "unavailable" => {
151 app.clipboard = ClipboardHandler::unavailable_for_test(false);
152 app.clipboard.enable_primary_for_test();
153 }
154 _ => app.clipboard = ClipboardHandler::for_test(false, false),
155 }
156 let status = app.visible_notice_text();
157 handle_mouse_event(
158 &mut app,
159 event(
160 MouseEventKind::Down(MouseButton::Middle),
161 text.x + 1,
162 text.y,
163 ),
164 Instant::now(),
165 );
166 assert_eq!(app.input, "KEEP_DRAFT", "{mode}");
167 assert_eq!(app.cursor_position, 4, "{mode}");
168 assert_eq!(app.selection_anchor, Some(1), "{mode}");
169 assert!(app.pending_composer_submit.is_none(), "{mode}");
170 assert_eq!(app.visible_notice_text(), status, "{mode}");
171 }
172 }
173
174 #[test]
175 fn transcript_drag_released_over_composer_keeps_caret_and_clears_drag() {
176 let (mut app, text) = composer();
177 app.input = "draft".into();
178 app.cursor_position = 5;
179 app.history = vec![HistoryCell::User {
180 content: "SELECTED_LINE".into(),
181 }];
182 app.resync_history_revisions();
183 app.viewport.transcript_cache.ensure(
184 &app.history,
185 &app.history_revisions,
186 80,
187 app.transcript_render_options(),
188 );
189 app.viewport.transcript_selection.anchor = Some(TranscriptSelectionPoint {
190 line_index: 0,
191 column: 0,
192 });
193 app.viewport.transcript_selection.head = Some(TranscriptSelectionPoint {
194 line_index: 0,
195 column: 80,
196 });
197 app.viewport.transcript_selection.dragging = true;
198 let expected = selection_to_text(&app).unwrap();
199 // Dragging across the composer must not move its caret or start a
200 // composer selection.
201 handle_mouse_event(
202 &mut app,
203 event(MouseEventKind::Drag(MouseButton::Left), text.x + 2, text.y),
204 Instant::now(),
205 );
206 assert_eq!(app.cursor_position, 5);
207 assert!(app.selection_anchor.is_none());
208 assert!(app.viewport.transcript_selection.dragging);
209 // Releasing over the composer finishes the transcript drag.
210 handle_mouse_event(
211 &mut app,
212 event(MouseEventKind::Up(MouseButton::Left), text.x + 2, text.y),
213 Instant::now(),
214 );
215 assert!(!app.viewport.transcript_selection.dragging);
216 assert_eq!(app.cursor_position, 5);
217 assert_eq!(app.input, "draft");
218 assert_eq!(app.clipboard.read_primary_text(), Some(expected));
219 assert_eq!(app.clipboard.last_written_text(), Some("REGULAR_CLIPBOARD"));
220 }
221
222 fn select_all_transcript_lines(app: &mut App) {
223 let last = app
224 .viewport
225 .transcript_cache
226 .lines()
227 .len()
228 .saturating_sub(1);
229 app.viewport.transcript_selection.anchor = Some(TranscriptSelectionPoint {
230 line_index: 0,
231 column: 0,
232 });
233 app.viewport.transcript_selection.head = Some(TranscriptSelectionPoint {
234 line_index: last,
235 column: 80,
236 });
237 }
238
239 fn ensure_transcript(app: &mut App) {
240 app.resync_history_revisions();
241 app.viewport.transcript_cache.ensure(
242 &app.history,
243 &app.history_revisions,
244 80,
245 app.transcript_render_options(),
246 );
247 }
248
249 #[test]
250 fn transcript_copy_defaults_to_markdown_source_and_names_cell_count() {
251 let (mut app, _) = composer();
252 assert!(
253 app.viewport.selection_copy_markdown,
254 "markdown copy defaults on"
255 );
256 let content = "# Title\n\n```rust\nlet x = 1;\n```".to_string();
257 app.history = vec![HistoryCell::Assistant {
258 content: content.clone(),
259 streaming: false,
260 }];
261 ensure_transcript(&mut app);
262 select_all_transcript_lines(&mut app);
263 let toasts_before = app.status_toasts.len();
264 copy_active_selection(&mut app);
265 assert_eq!(app.clipboard.last_written_text(), Some(content.as_str()));
266 assert_eq!(app.status_toasts.len(), toasts_before + 1);
267 let toast = app.status_toasts.back().expect("markdown copy toast");
268 assert!(
269 toast.text.contains("(1)"),
270 "toast names the projected cell count, got {:?}",
271 toast.text
272 );
273 let expected = toast.text.clone();
274 assert_eq!(app.visible_notice_text(), Some(expected));
275 }
276
277 #[test]
278 fn transcript_copy_falls_back_to_rendered_text_when_markdown_is_off() {
279 let (mut app, _) = composer();
280 app.viewport.selection_copy_markdown = false;
281 app.history = vec![HistoryCell::Assistant {
282 content: "# Title\n\n```rust\nlet x = 1;\n```".to_string(),
283 streaming: false,
284 }];
285 ensure_transcript(&mut app);
286 select_all_transcript_lines(&mut app);
287 let expected = selection_to_text(&app).expect("rendered fallback text");
288 let toasts_before = app.status_toasts.len();
289 copy_active_selection(&mut app);
290 assert_eq!(app.clipboard.last_written_text(), Some(expected.as_str()));
291 assert_eq!(app.status_toasts.len(), toasts_before + 1);
292 assert_eq!(
293 app.status_toasts.back().map(|toast| toast.text.as_str()),
294 Some("Copied to the clipboard")
295 );
296 }
297
298 #[test]
299 fn transcript_copy_joins_cells_with_blank_lines_and_counts_them() {
300 let (mut app, _) = composer();
301 app.history = vec![
302 HistoryCell::User {
303 content: "first question".into(),
304 },
305 HistoryCell::Assistant {
306 content: "second answer".into(),
307 streaming: false,
308 },
309 ];
310 ensure_transcript(&mut app);
311 select_all_transcript_lines(&mut app);
312 // A partial drag still rounds out to whole cells.
313 app.viewport.transcript_selection.anchor = Some(TranscriptSelectionPoint {
314 line_index: 0,
315 column: 2,
316 });
317 let toasts_before = app.status_toasts.len();
318 copy_active_selection(&mut app);
319 assert_eq!(
320 app.clipboard.last_written_text(),
321 Some("first question\n\nsecond answer")
322 );
323 assert_eq!(app.status_toasts.len(), toasts_before + 1);
324 let toast = app.status_toasts.back().expect("multi-cell copy toast");
325 assert!(
326 toast.text.contains("(2)"),
327 "toast names the projected cell count, got {:?}",
328 toast.text
329 );
330 }
331
331 lines RUST