31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | mod support; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use std::time::Duration; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | use macltok::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | ClientHandle, Command, Engine, EngineConfig, EngineError, ErrorKind, Event, EventStream, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | MessageContent, MessageEditContent, RichText, RoomCommand, TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | ruma::{RoomId, api::client::sync::sync_events::v5, event_id, room_id}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | test_utils::mocks::MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | use support::shut_down; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | use tokio::task::JoinHandle; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | #[tokio::test] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | async fn empty_message_bodies_fail_with_empty_body() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | let room_id = room_id!("!empty-bodies:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | let (server, handle, mut events, running) = start_engine(room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | open_room(&handle, &mut events, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | let commands = [ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | content: MessageContent::text(String::new()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | content: MessageContent::emote(RichText::plain(" "), Default::default()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | content: MessageContent::reply( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | event_id!("$reply:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | RichText::plain("\n"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | Default::default(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | TimelineCommand::Edit { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | timeline_id: room_id.to_owned().into(), |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 41 | target: event_id!("$edit:localhost").to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | content: MessageEditContent::text("\t"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | ]; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | for command in commands { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | let error = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | .execute(Command::Timeline(command)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | .expect_err("empty body command unexpectedly succeeded"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | assert_eq!(error.kind, ErrorKind::EmptyBody); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | drop(server); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | async fn open_resets_the_timeline_before_succeeding() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | let room_id = room_id!("!open-order:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | let (server, handle, mut events, running) = start_engine(room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | open_room(&handle, &mut events, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | drop(server); |
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 same_room_sends_complete_in_submission_order() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | let room_id = room_id!("!send-order:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | let (server, handle, mut events, running) = start_engine(room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | open_room(&handle, &mut events, room_id).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | server |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | .mock_room_send() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | .ok(event_id!("$sent:localhost")) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | .expect(2) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | .mount() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | let first = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | .try_send(Command::Timeline(TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | content: MessageContent::text("first"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | let second = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | .try_send(Command::Timeline(TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | content: MessageContent::text("second"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | // both answers land almost together, so racing them can't show which was |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | // processed first, only the order the sends actually reached the homeserver can |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | let (first_result, second_result) = tokio::join!(first, second); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | first_result.expect("first send failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | second_result.expect("second send failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | tokio::time::timeout(Duration::from_secs(2), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | let bodies: Vec<String> = server |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | .received_requests() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | .unwrap() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | .iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | .filter(|request| request.url.path().contains("/send/m.room.message/")) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | .filter_map(|request| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | serde_json::from_slice::<serde_json::Value>(&request.body).ok() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | .filter_map(|body| body["body"].as_str().map(str::to_owned)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | .collect(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | if bodies.len() >= 2 { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | assert_eq!(bodies, ["first", "second"]); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | tokio::task::yield_now().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | .expect("both sends did not reach the homeserver in submission order"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | drop(server); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | async fn start_engine( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | ) -> ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | ClientHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | JoinHandle<Result<(), EngineError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | let (engine, handle, events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | (server, handle, events, running) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | /// opens `room_id` and confirms its `TimelineReset` was already on the |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | /// stream by the time the open command answered |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | async fn open_room(handle: &ClientHandle, events: &mut EventStream, room_id: &RoomId) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | let mut pending = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | let mut reset_received = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | biased; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | event = events.recv() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | if let Event::TimelineReset { timeline_id, .. } = event.expect("event stream closed") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | && timeline_id.room_id() == room_id |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | reset_received = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 169 | result = &mut pending => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | result.expect("opening room failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | .expect("room did not open"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | assert!(reset_received, "open completed before its reset"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | } |