use trusty_mpm::client::ManagedSessionSummary;
use super::{LaunchIsolation, LaunchNewRequest, shared_checkout_note, split_isolation_flag};
fn session(name: &str, state: &str, workspace_path: Option<&str>) -> ManagedSessionSummary {
ManagedSessionSummary {
id: format!("{name}-id"),
name: name.to_string(),
state: state.to_string(),
persisted_state: None,
workspace_path: workspace_path.map(str::to_string),
repo_url: None,
branch: None,
created_at: None,
last_activity_at: None,
pending_decision: None,
proposed_default: None,
source_id: None,
task: None,
cwd: None,
claude_session_id: None,
deliverable_id: None,
pane_id: None,
injection_status: None,
unresumable: false,
stale_assets: false,
stale_assets_unchecked: false,
attached: false,
slot: 1,
deleted: false,
auto_resume_parked: None,
kind: None,
}
}
#[test]
fn split_isolation_flag_bare_remainder() {
assert_eq!(
split_isolation_flag("auth refactor"),
("auth refactor", LaunchIsolation::SessionCheckout)
);
assert_eq!(
split_isolation_flag(""),
("", LaunchIsolation::SessionCheckout)
);
}
#[test]
fn split_isolation_flag_trailing() {
assert_eq!(
split_isolation_flag("auth-refactor --worktree"),
("auth-refactor", LaunchIsolation::OwnWorktree)
);
assert_eq!(
split_isolation_flag("My Auth Fix! --worktree"),
("My Auth Fix!", LaunchIsolation::OwnWorktree)
);
}
#[test]
fn split_isolation_flag_leading() {
assert_eq!(
split_isolation_flag("--worktree auth-refactor"),
("auth-refactor", LaunchIsolation::OwnWorktree)
);
}
#[test]
fn split_isolation_flag_flag_only() {
assert_eq!(
split_isolation_flag("--worktree"),
("", LaunchIsolation::OwnWorktree)
);
}
#[test]
fn split_isolation_flag_requires_a_whole_token() {
assert_eq!(
split_isolation_flag("refactor--worktree"),
("refactor--worktree", LaunchIsolation::SessionCheckout)
);
assert_eq!(
split_isolation_flag("--worktreeish name"),
("--worktreeish name", LaunchIsolation::SessionCheckout)
);
}
#[test]
fn launch_new_request_carries_name_and_isolation() {
assert_eq!(LaunchNewRequest::unnamed().name_hint, None);
assert_eq!(
LaunchNewRequest::unnamed().isolation,
LaunchIsolation::SessionCheckout
);
let req = LaunchNewRequest::named("auth").with_isolation(LaunchIsolation::OwnWorktree);
assert_eq!(req.name_hint.as_deref(), Some("auth"));
assert!(req.isolation.requests_worktree());
}
#[test]
fn shared_checkout_note_absent_for_an_empty_checkout() {
let sessions = vec![session("tm-other-01", "active", Some("/work/elsewhere"))];
assert_eq!(
shared_checkout_note(&sessions, "/work/proj", LaunchIsolation::SessionCheckout),
None
);
assert_eq!(
shared_checkout_note(&[], "/work/proj", LaunchIsolation::SessionCheckout),
None
);
}
#[test]
fn shared_checkout_note_names_the_occupants() {
let sessions = vec![
session("tm-task-a-01", "active", Some("/work/proj")),
session("tm-task-b-01", "active", Some("/work/proj")),
];
let note = shared_checkout_note(&sessions, "/work/proj", LaunchIsolation::SessionCheckout)
.expect("a launch joining two live sessions must say so");
assert!(note.contains("session 3"), "ordinal missing: {note}");
assert!(note.contains("/work/proj"), "checkout missing: {note}");
assert!(note.contains("tm-task-a-01"), "occupant missing: {note}");
assert!(note.contains("tm-task-b-01"), "occupant missing: {note}");
assert!(note.contains("--worktree"), "remedy missing: {note}");
let lowered = note.to_lowercase();
for alarm in ["warn", "collision", "race", "danger", "unsafe"] {
assert!(
!lowered.contains(alarm),
"the note must inform, not alarm — found {alarm:?} in: {note}"
);
}
}
#[test]
fn shared_checkout_note_counts_only_active_sessions() {
let sessions = vec![
session("tm-stopped-01", "stopped", Some("/work/proj")),
session("tm-live-01", "active", Some("/work/proj")),
];
let note = shared_checkout_note(&sessions, "/work/proj", LaunchIsolation::SessionCheckout)
.expect("one live occupant must still produce the note");
assert!(
note.contains("session 2"),
"ordinal must count 1 live: {note}"
);
assert!(
!note.contains("tm-stopped-01"),
"a stopped record is not standing there: {note}"
);
}
#[test]
fn shared_checkout_note_absent_when_isolation_was_requested() {
let sessions = vec![session("tm-task-a-01", "active", Some("/work/proj"))];
assert_eq!(
shared_checkout_note(&sessions, "/work/proj", LaunchIsolation::OwnWorktree),
None
);
}