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, ErrorKind, Event, EventStream, HistoryState, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 7 | MediaSize, MessageContent, RoomCommand, TimelineCommand, TimelineId, TimelineItem, |
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::{api::client::sync::sync_events::v5, event_id, mxc_uri, room_id}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | test_utils::mocks::{MatrixMockServer, RoomMessagesResponseTemplate}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | use support::{event_messages, receive_until, shut_down}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | async fn opens_a_room_and_sends_messages() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | let room_id = room_id!("!toolkit:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | .body_matches_partial_json(serde_json::json!({ "body": "hello from macltok" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | .ok(event_id!("$sent:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | handle.open_room(room_id.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | |event| matches!(event, Event::TimelineReset { timeline_id: reset, .. } if reset.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | .send_message( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | room_id.to_owned().into(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | MessageContent::text("hello from macltok"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | let sent = server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | .any(|request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | request.url.path().contains("/send/m.room.message/") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | && serde_json::from_slice::<serde_json::Value>(&request.body) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | .is_ok_and(|body| body["body"] == "hello from macltok") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | if sent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | .expect("message did not reach the homeserver"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | server.mock_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | server.mock_authed_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | JoinedRoomBuilder::new(room_id).add_timeline_event( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | EventFactory::new() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | .image( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | "image.jpg".into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | mxc_uri!("mxc://localhost/image").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | .sender(&ALICE), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | let media_event = receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | event_messages(event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | .into_iter() |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 94 | .any(|event| support::media_of(event).is_some()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | let media_id = event_messages(&media_event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | .into_iter() |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 99 | .find_map(|event| support::media_of(event).map(|media| media.id.clone())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | let media = handle.media(&media_id, MediaSize::Full).await.unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | assert!(!media.bytes.is_empty()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | async fn keeps_open_room_timelines_independent() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | let first_id = room_id!("!first:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | let second_id = room_id!("!second:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | server.sync_joined_room(&client, first_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | server.sync_joined_room(&client, second_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | let (first_open, second_open) = tokio::join!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | handle.open_room(first_id.to_owned()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | handle.open_room(second_id.to_owned()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | first_open.unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | second_open.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | |event| matches!(event, Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == first_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | |event| matches!(event, Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == second_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | wait_for_room_subscriptions(&server, &[first_id, second_id]).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | let factory = EventFactory::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | JoinedRoomBuilder::new(first_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | .add_timeline_event(factory.text_msg("first update").sender(&ALICE)), |
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 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | JoinedRoomBuilder::new(second_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | .add_timeline_event(factory.text_msg("second update").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | let mut first_updated = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | let mut second_updated = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | while !(first_updated && second_updated) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | let event = events.recv().await.expect("event stream closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | if let Event::TimelineChanged { timeline_id, .. } = &event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | first_updated |= timeline_id.room_id() == first_id |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 167 | && event_messages(&event).into_iter().any(|event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 168 | support::message(event) |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 169 | .is_some_and(|message| message.body == "first update") |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 170 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | second_updated |= timeline_id.room_id() == second_id |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 172 | && event_messages(&event).into_iter().any(|event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 173 | support::message(event) |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 174 | .is_some_and(|message| message.body == "second update") |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 175 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | .expect("both retained rooms did not update"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | handle.close_room(first_id.to_owned()).await.unwrap(); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 183 | receive_until(&mut events, |event| { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 184 | matches!(event, Event::TimelineClosed { timeline_id } if timeline_id.room_id() == first_id) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 185 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 186 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | JoinedRoomBuilder::new(second_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | .add_timeline_event(factory.text_msg("still live").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | matches!(event, Event::TimelineChanged { timeline_id, .. } if timeline_id.room_id() == second_id) |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 196 | && event_messages(event).into_iter().any(|event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 197 | support::message(event).is_some_and(|message| message.body == "still live") |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 198 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | async fn resync_replaces_timeline_before_ack_and_preserves_following_diffs() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | let room_id = room_id!("!resync:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | handle.open_room(room_id.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | matches!(event, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | .add_timeline_event(EventFactory::new().text_msg("before reset").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | receive_until(&mut events, |event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 237 | event_messages(event).into_iter().any(|event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 238 | support::message(event).is_some_and(|message| message.body == "before reset") |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 239 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | let mut items = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | resync_and_confirm_reset_first(&handle, &mut events, room_id.to_owned().into()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | assert!(items.iter().any(|item| matches!(item, |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 246 | TimelineItem::Event(event) if support::message(event).is_some_and(|message| message.body == "before reset") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | ))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | .add_timeline_event(EventFactory::new().text_msg("after reset").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | if let Event::TimelineChanged { timeline_id, diffs } = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | events.recv().await.expect("event stream closed") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | && timeline_id.room_id() == room_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | for diff in diffs { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | diff.try_apply(&mut items) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | .expect("resynced snapshot accepts following diffs"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | if items.iter().any(|item| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | matches!(item, |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 268 | TimelineItem::Event(event) if support::message(event).is_some_and(|message| message.body == "after reset") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | }) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | .expect("resynced timeline did not update"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 280 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | async fn closes_and_reopens_a_room_in_command_order() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | let room_id = room_id!("!reopen:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 293 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 294 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 295 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | handle.open_room(room_id.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | |event| matches!(event, Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | let mut close = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | .try_send(Command::Room(RoomCommand::Close(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | let mut replacement_open = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | .try_send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | let mut second_close = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | .try_send(Command::Room(RoomCommand::Close(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | let mut final_open = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | .try_send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 318 | // biased select drains buffered events before it reports ready replies |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | let mut resets = 0; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | let mut order = Vec::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | let (mut close_done, mut replacement_open_done, mut second_close_done, mut final_open_done) = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | (false, false, false, false); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | tokio::time::timeout(Duration::from_secs(3), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | while order.len() < 4 { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | biased; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | event = events.recv() => { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 328 | match event.expect("event stream closed") { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 329 | Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id => { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 330 | resets += 1; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 331 | assert!(resets <= 2, "a room generation reset more than once"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 332 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 333 | _ => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | result = &mut close, if !close_done => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | result.expect("close failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | close_done = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | order.push("close"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | result = &mut replacement_open, if !replacement_open_done => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | result.expect("replacement open failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | replacement_open_done = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | order.push("replacement_open"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | result = &mut second_close, if !second_close_done => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | result.expect("second close failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | second_close_done = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | order.push("second_close"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | result = &mut final_open, if !final_open_done => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | result.expect("final open failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | final_open_done = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | order.push("final_open"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | .expect("room lifecycle commands did not complete"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | assert_eq!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | order, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | ["close", "replacement_open", "second_close", "final_open"] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | assert_eq!(resets, 2, "each open should have reset the timeline once"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 369 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | async fn thread_command_waits_for_opening_timeline() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | let room_id = room_id!("!thread:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | let root = event_id!("$thread-root:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | let (engine, handle, mut events) = Engine::from_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | EngineConfig::default().with_initial_history_limit(0), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | handle.open_room(room_id.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | let mut open = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | .try_send(Command::Timeline(TimelineCommand::OpenThread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | room_id: room_id.to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | root_event_id: root.to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | let mut send = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | .try_send(Command::Timeline(TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | timeline_id: TimelineId::thread(room_id.to_owned(), root.to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | content: MessageContent::text(""), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | let mut open_succeeded = false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | let mut send_done = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | tokio::time::timeout(Duration::from_secs(3), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | while !send_done { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | biased; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | result = &mut open, if !open_succeeded => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | result.expect("thread open failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | open_succeeded = true; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 413 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | result = &mut send, if !send_done => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | assert!(open_succeeded, "thread command completed before opening"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | let error = result.expect_err("empty body send unexpectedly succeeded"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | assert_eq!(error.kind, ErrorKind::EmptyBody); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | send_done = true; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | .expect("thread commands did not complete"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 426 | resync_and_confirm_reset_first( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | &handle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | &mut events, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | TimelineId::thread(room_id.to_owned(), root.to_owned()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 432 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 433 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | async fn pagination_reports_and_remembers_start_of_history() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 438 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | let room_id = room_id!("!history:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 450 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 451 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 452 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 453 | handle.open_room(room_id.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 454 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 455 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 456 | |event| matches!(event, Event::TimelineReset { timeline_id: reset, .. } if reset.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 457 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 458 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 459 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 460 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 461 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 462 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | .add_timeline_event(EventFactory::new().text_msg("latest").sender(&ALICE)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | .set_timeline_limited() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | .set_timeline_prev_batch("before-latest"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | matches!(event, Event::TimelineChanged { timeline_id: changed, .. } if changed.room_id() == room_id) |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 471 | && event_messages(event).into_iter().any(|event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 472 | support::message(event).is_some_and(|message| message.body == "latest") |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 473 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 477 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | .mock_room_messages() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | .match_from("before-latest") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | .ok(RoomMessagesResponseTemplate::default()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | .mock_once() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 484 | let mut reached_start = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 485 | for _ in 0..3 { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 486 | handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 487 | .paginate(room_id.to_owned().into(), 10) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 488 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 489 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 490 | receive_history(&mut events, room_id, HistoryState::Loading).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 491 | match receive_history_terminal(&mut events, room_id).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | HistoryState::StartReached => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | reached_start = true; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 494 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 496 | HistoryState::Idle => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 497 | state => panic!("unexpected pagination state {state:?}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 499 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 500 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | reached_start, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | "pagination did not reach the mocked room start" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 506 | .paginate(room_id.to_owned().into(), 10) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 507 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | receive_history(&mut events, room_id, HistoryState::StartReached).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 511 | shut_down(&handle, &mut events, running).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 514 | /// resyncs `timeline_id` and confirms its `TimelineReset` was already on the |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 515 | /// stream by the time the resync command answered, racing the two so we |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 516 | /// don't have to drain the whole stream first |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 517 | async fn resync_and_confirm_reset_first( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 518 | handle: &ClientHandle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 519 | events: &mut EventStream, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 520 | timeline_id: TimelineId, |
d5d8097c3afd | atagen | 2026-09-26 09:33 UTC | 521 | ) -> macltok::imbl::Vector<TimelineItem> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 522 | let mut pending = handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 523 | .send(Command::Timeline(TimelineCommand::Resync { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 524 | timeline_id: timeline_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 525 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 526 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 527 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 528 | let mut reset_items = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 529 | tokio::time::timeout(Duration::from_secs(3), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 530 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 531 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 532 | biased; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 533 | event = events.recv() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 534 | if let Event::TimelineReset { timeline_id: reset_id, items } = event.expect("event stream closed") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 535 | && reset_id == timeline_id |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 536 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 537 | reset_items = Some(items); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 538 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 539 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 540 | result = &mut pending => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 541 | result.expect("resync failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 542 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 543 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 544 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 545 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 546 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 547 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 548 | .expect("resync did not complete"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 549 | reset_items.expect("resync completed before its reset") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 550 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 551 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 552 | async fn receive_history( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 553 | events: &mut EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 554 | room_id: &matrix_sdk::ruma::RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 555 | expected: HistoryState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 556 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 557 | receive_until(events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 558 | matches!(event, Event::HistoryChanged { timeline_id: changed, state } if changed.room_id() == room_id && state == &expected) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 559 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 560 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 561 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 562 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 563 | async fn receive_history_terminal( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 564 | events: &mut EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 565 | room_id: &matrix_sdk::ruma::RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 566 | ) -> HistoryState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | match receive_until(events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | matches!(event, Event::HistoryChanged { timeline_id: changed, state } if changed.room_id() == room_id && !matches!(state, HistoryState::Loading)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 570 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 571 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | Event::HistoryChanged { state, .. } => state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 573 | _ => unreachable!(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 574 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 576 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 577 | async fn wait_for_room_subscriptions( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | server: &MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | room_ids: &[&matrix_sdk::ruma::RoomId], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 580 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 583 | let found = server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 586 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 587 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | .filter_map(|request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 589 | serde_json::from_slice::<serde_json::Value>(&request.body).ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 590 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | .filter_map(|body| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | body.get("room_subscriptions") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | .and_then(|value| value.as_object()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | .cloned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 596 | .any(|subscriptions| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 597 | room_ids |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 598 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 599 | .all(|room_id| subscriptions.contains_key(room_id.as_str())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 600 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 601 | if found { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 602 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 603 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 604 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 605 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 606 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 607 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 608 | .expect("Sliding Sync did not retain every open room subscription"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | } |