返回 CodeWhale
core_copy_host_tests.rs
根目录 / crates / tui / src / commands / core_copy_host_tests.rs
1 use crate::tui::app::App;
2 use crate::tui::app::TuiOptions;
3 use crate::tui::clipboard::ClipboardHandler;
4 use crate::tui::history::{HistoryCell, history_cells_from_message};
5 use codewhale_models::{ContentBlock, Message, Role};
6 use codewhale_runtime::config::Config;
7 use std::path::{Path, PathBuf};
8 use tempfile::TempDir;
9
10 fn test_app() -> App {
11 App::new(
12 TuiOptions {
13 model: "deepseek-v4-flash".to_string(),
14 ..crate::tui::test_support::test_tui_options(PathBuf::from("."))
15 },
16 &Config::default(),
17 )
18 }
19
20 fn add_completed_assistant(app: &mut App, text: &str) -> usize {
21 let history_index = app.history.len();
22 app.add_message(HistoryCell::Assistant {
23 content: text.to_string(),
24 streaming: false,
25 });
26 app.record_completed_assistant_output(history_index, text);
27 history_index
28 }
29
30 fn isolate_state_home(
31 path: &Path,
32 ) -> (
33 crate::test_support::EnvVarGuard,
34 crate::test_support::EnvVarGuard,
35 ) {
36 (
37 crate::test_support::EnvVarGuard::set("HOME", path),
38 crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", path),
39 )
40 }
41
42 #[test]
43 fn copies_the_latest_completed_assistant_output_only() {
44 let tmp = TempDir::new().expect("tempdir");
45 let _env_lock = crate::test_support::lock_test_env();
46 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
47 let mut app = test_app();
48 app.clipboard = ClipboardHandler::for_test(false, false);
49 add_completed_assistant(&mut app, "older");
50 app.add_message(HistoryCell::System {
51 content: "system".to_string(),
52 });
53 app.add_message(HistoryCell::Thinking {
54 content: "hidden reasoning".to_string(),
55 streaming: false,
56 duration_secs: None,
57 });
58 add_completed_assistant(&mut app, "latest **answer**\nsecond line");
59 // Interrupted salvage may be visible, but it never gets a completion
60 // receipt and therefore cannot become `/copy` authority.
61 app.add_message(HistoryCell::Assistant {
62 content: "active partial".to_string(),
63 streaming: false,
64 });
65
66 let result = crate::commands::execute("/copy", &mut app);
67
68 let expected = format!(
69 "Accepted the last completed assistant response for clipboard delivery; a recovery copy is at {}",
70 tmp.path().join("exports").join("last-copy.md").display()
71 );
72 assert_eq!(result.message.as_deref(), Some(expected.as_str()));
73 assert_eq!(
74 app.clipboard.last_written_text(),
75 Some("latest **answer**\nsecond line")
76 );
77 assert_eq!(
78 std::fs::read_to_string(tmp.path().join("exports").join("last-copy.md"))
79 .expect("recovery copy"),
80 "latest **answer**\nsecond line"
81 );
82 }
83
84 #[test]
85 fn skips_empty_and_non_assistant_history() {
86 let mut app = test_app();
87 add_completed_assistant(&mut app, " \n");
88 app.add_message(HistoryCell::System {
89 content: "system".to_string(),
90 });
91 app.add_message(HistoryCell::Assistant {
92 content: "partial".to_string(),
93 streaming: false,
94 });
95
96 let result = crate::commands::execute("/copy", &mut app);
97
98 assert_eq!(
99 result.message.as_deref(),
100 Some("No completed assistant response is available to copy")
101 );
102 }
103
104 #[test]
105 fn active_turn_does_not_change_which_completed_output_is_copied() {
106 let tmp = TempDir::new().expect("tempdir");
107 let _env_lock = crate::test_support::lock_test_env();
108 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
109 let mut app = test_app();
110 app.clipboard = ClipboardHandler::for_test(false, false);
111 add_completed_assistant(&mut app, "completed before the active turn");
112 app.is_loading = true;
113
114 let result = crate::commands::execute("/copy", &mut app);
115
116 assert!(!result.is_error);
117 assert_eq!(
118 app.clipboard.last_written_text(),
119 Some("completed before the active turn")
120 );
121 assert!(app.is_loading);
122 }
123
124 #[test]
125 fn interrupted_assistant_role_never_replaces_the_last_completed_answer() {
126 let tmp = TempDir::new().expect("tempdir");
127 let _env_lock = crate::test_support::lock_test_env();
128 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
129 let mut app = test_app();
130 app.clipboard = ClipboardHandler::for_test(false, false);
131 add_completed_assistant(&mut app, "completed answer");
132 app.add_message(HistoryCell::Assistant {
133 content: "salvaged partial".to_string(),
134 streaming: false,
135 });
136
137 let result = crate::commands::execute("/copy", &mut app);
138
139 assert!(!result.is_error);
140 assert_eq!(app.clipboard.last_written_text(), Some("completed answer"));
141 }
142
143 #[test]
144 fn compacted_context_uses_the_typed_completed_output_receipt() {
145 let tmp = TempDir::new().expect("tempdir");
146 let _env_lock = crate::test_support::lock_test_env();
147 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
148 let mut app = test_app();
149 app.clipboard = ClipboardHandler::for_test(false, false);
150 add_completed_assistant(&mut app, "visible answer before compaction");
151 app.api_messages = std::sync::Arc::new(vec![
152 Message {
153 role: Role::User,
154 content: vec![ContentBlock::Text {
155 text: "compaction checkpoint".to_string(),
156 cache_control: None,
157 }],
158 },
159 Message {
160 role: Role::InterruptedAssistant,
161 content: vec![ContentBlock::Text {
162 text: "partial after compaction".to_string(),
163 cache_control: None,
164 }],
165 },
166 ]);
167
168 let result = crate::commands::execute("/copy", &mut app);
169
170 assert!(!result.is_error);
171 assert_eq!(
172 app.clipboard.last_written_text(),
173 Some("visible answer before compaction")
174 );
175 }
176
177 #[test]
178 fn completed_receipt_survives_history_folding() {
179 let mut app = test_app();
180 for index in 0..(App::HISTORY_SOFT_CAP - 1) {
181 app.add_message(HistoryCell::System {
182 content: format!("status {index}"),
183 });
184 }
185 add_completed_assistant(&mut app, "completed answer before fold");
186 app.add_message(HistoryCell::System {
187 content: "trigger fold".to_string(),
188 });
189
190 assert_eq!(
191 app.completed_assistant_output_receipt(),
192 Some("completed answer before fold")
193 );
194 }
195
196 #[test]
197 fn popping_the_latest_cell_reveals_the_prior_completed_receipt() {
198 let tmp = TempDir::new().expect("tempdir");
199 let _env_lock = crate::test_support::lock_test_env();
200 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
201 let mut app = test_app();
202 app.clipboard = ClipboardHandler::for_test(false, false);
203 add_completed_assistant(&mut app, "older answer");
204 add_completed_assistant(&mut app, "newer answer");
205 app.api_messages_mut().clear();
206 app.pop_history();
207
208 let result = crate::commands::execute("/copy", &mut app);
209
210 assert!(!result.is_error);
211 assert_eq!(app.clipboard.last_written_text(), Some("older answer"));
212 }
213
214 #[test]
215 fn backtrack_truncation_drops_receipts_after_the_selected_boundary() {
216 let tmp = TempDir::new().expect("tempdir");
217 let _env_lock = crate::test_support::lock_test_env();
218 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
219 let mut app = test_app();
220 app.clipboard = ClipboardHandler::for_test(false, false);
221 add_completed_assistant(&mut app, "older answer");
222 app.add_message(HistoryCell::User {
223 content: "next prompt".to_string(),
224 });
225 add_completed_assistant(&mut app, "newer answer");
226 app.api_messages_mut().clear();
227 app.truncate_history_to(2);
228
229 let result = crate::commands::execute("/copy", &mut app);
230
231 assert!(!result.is_error);
232 assert_eq!(app.clipboard.last_written_text(), Some("older answer"));
233 }
234
235 #[test]
236 fn restored_repair_receipt_is_never_copyable_assistant_output() {
237 let tmp = TempDir::new().expect("tempdir");
238 let _env_lock = crate::test_support::lock_test_env();
239 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
240 let mut app = test_app();
241 app.clipboard = ClipboardHandler::for_test(false, false);
242 let visible = Message {
243 role: Role::Assistant,
244 content: vec![ContentBlock::Text {
245 text: "real assistant answer".to_string(),
246 cache_control: None,
247 }],
248 };
249 let repair = Message {
250 role: Role::Assistant,
251 content: vec![ContentBlock::Text {
252 text: "[tool_history_repair] synthetic recovery receipt".to_string(),
253 cache_control: None,
254 }],
255 };
256 app.extend_restored_history(&visible, history_cells_from_message(&visible));
257 app.extend_restored_history(&repair, history_cells_from_message(&repair));
258
259 let result = crate::commands::execute("/copy", &mut app);
260
261 assert!(!result.is_error);
262 assert_eq!(
263 app.clipboard.last_written_text(),
264 Some("real assistant answer")
265 );
266 }
267
268 #[test]
269 fn successful_delivery_without_recovery_file_is_explicit() {
270 let tmp = TempDir::new().expect("tempdir");
271 let unusable_home = tmp.path().join("home-file");
272 std::fs::write(&unusable_home, "not a directory").expect("home fixture");
273 let _env_lock = crate::test_support::lock_test_env();
274 let (_home, _codewhale_home) = isolate_state_home(&unusable_home);
275 let mut app = test_app();
276 app.clipboard = ClipboardHandler::for_test(false, false);
277 add_completed_assistant(&mut app, "answer");
278
279 let result = crate::commands::execute("/copy", &mut app);
280
281 assert!(!result.is_error);
282 let message = result.message.as_deref().unwrap_or_default();
283 assert!(
284 message.contains("no recovery file could be written"),
285 "{message}"
286 );
287 assert!(message.contains("/export file <path>"), "{message}");
288 }
289
290 #[test]
291 fn terminal_client_copy_is_queued_and_names_the_recovery_file() {
292 let tmp = TempDir::new().expect("tempdir");
293 let _env_lock = crate::test_support::lock_test_env();
294 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
295 let mut app = test_app();
296 app.clipboard = ClipboardHandler::for_test(true, true);
297 add_completed_assistant(&mut app, "remote answer");
298
299 let result = crate::commands::execute("/copy", &mut app);
300
301 assert!(!result.is_error);
302 let message = result.message.as_deref().unwrap_or_default();
303 assert!(message.contains("Queued"), "{message}");
304 assert!(message.contains("last-copy.md"), "{message}");
305 assert_eq!(
306 std::fs::read_to_string(tmp.path().join("exports").join("last-copy.md"))
307 .expect("recovery copy"),
308 "remote answer"
309 );
310 }
311
312 #[test]
313 fn clipboard_failure_names_the_written_recovery_file() {
314 let tmp = TempDir::new().expect("tempdir");
315 let _env_lock = crate::test_support::lock_test_env();
316 let (_home, _codewhale_home) = isolate_state_home(tmp.path());
317 let mut app = test_app();
318 app.clipboard = ClipboardHandler::unavailable_for_test(false);
319 add_completed_assistant(&mut app, "recoverable answer");
320
321 let result = crate::commands::execute("/copy", &mut app);
322
323 assert!(result.is_error);
324 let message = result.message.as_deref().unwrap_or_default();
325 assert!(message.contains("last-copy.md"), "{message}");
326 assert_eq!(
327 std::fs::read_to_string(tmp.path().join("exports").join("last-copy.md"))
328 .expect("recovery copy"),
329 "recoverable answer"
330 );
331 }
332
333 #[test]
334 fn clipboard_and_recovery_failure_explain_the_explicit_export_path() {
335 let tmp = TempDir::new().expect("tempdir");
336 let unusable_home = tmp.path().join("home-file");
337 std::fs::write(&unusable_home, "not a directory").expect("home fixture");
338 let _env_lock = crate::test_support::lock_test_env();
339 let (_home, _codewhale_home) = isolate_state_home(&unusable_home);
340 let mut app = test_app();
341 app.clipboard = ClipboardHandler::unavailable_for_test(false);
342 add_completed_assistant(&mut app, "answer");
343
344 let result = crate::commands::execute("/copy", &mut app);
345
346 assert!(result.is_error);
347 let message = result.message.as_deref().unwrap_or_default();
348 assert!(message.contains("/export file <path>"), "{message}");
349 }
350 #[test]
351 fn copy_command_failure_surfaces_the_existing_recovery_receipt_over_a_finished_turn() {
352 let tmp = TempDir::new().unwrap();
353 let _env_lock = crate::test_support::lock_test_env();
354 let (_home, _state) = isolate_state_home(tmp.path());
355 let mut app = test_app();
356 app.clipboard = ClipboardHandler::unavailable_for_test(false);
357 add_completed_assistant(&mut app, "recoverable answer");
358 let result = crate::commands::execute("/copy", &mut app);
359 assert!(result.is_error);
360 let toast = app
361 .active_status_toast(crate::tui::underwater::ShellPhase::Done)
362 .expect("copy failure must stay visible");
363 assert_eq!(toast.text, result.message.unwrap());
364 assert_eq!(toast.level, crate::tui::app::StatusToastLevel::Error);
365 assert_eq!(toast.ttl_ms, Some(8_000));
366 assert!(toast.text.contains("last-copy.md"));
367 }
368
368 lines RUST