mod support;
use std::time::Duration;
use serde_json::json;
use support::{
StdioSession, assert_envelopes_contiguous, find_response, find_session_event, open_sse,
parse_sse_frames, read_sse_until, spawn_http_daemon,
};
#[tokio::test]
async fn session_lifecycle_over_stdio() {
let mut daemon = StdioSession::spawn();
let create_resp = daemon
.call(1, "session.create", json!({"task": "e2e stdio task"}))
.await;
assert!(
create_resp["error"].is_null(),
"create failed: {create_resp}"
);
let session_id = create_resp["result"]["id"]
.as_str()
.expect("session.create must return an id")
.to_string();
assert_eq!(create_resp["result"]["status"], "running");
let attach_resp = daemon
.call(2, "session.attach", json!({"session_id": session_id}))
.await;
assert!(
attach_resp["error"].is_null(),
"attach failed: {attach_resp}"
);
let replay = attach_resp["result"]["events"]
.as_array()
.expect("attach must return a replay events array")
.clone();
let (first_seq, last_seq) = assert_envelopes_contiguous(&replay);
assert_eq!(first_seq, 1);
assert_eq!(last_seq, 2);
let kinds: Vec<&str> = replay.iter().map(|e| e["kind"].as_str().unwrap()).collect();
assert_eq!(kinds, vec!["session_started", "session_status_changed"]);
assert_eq!(
attach_resp["result"]["stream_url"],
format!("/sessions/{session_id}/events")
);
daemon
.write_request(
3,
"session.send",
json!({"session_id": session_id, "input": "hello"}),
)
.await;
let mut got_ack = false;
let mut input_envelope = None;
for line in daemon.read_lines(2).await {
if let Some(resp) = find_response(&line, 3) {
assert!(resp["error"].is_null(), "session.send failed: {resp}");
assert_eq!(resp["result"]["acknowledged"], true);
got_ack = true;
}
if let Some(envelope) = find_session_event(&line, &session_id) {
input_envelope = Some(envelope);
}
}
assert!(got_ack, "never saw the session.send acknowledgement");
let input_envelope = input_envelope.expect("never saw the streamed session_input event");
assert_envelopes_contiguous(std::slice::from_ref(&input_envelope));
assert_eq!(input_envelope["seq"], last_seq + 1);
assert_eq!(input_envelope["kind"], "session_input");
assert_eq!(input_envelope["event"]["input"], "hello");
let detach_resp = daemon
.call(4, "session.detach", json!({"session_id": session_id}))
.await;
assert!(
detach_resp["error"].is_null(),
"detach failed: {detach_resp}"
);
let status_resp = daemon
.call(5, "session.status", json!({"session_id": session_id}))
.await;
assert!(
status_resp["error"].is_null(),
"status failed: {status_resp}"
);
assert_eq!(status_resp["result"]["status"], "running");
assert_eq!(status_resp["result"]["id"], session_id);
let reattach_resp = daemon
.call(6, "session.attach", json!({"session_id": session_id}))
.await;
assert!(
reattach_resp["error"].is_null(),
"re-attach failed: {reattach_resp}"
);
let reattach_replay = reattach_resp["result"]["events"]
.as_array()
.unwrap()
.clone();
let (re_first, re_last) = assert_envelopes_contiguous(&reattach_replay);
assert_eq!(re_first, 1);
assert_eq!(re_last, 3);
daemon
.write_request(7, "session.cancel", json!({"session_id": session_id}))
.await;
let mut cancel_events = Vec::new();
let mut got_cancel_ack = false;
for line in daemon.read_lines(4).await {
if let Some(resp) = find_response(&line, 7) {
assert!(resp["error"].is_null(), "session.cancel failed: {resp}");
assert_eq!(resp["result"]["status"], "cancelled");
got_cancel_ack = true;
}
if let Some(envelope) = find_session_event(&line, &session_id) {
cancel_events.push(envelope);
}
}
assert!(
got_cancel_ack,
"never saw the session.cancel acknowledgement"
);
assert_eq!(
cancel_events.len(),
3,
"expected status_changed + session_cancelled + session_done: {cancel_events:?}"
);
let (cancel_first, cancel_last) = assert_envelopes_contiguous(&cancel_events);
assert_eq!(
cancel_first,
re_last + 1,
"cancel events must continue the seq with no gap after re-attach's replay"
);
assert_eq!(cancel_last, re_last + 3);
let cancel_kinds: Vec<&str> = cancel_events
.iter()
.map(|e| e["kind"].as_str().unwrap())
.collect();
assert_eq!(
cancel_kinds,
vec![
"session_status_changed",
"session_cancelled",
"session_done"
]
);
let mut full_history = replay;
full_history.push(input_envelope);
full_history.extend(cancel_events);
let (full_first, full_last) = assert_envelopes_contiguous(&full_history);
assert_eq!(full_first, 1);
assert_eq!(full_last, 6);
daemon.shutdown_via_eof_and_assert_clean_exit().await;
}
#[tokio::test]
async fn session_lifecycle_over_http_with_sse() {
let daemon = spawn_http_daemon().await;
let client = reqwest::Client::new();
let rpc_url = format!("{}/rpc", daemon.base_url);
let create_resp: serde_json::Value = client
.post(&rpc_url)
.json(&json!({"jsonrpc": "2.0", "id": 1, "method": "session.create", "params": {"task": "e2e http task"}}))
.send()
.await
.expect("POST session.create")
.json()
.await
.expect("parse session.create response");
assert!(
create_resp["error"].is_null(),
"create failed: {create_resp}"
);
let session_id = create_resp["result"]["id"].as_str().unwrap().to_string();
let events_url = format!("{}/sessions/{session_id}/events", daemon.base_url);
let mut sse = open_sse(&client, &events_url).await;
let mut buffer = Vec::new();
read_sse_until(
&mut sse,
&mut buffer,
"session_status_changed",
Duration::from_secs(5),
)
.await;
let replay_frames = parse_sse_frames(&String::from_utf8_lossy(&buffer));
let (first_seq, last_seq) = assert_envelopes_contiguous(&replay_frames);
assert_eq!(first_seq, 1);
assert_eq!(last_seq, 2);
let kinds: Vec<&str> = replay_frames
.iter()
.map(|e| e["kind"].as_str().unwrap())
.collect();
assert_eq!(kinds, vec!["session_started", "session_status_changed"]);
let send_resp: serde_json::Value = client
.post(&rpc_url)
.json(&json!({"jsonrpc": "2.0", "id": 2, "method": "session.send", "params": {"session_id": session_id, "input": "hello-http"}}))
.send()
.await
.expect("POST session.send")
.json()
.await
.expect("parse session.send response");
assert!(send_resp["error"].is_null(), "send failed: {send_resp}");
assert_eq!(send_resp["result"]["acknowledged"], true);
read_sse_until(&mut sse, &mut buffer, "hello-http", Duration::from_secs(5)).await;
let all_frames = parse_sse_frames(&String::from_utf8_lossy(&buffer));
let (full_first, full_last) = assert_envelopes_contiguous(&all_frames);
assert_eq!(full_first, 1);
assert_eq!(
full_last, 3,
"the live event must continue the replay's seq with no gap"
);
assert_eq!(all_frames.last().unwrap()["kind"], "session_input");
assert_eq!(all_frames.last().unwrap()["event"]["input"], "hello-http");
let cancel_resp: serde_json::Value = client
.post(&rpc_url)
.json(&json!({"jsonrpc": "2.0", "id": 3, "method": "session.cancel", "params": {"session_id": session_id}}))
.send()
.await
.expect("POST session.cancel")
.json()
.await
.expect("parse session.cancel response");
assert!(
cancel_resp["error"].is_null(),
"cancel failed: {cancel_resp}"
);
assert_eq!(cancel_resp["result"]["status"], "cancelled");
read_sse_until(
&mut sse,
&mut buffer,
"session_done",
Duration::from_secs(5),
)
.await;
let final_frames = parse_sse_frames(&String::from_utf8_lossy(&buffer));
let (_, cancel_last) = assert_envelopes_contiguous(&final_frames);
assert_eq!(cancel_last, 6);
let final_kinds: Vec<&str> = final_frames
.iter()
.map(|e| e["kind"].as_str().unwrap())
.collect();
assert_eq!(
final_kinds,
vec![
"session_started",
"session_status_changed",
"session_input",
"session_status_changed",
"session_cancelled",
"session_done",
]
);
drop(sse);
daemon.shutdown_via_sigterm_and_assert_clean_exit().await;
}