Watch
0
0
Fork
You've already forked hyperhive
0

hive-matrix-mcp: test the room handlers against the SDK's mock homeserver

This commit is contained in:
damocles 2026-11-16 23:21:37 +01:00 • committed by mara
commit ec4274f39e
2 changed files with 546 additions and 3 deletions

View file

@ -1886,3 +1886,7 @@ mod recipient_tests {
);
}
}
#[cfg(test)]
#[path = "handlers_mock_tests.rs"]
mod mock_tests;

View file

@ -0,0 +1,238 @@
//! The handlers against the SDK's own mock homeserver
//! (`matrix_sdk::test_utils::mocks`): what each one asks the server and what it
//! answers the agent, with the server's replies scripted per test. The pure
//! helpers have their own tests next to them in `handlers.rs`.
use matrix_sdk::Client;
use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType;
use matrix_sdk::ruma::events::AnyTimelineEvent;
use matrix_sdk::ruma::serde::Raw;
use matrix_sdk::ruma::{EventId, event_id, room_id, user_id};
use matrix_sdk::test_utils::mocks::{MatrixMockServer, RoomMessagesResponseTemplate};
use matrix_sdk_test::InvitedRoomBuilder;
use matrix_sdk_test::event_factory::EventFactory;
use serde_json::Value;
use super::*;
/// The payload of an `Ok` response, or a panic naming the error.
fn ok(response: DaemonResponse) -> Value {
match response {
DaemonResponse::Ok { payload } => payload,
DaemonResponse::Error { message } => panic!("expected ok, got error: {message}"),
}
}
/// The message of an `Error` response, or a panic naming the payload.
fn err(response: DaemonResponse) -> String {
match response {
DaemonResponse::Error { message } => message,
DaemonResponse::Ok { payload } => panic!("expected an error, got ok: {payload}"),
}
}
/// A client on a mock server that has joined `!room:localhost`, whose history
/// is `history` (newest first) and which is not encrypted.
async fn room_with(history: Vec<Raw<AnyTimelineEvent>>) -> (MatrixMockServer, Client) {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;
server
.sync_joined_room(&client, room_id!("!room:localhost"))
.await;
server.mock_room_state_encryption().plain().mount().await;
server
.mock_room_messages()
.ok(RoomMessagesResponseTemplate::default().events(history))
.mount()
.await;
(server, client)
}
/// [`room_with`] an empty history: the agent is caught up.
async fn caught_up_room() -> (MatrixMockServer, Client) {
room_with(Vec::new()).await
}
/// A text message from `@bob:localhost` in `!room:localhost`.
fn bobs_message(id: &str, body: &str) -> Raw<AnyTimelineEvent> {
EventFactory::new()
.room(room_id!("!room:localhost"))
.sender(user_id!("@bob:localhost"))
.text_msg(body)
.event_id(<&EventId>::try_from(id).expect("event id"))
.into_raw_timeline()
}
#[tokio::test]
async fn a_message_to_a_caught_up_room_is_sent() {
let (server, client) = caught_up_room().await;
server
.mock_room_send()
.ok(event_id!("$sent:localhost"))
.expect(1)
.mount()
.await;
let got = ok(send_message(&client, "!room:localhost", "hello").await);
assert_eq!(got["event_id"], "$sent:localhost");
assert_eq!(got["room_id"], "!room:localhost");
}
#[tokio::test]
async fn a_message_from_someone_else_blocks_the_send() {
let (server, client) = room_with(vec![bobs_message("$new:localhost", "are you there?")]).await;
server
.mock_room_send()
.ok(event_id!("$x:localhost"))
.never()
.mount()
.await;
let message = err(send_message(&client, "!room:localhost", "hello").await);
assert!(message.contains("unread message"), "{message}");
}
#[tokio::test]
async fn marking_an_event_read_sends_a_read_receipt() {
let (server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
server
.mock_send_receipt(ReceiptType::Read)
.ok()
.expect(1)
.mount()
.await;
let got = ok(mark_read(&client, "!room:localhost", "$new:localhost").await);
assert_eq!(got["marked_read"], "$new:localhost");
}
#[tokio::test]
async fn a_room_that_is_not_joined_or_not_a_room_is_refused() {
let (_server, client) = caught_up_room().await;
let unknown = err(send_message(&client, "!other:localhost", "hi").await);
assert!(unknown.contains("not joined"), "{unknown}");
let malformed = err(send_message(&client, "not a room", "hi").await);
assert!(malformed.contains("invalid room reference"), "{malformed}");
}
#[tokio::test]
async fn a_reaction_names_its_target_and_needs_no_catch_up() {
let (server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
server
.mock_room_send()
.ok(event_id!("$reaction:localhost"))
.expect(1)
.mount()
.await;
let got = ok(send_reaction(&client, "!room:localhost", "$new:localhost", "👍").await);
assert_eq!(got["event_id"], "$reaction:localhost");
assert_eq!(got["target"], "$new:localhost");
}
#[tokio::test]
async fn a_malformed_event_id_is_refused_before_the_server_is_asked() {
let (_server, client) = caught_up_room().await;
for response in [
send_reaction(&client, "!room:localhost", "nope", "👍").await,
mark_read(&client, "!room:localhost", "nope").await,
send_redact(&client, "!room:localhost", "nope", None).await,
] {
assert!(err(response).contains("invalid event_id"));
}
}
#[tokio::test]
async fn a_redaction_goes_to_the_server_and_names_its_target() {
let (server, client) = caught_up_room().await;
server
.mock_room_redact()
.ok(event_id!("$redaction:localhost"))
.expect(1)
.mount()
.await;
let got = ok(send_redact(&client, "!room:localhost", "$old:localhost", Some("spam")).await);
assert_eq!(got["event_id"], "$redaction:localhost");
assert_eq!(got["target"], "$old:localhost");
}
#[tokio::test]
async fn a_server_refusal_reaches_the_agent_as_an_error() {
let (server, client) = caught_up_room().await;
server.mock_room_send().error500().mount().await;
let message = err(send_message(&client, "!room:localhost", "hello").await);
assert!(message.contains("send to !room:localhost"), "{message}");
}
#[tokio::test]
async fn the_joined_rooms_are_listed() {
let (_server, client) = caught_up_room().await;
let got = ok(list_rooms(&client).await);
let rooms = got.as_array().expect("a list");
assert_eq!(rooms.len(), 1);
assert_eq!(rooms[0]["room_id"], "!room:localhost");
}
#[tokio::test]
async fn an_invite_is_listed_and_a_joined_room_is_not_an_invite() {
let (server, client) = caught_up_room().await;
server
.sync_room(
&client,
InvitedRoomBuilder::new(room_id!("!invited:localhost")),
)
.await;
let got = ok(list_invites(&client).await);
let invites = got.as_array().expect("a list");
assert_eq!(invites.len(), 1);
assert_eq!(invites[0]["room_id"], "!invited:localhost");
}
#[tokio::test]
async fn an_invite_to_a_room_is_sent_to_the_server_and_a_bad_user_is_refused() {
let (server, client) = caught_up_room().await;
server.mock_invite_user_by_id().ok().expect(1).mount().await;
let got = ok(invite_user(&client, "!room:localhost", "@bob:localhost").await);
assert_eq!(got["invited"], true);
assert_eq!(got["user_id"], "@bob:localhost");
let bad = err(invite_user(&client, "!room:localhost", "bob").await);
assert!(bad.contains("invalid user_id"), "{bad}");
}
#[tokio::test]
async fn rejecting_an_invite_leaves_the_room() {
let (server, client) = caught_up_room().await;
server
.sync_room(
&client,
InvitedRoomBuilder::new(room_id!("!invited:localhost")),
)
.await;
server
.mock_room_leave()
.ok(room_id!("!invited:localhost"))
.expect(1)
.mount()
.await;
let got = ok(resolve_invite(&client, "!invited:localhost", InviteAction::Reject).await);
assert_eq!(got["rejected"], true);
assert_eq!(got["room_id"], "!invited:localhost");
}