use std::sync::Arc;
use std::time::Duration;
use serde_json::Value;
use tokio::time::timeout;
use super::{SessionEventsParams, open};
use crate::session::SessionRegistry;
async fn next_seq(rx: &mut trusty_common::uds::server::RpcStreamItems) -> u64 {
let frame = timeout(Duration::from_secs(5), rx.recv())
.await
.expect("a frame must arrive within the budget")
.expect("the stream must still be open")
.expect("the frame must not be a terminal error");
frame
.get("seq")
.and_then(Value::as_u64)
.expect("every session event envelope carries a seq")
}
#[tokio::test]
async fn session_events_replays_ring_buffer_then_live() {
let registry = Arc::new(SessionRegistry::new());
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
registry.send(&session.id, "before-open").unwrap();
let mut rx = open(
registry.clone(),
SessionEventsParams {
session_id: session.id.clone(),
after_seq: None,
},
)
.await
.expect("open must succeed for a live session");
let replayed = next_seq(&mut rx).await;
assert_eq!(replayed, 1, "the ring buffer replays from seq 1");
registry.send(&session.id, "after-open").unwrap();
let live = next_seq(&mut rx).await;
assert_eq!(
live,
replayed + 1,
"no gap and no duplicate between replay and live"
);
}
#[tokio::test]
async fn session_events_after_seq_skips_replayed_events() {
let registry = Arc::new(SessionRegistry::new());
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
registry.send(&session.id, "first").unwrap();
registry.send(&session.id, "second").unwrap();
let mut rx = open(
registry.clone(),
SessionEventsParams {
session_id: session.id.clone(),
after_seq: Some(1),
},
)
.await
.expect("open must succeed for a live session");
let frame = timeout(Duration::from_secs(5), rx.recv())
.await
.expect("a frame must arrive within the budget")
.expect("the stream must still be open")
.expect("the frame must not be a terminal error");
assert_eq!(
frame.get("seq").and_then(Value::as_u64),
Some(2),
"seq 1 was already confirmed and must not be replayed"
);
assert!(
!frame.to_string().contains("first"),
"nothing at or below the confirmed seq may be replayed, got: {frame}"
);
}
#[tokio::test]
async fn session_events_unknown_session_is_refused() {
let registry = Arc::new(SessionRegistry::new());
let err = open(
registry,
SessionEventsParams {
session_id: "no-such-session".to_string(),
after_seq: None,
},
)
.await
.expect_err("an unknown session must not open a stream");
assert_eq!(
err.code,
i64::from(crate::jsonrpc::RpcError::session_not_found("x").code)
);
}
#[tokio::test]
async fn session_events_stream_is_a_prompter() {
let registry = Arc::new(SessionRegistry::new());
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
assert_eq!(
registry.prompter_count(&session.id),
0,
"nobody is watching a session nobody opened a stream on"
);
let mut rx = open(
registry.clone(),
SessionEventsParams {
session_id: session.id.clone(),
after_seq: None,
},
)
.await
.expect("open must succeed for a live session");
let _first = next_seq(&mut rx).await;
assert_eq!(
registry.prompter_count(&session.id),
1,
"an events stream alone must count as a prompter, or every gated call \
on a TUI session is denied unprompted"
);
}