use crate::common;
use common::{OutputAssertions, TestRepo};
#[test]
fn test_detach_middle_of_stack() {
let repo = TestRepo::new();
repo.run_stax(&["status"]).assert_success();
let branches = repo.create_stack(&["detach-a", "detach-b", "detach-c"]);
repo.run_stax(&["checkout", &branches[1]]);
assert!(repo.current_branch_contains("detach-b"));
let output = repo.run_stax(&["detach", "--yes"]);
output.assert_success();
let stdout = TestRepo::stdout(&output);
assert!(
stdout.contains("Detached") || stdout.contains("detach"),
"Expected detach confirmation, got: {}",
stdout
);
let parent = repo.get_current_parent();
assert_eq!(
parent,
Some("main".to_string()),
"Detached branch should have trunk as parent"
);
repo.run_stax(&["checkout", &branches[2]]);
let c_parent = repo.get_current_parent();
assert!(
c_parent.as_ref().is_some_and(|p| p.contains("detach-a")),
"C should be reparented to A, got parent: {:?}",
c_parent
);
}
#[test]
fn test_detach_leaf_branch() {
let repo = TestRepo::new();
repo.run_stax(&["status"]).assert_success();
let branches = repo.create_stack(&["leaf-a", "leaf-b"]);
repo.run_stax(&["checkout", &branches[1]]);
let output = repo.run_stax(&["detach", "--yes"]);
output.assert_success();
let parent = repo.get_current_parent();
assert_eq!(
parent,
Some("main".to_string()),
"Detached leaf should have trunk as parent"
);
}
#[test]
fn test_detach_trunk_fails() {
let repo = TestRepo::new();
repo.run_stax(&["status"]).assert_success();
repo.create_stack(&["trunk-test"]);
repo.run_stax(&["t"]);
let output = repo.run_stax(&["detach", "--yes"]);
output.assert_failure();
let stderr = TestRepo::stderr(&output);
assert!(
stderr.contains("trunk") || stderr.contains("Cannot"),
"Expected trunk error, got stderr: {}",
stderr
);
}
#[test]
fn test_detach_preserves_pr_info() {
let repo = TestRepo::new();
repo.run_stax(&["status"]).assert_success();
let branches = repo.create_stack(&["pr-a", "pr-b", "pr-c"]);
repo.run_stax(&["checkout", &branches[1]]);
let output = repo.run_stax(&["detach", "--yes"]);
output.assert_success();
repo.run_stax(&["checkout", &branches[2]]);
let parent = repo.get_current_parent();
assert!(parent.is_some(), "C should still be tracked after detach");
}
#[test]
fn test_detach_specific_branch() {
let repo = TestRepo::new();
repo.run_stax(&["status"]).assert_success();
let branches = repo.create_stack(&["spec-a", "spec-b"]);
repo.run_stax(&["t"]);
let output = repo.run_stax(&["detach", &branches[0], "--yes"]);
output.assert_success();
}