Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
test(prompt): read the prompt the LLM receives, not --show-prompt's stdout (#4207)
Maximilian Roos5d994f93cf90
diff --git a/src/commands/picker/summary.rs b/src/commands/picker/summary.rsindex 18e8a8b97..b368de60f 100644--- a/src/commands/picker/summary.rs+++ b/src/commands/picker/summary.rs@@ -640,6 +640,54 @@ mod tests {assert_eq!(summary, "Add new file");}+ /// The branch summary's diff goes into the prompt, so the user's diff+ /// display settings must not change what the LLM receives. The prompt is+ /// read from the command's stdin: it never reaches a terminal, so nothing+ /// downstream would strip an escape byte that leaked into it.+ #[test]+ fn test_generate_summary_prompt_ignores_diff_display_config() {+ use crate::summary::CachedSummary;+ use path_slash::PathExt as _;++ let (t, repo, head) = temp_repo_with_feature();+ let capture = t.path().join(".git").join("summary-prompt");+ let capture_arg = capture.to_slash_lossy();+ assert!(+ !capture_arg.contains('\''),+ "capture path must not contain single quotes: {capture_arg}"+ );+ let command = format!("cat > '{capture_arg}'; echo 'Add new file'");++ let prompt = || {+ CachedSummary::clear_all(&repo).unwrap();+ let summary =+ crate::summary::generate_summary("feature", &head, Some(t.path()), &command, &repo);+ assert_eq!(summary, "Add new file");+ fs::read(&capture).unwrap()+ };++ let default_prompt = prompt();+ assert!(+ !default_prompt.is_empty(),+ "the summary command must have received a prompt to compare"+ );++ for (key, value) in [+ ("color.ui", "always"),+ ("diff.external", "echo"),+ ("diff.noprefix", "true"),+ ] {+ repo.run_command(&["config", key, value]).unwrap();+ }++ let configured_prompt = prompt();+ assert!(+ !configured_prompt.contains(&0x1b),+ "under `color.ui = always` the summary prompt must carry no escape bytes"+ );+ assert_eq!(configured_prompt, default_prompt);+ }+#[test]fn test_generate_summary_caches_result() {let (t, repo, head) = temp_repo_with_feature();diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 4d324c0ee..5ec7fad2c 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -3175,28 +3175,65 @@ fn test_squash_prompt_covers_what_its_run_would_commit(repo_with_multi_commit_fe/// The commit and squash prompts split git's diff into per-file sections, so/// the user's diff display settings must not change what the LLM receives.-#[rstest]-fn test_show_prompt_ignores_diff_display_config(repo_with_multi_commit_feature: TestRepo) {+///+/// The prompt is captured from the generation command's stdin rather than from+/// `--show-prompt`'s stdout, which is where the `color.ui` case has to be read:+/// `.output()` pipes stdout, anstream strips ANSI on a pipe, and the two runs+/// then agree byte for byte whether or not the diff is colored. Reading the+/// bytes `wt` actually hands the command is the only place the difference+/// survives. `--dry-run` stages into a temp index, so neither run mutates the+/// repository the other reads.+#[rstest]+fn test_generation_prompt_ignores_diff_display_config(repo_with_multi_commit_feature: TestRepo) {let repo = repo_with_multi_commit_feature;- let feature_wt = repo.worktree_path("feature");+ let feature_wt = repo.worktree_path("feature").to_path_buf();fs::write(feature_wt.join("staged.txt"), "staged content\n").unwrap();repo.git_command().args(["add", "staged.txt"])- .current_dir(feature_wt)+ .current_dir(&feature_wt).run().unwrap();- let prompts = || {- ["commit", "squash"].map(|step| {- let output =- make_snapshot_cmd(&repo, "step", &[step, "--show-prompt"], Some(feature_wt))- .output()- .unwrap();- assert!(output.status.success(), "{output:?}");- String::from_utf8(output.stdout).unwrap()- })++ // Captures live under `.git/`, which no diff `wt` builds ever reads.+ let capture_dir = repo.root_path().join(".git");+ let capture_for = |label: &str, step: &str| capture_dir.join(format!("prompt-{label}-{step}"));++ // One config write per step, each pointing `commit.generation.command` at+ // that step's own capture path, so the two prompts can't overwrite each+ // other.+ let prompts = |label: &str| {+ for step in ["commit", "squash"] {+ let target = capture_for(label, step);+ let target = target.to_slash_lossy().replace('\\', r"\\");+ assert!(+ !target.contains('\''),+ "capture path must not contain single quotes: {target}"+ );+ repo.write_test_config(&format!(+ "\n[commit.generation]\ncommand = \"cat > '{target}'; echo summary\"\n"+ ));+ let output = make_snapshot_cmd(+ &repo,+ "step",+ &[step, "--dry-run", "--yes"],+ Some(&feature_wt),+ )+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "{step} --dry-run failed: {output:?}"+ );+ }+ ["commit", "squash"].map(|step| fs::read(capture_for(label, step)).unwrap())};- let default_prompts = prompts();+ let default_prompts = prompts("default");+ assert!(+ !default_prompts[0].is_empty() && !default_prompts[1].is_empty(),+ "the generation command must have received a prompt to compare"+ );+for (key, value) in [("color.ui", "always"),("diff.external", "echo"),@@ -3205,7 +3242,14 @@ fn test_show_prompt_ignores_diff_display_config(repo_with_multi_commit_feature:repo.run_git(&["config", key, value]);}- assert_eq!(prompts(), default_prompts);+ let configured_prompts = prompts("configured");+ for (step, prompt) in ["commit", "squash"].iter().zip(&configured_prompts) {+ assert!(+ !prompt.contains(&0x1b),+ "under `color.ui = always` the {step} prompt must carry no escape bytes"+ );+ }+ assert_eq!(configured_prompts, default_prompts);}#[rstest]