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 | AttachmentUpload, ClientHandle, Engine, EngineConfig, ErrorKind, EventStream, MessageContent, |
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 | Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | ruma::{OwnedTransactionId, 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 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | async fn cancel_send_uses_transaction_id_from_public_timeline() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | let (server, client, handle, mut events, running) = setup().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | let room_id = room_id!("!queue-controls:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | client.send_queue().set_enabled(false).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | let transaction_id = queue_message(&handle, &mut events, room_id, "discard me").await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | .pending_send_ids(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | .contains(&transaction_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | .cancel_send(room_id.to_owned(), transaction_id.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | let (echoes, _) = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | .get_room(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | .send_queue() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | .subscribe() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | !echoes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | .any(|echo| echo.transaction_id == transaction_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | let error = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | .cancel_send(room_id.to_owned(), transaction_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | .unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | drop(server); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | async fn cancel_queued_attachment_by_pending_send_id() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | let (server, client, handle, mut events, running) = setup().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | let room_id = room_id!("!queue-controls:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | client.send_queue().set_enabled(false).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | .upload(AttachmentUpload::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | room_id.to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | "note.txt", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | "text/plain", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | b"attachment".to_vec(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | let pending = handle.pending_send_ids(room_id).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | assert_eq!(pending.len(), 1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | .cancel_send(room_id.to_owned(), pending[0].clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | assert!(handle.pending_send_ids(room_id).await.unwrap().is_empty()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | drop(server); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | async fn retry_send_wakes_paused_queue() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | let (server, client, handle, mut events, running) = setup().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | let room_id = room_id!("!queue-controls:localhost"); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 88 | client |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 89 | .get_room(room_id) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 90 | .unwrap() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 91 | .send_queue() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 92 | .set_enabled(false); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | .body_matches_partial_json(serde_json::json!({ "body": "retry me" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | .ok(event_id!("$retried:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | let transaction_id = queue_message(&handle, &mut events, room_id, "retry me").await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | .retry_send(room_id.to_owned(), transaction_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | .unwrap(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 106 | assert!(client.get_room(room_id).unwrap().send_queue().is_enabled()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | tokio::time::timeout(Duration::from_secs(5), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | let (echoes, _) = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | .get_room(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | .send_queue() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | .subscribe() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | if echoes.is_empty() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | .expect("retried message was not sent"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | drop(server); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | async fn setup() -> ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | ClientHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | EventStream, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 135 | tokio::task::JoinHandle<Result<macltok::Termination, macltok::EngineError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | let room_id = room_id!("!queue-controls:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | server.sync_joined_room(&client, room_id).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | let (engine, handle, mut events) = Engine::from_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | client.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | EngineConfig::default().with_initial_history_limit(0), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | let (opened, _) = tokio::join!(handle.open_room(room_id.to_owned()), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | matches!(event, macltok::Event::TimelineReset { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | opened.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | (server, client, handle, events, running) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | /// sends a message and waits for its local echo, since only the timeline reveals the transaction id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | async fn queue_message( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | handle: &ClientHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | events: &mut EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | body: &str, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | ) -> OwnedTransactionId { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | let send = handle.send_message(room_id.to_owned().into(), MessageContent::text(body)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 172 | let wait_for_echo = async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | tokio::time::timeout(Duration::from_secs(5), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | let event = events.recv().await.expect("event stream closed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | let transaction = support::event_messages(&event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | .into_iter() |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 178 | .find(|event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 179 | support::message(event).is_some_and(|message| message.body == body) |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 180 | }) |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 181 | .and_then(|event| event.transaction_id.clone()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | if let Some(transaction) = transaction { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | return transaction; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | .expect("local echo did not appear") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | let (result, transaction_id) = tokio::join!(send, wait_for_echo); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | result.unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | transaction_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | } |