use std::path::Path;
use std::process::Command;
use tracevault_cli::commands::context::{run_clear, run_set, run_update};
use tracevault_cli::context::{resolve_context_paths, Context, WorktreeScope};
fn init_git_repo(dir: &Path) {
let status = Command::new("git")
.args(["-C", &dir.to_string_lossy(), "init"])
.status()
.expect("git init failed");
assert!(status.success(), "git init must succeed");
for (key, val) in [
("user.email", "test@example.com"),
("user.name", "Test User"),
] {
Command::new("git")
.args(["-C", &dir.to_string_lossy(), "config", key, val])
.status()
.expect("git config failed");
}
let status = Command::new("git")
.args([
"-C",
&dir.to_string_lossy(),
"commit",
"--allow-empty",
"-m",
"init",
])
.status()
.expect("git commit failed");
assert!(status.success(), "empty init commit must succeed");
}
fn add_worktree(repo_dir: &Path, wt_dir: &Path) {
let status = Command::new("git")
.args([
"-C",
&repo_dir.to_string_lossy(),
"worktree",
"add",
"--detach",
&wt_dir.to_string_lossy(),
])
.status()
.expect("git worktree add failed");
assert!(status.success(), "git worktree add must succeed");
}
fn make_ctx(flow: Option<&str>, labels: &[&str], params: &[(&str, &str)]) -> Context {
Context {
flow_id: flow.map(str::to_string),
labels: labels.iter().map(|s| s.to_string()).collect(),
params: params
.iter()
.map(|(k, v)| (k.to_string(), Some(v.to_string())))
.collect(),
}
}
#[test]
fn resolution_from_primary_is_primary_scope() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
let paths = resolve_context_paths(&repo_dir);
let expected_tv = repo_dir.canonicalize().unwrap().join(".visdom");
assert_eq!(
paths.state_dir.canonicalize().unwrap(),
expected_tv,
"state_dir should be repo_dir/.visdom"
);
assert_eq!(
paths.scope,
WorktreeScope::Primary,
"primary checkout must yield Primary scope"
);
assert!(
paths.worktree_path().is_none(),
"Primary scope has no per-worktree path"
);
}
#[test]
fn resolution_from_linked_worktree_is_linked_scope() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
let paths = resolve_context_paths(&wt_dir);
let expected_tv = repo_dir.canonicalize().unwrap().join(".visdom");
assert_eq!(
paths.state_dir.canonicalize().unwrap(),
expected_tv,
"state_dir should always point to primary repo's .visdom/"
);
match &paths.scope {
WorktreeScope::Linked { key } => {
assert!(!key.is_empty(), "worktree key must not be empty");
let expected_wt_path = expected_tv.join("worktrees").join(key).join("context.json");
assert_eq!(
paths
.worktree_path()
.unwrap()
.canonicalize()
.unwrap_or_else(|_| paths.worktree_path().unwrap()),
expected_wt_path,
"worktree_path should be state_dir/worktrees/<key>/context.json"
);
}
WorktreeScope::Primary => panic!("linked worktree must resolve to Linked scope"),
}
}
#[test]
fn linked_worktree_key_is_basename_of_git_dir() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
add_worktree(&repo_dir, &wt_dir);
let paths = resolve_context_paths(&wt_dir);
let worktrees_dir = repo_dir
.canonicalize()
.unwrap()
.join(".git")
.join("worktrees");
if let WorktreeScope::Linked { key } = &paths.scope {
assert!(
worktrees_dir.join(key).exists(),
"key '{}' must be a valid directory under .git/worktrees/",
key
);
} else {
panic!("expected Linked scope");
}
}
#[test]
fn merge_rules_flow_labels_params_with_real_worktree() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
let global = make_ctx(Some("g"), &["a"], &[("k", "1")]);
let primary_paths = resolve_context_paths(&repo_dir);
global.save_to(&primary_paths.global_path()).unwrap();
let wt_ctx = make_ctx(Some("w"), &["b"], &[("k", "2"), ("m", "3")]);
let linked_paths = resolve_context_paths(&wt_dir);
let wt_path = linked_paths
.worktree_path()
.expect("must have a worktree path");
wt_ctx.save_to(&wt_path).unwrap();
let effective = Context::effective(&wt_dir, None);
assert_eq!(
effective.flow_id,
Some("w".to_string()),
"flow: worktree wins"
);
assert_eq!(
effective.labels,
vec!["a", "b"],
"labels: global ∪ worktree"
);
assert_eq!(effective.params["k"], "2", "params: worktree overrides k");
assert_eq!(effective.params["m"], "3", "params: worktree adds m");
}
#[test]
fn effective_from_primary_equals_global() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
let global = make_ctx(Some("g"), &["a"], &[("k", "1")]);
let paths = resolve_context_paths(&repo_dir);
global.save_to(&paths.global_path()).unwrap();
let effective = Context::effective(&repo_dir, None);
assert_eq!(
effective,
Context::merge_layers(&[&global]),
"from primary, effective == global"
);
}
#[test]
fn worktree_file_namespaced_does_not_affect_global() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
let global = make_ctx(Some("global-flow"), &["global-label"], &[]);
let primary_paths = resolve_context_paths(&repo_dir);
global.save_to(&primary_paths.global_path()).unwrap();
let wt_ctx = make_ctx(Some("wt-flow"), &["wt-label"], &[]);
let linked_paths = resolve_context_paths(&wt_dir);
wt_ctx
.save_to(&linked_paths.worktree_path().unwrap())
.unwrap();
let loaded_global = Context::load_from(&primary_paths.global_path());
assert_eq!(
loaded_global.flow_id,
Some("global-flow".to_string()),
"global file must not be touched by worktree save"
);
}
#[test]
fn git_failure_fallback_walks_up_to_tracevault() {
let tmp = tempfile::tempdir().unwrap();
let tv_dir = tmp.path().join(".visdom");
std::fs::create_dir_all(&tv_dir).unwrap();
let subdir = tmp.path().join("sub/dir");
std::fs::create_dir_all(&subdir).unwrap();
let paths = resolve_context_paths(&subdir);
let expected_tv = tmp.path().canonicalize().unwrap().join(".visdom");
let resolved_tv = paths
.state_dir
.canonicalize()
.unwrap_or_else(|_| paths.state_dir.clone());
assert_eq!(
resolved_tv, expected_tv,
"fallback should walk up to nearest .visdom/"
);
assert_eq!(
paths.scope,
WorktreeScope::Primary,
"fallback must yield Primary scope"
);
assert!(
paths.worktree_path().is_none(),
"fallback has no worktree path"
);
}
#[test]
fn git_failure_fallback_no_panic_on_no_tracevault() {
let tmp = tempfile::tempdir().unwrap();
let paths = resolve_context_paths(tmp.path());
assert_eq!(paths.scope, WorktreeScope::Primary);
assert_eq!(paths.state_dir.file_name().unwrap(), ".visdom");
}
#[test]
fn save_to_creates_nested_worktrees_dir() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
let linked_paths = resolve_context_paths(&wt_dir);
let wt_path = linked_paths
.worktree_path()
.expect("must have worktree path");
assert!(!wt_path.exists(), "file must not exist before save");
let ctx = make_ctx(Some("wt"), &["tag"], &[]);
ctx.save_to(&wt_path).unwrap();
assert!(wt_path.exists(), "save_to must create the file");
let loaded = Context::load_from(&wt_path);
assert_eq!(loaded.flow_id, Some("wt".to_string()));
}
#[test]
fn effective_with_parts_returns_all_three() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
let global = make_ctx(Some("g"), &["a"], &[("k", "1")]);
let primary_paths = resolve_context_paths(&repo_dir);
global.save_to(&primary_paths.global_path()).unwrap();
let wt_ctx = make_ctx(Some("w"), &["b"], &[("k", "99")]);
let linked_paths = resolve_context_paths(&wt_dir);
wt_ctx
.save_to(&linked_paths.worktree_path().unwrap())
.unwrap();
let (_paths, _user, loaded_global, loaded_wt, effective) =
Context::effective_with_parts(&wt_dir, None);
assert_eq!(loaded_global.flow_id, Some("g".to_string()));
assert_eq!(
loaded_wt.as_ref().and_then(|w| w.flow_id.as_deref()),
Some("w")
);
assert_eq!(effective.flow_id, Some("w".to_string()));
assert_eq!(effective.params["k"], "99");
}
#[test]
fn set_from_linked_worktree_writes_worktree_file_not_global() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
let global_before = make_ctx(Some("global-flow"), &["global-label"], &[]);
let primary_paths = resolve_context_paths(&repo_dir);
global_before.save_to(&primary_paths.global_path()).unwrap();
run_set(
&wt_dir,
Some("wt-flow".to_string()),
vec!["wt-label".to_string()],
vec![],
false, false,
)
.unwrap();
let linked_paths = resolve_context_paths(&wt_dir);
let wt_path = linked_paths.worktree_path().expect("linked scope");
assert!(
wt_path.exists(),
"per-worktree context.json must be created"
);
let wt_ctx = Context::load_from(&wt_path);
assert_eq!(wt_ctx.flow_id, Some("wt-flow".to_string()));
assert_eq!(wt_ctx.labels, vec!["wt-label"]);
let global_after = Context::load_from(&primary_paths.global_path());
assert_eq!(
global_after.flow_id,
Some("global-flow".to_string()),
"global context must not be modified by a linked-worktree set"
);
}
#[test]
fn set_global_flag_from_linked_worktree_writes_global_file() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
run_set(
&wt_dir,
Some("forced-global-flow".to_string()),
vec!["forced-label".to_string()],
vec![],
true, false,
)
.unwrap();
let primary_paths = resolve_context_paths(&repo_dir);
let global_ctx = Context::load_from(&primary_paths.global_path());
assert_eq!(
global_ctx.flow_id,
Some("forced-global-flow".to_string()),
"--global flag must write to the global file"
);
let linked_paths = resolve_context_paths(&wt_dir);
let wt_path = linked_paths.worktree_path().expect("linked scope");
assert!(
!wt_path.exists(),
"--global set must not create a per-worktree file"
);
}
#[test]
fn set_from_primary_writes_global_file() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
run_set(
&repo_dir,
Some("primary-flow".to_string()),
vec!["primary-label".to_string()],
vec![],
false,
false,
)
.unwrap();
let paths = resolve_context_paths(&repo_dir);
let global_ctx = Context::load_from(&paths.global_path());
assert_eq!(
global_ctx.flow_id,
Some("primary-flow".to_string()),
"set from primary must write to global file"
);
}
#[test]
fn linked_worktree_effective_is_merge_of_global_and_worktree() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
run_set(
&repo_dir,
Some("global-flow".to_string()),
vec!["global-label".to_string()],
vec!["shared=yes".to_string(), "global-only=1".to_string()],
false,
false,
)
.unwrap();
run_set(
&wt_dir,
Some("wt-flow".to_string()), vec!["wt-label".to_string()], vec!["shared=override".to_string(), "wt-only=2".to_string()],
false,
false,
)
.unwrap();
let effective = Context::effective(&wt_dir, None);
assert_eq!(
effective.flow_id,
Some("wt-flow".to_string()),
"worktree flow must win"
);
assert!(
effective.labels.contains(&"global-label".to_string()),
"global label must be present"
);
assert!(
effective.labels.contains(&"wt-label".to_string()),
"wt label must be present"
);
assert_eq!(
effective.params.get("shared").map(String::as_str),
Some("override")
);
assert_eq!(
effective.params.get("global-only").map(String::as_str),
Some("1")
);
assert_eq!(
effective.params.get("wt-only").map(String::as_str),
Some("2")
);
}
#[test]
fn update_from_linked_worktree_writes_worktree_file() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
run_set(
&wt_dir,
Some("initial".to_string()),
vec!["a".to_string()],
vec![],
false,
false,
)
.unwrap();
run_update(
&wt_dir,
Some("updated".to_string()),
vec!["b".to_string()],
vec![],
vec![],
vec![],
false,
false,
)
.unwrap();
let linked_paths = resolve_context_paths(&wt_dir);
let wt_ctx = Context::load_from(&linked_paths.worktree_path().unwrap());
assert_eq!(wt_ctx.flow_id, Some("updated".to_string()));
assert_eq!(wt_ctx.labels, vec!["a", "b"]);
}
#[test]
fn clear_from_linked_worktree_deletes_worktree_file_not_global() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
run_set(
&repo_dir,
Some("global-flow".to_string()),
vec!["g-label".to_string()],
vec![],
false,
false,
)
.unwrap();
run_set(
&wt_dir,
Some("wt-flow".to_string()),
vec![],
vec![],
false,
false,
)
.unwrap();
let linked_paths = resolve_context_paths(&wt_dir);
let wt_path = linked_paths
.worktree_path()
.expect("linked scope must have worktree path");
assert!(
wt_path.exists(),
"per-worktree file must exist before clear"
);
run_clear(&wt_dir, false, false).unwrap();
assert!(
!wt_path.exists(),
"clear on linked worktree must DELETE the per-worktree file, not write an empty one"
);
let wt_opt = Context::load_worktree(&linked_paths);
assert!(
wt_opt.is_none(),
"load_worktree must return None after clear (file deleted)"
);
let effective = Context::effective(&wt_dir, None);
assert_eq!(
effective.flow_id,
Some("global-flow".to_string()),
"effective must fall back to global flow after worktree clear"
);
assert_eq!(
effective.labels,
vec!["g-label"],
"effective must fall back to global labels after worktree clear"
);
let primary_paths = resolve_context_paths(&repo_dir);
let global_ctx = Context::load_from(&primary_paths.global_path());
assert_eq!(
global_ctx.flow_id,
Some("global-flow".to_string()),
"global context must not be affected by linked-worktree clear"
);
}
#[test]
fn clear_from_linked_worktree_with_no_file_is_noop() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
let linked_paths = resolve_context_paths(&wt_dir);
let wt_path = linked_paths.worktree_path().expect("linked scope");
assert!(!wt_path.exists(), "sanity: file must be absent before test");
run_clear(&wt_dir, false, false)
.expect("clear must succeed even when per-worktree file is absent");
assert!(
!wt_path.exists(),
"file must remain absent after clear on absent file"
);
assert!(
Context::load_worktree(&linked_paths).is_none(),
"load_worktree must return None after no-op clear"
);
}
#[test]
fn two_linked_worktrees_are_isolated() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_a_dir = tmp.path().join("wt_a");
let wt_b_dir = tmp.path().join("wt_b");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_a_dir);
add_worktree(&repo_dir, &wt_b_dir);
run_set(
&repo_dir,
Some("global-flow".to_string()),
vec!["global-label".to_string()],
vec!["env=prod".to_string()],
false,
false,
)
.unwrap();
run_set(
&wt_a_dir,
Some("flow-A".to_string()),
vec!["label-A".to_string()],
vec!["wt=A".to_string()],
false,
false,
)
.unwrap();
run_set(
&wt_b_dir,
Some("flow-B".to_string()),
vec!["label-B".to_string()],
vec!["wt=B".to_string()],
false,
false,
)
.unwrap();
let effective_a = Context::effective(&wt_a_dir, None);
let effective_b = Context::effective(&wt_b_dir, None);
assert_eq!(
effective_a.flow_id,
Some("flow-A".to_string()),
"worktree A must see its own flow"
);
assert!(
effective_a.labels.contains(&"label-A".to_string()),
"worktree A must see label-A"
);
assert!(
!effective_a.labels.contains(&"label-B".to_string()),
"worktree A must NOT see label-B"
);
assert_eq!(
effective_a.params.get("wt").map(String::as_str),
Some("A"),
"worktree A param wt must be A"
);
assert_eq!(
effective_b.flow_id,
Some("flow-B".to_string()),
"worktree B must see its own flow"
);
assert!(
effective_b.labels.contains(&"label-B".to_string()),
"worktree B must see label-B"
);
assert!(
!effective_b.labels.contains(&"label-A".to_string()),
"worktree B must NOT see label-A"
);
assert_eq!(
effective_b.params.get("wt").map(String::as_str),
Some("B"),
"worktree B param wt must be B"
);
assert!(
effective_a.labels.contains(&"global-label".to_string()),
"worktree A must inherit global label"
);
assert!(
effective_b.labels.contains(&"global-label".to_string()),
"worktree B must inherit global label"
);
let paths_a = resolve_context_paths(&wt_a_dir);
let paths_b = resolve_context_paths(&wt_b_dir);
let wt_path_a = paths_a
.worktree_path()
.expect("wt_a must have a worktree path");
let wt_path_b = paths_b
.worktree_path()
.expect("wt_b must have a worktree path");
assert_ne!(
wt_path_a, wt_path_b,
"worktrees A and B must use distinct context file paths"
);
assert!(wt_path_a.exists(), "worktree A context file must exist");
assert!(wt_path_b.exists(), "worktree B context file must exist");
let primary_paths = resolve_context_paths(&repo_dir);
let global_ctx = Context::load_from(&primary_paths.global_path());
assert_eq!(
global_ctx.flow_id,
Some("global-flow".to_string()),
"global context must be unchanged after worktree-scoped sets"
);
assert_eq!(
global_ctx.labels,
vec!["global-label"],
"global labels must be unchanged after worktree-scoped sets"
);
}
#[test]
fn update_remove_label_and_param_at_worktree_scope() {
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
run_set(
&repo_dir,
None,
vec!["global-only".to_string()],
vec!["g=global".to_string()],
false,
false,
)
.unwrap();
run_set(
&wt_dir,
None,
vec!["keep-label".to_string(), "drop-label".to_string()],
vec!["keep=yes".to_string(), "drop=yes".to_string()],
false,
false,
)
.unwrap();
run_update(
&wt_dir,
None,
vec![],
vec![],
vec!["drop-label".to_string()], vec!["drop".to_string()], false,
false,
)
.unwrap();
let linked_paths = resolve_context_paths(&wt_dir);
let wt_ctx = Context::load_from(&linked_paths.worktree_path().unwrap());
assert!(
wt_ctx.labels.contains(&"keep-label".to_string()),
"keep-label must remain in the worktree file"
);
assert!(
!wt_ctx.labels.contains(&"drop-label".to_string()),
"drop-label must be removed from the worktree file"
);
assert_eq!(
wt_ctx.params.get("keep"),
Some(&Some("yes".to_string())),
"keep param must remain in the worktree file"
);
assert_eq!(
wt_ctx.params.get("drop"),
Some(&None),
"drop param must be recorded as a tombstone in the worktree file"
);
let primary_paths = resolve_context_paths(&repo_dir);
let global_ctx = Context::load_from(&primary_paths.global_path());
assert_eq!(
global_ctx.labels,
vec!["global-only"],
"global labels must not be touched by worktree-scoped update"
);
assert_eq!(
global_ctx.params.get("g"),
Some(&Some("global".to_string())),
"global params must not be touched by worktree-scoped update"
);
}
#[test]
fn apply_context_stamps_effective_merged_context() {
use tracevault_cli::commands::stream::apply_context;
let tmp = tempfile::tempdir().unwrap();
let repo_dir = tmp.path().join("repo");
let wt_dir = tmp.path().join("wt");
std::fs::create_dir_all(&repo_dir).unwrap();
init_git_repo(&repo_dir);
std::fs::create_dir_all(repo_dir.join(".visdom")).unwrap();
add_worktree(&repo_dir, &wt_dir);
run_set(
&repo_dir,
Some("global-flow".to_string()),
vec!["global-label".to_string()],
vec!["env=prod".to_string()],
false,
false,
)
.unwrap();
run_set(
&wt_dir,
Some("wt-flow".to_string()),
vec!["wt-label".to_string()],
vec!["env=staging".to_string()],
false,
false,
)
.unwrap();
let effective = Context::effective(&wt_dir, None);
let (flow_id, labels, params) = apply_context(effective);
assert_eq!(
flow_id,
Some("wt-flow".to_string()),
"hook must stamp effective flow"
);
let labels = labels.expect("labels should be Some");
assert!(
labels.contains(&"global-label".to_string()),
"global label must be in hook output"
);
assert!(
labels.contains(&"wt-label".to_string()),
"wt label must be in hook output"
);
let params = params.expect("params should be Some");
assert_eq!(
params.get("env").map(String::as_str),
Some("staging"),
"worktree param must override global"
);
}