use std::path::PathBuf;
use super::manager::ManagedError;
use super::record::{ManagedSessionId, ManagedSessionState};
use super::tests::make_manager;
#[tokio::test]
async fn create_ensures_server_up_before_creating_session() {
let dir = crate::test_support::hermetic_temp_dir();
let (mgr, fake) = make_manager(&dir).await;
let record = mgr
.create(
"task".into(),
Some(PathBuf::from("/tmp/wt-server-up")),
None,
None,
None,
None,
)
.await
.expect("create must succeed when the server can be started");
assert_eq!(
*fake.ensure_server_up_calls.lock().unwrap(),
1,
"create must confirm the tmux server is up exactly once"
);
assert_eq!(
fake.create_cwd_calls.lock().unwrap().len(),
1,
"create_session must still run once the server-up guard succeeds"
);
assert_eq!(record.state, ManagedSessionState::Provisioning);
}
#[tokio::test]
async fn create_fails_loudly_when_server_cannot_start() {
let dir = crate::test_support::hermetic_temp_dir();
let (mgr, fake) = make_manager(&dir).await;
*fake.ensure_server_up_should_fail.lock().unwrap() = true;
let err = mgr
.create(
"task".into(),
Some(PathBuf::from("/tmp/wt-server-down")),
None,
None,
None,
None,
)
.await
.expect_err("create must fail when the tmux server cannot be started");
assert!(
matches!(err, ManagedError::TmuxUnavailable(_)),
"must surface as TmuxUnavailable, not a generic error: {err:?}"
);
assert_eq!(
*fake.ensure_server_up_calls.lock().unwrap(),
1,
"the server-up guard must have run (and been the thing that failed)"
);
assert!(
fake.create_cwd_calls.lock().unwrap().is_empty(),
"create_session must NEVER run when the server-up guard fails first"
);
}
#[tokio::test]
async fn create_with_reserved_name_ensures_server_up_before_creating_session() {
let dir = crate::test_support::hermetic_temp_dir();
let (mgr, fake) = make_manager(&dir).await;
mgr.create_with_reserved_name(
ManagedSessionId::new(),
"tm-reserved-server-up-01".into(),
"task".into(),
Some(PathBuf::from("/tmp/wt-reserved")),
None,
None,
None,
crate::runtime::RuntimeKind::default(),
false,
false,
)
.await
.expect("create_with_reserved_name must succeed when the server can be started");
assert_eq!(
*fake.ensure_server_up_calls.lock().unwrap(),
1,
"create_with_reserved_name must confirm the tmux server is up exactly once"
);
assert_eq!(fake.create_cwd_calls.lock().unwrap().len(), 1);
}
#[tokio::test]
async fn resume_fails_loudly_when_server_cannot_start() {
let dir = crate::test_support::hermetic_temp_dir();
let workspace_dir = crate::test_support::hermetic_temp_dir();
let (mgr, fake) = make_manager(&dir).await;
let record = mgr
.create(
"task".into(),
Some(workspace_dir.path().to_owned()),
Some("resume-server-down".into()),
Some(workspace_dir.path().to_owned()),
None,
None,
)
.await
.expect("create");
mgr.stop(&record.id).await.expect("stop");
*fake.ensure_server_up_calls.lock().unwrap() = 0;
*fake.ensure_server_up_should_fail.lock().unwrap() = true;
let creates_before = fake.create_cwd_calls.lock().unwrap().len();
let err = mgr
.resume(&record.id)
.await
.expect_err("resume must fail when the tmux server cannot be started");
assert!(
matches!(err, ManagedError::TmuxUnavailable(_)),
"must surface as TmuxUnavailable: {err:?}"
);
assert_eq!(*fake.ensure_server_up_calls.lock().unwrap(), 1);
assert_eq!(
fake.create_cwd_calls.lock().unwrap().len(),
creates_before,
"resume must never attempt to recreate the session when the \
server-up guard fails first"
);
}