96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | use std::time::Duration; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use macltok::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | ClientHandle, ClientQueryError, Command, CommandErrorKind, CommandId, CommandKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | CreateRoomOptions, Engine, EngineConfig, EngineError, Event, EventStream, LifecycleCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | RoomCommand, RoomPreset, RoomVisibility, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | ruma::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | api::client::sync::sync_events::v5, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | events::{GlobalAccountDataEventType, RoomAccountDataEventType}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | serde::Raw, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | server_name, user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | test_utils::mocks::MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | use matrix_sdk_test::{InvitedRoomBuilder, JoinedRoomBuilder}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | use serde_json::json; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | use tokio::task::JoinHandle; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | use wiremock::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | Mock, ResponseTemplate, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | matchers::{body_partial_json, method, path, path_regex, query_param}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | async fn joining_by_room_id_forwards_via_servers() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | let room_id = room_id!("!join:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | let (server, handle, mut events, running) = start_engine().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | Mock::given(method("POST")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | .and(path(format!("/_matrix/client/v3/join/{room_id}"))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | .and(query_param("via", "relay.example")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "room_id": room_id }))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | .mount(server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | let joined_room = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | .join_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | vec![server_name!("relay.example").to_owned()], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | assert_eq!(joined_room, room_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | stop_engine(handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | async fn leave_and_forget_report_correlated_success() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | let room_id = room_id!("!leave:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | let (server, client) = configured_client().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | server.mock_room_leave().ok(room_id).expect(1).mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | server.mock_room_forget().ok().expect(1).mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | let (handle, mut events, running) = run_client(client).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | let leave_id = handle.leave_room(room_id.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | receive_room_success(&mut events, leave_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | let forget_id = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | .send(Command::Room(RoomCommand::Forget(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | receive_room_success(&mut events, forget_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | assert_ne!(leave_id, forget_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | stop_engine(handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | async fn accepting_and_rejecting_invites_use_the_expected_endpoints() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | let accepted = room_id!("!accepted:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | let rejected = room_id!("!rejected:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | let (server, client) = configured_client().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | .sync_room(&client, InvitedRoomBuilder::new(accepted)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | .sync_room(&client, InvitedRoomBuilder::new(rejected)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | server.mock_room_join(accepted).ok().expect(1).mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | .mock_room_leave() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | .ok(rejected) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | let (handle, mut events, running) = run_client(client).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | let accept_id = handle.accept_invite(accepted.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | receive_room_success(&mut events, accept_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | let reject_id = handle.reject_invite(rejected.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | receive_room_success(&mut events, reject_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | stop_engine(handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | async fn creating_a_room_forwards_options() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | let (server, handle, mut events, running) = start_engine().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | Mock::given(method("POST")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | .and(path("/_matrix/client/v3/createRoom")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | .and(body_partial_json(json!({ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | "name": "Planning", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | "topic": "Next release", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | "room_alias_name": "planning", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | "invite": ["@partner:localhost"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | "visibility": "public", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | "preset": "public_chat" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | }))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | .respond_with( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | ResponseTemplate::new(200).set_body_json(json!({ "room_id": "!created:localhost" })), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | .mount(server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | let created_room = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | .create_room(CreateRoomOptions { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | name: Some("Planning".to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | topic: Some("Next release".to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | alias_localpart: Some("planning".to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | invite: vec![user_id!("@partner:localhost").to_owned()], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | visibility: RoomVisibility::Public, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | preset: Some(RoomPreset::PublicChat), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | ..Default::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | assert_eq!(created_room, room_id!("!created:localhost").to_owned()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | stop_engine(handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | async fn creating_a_direct_room_invites_the_other_user() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | let (server, handle, mut events, running) = start_engine().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | Mock::given(method("POST")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | .and(path("/_matrix/client/v3/createRoom")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | .and(body_partial_json(json!({ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | "invite": ["@partner:localhost"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | "is_direct": true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | "preset": "trusted_private_chat" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | }))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | .respond_with( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | ResponseTemplate::new(200).set_body_json(json!({ "room_id": "!direct:localhost" })), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | .mount(server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | Mock::given(method("GET")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | .and(path_regex( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | r"^/_matrix/client/v3/user/.*/account_data/m\.direct$", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | .mount(server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | Mock::given(method("PUT")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | .and(path_regex( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | r"^/_matrix/client/v3/user/.*/account_data/m\.direct$", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | .mount(server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | let direct_room = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | .create_direct(user_id!("@partner:localhost").to_owned()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | assert_eq!(direct_room, room_id!("!direct:localhost").to_owned()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | stop_engine(handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | async fn unknown_rooms_fail_membership_commands_with_their_ids() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | let (server, handle, mut events, running) = start_engine().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | let unknown = room_id!("!unknown:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | for command in [ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | RoomCommand::Leave(unknown.to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | RoomCommand::Forget(unknown.to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | RoomCommand::AcceptInvite(unknown.to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | RoomCommand::RejectInvite(unknown.to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | ] { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | let id = handle.send(Command::Room(command)).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | receive_room_failure(&mut events, id, CommandErrorKind::RoomUnknown).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 186 | stop_engine(handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | drop(server); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | async fn account_data_reads_synced_values_and_writes_raw_content() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | let room_id = room_id!("!account-data:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | let (server, client) = configured_client().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | &client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | JoinedRoomBuilder::new(room_id).add_account_data( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | Raw::new(&json!({ "type": "m.fully_read", "content": { "event_id": "$seen:localhost" } })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | .cast_unchecked(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | .mock_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | .ok_and_run(&client, |builder| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | builder.add_custom_global_account_data(json!({ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | "type": "com.example.preferences", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | "content": { "theme": "dark" } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | })); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | let (engine, handle, mut events) = Engine::from_client(client, EngineConfig::default()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | let global_type = GlobalAccountDataEventType::from("com.example.preferences".to_owned()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | let room_type = RoomAccountDataEventType::FullyRead; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | let global = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | .global_account_data(global_type.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | global.deserialize_as::<serde_json::Value>().unwrap()["theme"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | "dark" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | let room = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | .room_account_data(room_id, room_type.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | room.deserialize_as::<serde_json::Value>().unwrap()["content"]["event_id"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | "$seen:localhost" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | assert!(matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 239 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | .room_account_data(room_id!("!missing:localhost"), room_type.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | .await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | Err(ClientQueryError::RoomUnknown(_)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | Mock::given(method("PUT")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | .and(path( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | "/_matrix/client/v3/user/@example:localhost/account_data/com.example.preferences", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | .and(body_partial_json(json!({ "theme": "light" }))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | .mount(server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | .mock_set_room_account_data(room_type.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | .ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | .set_global_account_data( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | global_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | Raw::new(&json!({ "theme": "light" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | .cast_unchecked(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | .set_room_account_data( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | room_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | Raw::new(&json!({ "event_id": "$later:localhost" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | .cast_unchecked(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 279 | assert!(matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 280 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | .set_room_account_data( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | room_id!("!missing:localhost"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | RoomAccountDataEventType::FullyRead, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | Raw::new(&json!({ "event_id": "$later:localhost" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | .cast_unchecked() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | .await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | Err(ClientQueryError::RoomUnknown(_)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | stop_engine(handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 293 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 294 | async fn configured_client() -> (MatrixMockServer, matrix_sdk::Client) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 295 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 298 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | (server, client) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 305 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 306 | async fn start_engine() -> ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | ClientHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | JoinHandle<Result<(), EngineError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 311 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | let (server, client) = configured_client().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | let (handle, events, running) = run_client(client).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | (server, handle, events, running) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | async fn run_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | client: matrix_sdk::Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | ) -> ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | ClientHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | JoinHandle<Result<(), EngineError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | let (engine, handle, events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | (handle, events, running) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | async fn receive_room_success(events: &mut EventStream, expected_id: CommandId) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | tokio::time::timeout(Duration::from_secs(3), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | match events.recv().await.expect("event stream closed") { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | Event::CommandSucceeded { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | } if id == expected_id => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | Event::CommandFailed(failure) if failure.id == expected_id => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | panic!("room command failed {failure:?}") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | _ => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | .expect("room command did not complete"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | async fn receive_room_failure( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | events: &mut EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | expected_id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | expected_kind: CommandErrorKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | tokio::time::timeout(Duration::from_secs(3), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | match events.recv().await.expect("event stream closed") { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | Event::CommandFailed(failure) if failure.id == expected_id => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | assert_eq!(failure.command, CommandKind::Room); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | assert_eq!(failure.kind, expected_kind); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | Event::CommandSucceeded { id, .. } if id == expected_id => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | panic!("room command unexpectedly succeeded") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | _ => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 369 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | .expect("room command did not fail"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | async fn stop_engine( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | handle: ClientHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | events: &mut EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | running: JoinHandle<Result<(), EngineError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | let id = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | .send(Command::Lifecycle(LifecycleCommand::Shutdown)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | tokio::time::timeout(Duration::from_secs(3), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | let mut succeeded = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | while let Some(event) = events.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | match event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | Event::CommandSucceeded { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | id: completed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | command: CommandKind::Lifecycle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | } if completed == id => succeeded = true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | Event::Stopped => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | assert!(succeeded); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | _ => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | .expect("engine did not stop"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | running.await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | } |