hive-matrix-mcp: test direct messages, replies and anchored reads
This commit is contained in:
parent
2df691bfe6
commit
7bd93ecfde
1 changed files with 250 additions and 8 deletions
|
|
@ -5,12 +5,15 @@
|
||||||
|
|
||||||
use matrix_sdk::Client;
|
use matrix_sdk::Client;
|
||||||
use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType;
|
use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType;
|
||||||
use matrix_sdk::ruma::events::AnyTimelineEvent;
|
use matrix_sdk::ruma::events::room::member::MembershipState;
|
||||||
|
use matrix_sdk::ruma::events::{AnyGlobalAccountDataEvent, AnyTimelineEvent};
|
||||||
use matrix_sdk::ruma::serde::Raw;
|
use matrix_sdk::ruma::serde::Raw;
|
||||||
use matrix_sdk::ruma::{EventId, event_id, room_id, user_id};
|
use matrix_sdk::ruma::{EventId, event_id, room_id, user_id};
|
||||||
use matrix_sdk::test_utils::mocks::{MatrixMockServer, RoomMessagesResponseTemplate};
|
use matrix_sdk::test_utils::mocks::{
|
||||||
use matrix_sdk_test::InvitedRoomBuilder;
|
MatrixMockServer, RoomContextResponseTemplate, RoomMessagesResponseTemplate,
|
||||||
|
};
|
||||||
use matrix_sdk_test::event_factory::EventFactory;
|
use matrix_sdk_test::event_factory::EventFactory;
|
||||||
|
use matrix_sdk_test::{InvitedRoomBuilder, JoinedRoomBuilder};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -34,17 +37,24 @@ fn err(response: DaemonResponse) -> String {
|
||||||
/// A client on a mock server that has joined `!room:localhost`, whose history
|
/// A client on a mock server that has joined `!room:localhost`, whose history
|
||||||
/// is `history` (newest first) and which is not encrypted.
|
/// is `history` (newest first) and which is not encrypted.
|
||||||
async fn room_with(history: Vec<Raw<AnyTimelineEvent>>) -> (MatrixMockServer, Client) {
|
async fn room_with(history: Vec<Raw<AnyTimelineEvent>>) -> (MatrixMockServer, Client) {
|
||||||
|
let (server, client) = joined_room().await;
|
||||||
|
server
|
||||||
|
.mock_room_messages()
|
||||||
|
.ok(RoomMessagesResponseTemplate::default().events(history))
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
(server, client)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A client on a mock server that has joined `!room:localhost`, which is not
|
||||||
|
/// encrypted, with no history mocked yet.
|
||||||
|
async fn joined_room() -> (MatrixMockServer, Client) {
|
||||||
let server = MatrixMockServer::new().await;
|
let server = MatrixMockServer::new().await;
|
||||||
let client = server.client_builder().build().await;
|
let client = server.client_builder().build().await;
|
||||||
server
|
server
|
||||||
.sync_joined_room(&client, room_id!("!room:localhost"))
|
.sync_joined_room(&client, room_id!("!room:localhost"))
|
||||||
.await;
|
.await;
|
||||||
server.mock_room_state_encryption().plain().mount().await;
|
server.mock_room_state_encryption().plain().mount().await;
|
||||||
server
|
|
||||||
.mock_room_messages()
|
|
||||||
.ok(RoomMessagesResponseTemplate::default().events(history))
|
|
||||||
.mount()
|
|
||||||
.await;
|
|
||||||
(server, client)
|
(server, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -447,3 +457,235 @@ async fn a_file_waits_for_the_agent_to_catch_up_like_a_message() {
|
||||||
|
|
||||||
assert!(message.contains("unread message"), "{message}");
|
assert!(message.contains("unread message"), "{message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A client with one direct room, `!dm:localhost`, with `@bob:localhost` in
|
||||||
|
/// the `m.direct` account data, empty history, and `bob` as `bob_is` there.
|
||||||
|
async fn dm_with_bob(bob_is: Option<MembershipState>) -> (MatrixMockServer, Client) {
|
||||||
|
let server = MatrixMockServer::new().await;
|
||||||
|
let client = server.client_builder().build().await;
|
||||||
|
let direct: Raw<AnyGlobalAccountDataEvent> = serde_json::from_value(serde_json::json!({
|
||||||
|
"type": "m.direct",
|
||||||
|
"content": {"@bob:localhost": ["!dm:localhost"]},
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
server
|
||||||
|
.mock_sync()
|
||||||
|
.ok_and_run(&client, |builder| {
|
||||||
|
builder.add_joined_room(JoinedRoomBuilder::new(room_id!("!dm:localhost")));
|
||||||
|
builder.add_global_account_data(direct);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
server.mock_room_state_encryption().plain().mount().await;
|
||||||
|
server
|
||||||
|
.mock_room_messages()
|
||||||
|
.ok(RoomMessagesResponseTemplate::default())
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
let f = EventFactory::new().room(room_id!("!dm:localhost"));
|
||||||
|
let mut members = vec![f.member(user_id!("@example:localhost")).into_raw()];
|
||||||
|
if let Some(state) = bob_is {
|
||||||
|
members.push(
|
||||||
|
f.member(user_id!("@bob:localhost"))
|
||||||
|
.membership(state)
|
||||||
|
.into_raw(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
server.mock_get_members().ok(members).mount().await;
|
||||||
|
(server, client)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_dm_goes_to_the_existing_direct_room_and_says_the_recipient_is_in_it() {
|
||||||
|
let (server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
||||||
|
server
|
||||||
|
.mock_room_send()
|
||||||
|
.ok(event_id!("$dm:localhost"))
|
||||||
|
.expect(1)
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let got = ok(send_dm(&client, "@bob:localhost", "hi").await);
|
||||||
|
|
||||||
|
assert_eq!(got["room_id"], "!dm:localhost");
|
||||||
|
assert_eq!(got["user_id"], "@bob:localhost");
|
||||||
|
assert_eq!(got["recipient"], "joined");
|
||||||
|
assert!(got.get("warning").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_bare_name_is_that_user_on_the_own_homeserver() {
|
||||||
|
let (server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
||||||
|
server
|
||||||
|
.mock_room_send()
|
||||||
|
.ok(event_id!("$dm:localhost"))
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let got = ok(send_dm(&client, "bob", "hi").await);
|
||||||
|
|
||||||
|
assert_eq!(got["user_id"], "@bob:localhost");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_dm_to_someone_who_has_not_joined_warns_that_they_will_see_it_later() {
|
||||||
|
let (server, client) = dm_with_bob(Some(MembershipState::Invite)).await;
|
||||||
|
server
|
||||||
|
.mock_room_send()
|
||||||
|
.ok(event_id!("$dm:localhost"))
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let got = ok(send_dm(&client, "@bob:localhost", "hi").await);
|
||||||
|
|
||||||
|
assert_eq!(got["recipient"], "invited");
|
||||||
|
assert!(got["warning"].as_str().unwrap().contains("not joined"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_dm_nobody_is_in_is_an_error_and_nothing_is_sent() {
|
||||||
|
let (server, client) = dm_with_bob(None).await;
|
||||||
|
server
|
||||||
|
.mock_room_send()
|
||||||
|
.ok(event_id!("$dm:localhost"))
|
||||||
|
.never()
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let message = err(send_dm(&client, "@bob:localhost", "hi").await);
|
||||||
|
|
||||||
|
assert!(message.contains("is not in DM room"), "{message}");
|
||||||
|
assert!(message.contains("Nothing was sent"), "{message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn opening_a_dm_names_the_room_and_sends_nothing() {
|
||||||
|
let (server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
||||||
|
server
|
||||||
|
.mock_room_send()
|
||||||
|
.ok(event_id!("$dm:localhost"))
|
||||||
|
.never()
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let got = ok(open_dm(&client, "@bob:localhost").await);
|
||||||
|
|
||||||
|
assert_eq!(got["room_id"], "!dm:localhost");
|
||||||
|
assert_eq!(got["recipient"], "joined");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_dm_with_a_malformed_user_is_refused() {
|
||||||
|
let (_server, client) = dm_with_bob(Some(MembershipState::Join)).await;
|
||||||
|
|
||||||
|
let message = err(send_dm(&client, "@bob", "hi").await);
|
||||||
|
|
||||||
|
assert!(message.contains("invalid user_id"), "{message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_reply_names_its_target_and_is_sent() {
|
||||||
|
let (server, client) = caught_up_room().await;
|
||||||
|
server
|
||||||
|
.mock_room_event()
|
||||||
|
.ok(EventFactory::new()
|
||||||
|
.room(room_id!("!room:localhost"))
|
||||||
|
.sender(user_id!("@bob:localhost"))
|
||||||
|
.text_msg("question?")
|
||||||
|
.event_id(event_id!("$q:localhost"))
|
||||||
|
.into_event())
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
server
|
||||||
|
.mock_room_send()
|
||||||
|
.ok(event_id!("$answer:localhost"))
|
||||||
|
.expect(1)
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let got = ok(send_reply(&client, "!room:localhost", "$q:localhost", "yes").await);
|
||||||
|
|
||||||
|
assert_eq!(got["event_id"], "$answer:localhost");
|
||||||
|
assert_eq!(got["target"], "$q:localhost");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_reply_waits_for_the_catch_up_and_needs_a_real_event_id() {
|
||||||
|
let (_server, client) = room_with(vec![bobs_message("$new:localhost", "hi")]).await;
|
||||||
|
let blocked = err(send_reply(&client, "!room:localhost", "$new:localhost", "yes").await);
|
||||||
|
assert!(blocked.contains("unread message"), "{blocked}");
|
||||||
|
|
||||||
|
let (_server, client) = caught_up_room().await;
|
||||||
|
let bad = err(send_reply(&client, "!room:localhost", "nope", "yes").await);
|
||||||
|
assert!(bad.contains("invalid event_id"), "{bad}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn reading_back_from_an_anchor_returns_the_anchor_then_the_older_events() {
|
||||||
|
let (server, client) = joined_room().await;
|
||||||
|
server
|
||||||
|
.mock_room_event_context()
|
||||||
|
.ok(RoomContextResponseTemplate::new(
|
||||||
|
EventFactory::new()
|
||||||
|
.room(room_id!("!room:localhost"))
|
||||||
|
.sender(user_id!("@bob:localhost"))
|
||||||
|
.text_msg("middle")
|
||||||
|
.event_id(event_id!("$anchor:localhost"))
|
||||||
|
.into_event(),
|
||||||
|
)
|
||||||
|
.start("before"))
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
server
|
||||||
|
.mock_room_messages()
|
||||||
|
.ok(RoomMessagesResponseTemplate::default()
|
||||||
|
.events(vec![bobs_message("$older:localhost", "older")]))
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let got = ok(read_room(
|
||||||
|
&client,
|
||||||
|
"!room:localhost",
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some("$anchor:localhost".to_owned()),
|
||||||
|
)
|
||||||
|
.await);
|
||||||
|
|
||||||
|
let bodies: Vec<&str> = got
|
||||||
|
.as_array()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.map(|e| e["body"].as_str().unwrap())
|
||||||
|
.collect();
|
||||||
|
assert_eq!(bodies, ["middle", "older"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn reading_forward_from_the_newest_event_returns_just_that_event() {
|
||||||
|
let (server, client) = joined_room().await;
|
||||||
|
server
|
||||||
|
.mock_room_event_context()
|
||||||
|
.ok(RoomContextResponseTemplate::new(
|
||||||
|
EventFactory::new()
|
||||||
|
.room(room_id!("!room:localhost"))
|
||||||
|
.sender(user_id!("@bob:localhost"))
|
||||||
|
.text_msg("latest")
|
||||||
|
.event_id(event_id!("$latest:localhost"))
|
||||||
|
.into_event(),
|
||||||
|
))
|
||||||
|
.mount()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let got = ok(read_room(
|
||||||
|
&client,
|
||||||
|
"!room:localhost",
|
||||||
|
None,
|
||||||
|
Some("$latest:localhost".to_owned()),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await);
|
||||||
|
|
||||||
|
let events = got.as_array().unwrap();
|
||||||
|
assert_eq!(events.len(), 1);
|
||||||
|
assert_eq!(events[0]["body"], "latest");
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue