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 | Command, CommandErrorKind, Engine, EngineConfig, Event, HistoryState, LifecycleCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | MediaCommand, MessageContent, RoomCommand, TimelineCommand, TimelineDiff, TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | TimelineItem, |
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::{api::client::sync::sync_events::v5, event_id, mxc_uri, room_id}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | test_utils::mocks::{MatrixMockServer, RoomMessagesResponseTemplate}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
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 opens_a_room_and_sends_messages() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | let room_id = room_id!("!toolkit:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | .body_matches_partial_json(serde_json::json!({ "body": "hello from macltok" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | .ok(event_id!("$sent:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | |event| matches!(event, Event::TimelineReset { timeline_id: reset, .. } if reset.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | .send(Command::Timeline(TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | content: MessageContent::text("hello from macltok"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | let sent = server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | .any(|request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | request.url.path().contains("/send/m.room.message/") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | && serde_json::from_slice::<serde_json::Value>(&request.body) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | .is_ok_and(|body| body["body"] == "hello from macltok") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | if sent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | .expect("message did not reach the homeserver"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | server.mock_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | server.mock_authed_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | JoinedRoomBuilder::new(room_id).add_timeline_event( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | EventFactory::new() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | .image( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | "image.jpg".into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | mxc_uri!("mxc://localhost/image").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | .sender(&ALICE), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | let media_event = receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | event_messages(event).any(|message| message.media.is_some()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | let media_id = event_messages(&media_event) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | .find_map(|message| message.media.as_ref().map(|media| media.id.clone())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | .send(Command::Media(MediaCommand::Fetch { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | id: media_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | size: macltok::MediaSize::Full, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | let media = receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | matches!(event, Event::MediaReady { media_id: received, .. } if received == &media_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | assert!(matches!(media, Event::MediaReady { bytes, .. } if !bytes.is_empty())); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | .send(Command::Lifecycle(LifecycleCommand::Shutdown)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | running.await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | async fn keeps_open_room_timelines_independent() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | let first_id = room_id!("!first:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | let second_id = room_id!("!second:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | server.sync_joined_room(&client, first_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | server.sync_joined_room(&client, second_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | .send(Command::Room(RoomCommand::Open(first_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | .send(Command::Room(RoomCommand::Open(second_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | |event| matches!(event, Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == first_id), |
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 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | |event| matches!(event, Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == second_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | wait_for_room_subscriptions(&server, &[first_id, second_id]).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | let factory = EventFactory::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | JoinedRoomBuilder::new(first_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | .add_timeline_event(factory.text_msg("first update").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | JoinedRoomBuilder::new(second_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | .add_timeline_event(factory.text_msg("second update").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | let mut first_updated = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | let mut second_updated = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | while !(first_updated && second_updated) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | let event = events.recv().await.expect("event stream closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | if let Event::TimelineChanged { timeline_id, .. } = &event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | first_updated |= timeline_id.room_id() == first_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | && event_messages(&event).any(|message| message.body == "first update"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | second_updated |= timeline_id.room_id() == second_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | && event_messages(&event).any(|message| message.body == "second update"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 186 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | .expect("both retained rooms did not update"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | .send(Command::Room(RoomCommand::Close(first_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | JoinedRoomBuilder::new(second_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | .add_timeline_event(factory.text_msg("still live").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | matches!(event, Event::TimelineChanged { timeline_id, .. } if timeline_id.room_id() == second_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | && event_messages(event).any(|message| message.body == "still live") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | .send(Command::Lifecycle(LifecycleCommand::Shutdown)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | running.await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | async fn resync_replaces_timeline_before_ack_and_preserves_following_diffs() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | let room_id = room_id!("!resync:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | matches!(event, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 239 | Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | .add_timeline_event(EventFactory::new().text_msg("before reset").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | event_messages(event).any(|message| message.body == "before reset") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | let id = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | .send(Command::Timeline(TimelineCommand::Resync { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | let mut reset = None; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | match events.recv().await.expect("event stream closed") { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | Event::TimelineReset { timeline_id, items } if timeline_id.room_id() == room_id => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | reset = Some(items); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | Event::CommandSucceeded { id: completed, .. } if completed == id => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | assert!(reset.is_some(), "resync completed before its reset"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | Event::CommandFailed(failure) if failure.id == id => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | panic!("resync failed: {failure:?}") |
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 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 279 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 280 | .expect("resync did not complete"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | let mut items = reset.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | assert!(items.iter().any(|item| matches!(item, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | TimelineItem::Message(message) if message.body == "before reset" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | ))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | .add_timeline_event(EventFactory::new().text_msg("after reset").sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 293 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 294 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 295 | if let Event::TimelineChanged { timeline_id, diffs } = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | events.recv().await.expect("event stream closed") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | && timeline_id.room_id() == room_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 298 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | for diff in diffs { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | diff.try_apply(&mut items) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | .expect("resynced snapshot accepts following diffs"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | if items.iter().any(|item| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | matches!(item, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 305 | TimelineItem::Message(message) if message.body == "after reset" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 306 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | }) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 311 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | .expect("resynced timeline did not update"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | .send(Command::Lifecycle(LifecycleCommand::Shutdown)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | running.await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | async fn closes_and_reopens_a_room_in_command_order() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | let room_id = room_id!("!reopen:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | let initial_open = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | |event| matches!(event, Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | |event| matches!(event, Event::CommandSucceeded { id, .. } if id == &initial_open), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | let close = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | .send(Command::Room(RoomCommand::Close(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | let replacement_open = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | let second_close = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | .send(Command::Room(RoomCommand::Close(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | let final_open = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 369 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | let expected = [close, replacement_open, second_close, final_open]; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | let mut completed = 0; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | let mut resets = 0; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | tokio::time::timeout(Duration::from_secs(3), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | while completed < expected.len() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | match events.recv().await.expect("event stream closed") { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | resets += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | assert!(resets <= 2, "a room generation reset more than once"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | Event::CommandSucceeded { id, .. } if expected.contains(&id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | id, expected[completed], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | "room lifecycle commands completed out of order" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | if completed == 1 || completed == 3 { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | resets, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | completed / 2 + 1, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | "room open completed before timeline reset" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | completed += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | Event::CommandFailed(failure) if expected.contains(&failure.id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | panic!("room lifecycle command failed: {failure:?}"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | _ => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | .expect("room lifecycle commands did not complete"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | .send(Command::Lifecycle(LifecycleCommand::Shutdown)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 408 | receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 409 | running.await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 411 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 412 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 413 | async fn thread_command_waits_for_opening_timeline() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 414 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 415 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | let room_id = room_id!("!thread:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | let root = event_id!("$thread-root:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 425 | let (engine, handle, mut events) = Engine::from_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 426 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 427 | EngineConfig::default().with_initial_history_limit(0), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 428 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 429 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 430 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 431 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 432 | let room_open = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 433 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 438 | |event| matches!(event, Event::CommandSucceeded { id, .. } if *id == room_open), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | let open = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | .send(Command::Timeline(TimelineCommand::OpenThread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | room_id: room_id.to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | root_event_id: root.to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | let send = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 450 | .send(Command::Timeline(TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 451 | timeline_id: TimelineId::thread(room_id.to_owned(), root.to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 452 | content: MessageContent::text(""), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 453 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 454 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 455 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 456 | let resync = handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 457 | .send(Command::Timeline(TimelineCommand::Resync { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 458 | timeline_id: TimelineId::thread(room_id.to_owned(), root.to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 459 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 460 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 461 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 462 | let mut open_succeeded = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | tokio::time::timeout(Duration::from_secs(3), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | match events.recv().await.expect("event stream closed") { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | Event::CommandSucceeded { id, .. } if id == open => open_succeeded = true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | Event::CommandFailed(failure) if failure.id == send => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | assert!(open_succeeded, "thread command completed before opening"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | assert_eq!(failure.kind, CommandErrorKind::EmptyBody); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 471 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 472 | Event::CommandFailed(failure) if failure.id == open => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 473 | panic!("thread open failed: {failure:?}"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | _ => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 477 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | .expect("thread commands did not complete"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | let mut reset_seen = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | tokio::time::timeout(Duration::from_secs(3), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 484 | match events.recv().await.expect("event stream closed") { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 485 | Event::TimelineReset { timeline_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 486 | if timeline_id.thread_root().is_some_and(|event| event == root) => |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 487 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 488 | reset_seen = true |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 489 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 490 | Event::CommandSucceeded { id, .. } if id == resync => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 491 | assert!(reset_seen, "thread resync completed before its reset"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 494 | Event::CommandFailed(failure) if failure.id == resync => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | panic!("thread resync failed: {failure:?}") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 496 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 497 | _ => {} |
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 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | .expect("thread resync did not complete"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | .send(Command::Lifecycle(LifecycleCommand::Shutdown)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 506 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 507 | receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | running.await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 511 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 512 | async fn pagination_reports_and_remembers_start_of_history() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 513 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 514 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 515 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 516 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 517 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 518 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 519 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 520 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 521 | let room_id = room_id!("!history:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 522 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 523 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 524 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 525 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 526 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 527 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 528 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 529 | .send(Command::Room(RoomCommand::Open(room_id.to_owned()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 530 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 531 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 532 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 533 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 534 | |event| matches!(event, Event::TimelineReset { timeline_id: reset, .. } if reset.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 535 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 536 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 537 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 538 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 539 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 540 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 541 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 542 | .add_timeline_event(EventFactory::new().text_msg("latest").sender(&ALICE)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 543 | .set_timeline_limited() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 544 | .set_timeline_prev_batch("before-latest"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 545 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 546 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 547 | receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 548 | matches!(event, Event::TimelineChanged { timeline_id: changed, .. } if changed.room_id() == room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 549 | && event_messages(event).any(|message| message.body == "latest") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 550 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 551 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 552 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 553 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 554 | .mock_room_messages() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 555 | .match_from("before-latest") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 556 | .ok(RoomMessagesResponseTemplate::default()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 557 | .mock_once() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 558 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 559 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 560 | let mut reached_start = false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 561 | for _ in 0..3 { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 562 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 563 | .send(Command::Timeline(TimelineCommand::Paginate { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 564 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 565 | limit: 10, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 566 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | receive_history(&mut events, room_id, HistoryState::Loading).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 570 | match receive_history_terminal(&mut events, room_id).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 571 | HistoryState::StartReached => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | reached_start = true; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 573 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 574 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | HistoryState::Idle => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 576 | state => panic!("unexpected pagination state {state:?}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 577 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 580 | reached_start, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | "pagination did not reach the mocked room start" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 583 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | .send(Command::Timeline(TimelineCommand::Paginate { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 586 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 587 | limit: 10, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 589 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 590 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | receive_history(&mut events, room_id, HistoryState::StartReached).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | .send(Command::Lifecycle(LifecycleCommand::Shutdown)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 596 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 597 | receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 598 | running.await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 599 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 600 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 601 | async fn receive_history( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 602 | events: &mut macltok::EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 603 | room_id: &matrix_sdk::ruma::RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 604 | expected: HistoryState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 605 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 606 | receive_until(events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 607 | matches!(event, Event::HistoryChanged { timeline_id: changed, state } if changed.room_id() == room_id && state == &expected) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 608 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 610 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 611 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 612 | fn event_messages(event: &Event) -> std::vec::IntoIter<&macltok::TimelineMessage> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | let items = match event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | Event::TimelineReset { items, .. } => items.iter().collect::<Vec<_>>(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | Event::TimelineChanged { diffs, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 616 | diffs.iter().flat_map(diff_items).collect::<Vec<_>>() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 617 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 618 | _ => Vec::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 619 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 620 | items |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 621 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 622 | .filter_map(|item| match item { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 623 | TimelineItem::Message(message) => Some(message.as_ref()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 624 | _ => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 625 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 626 | .collect::<Vec<_>>() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 627 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 628 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 629 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 630 | fn diff_items(diff: &TimelineDiff) -> Vec<&TimelineItem> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 631 | match diff { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 632 | TimelineDiff::Append { values } | TimelineDiff::Reset { values } => values.iter().collect(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 633 | TimelineDiff::PushFront { value } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 634 | | TimelineDiff::PushBack { value } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 635 | | TimelineDiff::Insert { value, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 636 | | TimelineDiff::Set { value, .. } => vec![value], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 637 | TimelineDiff::Clear |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 638 | | TimelineDiff::PopFront |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 639 | | TimelineDiff::PopBack |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 640 | | TimelineDiff::Remove { .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 641 | | TimelineDiff::Truncate { .. } => Vec::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 642 | _ => Vec::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 643 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 644 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 645 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 646 | async fn receive_history_terminal( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 647 | events: &mut macltok::EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 648 | room_id: &matrix_sdk::ruma::RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 649 | ) -> HistoryState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 650 | match receive_until(events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 651 | 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 | 652 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 653 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 654 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 655 | Event::HistoryChanged { state, .. } => state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 656 | _ => unreachable!(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 657 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 658 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 659 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 660 | async fn wait_for_room_subscriptions( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 661 | server: &MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 662 | room_ids: &[&matrix_sdk::ruma::RoomId], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 663 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 664 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 665 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 666 | let found = server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 667 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 668 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 669 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 670 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 671 | .filter_map(|request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 672 | serde_json::from_slice::<serde_json::Value>(&request.body).ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 673 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 674 | .filter_map(|body| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 675 | body.get("room_subscriptions") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 676 | .and_then(|value| value.as_object()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 677 | .cloned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 678 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 679 | .any(|subscriptions| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 680 | room_ids |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 681 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 682 | .all(|room_id| subscriptions.contains_key(room_id.as_str())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 683 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 684 | if found { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 685 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 686 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 687 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 688 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 689 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 690 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 691 | .expect("Sliding Sync did not retain every open room subscription"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 692 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 693 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 694 | async fn receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 695 | events: &mut macltok::EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 696 | predicate: impl Fn(&Event) -> bool, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 697 | ) -> Event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 698 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 699 | while let Some(event) = events.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 700 | if predicate(&event) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 701 | return event; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 702 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 703 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 704 | panic!("event stream closed before the expected event"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 705 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 706 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 707 | .expect("expected event did not arrive") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 708 | } |