Watch
0
0
Fork
You've already forked hyperhive
0

hive-matrix-mcp: test reading, members, joining, files, unread summaries and the MCP tools

This commit is contained in:
damocles 2026-11-17 00:17:42 +01:00 • committed by mara
commit 2e30163d5c

View file

@ -236,3 +236,214 @@ async fn rejecting_an_invite_leaves_the_room() {
assert_eq!(got["rejected"], true);
assert_eq!(got["room_id"], "!invited:localhost");
}
#[tokio::test]
async fn reading_a_room_returns_its_events_newest_first_with_sender_and_body() {
let (_server, client) = room_with(vec![
bobs_message("$two:localhost", "second"),
bobs_message("$one:localhost", "first"),
])
.await;
let got = ok(read_room(&client, "!room:localhost", None, None, None).await);
let events = got.as_array().expect("a list");
assert_eq!(events.len(), 2);
assert_eq!(events[0]["event_id"], "$two:localhost");
assert_eq!(events[0]["sender"], "@bob:localhost");
assert_eq!(events[0]["body"], "second");
assert_eq!(events[1]["body"], "first");
}
#[tokio::test]
async fn a_read_asks_for_the_limit_clamped_to_the_allowed_range() {
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()
.match_limit(200)
.ok(RoomMessagesResponseTemplate::default())
.expect(1)
.mount()
.await;
ok(read_room(&client, "!room:localhost", Some(5000), None, None).await);
}
#[tokio::test]
async fn a_read_with_both_anchors_or_a_bad_anchor_is_refused() {
let (_server, client) = caught_up_room().await;
let both = err(read_room(
&client,
"!room:localhost",
None,
Some("$a:localhost".to_owned()),
Some("$b:localhost".to_owned()),
)
.await);
assert!(both.contains("mutually exclusive"), "{both}");
let bad = err(read_room(
&client,
"!room:localhost",
None,
Some("nope".to_owned()),
None,
)
.await);
assert!(bad.contains("invalid event_id"), "{bad}");
}
#[tokio::test]
async fn the_joined_members_are_listed_with_their_display_names() {
let (server, client) = caught_up_room().await;
let f = EventFactory::new().room(room_id!("!room:localhost"));
server
.mock_get_members()
.ok(vec![
f.member(user_id!("@bob:localhost"))
.display_name("Bob")
.into_raw(),
])
.mount()
.await;
let got = ok(list_room_members(&client, "!room:localhost").await);
let members = got.as_array().expect("a list");
assert_eq!(members.len(), 1);
assert_eq!(members[0]["user_id"], "@bob:localhost");
assert_eq!(members[0]["display_name"], "Bob");
}
/// `join_room_by_id_or_alias` posts to `/join/<room>`, which the SDK's mock
/// server has no prefab for (`mock_room_join` is `/rooms/<room>/join`).
async fn mock_join(server: &MatrixMockServer, room: &str) {
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
Mock::given(method("POST"))
.and(path(format!("/_matrix/client/v3/join/{room}")))
.respond_with(
ResponseTemplate::new(200).set_body_json(serde_json::json!({"room_id": room})),
)
.expect(1)
.mount(server.server())
.await;
}
#[tokio::test]
async fn joining_a_room_asks_the_server_and_a_bad_reference_is_refused() {
let (server, client) = caught_up_room().await;
mock_join(&server, "!other:localhost").await;
let got = ok(join_room(&client, "!other:localhost").await);
assert_eq!(got["joined"], true);
assert_eq!(got["room_id"], "!other:localhost");
let bad = err(join_room(&client, "not a room").await);
assert!(bad.contains("invalid room reference"), "{bad}");
}
#[tokio::test]
async fn accepting_an_invite_is_joining_the_room() {
let (server, client) = caught_up_room().await;
mock_join(&server, "!invited:localhost").await;
let got = ok(resolve_invite(&client, "!invited:localhost", InviteAction::Accept).await);
assert_eq!(got["joined"], true);
}
#[tokio::test]
async fn the_rooms_with_unread_messages_are_summarised_with_the_latest_sender() {
let (_server, client) = room_with(vec![bobs_message("$new:localhost", "are you there?")]).await;
let got = ok(unread_summary(&client).await);
let rooms = got.as_array().expect("a list");
assert_eq!(rooms.len(), 1);
assert_eq!(rooms[0]["label"], "!room:localhost");
assert_eq!(rooms[0]["last_sender"], "@bob:localhost");
assert_eq!(rooms[0]["last_body"], "are you there?");
}
#[tokio::test]
async fn a_caught_up_room_has_nothing_to_summarise() {
let (_server, client) = caught_up_room().await;
let got = ok(unread_summary(&client).await);
assert_eq!(got.as_array().expect("a list").len(), 0);
}
#[tokio::test]
async fn a_file_is_uploaded_and_posted_with_its_guessed_type_and_a_caption() {
let (server, client) = caught_up_room().await;
// The SDK asks for the server's upload limit before it uploads.
server
.mock_authenticated_media_config()
.ok_default()
.mount()
.await;
server
.mock_media_config()
.ok(matrix_sdk::ruma::UInt::from(10_000_000_u32))
.mount()
.await;
let (captured, upload) = server
.mock_upload()
.ok_with_capture(matrix_sdk::ruma::mxc_uri!("mxc://localhost/abc"));
upload.mount().await;
server
.mock_room_send()
.ok(event_id!("$file:localhost"))
.expect(2)
.mount()
.await;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("notes.txt");
std::fs::write(&path, b"hello").unwrap();
let got = ok(send_file(
&client,
"!room:localhost",
path.to_str().unwrap(),
Some("the notes"),
)
.await);
assert_eq!(got["filename"], "notes.txt");
assert_eq!(got["mime"], "text/plain");
assert_eq!(got["event_id"], "$file:localhost");
assert_eq!(captured.await.unwrap(), b"hello");
}
#[tokio::test]
async fn a_file_that_is_missing_or_not_a_file_is_refused_before_any_upload() {
let (_server, client) = caught_up_room().await;
let dir = tempfile::tempdir().unwrap();
let missing = err(send_file(&client, "!room:localhost", "/nonexistent/file", None).await);
assert!(missing.contains("stat /nonexistent/file"), "{missing}");
let directory = err(send_file(
&client,
"!room:localhost",
dir.path().to_str().unwrap(),
None,
)
.await);
assert!(directory.contains("not a regular file"), "{directory}");
}
#[tokio::test]
async fn a_file_waits_for_the_agent_to_catch_up_like_a_message() {
let (_server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
let message = err(send_file(&client, "!room:localhost", "/nonexistent", None).await);
assert!(message.contains("unread message"), "{message}");
}