use liminal::protocol::Frame;
use super::records::AgentHarness;
use super::test_support::TestResult;
use super::wire_tests::{Client, Exchange};
use crate::server::connection::ConnectionServices;
#[test]
fn access013_a_ticket_places_its_own_station_on_its_first_inbox_listen() -> TestResult {
let exchange = Exchange::new()?;
let address = exchange.address()?;
let agents = exchange
.services
.agents()
.ok_or("agent runtime missing")?
.clone();
assert!(
agents.registry().record("s").is_err(),
"no station for s yet"
);
let mut c = Client::connect(address, "s")?;
let answer = c.subscribe("agents.s.inbox")?;
assert!(
matches!(answer, Frame::SubscribeAck { .. }),
"the holder's own inbox is admitted: {answer:?}"
);
let record = agents.registry().record("s")?;
assert_eq!(record.harness, AgentHarness::Seat);
assert_eq!(record.owner, "s", "a seat owns its own station");
assert_eq!(
record.purpose,
"Its own station, placed by the seat itself."
);
let stations = agents.registry().records()?.len();
let mut b = Client::connect(address, "b")?;
let message = liminal::protocol::MessageEnvelope::new(
liminal::protocol::SchemaId::new([0; liminal::protocol::SchemaId::WIRE_LEN]),
liminal::protocol::CausalContext::independent(),
br#"{"text":"to s"}"#.to_vec(),
);
b.write(&Frame::new_publish(9, "agents.s.inbox", message)?)?;
let published = b.read()?;
assert!(
matches!(published, Frame::PublishAck { .. }),
"{published:?}"
);
let Frame::Deliver { envelope, .. } = c.read()? else {
return Err("the message to s did not arrive".into());
};
let body: serde_json::Value = serde_json::from_slice(&envelope.payload)?;
assert_eq!(body["text"], "to s");
let Frame::SubscribeError { message, .. } = c.subscribe("agents.d.inbox")? else {
return Err("another name was placed".into());
};
assert_eq!(message.as_deref(), Some("pass holder s is not agent d"));
c.write(&Frame::Disconnect { flags: 0 })?;
drop(c);
let mut again = Client::connect(address, "s")?;
assert!(matches!(
again.subscribe("agents.s.inbox")?,
Frame::SubscribeAck { .. }
));
assert_eq!(agents.registry().records()?.len(), stations, "placed once");
again.write(&Frame::Disconnect { flags: 0 })?;
drop(again);
drop(b);
exchange.finish()
}
#[test]
fn access013_under_enforce_a_seat_s_own_first_listen_is_admitted_on_identity() -> TestResult {
let exchange = Exchange::new()?;
let address = exchange.address()?;
let agents = exchange
.services
.agents()
.ok_or("agent runtime missing")?
.clone();
let mut d = Client::connect(address, "d")?;
let answer = d.subscribe("agents.d.inbox")?;
assert!(
matches!(answer, Frame::SubscribeAck { .. }),
"a pass with no rights still listens its own inbox under enforce: {answer:?}"
);
assert_eq!(agents.registry().record("d")?.harness, AgentHarness::Seat);
let Frame::SubscribeError { .. } = d.subscribe("agents.s.inbox")? else {
return Err("identity admitted another seat's inbox".into());
};
d.write(&Frame::Disconnect { flags: 0 })?;
drop(d);
exchange.finish()
}