use super::*;
use tokio::sync::mpsc;
fn ctx() -> ConnectionContext {
let (tx, _rx) = mpsc::unbounded_channel();
ConnectionContext::new(tx)
}
async fn store_with_active_workstream() -> (
crate::workstreams::SharedWorkstreamStore,
crate::workstreams::WorkstreamId,
) {
let store = crate::workstreams::test_shared_store().await;
let id = store.lock().await.create("active").await.expect("create");
crate::workstreams::activation::activate(&store, id, false)
.await
.expect("activate");
(store, id)
}
#[tokio::test]
async fn create_binds_ambient_active_workstream() {
let registry = SessionRegistry::new();
let (workstreams, active_id) = store_with_active_workstream().await;
let value = create(®istry, &workstreams, json!({"task": "t"}), ctx())
.await
.expect("create must succeed");
assert_eq!(value["workstream_id"], active_id.to_string());
let ws = workstreams.lock().await.get(active_id).await.expect("get");
assert_eq!(
ws.session_ids,
vec![value["id"].as_str().unwrap().to_string()]
);
}
#[tokio::test]
async fn create_binds_explicit_workstream_overriding_ambient() {
let registry = SessionRegistry::new();
let (workstreams, _active_id) = store_with_active_workstream().await;
let explicit_id = workstreams
.lock()
.await
.create("explicit")
.await
.expect("create");
let value = create(
®istry,
&workstreams,
json!({"task": "t", "workstream_id": explicit_id.to_string()}),
ctx(),
)
.await
.expect("create must succeed");
assert_eq!(value["workstream_id"], explicit_id.to_string());
}
#[tokio::test]
async fn create_stays_projectless_without_explicit_or_active() {
let registry = SessionRegistry::new();
let workstreams = crate::workstreams::test_shared_store().await;
let value = create(®istry, &workstreams, json!({"task": "t"}), ctx())
.await
.expect("create must succeed");
assert!(value["workstream_id"].is_null());
}
#[tokio::test]
async fn create_rejects_closed_explicit_workstream() {
let registry = SessionRegistry::new();
let workstreams = crate::workstreams::test_shared_store().await;
let id = workstreams
.lock()
.await
.create("closed")
.await
.expect("create");
workstreams.lock().await.close(id).await.expect("close");
let err = create(
®istry,
&workstreams,
json!({"task": "t", "workstream_id": id.to_string()}),
ctx(),
)
.await
.expect_err("closed workstream must reject the bind");
assert_eq!(err.code, -32003);
}
#[tokio::test]
async fn create_rejected_bind_leaves_no_phantom_session() {
let registry = SessionRegistry::new();
let workstreams = crate::workstreams::test_shared_store().await;
let closed = workstreams
.lock()
.await
.create("closed")
.await
.expect("create");
workstreams.lock().await.close(closed).await.expect("close");
let cases: &[(&str, String, i32)] = &[
("closed workstream", closed.to_string(), -32003),
(
"unknown workstream",
crate::workstreams::WorkstreamId::new().to_string(),
-32002,
),
("malformed id", "not-a-uuid".to_string(), -32602),
];
for (label, ws_id, expected_code) in cases {
let before = registry.list().len();
let err = create(
®istry,
&workstreams,
json!({"task": "t", "workstream_id": ws_id}),
ctx(),
)
.await
.expect_err("bind must be rejected");
assert_eq!(err.code, *expected_code, "{label}: wrong error code");
assert_eq!(
registry.list().len(),
before,
"{label}: a rejected bind must not leave a phantom session"
);
}
assert_eq!(
registry.list().len(),
0,
"no session may exist after three rejected creates"
);
}