Consumer half of ruling eureka-body:ruling:addressed-delivery-only. A delivery is for a Persona when it is a message and Matrix says direct/tag/reply, or its mentions or text name her (address::is_for); notices, edits, @room and a nil basis (unproven) wake no one on their own. The heartbeat's off-beat wake keeps its sender gate. The Persona acks the leading run of what is not hers in one ack and a live session is told only what is hers. A mouth that does not serve the v2 request is asked the v1 way. Golden bytes pinned from Bifrost tests/fixtures/persona-chat-v5; the public-surface snapshot is regenerated. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
306 lines
17 KiB
Rust
306 lines
17 KiB
Rust
//! The chat transport against a real cultnet-rs RUDP server speaking the mouth's
|
|
//! documents: the mouth's golden fixtures are what the server expects to read
|
|
//! and what it answers with, so the client is held to the mouth's bytes in both
|
|
//! directions. The server is the mouth's behaviour (Bifrost
|
|
//! `startChatEndpoint`): it answers a pending request with a pending document,
|
|
//! a send with a receipt, an ack with nothing, and a request for another
|
|
//! Persona with nothing.
|
|
|
|
use std::net::{SocketAddr, UdpSocket};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::{Arc, Mutex};
|
|
use std::thread::JoinHandle;
|
|
use std::time::Duration;
|
|
|
|
use cultnet_rs::{CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding, CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions};
|
|
use mind_body::chat_client::{self, Basis, Form, ChatRefusal, MouthRefused, Refusal, REFUSAL, ACK, Chat, ChatAck, ChatOpenDirectReceipt, ChatOpenDirectRequest, DirectOutcome, OPEN_DIRECT_RECEIPT, OPEN_DIRECT_REQUEST, ChatPending, ChatPendingRequest, ChatRead, ChatSendReceipt, ChatSendRequest, PENDING, PENDING_REQUEST, PENDING_REQUEST_V1, PENDING_V1, PENDING_V2, READ, SEND_RECEIPT, SEND_REQUEST};
|
|
use mind_body::chat_rudp::{RudpChat, connection_id};
|
|
|
|
const ACK_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v1/gamecult.persona.chat_ack.v1.msgpack");
|
|
const PENDING_V1_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v1/gamecult.persona.chat_pending.v1.msgpack");
|
|
const PENDING_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v5/gamecult.persona.chat_pending.v3.tag.msgpack");
|
|
const PENDING_V2_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v3/gamecult.persona.chat_pending.v2.image.msgpack");
|
|
const PENDING_REQUEST_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v5/gamecult.persona.chat_pending_request.v2.msgpack");
|
|
const PENDING_REQUEST_V1_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v1/gamecult.persona.chat_pending_request.v1.msgpack");
|
|
const SEND_RECEIPT_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v2/gamecult.persona.chat_send_receipt.v2.sent.msgpack");
|
|
const SEND_REQUEST_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v2/gamecult.persona.chat_send_request.v2.nil-basis.msgpack");
|
|
const OPEN_REQUEST_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v4/gamecult.persona.chat_open_direct_request.v1.msgpack");
|
|
const UNKNOWN_KIND_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v4/gamecult.persona.chat_refusal.v1.unknown-kind.msgpack");
|
|
const REFUSAL_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v1/gamecult.persona.chat_refusal.v1.msgpack");
|
|
const OPENED_BYTES: &[u8] = include_bytes!("fixtures/persona-chat-v4/gamecult.persona.chat_open_direct_receipt.v1.opened.msgpack");
|
|
|
|
const KEY: &str = "connection-key-for-test";
|
|
const TAG: &str = "cultnet.transport.rudp.v0";
|
|
|
|
/// What a fixture mouth answers an open with: the document kind, a record key (none: the request's own), and its bytes.
|
|
type OpenReply = (&'static str, Option<&'static str>, &'static [u8]);
|
|
|
|
type Seen = Arc<Mutex<Vec<(String, Vec<u8>)>>>;
|
|
|
|
struct Mouth {
|
|
address: SocketAddr,
|
|
seen: Seen,
|
|
stop: Arc<AtomicBool>,
|
|
thread: Option<JoinHandle<()>>,
|
|
}
|
|
|
|
impl Drop for Mouth {
|
|
fn drop(&mut self) {
|
|
self.stop.store(true, Ordering::SeqCst);
|
|
if let Some(thread) = self.thread.take() {
|
|
let _ = thread.join();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn answer(kind: &str, record_key: &str, payload: &[u8]) -> CultNetMessage {
|
|
CultNetMessage::DocumentPutRaw {
|
|
message_id: format!("{kind}:{record_key}"),
|
|
document: CultNetRawDocumentRecord {
|
|
schema_id: kind.into(),
|
|
record_key: record_key.into(),
|
|
stored_at: "2026-10-29T00:00:00.000Z".into(),
|
|
payload_encoding: CultNetRawPayloadEncoding::Messagepack,
|
|
payload: payload.to_vec(),
|
|
source_runtime_id: Some("bifrost-persona-mouth-eureka".into()),
|
|
source_agent_id: Some("eureka".into()),
|
|
source_role: Some("bifrost-persona-matrix-mouth".into()),
|
|
tags: Some(vec![TAG.into()]),
|
|
},
|
|
}
|
|
}
|
|
|
|
/// The mouth of the Persona `eureka`, whose connection id derives from `key`.
|
|
fn mouth(key: &str) -> Mouth {
|
|
mouth_at(key, "127.0.0.1:0")
|
|
}
|
|
|
|
/// A mouth that has no image field yet: it answers a pending request in the v1 kind, and does not serve the v2 request.
|
|
fn legacy_mouth(key: &str) -> Mouth {
|
|
serve(key, "127.0.0.1:0", PENDING_V1, PENDING_V1_BYTES, (OPEN_DIRECT_RECEIPT, None, OPENED_BYTES))
|
|
}
|
|
|
|
/// A mouth that has the image field and not the addressing facts: it answers the v1 request in the v2 kind, and does not serve the v2 request.
|
|
fn imaged_mouth(key: &str) -> Mouth {
|
|
serve(key, "127.0.0.1:0", PENDING_V2, PENDING_V2_BYTES, (OPEN_DIRECT_RECEIPT, None, OPENED_BYTES))
|
|
}
|
|
|
|
/// The same mouth on a chosen address: a restart binds the address the old one held.
|
|
fn mouth_at(key: &str, bind: &str) -> Mouth {
|
|
serve(key, bind, PENDING, PENDING_BYTES, (OPEN_DIRECT_RECEIPT, None, OPENED_BYTES))
|
|
}
|
|
|
|
fn serve(key: &str, bind: &str, pending_kind: &'static str, pending_bytes: &'static [u8], open: OpenReply) -> Mouth {
|
|
let socket = UdpSocket::bind(bind).unwrap();
|
|
socket.set_read_timeout(Some(Duration::from_millis(20))).unwrap();
|
|
let address = socket.local_addr().unwrap();
|
|
let mut server = CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::server("mouth-test", socket, connection_id(key))).unwrap();
|
|
let (seen, stop) = (Seen::default(), Arc::new(AtomicBool::new(false)));
|
|
let (log, halt) = (seen.clone(), stop.clone());
|
|
let thread = std::thread::spawn(move || {
|
|
while !halt.load(Ordering::SeqCst) {
|
|
let Ok(Some(CultNetMessage::DocumentPutRaw { document, .. })) = server.receive_schema_message_once() else {
|
|
let _ = server.poll_resends();
|
|
continue;
|
|
};
|
|
assert_eq!(document.tags.as_deref(), Some(&[TAG.to_string()][..]), "the mouth refuses a document whose tag is not its own");
|
|
assert_eq!(document.payload_encoding, CultNetRawPayloadEncoding::Messagepack);
|
|
log.lock().unwrap().push((document.schema_id.clone(), document.payload.clone()));
|
|
let for_eureka = |persona: &str| persona == "eureka";
|
|
let reply = match document.schema_id.as_str() {
|
|
PENDING_REQUEST if for_eureka(&chat_client::decode::<ChatPendingRequest>(&document.payload).unwrap().persona) => {
|
|
Some(if pending_kind == PENDING { answer(pending_kind, "eureka", pending_bytes) } else { answer(REFUSAL, "eureka", UNKNOWN_KIND_BYTES) })
|
|
}
|
|
PENDING_REQUEST_V1 if pending_kind != PENDING && for_eureka(&chat_client::decode::<ChatPendingRequest>(&document.payload).unwrap().persona) => Some(answer(pending_kind, "eureka", pending_bytes)),
|
|
SEND_REQUEST if for_eureka(&chat_client::decode::<ChatSendRequest>(&document.payload).unwrap().persona) => Some(answer(SEND_RECEIPT, &document.record_key, SEND_RECEIPT_BYTES)),
|
|
OPEN_DIRECT_REQUEST if for_eureka(&chat_client::decode::<ChatOpenDirectRequest>(&document.payload).unwrap().persona) => Some(answer(open.0, open.1.unwrap_or(&document.record_key), open.2)),
|
|
_ => None,
|
|
};
|
|
if let Some(reply) = reply {
|
|
server.send_schema_message(&reply).unwrap();
|
|
}
|
|
}
|
|
});
|
|
Mouth { address, seen, stop, thread: Some(thread) }
|
|
}
|
|
|
|
fn client(mouth: &Mouth, key: &str, persona: &str, timeout: Duration) -> RudpChat {
|
|
RudpChat::new(mouth.address, connection_id(key), persona, timeout)
|
|
}
|
|
|
|
#[test]
|
|
fn the_connection_id_is_the_first_four_bytes_of_the_trimmed_keys_sha256_as_the_mouth_derives_it() {
|
|
// Node: createHash("sha256").update("connection-key-for-test").digest().readUInt32BE(0)
|
|
assert_eq!(connection_id(KEY), 3_188_925_986);
|
|
assert_eq!(connection_id(&format!(" {KEY}\r\n")), 3_188_925_986, "the key file's whitespace is trimmed, as the mouth reads it");
|
|
assert_ne!(connection_id("another-key"), connection_id(KEY));
|
|
}
|
|
|
|
#[test]
|
|
fn pending_send_and_ack_cross_a_real_rudp_session_in_the_mouths_bytes() {
|
|
let mouth = mouth(KEY);
|
|
let chat = client(&mouth, KEY, "eureka", Duration::from_secs(5));
|
|
|
|
let deliveries = chat.pending(3).unwrap();
|
|
assert_eq!(deliveries, chat_client::decode::<ChatPending>(PENDING_BYTES).unwrap().deliveries);
|
|
assert_eq!(deliveries[0].seq, 7);
|
|
|
|
let request = chat_client::decode::<ChatSendRequest>(SEND_REQUEST_BYTES).unwrap();
|
|
let receipt = chat.send(&request).unwrap();
|
|
assert_eq!(receipt, chat_client::decode::<ChatSendReceipt>(SEND_RECEIPT_BYTES).unwrap());
|
|
|
|
chat.ack(chat_client::decode::<ChatAck>(ACK_BYTES).unwrap().through_seq).unwrap();
|
|
|
|
// What the mouth read is its fixtures, byte for byte, in the order they were made.
|
|
let seen = mouth.seen.lock().unwrap().clone();
|
|
assert_eq!(
|
|
seen,
|
|
vec![
|
|
(PENDING_REQUEST.to_string(), PENDING_REQUEST_BYTES.to_vec()),
|
|
(SEND_REQUEST.to_string(), SEND_REQUEST_BYTES.to_vec()),
|
|
(ACK.to_string(), ACK_BYTES.to_vec()),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_read_reaches_the_mouth_as_the_positional_tuple_persona_room_event_instant() {
|
|
let mouth = mouth(KEY);
|
|
let chat = client(&mouth, KEY, "eureka", Duration::from_secs(5));
|
|
let read = ChatRead { persona: "eureka".into(), room: "!abc:gamecult.org".into(), event_id: "$e1".into(), at_unix_ms: 1_761_700_000_000 };
|
|
chat.read(&read).unwrap();
|
|
let seen = mouth.seen.lock().unwrap().clone();
|
|
assert_eq!(seen.len(), 1);
|
|
assert_eq!(seen[0].0, READ);
|
|
assert_eq!(chat_client::decode::<ChatRead>(&seen[0].1).unwrap(), read);
|
|
let tuple: serde_json::Value = rmp_serde::from_slice(&seen[0].1).unwrap();
|
|
assert_eq!(tuple, serde_json::json!(["eureka", "!abc:gamecult.org", "$e1", 1_761_700_000_000u64]), "a positional tuple, not a map");
|
|
}
|
|
|
|
#[test]
|
|
fn a_peer_without_the_credential_cannot_open_the_session() {
|
|
let mouth = mouth(KEY);
|
|
let chat = client(&mouth, "not-the-key", "eureka", Duration::from_millis(600));
|
|
assert!(chat.pending(0).is_err());
|
|
assert!(mouth.seen.lock().unwrap().is_empty(), "nothing reached the mouth");
|
|
}
|
|
|
|
#[test]
|
|
fn a_request_the_mouth_does_not_answer_ends_in_an_error_not_a_hang() {
|
|
let mouth = mouth(KEY);
|
|
let chat = client(&mouth, KEY, "someone-else", Duration::from_millis(600));
|
|
assert!(chat.pending(0).is_err(), "the mouth answers only its own Persona");
|
|
let sent = ChatSendRequest { request_id: "r".into(), persona: "someone-else".into(), room_alias: "#a:b".into(), text: "x".into(), basis: None };
|
|
assert!(chat.send(&sent).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn a_client_recovers_from_a_restarted_mouth_on_its_next_call() {
|
|
let first = mouth(KEY);
|
|
let address = first.address;
|
|
let chat = client(&first, KEY, "eureka", Duration::from_millis(1500));
|
|
assert!(chat.pending(0).is_ok());
|
|
drop(first);
|
|
let second = mouth_at(KEY, &address.to_string());
|
|
assert!(chat.pending(0).is_err(), "the restarted mouth ignores a link it never saw: the call times out");
|
|
assert_eq!(chat.pending(0).unwrap(), chat_client::decode::<ChatPending>(PENDING_BYTES).unwrap().deliveries, "the next call opens a fresh link");
|
|
assert_eq!(chat.pending(0).unwrap().len(), 1, "and keeps it");
|
|
let reads = second.seen.lock().unwrap().iter().filter(|(kind, _)| kind == PENDING_REQUEST).count();
|
|
assert_eq!(reads, 2, "the restarted mouth read the two requests that reached it on a fresh link");
|
|
}
|
|
|
|
#[test]
|
|
fn a_send_that_timed_out_is_not_repeated_by_the_client() {
|
|
let mouth = mouth(KEY);
|
|
let chat = client(&mouth, KEY, "someone-else", Duration::from_millis(600));
|
|
let sent = ChatSendRequest { request_id: "r-1".into(), persona: "someone-else".into(), room_alias: "#a:b".into(), text: "x".into(), basis: None };
|
|
assert!(chat.send(&sent).is_err());
|
|
chat.ack(1).unwrap();
|
|
let sends: Vec<_> = mouth.seen.lock().unwrap().iter().filter(|(kind, _)| kind == SEND_REQUEST).map(|(_, payload)| chat_client::decode::<ChatSendRequest>(payload).unwrap().request_id).collect();
|
|
assert_eq!(sends, ["r-1"], "the send reached the mouth once; a repeat is the caller's, under the same request id");
|
|
}
|
|
|
|
#[test]
|
|
fn a_mouth_without_the_v2_request_is_asked_the_v1_way_and_its_deliveries_carry_no_addressing_facts() {
|
|
for (mouth, text) in [(legacy_mouth(KEY), "v1 answer"), (imaged_mouth(KEY), "v2 answer")] {
|
|
let old = client(&mouth, KEY, "eureka", Duration::from_millis(1500)).pending(3).unwrap();
|
|
assert_eq!(old.len(), 1, "{text}");
|
|
assert_eq!((old[0].basis, old[0].form), (None, Form::Message), "{text}");
|
|
let seen: Vec<String> = mouth.seen.lock().unwrap().iter().map(|(kind, _)| kind.clone()).collect();
|
|
assert_eq!(seen, [PENDING_REQUEST, PENDING_REQUEST_V1], "{text}: the v2 request first, refused as an unknown kind, then the v1 request");
|
|
assert_eq!(mouth.seen.lock().unwrap()[1].1, PENDING_REQUEST_V1_BYTES, "{text}");
|
|
}
|
|
assert!(client(&imaged_mouth(KEY), KEY, "eureka", Duration::from_millis(1500)).pending(0).unwrap()[0].image.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn a_current_mouth_answers_the_v2_request_with_basis_and_form() {
|
|
let current = mouth(KEY);
|
|
let new = client(¤t, KEY, "eureka", Duration::from_millis(1500)).pending(3).unwrap();
|
|
assert_eq!((new[0].basis, new[0].form), (Some(Basis::Tag), Form::Message));
|
|
let kinds: Vec<String> = current.seen.lock().unwrap().iter().map(|(kind, _)| kind.clone()).collect();
|
|
assert_eq!(kinds, [PENDING_REQUEST], "no v1 request follows an answered v2 request");
|
|
}
|
|
|
|
#[test]
|
|
fn an_open_crosses_a_real_rudp_session_in_the_mouths_bytes_and_a_receipt_for_another_open_is_refused() {
|
|
let mouth = mouth(KEY);
|
|
let chat = client(&mouth, KEY, "eureka", Duration::from_secs(5));
|
|
let request = chat_client::decode::<ChatOpenDirectRequest>(OPEN_REQUEST_BYTES).unwrap();
|
|
let receipt = chat.open_direct(&request).unwrap();
|
|
assert_eq!(receipt, chat_client::decode::<ChatOpenDirectReceipt>(OPENED_BYTES).unwrap());
|
|
assert_eq!(receipt.outcome, DirectOutcome::Opened);
|
|
|
|
let another = ChatOpenDirectRequest { request_id: "req-other".into(), ..request };
|
|
assert!(chat.open_direct(&another).is_err(), "the answer names req-1, not req-other");
|
|
let seen = mouth.seen.lock().unwrap().clone();
|
|
assert_eq!(seen[0], (OPEN_DIRECT_REQUEST.to_string(), OPEN_REQUEST_BYTES.to_vec()));
|
|
}
|
|
|
|
/// A mouth that answers every open with `reply`.
|
|
fn opening_with(reply: OpenReply) -> Mouth {
|
|
serve(KEY, "127.0.0.1:0", PENDING, PENDING_BYTES, reply)
|
|
}
|
|
|
|
#[test]
|
|
fn a_mouth_that_does_not_serve_the_open_kind_refuses_it_and_the_open_is_told_not_sent_at_once() {
|
|
let mouth = opening_with((REFUSAL, Some("eureka"), UNKNOWN_KIND_BYTES));
|
|
let chat = client(&mouth, KEY, "eureka", Duration::from_secs(20));
|
|
let request = chat_client::decode::<ChatOpenDirectRequest>(OPEN_REQUEST_BYTES).unwrap();
|
|
let started = std::time::Instant::now();
|
|
let error = chat.open_direct(&request).unwrap_err();
|
|
assert!(started.elapsed() < Duration::from_secs(5), "the refusal ends the wait: {:?}", started.elapsed());
|
|
assert_eq!(error.downcast_ref::<MouthRefused>(), Some(&MouthRefused(Refusal::UnknownKind)), "{error}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_refusal_for_another_persona_or_another_kind_does_not_satisfy_the_open() {
|
|
let wrong_persona = opening_with((REFUSAL, Some("someone-else"), UNKNOWN_KIND_BYTES));
|
|
let request = chat_client::decode::<ChatOpenDirectRequest>(OPEN_REQUEST_BYTES).unwrap();
|
|
let error = client(&wrong_persona, KEY, "eureka", Duration::from_millis(800)).open_direct(&request).unwrap_err();
|
|
assert!(error.downcast_ref::<MouthRefused>().is_none(), "a refusal keyed by another persona is not heard: {error}");
|
|
let wrong_kind = opening_with((SEND_RECEIPT, Some("eureka"), SEND_RECEIPT_BYTES));
|
|
let error = client(&wrong_kind, KEY, "eureka", Duration::from_millis(800)).open_direct(&request).unwrap_err();
|
|
assert!(error.downcast_ref::<MouthRefused>().is_none(), "another kind is not heard: {error}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_refusal_of_another_kind_ends_the_wait_but_is_not_told_as_not_sent() {
|
|
// The fixture refusal is persona "ghost", UnknownPersona: the mouth refuses this Persona's open for a reason other than the kind.
|
|
let mouth = opening_with((REFUSAL, Some("ghost"), REFUSAL_BYTES));
|
|
let request = chat_client::decode::<ChatOpenDirectRequest>(OPEN_REQUEST_BYTES).unwrap();
|
|
let started = std::time::Instant::now();
|
|
let error = client(&mouth, KEY, "ghost", Duration::from_secs(20)).open_direct(&request).unwrap_err();
|
|
assert!(started.elapsed() < Duration::from_secs(5), "the refusal ends the wait: {:?}", started.elapsed());
|
|
assert!(error.downcast_ref::<MouthRefused>().is_none(), "only UnknownKind is a known not-sent: {error}");
|
|
assert!(error.to_string().contains("UnknownPersona"), "{error}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_unknown_kind_refusal_keyed_by_this_persona_but_naming_another_is_not_heard_as_not_sent() {
|
|
let other = chat_client::encode(&ChatRefusal { persona: "someone-else".into(), refusal: Refusal::UnknownKind }).unwrap();
|
|
let mouth = opening_with((REFUSAL, Some("eureka"), Box::leak(other.into_boxed_slice())));
|
|
let request = chat_client::decode::<ChatOpenDirectRequest>(OPEN_REQUEST_BYTES).unwrap();
|
|
let error = client(&mouth, KEY, "eureka", Duration::from_secs(20)).open_direct(&request).unwrap_err();
|
|
assert!(error.downcast_ref::<MouthRefused>().is_none(), "the payload names another Persona: {error}");
|
|
}
|