Skip to main content

laser_sdk/
testing.rs

1use crate::agent::{AgentCtx, AgentMessage, InboxRoute};
2use crate::laser::Laser;
3use crate::provenance::{AgentTopic, Provenance};
4use crate::types::{AgentId, MessageId};
5
6/// Build an [`AgentMessage`] for a handler unit test: a plain (non-envelope)
7/// message carrying `payload` and `provenance`, seated at the start of a
8/// synthetic partition. Feed it to an [`AgentHandler`](crate::agent::AgentHandler)
9/// to exercise the handler without the reliable consumer or a live server.
10pub fn agent_message(payload: impl Into<Vec<u8>>, provenance: Provenance) -> AgentMessage {
11    AgentMessage {
12        provenance,
13        payload: payload.into(),
14        id: MessageId::new(0, 0),
15        envelope: None,
16        content_type: None,
17        verified_principal: None,
18    }
19}
20
21/// Build an [`AgentCtx`] over a caller-owned `laser` and `message`, so a handler
22/// test can call `handle(&message, &ctx)` directly. The ctx borrows both, so keep
23/// them alive for the duration of the call. The `laser` supplies whatever IO the
24/// ctx helpers use (point it at a test server for `respond`/`fan_out`). A handler
25/// that only reads its message needs no server at all.
26pub fn agent_ctx<'a>(
27    laser: &'a Laser,
28    message: &'a AgentMessage,
29    agent: Option<AgentId>,
30    respond_on: Option<AgentTopic<'static>>,
31    inbox_route: InboxRoute,
32) -> AgentCtx<'a> {
33    AgentCtx::new(
34        laser,
35        message,
36        agent,
37        respond_on,
38        inbox_route,
39        #[cfg(feature = "sign")]
40        None,
41    )
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::types::ConversationId;
48
49    #[test]
50    fn given_a_test_message_when_built_then_should_carry_payload_and_provenance() {
51        let conversation = ConversationId::new();
52        let provenance = Provenance::builder().conversation_id(conversation).build();
53        let message = agent_message(b"hello".to_vec(), provenance);
54        assert_eq!(message.body(), b"hello");
55        assert_eq!(message.provenance.conversation_id, conversation);
56        assert!(message.envelope.is_none());
57    }
58}