//! Cards — tool cards, their diffs and what a rejected edit says. use super::*; /// Tool cards read as a sentence, not a payload, and edit results render as a line-numbered diff /// with elision instead of `@@` hunk headers. #[test] fn tool_cards_show_human_labels_and_line_numbered_diffs() { let (width, height) = (100, 24); let backend = test_backend(width, height); let mut app = create_app(); // A tool with no path/command/query key used to fall back to its raw argument JSON. let arguments = serde_json::json!({"action": "add", "subject": "wire the dock"}); let label = luxe_core::describe_call("todo", &arguments, "/project"); let mut todo = ToolActivity::running("session-a", "call-todo", "todo", label, 1); todo.state = ToolActivityState::Succeeded; todo.summary = Some("Completed".into()); app.push_convo(ConvoBlock::Tool(Box::new(ToolItem { activity: todo, expanded: false, details_toggled: false, waited_on_user_ms: 0, }))); let mut edit = ToolActivity::running("session-a", "call-edit", "edit", "src/lib.rs", 1); edit.state = ToolActivityState::Succeeded; edit.summary = Some("Completed".into()); edit.output_preview = Some( "Applied 1 edit(s) to 'src/lib.rs'\n ...\n 11 fn main() {\n-12 old();\n+12 new();\n 13 }\n ..." .into(), ); app.push_convo(ConvoBlock::Tool(Box::new(ToolItem { activity: edit, expanded: false, details_toggled: false, waited_on_user_ms: 0, }))); let buffer = render_app(&mut app, &backend); let content = buffer_to_string(&buffer); assert!(content.contains("add · wire the dock"), "human tool label: {content}"); assert!(!content.contains("\"action\""), "no raw argument JSON: {content}"); assert!(!content.contains("@@"), "no unified hunk headers on the card: {content}"); assert!(content.contains("-12"), "removed line carries its number: {content}"); assert!(content.contains("+12"), "added line carries its number: {content}"); assert!(content.contains('⋯'), "elided context is marked: {content}"); assert!(!content.contains("Completed"), "a green card already says it completed: {content}"); assert!( !content.contains("Applied 1 edit"), "the header already names the file it edited: {content}" ); } /// Builds a succeeded card for `tool`, with a body taken from the call's arguments the way the live /// projection does. pub(super) fn card_with_arguments( tool: &str, label: &str, arguments: serde_json::Value, ) -> ToolItem { let mut activity = ToolActivity::running("session-a", "call-a", tool, label, 1); activity.state = ToolActivityState::Succeeded; activity.summary = Some("Completed".into()); activity.card_body = luxe_core::tool_card_body(tool, &arguments, "/project"); ToolItem { activity, expanded: false, details_toggled: false, waited_on_user_ms: 0 } } /// A `write` shows the file it wrote, highlighted as that language, and accounts for what it hid. /// /// It used to render the tool's *result* — a wall of green `+` rows capped at the model's token /// budget, with no language to highlight and no honest count of what was cut. #[test] fn a_write_card_shows_its_content_and_admits_what_it_hid() { let backend = test_backend(100, 40); let mut app = create_app(); let content = (1..=30).map(|n| format!("let value_{n} = {n};")).collect::>().join("\n"); let mut card = card_with_arguments( "write", "src/generated.rs", serde_json::json!({"path": "src/generated.rs", "content": content}), ); card.activity.output_preview = Some("Created 'src/generated.rs' (620 bytes)".into()); app.push_convo(ConvoBlock::Tool(Box::new(card))); let rendered = buffer_to_string(&render_app(&mut app, &backend)); assert!(rendered.contains("write src/generated.rs"), "{rendered}"); assert!(rendered.contains("Created · 620 B"), "the header says what it did: {rendered}"); assert!(rendered.contains("1 let value_1 = 1;"), "content is numbered, not signed: {rendered}"); assert!(!rendered.contains("+1 let value_1"), "a write is not a wall of additions: {rendered}"); assert!( rendered.contains("20 more lines · 30 total · click or ctrl+o to expand"), "the footer accounts for the rest and names both ways in: {rendered}" ); assert!(!rendered.contains("let value_11"), "the eleventh line is past the budget: {rendered}"); } /// Ctrl+O opens every truncated card at once, and closes them again. #[test] fn toggling_tool_output_expands_every_card() { let backend = test_backend(100, 60); let mut app = create_app(); let content = (1..=20).map(|n| format!("line {n}")).collect::>().join("\n"); app.push_convo(ConvoBlock::Tool(Box::new(card_with_arguments( "write", "notes.txt", serde_json::json!({"path": "notes.txt", "content": content}), )))); let collapsed = buffer_to_string(&render_app(&mut app, &backend)); assert!(collapsed.contains("10 more lines · 20 total"), "{collapsed}"); assert!(!collapsed.contains("line 20")); app.update(Msg::Key(KeyEvent::new(KeyCode::Char('o'), KeyModifiers::CONTROL))); let expanded = buffer_to_string(&render_app(&mut app, &backend)); assert!(expanded.contains("line 20"), "ctrl+o opened the card: {expanded}"); assert!(!expanded.contains("more lines"), "nothing left to account for: {expanded}"); app.update(Msg::Key(KeyEvent::new(KeyCode::Char('o'), KeyModifiers::CONTROL))); let recollapsed = buffer_to_string(&render_app(&mut app, &backend)); assert!(recollapsed.contains("10 more lines · 20 total"), "and closed it again: {recollapsed}"); } /// A multi-line `bash` command is shown in full, and the header stops trying to preview it. #[test] fn a_multiline_command_is_shown_under_its_card() { let backend = test_backend(100, 30); let mut app = create_app(); let command = "cd /tmp && python3 - <<'PY'\nimport json\nprint(json.dumps({'ok': True}))\nPY"; app.push_convo(ConvoBlock::Tool(Box::new(card_with_arguments( "bash", &luxe_core::describe_call("bash", &serde_json::json!({"command": command}), "/project"), serde_json::json!({"command": command}), )))); let rendered = buffer_to_string(&render_app(&mut app, &backend)); assert!(rendered.contains("print(json.dumps"), "the heredoc is no longer hidden: {rendered}"); assert!( !rendered.contains("cd /tmp && python3 - <<'PY' …"), "the header's ellipsized peek would sit directly above the real thing: {rendered}" ); assert!(rendered.contains("cd /tmp && python3 - <<'PY'"), "which the body draws: {rendered}"); // A command is not a file, so it gets no line-number gutter. assert!(!rendered.contains("2 import json"), "{rendered}"); // A one-liner says everything on its header already. let mut single = create_app(); single.push_convo(ConvoBlock::Tool(Box::new(card_with_arguments( "bash", "cargo build", serde_json::json!({"command": "cargo build"}), )))); let one_line = buffer_to_string(&render_app(&mut single, &backend)); assert_eq!(one_line.matches("cargo build").count(), 1, "said once, not twice: {one_line}"); } /// A `cd` into the directory the shell already starts in never reaches the card. /// /// Most of what a model writes opens with one, and on a real project path it is fifty characters of /// nothing in front of the command the reader came for — wide enough on a narrow card to push the /// command off the end of the line. The sibling above proves the other half: a `cd` that actually /// goes somewhere is kept. #[test] fn a_card_drops_a_cd_into_the_directory_the_command_already_ran_in() { let backend = test_backend(100, 30); let root = "/home/dev/project"; let command = format!("cd {root} && cargo build --workspace"); let mut app = create_app(); app.push_convo(ConvoBlock::Tool(Box::new(card_with_arguments( "bash", &luxe_core::describe_call("bash", &serde_json::json!({ "command": command }), root), serde_json::json!({ "command": command }), )))); let rendered = buffer_to_string(&render_app(&mut app, &backend)); assert!(rendered.contains("cargo build --workspace"), "the command survives: {rendered}"); assert!(!rendered.contains("cd "), "and the no-op does not: {rendered}"); } /// A wide card draws its diff side by side; a narrow one stacks. Same rows, same numbers. #[test] fn a_card_diff_splits_when_wide_and_stacks_when_narrow() { let preview = "Applied 1 edit(s) to 'src/lib.rs'\n ...\n 11 fn main() {\n\ -12 let label: String = category.to_string();\n\ +12 let label = category.display_name();\n 13 }"; let card = |width: u16| { let mut app = create_app(); let mut edit = ToolActivity::running("session-a", "call-edit", "edit", "src/lib.rs", 1); edit.state = ToolActivityState::Succeeded; edit.output_preview = Some(preview.into()); app.push_convo(ConvoBlock::Tool(Box::new(ToolItem { activity: edit, expanded: false, details_toggled: false, waited_on_user_ms: 0, }))); buffer_to_string(&render_app(&mut app, &test_backend(width, 24))) }; // 197 columns is a full-screen terminal; the rule puts old beside new on one row. let wide = card(197); let paired = wide.lines().find(|line| line.contains("category.to_string")).expect("the replacement row"); assert!(paired.contains('│'), "a rule divides the columns: {paired:?}"); assert!( paired.contains("category.display_name"), "old and new share one row when wide: {paired:?}" ); // At 100 columns each side would hold half a line of Rust, so it stacks instead. let narrow = card(100); let removed = narrow.lines().find(|line| line.contains("category.to_string")).expect("the removal"); assert!(!removed.contains("display_name"), "a narrow card stacks: {removed:?}"); assert!(narrow.contains("-12") && narrow.contains("+12"), "both rows are there: {narrow}"); } /// A tab-indented file draws on the same columns as a space-indented one, down to the buffer. /// /// `unicode-width` scores a tab nothing and the terminal advances it to a stop, so a card that let /// one through laid its columns out against a width the screen did not agree with. The rule stepped /// right once per level of indentation, and the surplus pushed the row past the card — where the /// transcript's word wrap broke it at a space, dropped the leading padding, and stranded the tail at /// the left margin: an addition drawn inside the column reserved for removals, with no gutter and no /// continuation marker to say what it was. /// /// Through the real render path, because that is where the two measurements meet: the card lays a /// row out, and the transcript wraps whatever the card handed it. #[test] fn a_tab_indented_card_diff_draws_on_the_columns_it_was_laid_out_for() { // Svelte markup, tab-indented the way prettier writes it. Every tab starts on a stop, so the // space-indented twin below is the same file once the tabs are spent. const TABBED: &str = concat!( "Applied 1 edit(s) to 'TopbarProfileMenu.svelte'\n", " 415 \t\t\t\n", " 416 \t\t\t\n", "-417 \t\t\t\n", "-418 \t\t\t
\n", "-419 \t\t\t\t