80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1 | mod support; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 2 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 3 | use std::time::Duration; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 4 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 5 | use macltok::{ |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 6 | ClientHandle, Engine, EngineConfig, EngineError, Event, EventStream, HistoryGap, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 7 | HistoryRepairPhase, HistoryRepairPolicy, RecentHistoryState, Termination, TimelineItem, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 8 | }; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 9 | #[cfg(feature = "mirror")] |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 10 | use macltok::{Mirror, TimelineId}; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 11 | use matrix_sdk::{ |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 12 | Client, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 13 | ruma::{EventId, RoomId, api::client::sync::sync_events::v5, event_id, room_id}, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 14 | test_utils::mocks::{MatrixMockServer, RoomMessagesResponseTemplate}, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 15 | }; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 16 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 17 | use support::{event_messages, receive_until, shut_down}; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 18 | use wiremock::ResponseTemplate; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 19 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 20 | struct TestRoom { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 21 | server: MatrixMockServer, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 22 | sync_client: Client, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 23 | handle: ClientHandle, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 24 | events: EventStream, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 25 | running: tokio::task::JoinHandle<Result<Termination, EngineError>>, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 26 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 27 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 28 | async fn start_room(room_id: &RoomId) -> TestRoom { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 29 | let server = MatrixMockServer::new().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 30 | server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 31 | .mock_sliding_sync() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 32 | .ok(v5::Response::new("1".to_owned())) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 33 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 34 | .await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 35 | let client = server.client_builder().build().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 36 | server.mock_room_state_encryption().plain().mount().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 37 | server.sync_joined_room(&client, room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 38 | let sync_client = client.clone(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 39 | let config = EngineConfig::default().with_initial_history_limit(0); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 40 | let (engine, handle, events) = Engine::from_client(client, config).await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 41 | let running = tokio::spawn(engine.run()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 42 | TestRoom { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 43 | server, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 44 | sync_client, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 45 | handle, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 46 | events, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 47 | running, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 48 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 49 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 50 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 51 | async fn open_room(room: &mut TestRoom, room_id: &RoomId) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 52 | room.handle.open_room(room_id.to_owned()).await.unwrap(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 53 | receive_until(&mut room.events, |event| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 54 | matches!(event, Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 55 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 56 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 57 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 58 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 59 | async fn cache_event_before_open(room: &TestRoom, room_id: &RoomId, id: &EventId, body: &str) { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 60 | room.server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 61 | .sync_room( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 62 | &room.sync_client, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 63 | JoinedRoomBuilder::new(room_id).add_timeline_event( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 64 | EventFactory::new() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 65 | .text_msg(body) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 66 | .event_id(id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 67 | .sender(&ALICE), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 68 | ), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 69 | ) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 70 | .await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 71 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 72 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 73 | fn history_event( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 74 | room_id: &RoomId, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 75 | id: &EventId, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 76 | body: &str, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 77 | ) -> matrix_sdk::ruma::serde::Raw<matrix_sdk::ruma::events::AnyTimelineEvent> { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 78 | EventFactory::new() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 79 | .room(room_id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 80 | .text_msg(body) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 81 | .event_id(id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 82 | .sender(&ALICE) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 83 | .into_raw_timeline() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 84 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 85 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 86 | async fn wait_for_recent(room: &mut TestRoom, room_id: &RoomId) -> RecentHistoryState { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 87 | let event = receive_until(&mut room.events, |event| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 88 | matches!(event, Event::RecentHistoryChanged { room_id: id, state, .. } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 89 | if id == room_id && !matches!(state, RecentHistoryState::Loading)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 90 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 91 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 92 | match event { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 93 | Event::RecentHistoryChanged { state, .. } => state, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 94 | _ => unreachable!(), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 95 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 96 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 97 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 98 | async fn wait_for_report(room: &mut TestRoom, room_id: &RoomId) -> macltok::HistoryRepairReport { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 99 | match receive_until( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 100 | &mut room.events, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 101 | |event| matches!(event, Event::HistoryRepairChanged(report) if &report.room_id == room_id), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 102 | ) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 103 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 104 | { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 105 | Event::HistoryRepairChanged(report) => report, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 106 | _ => unreachable!(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 107 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 108 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 109 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 110 | fn gap_in(items: &imbl::Vector<TimelineItem>) -> Option<HistoryGap> { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 111 | items.iter().find_map(|item| match item { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 112 | TimelineItem::HistoryGap(gap) => Some(gap.clone()), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 113 | _ => None, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 114 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 115 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 116 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 117 | #[tokio::test] |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 118 | async fn automatic_recent_load_is_bounded_and_keeps_cached_history_behind_a_gap() { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 119 | let room_id = room_id!("!recent-repair:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 120 | let old_id = event_id!("$old:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 121 | let new_ids = [ |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 122 | event_id!("$fresh-a:localhost"), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 123 | event_id!("$fresh-b:localhost"), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 124 | event_id!("$fresh-c:localhost"), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 125 | event_id!("$fresh-d:localhost"), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 126 | ]; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 127 | let mut room = start_room(room_id).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 128 | room.handle |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 129 | .set_history_repair_policy(HistoryRepairPolicy::Bridge) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 130 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 131 | .unwrap(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 132 | for (index, id) in new_ids.iter().enumerate() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 133 | let token = format!("recent-{index}"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 134 | let response = RoomMessagesResponseTemplate::default() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 135 | .end_token(token.clone()) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 136 | .events(vec![history_event(room_id, id, "recent event")]); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 137 | if index > 0 { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 138 | let previous = format!("recent-{}", index - 1); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 139 | room.server |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 140 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 141 | .match_from(&previous) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 142 | .ok(response) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 143 | .mock_once() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 144 | .mount() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 145 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 146 | } else { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 147 | room.server |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 148 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 149 | .ok(response) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 150 | .mock_once() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 151 | .mount() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 152 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 153 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 154 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 155 | cache_event_before_open(&room, room_id, old_id, "retained old event").await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 156 | open_room(&mut room, room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 157 | let reset = receive_until(&mut room.events, |event| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 158 | let items = match event { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 159 | Event::TimelineReset { timeline_id, items } if timeline_id.room_id() == room_id => { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 160 | items |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 161 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 162 | _ => return false, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 163 | }; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 164 | gap_in(items).is_some() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 165 | && items.iter().any(|item| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 166 | matches!(item, TimelineItem::Event(event) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 167 | if event.event_id.as_deref() == Some(old_id)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 168 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 169 | && new_ids.iter().all(|id| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 170 | items.iter().any(|item| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 171 | matches!(item, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 172 | TimelineItem::Event(event) if event.event_id.as_deref() == Some(id)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 173 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 174 | }) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 175 | }) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 176 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 177 | let Event::TimelineReset { items, .. } = reset else { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 178 | unreachable!(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 179 | }; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 180 | assert!(gap_in(&items).is_some()); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 181 | assert_eq!( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 182 | wait_for_recent(&mut room, room_id).await, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 183 | RecentHistoryState::Loaded |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 184 | ); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 185 | let report = wait_for_report(&mut room, room_id).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 186 | assert_eq!(report.requested_policy, HistoryRepairPolicy::StartRecent); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 187 | assert_eq!(report.phase, HistoryRepairPhase::Ready); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 188 | assert_eq!(report.pages, 4); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 189 | assert_eq!(report.events, 4); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 190 | assert!(report.recent_available); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 191 | let requests = room.server.received_requests().await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 192 | let messages = requests |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 193 | .iter() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 194 | .filter(|request| request.url.path().ends_with("/messages")) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 195 | .collect::<Vec<_>>(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 196 | assert_eq!(messages.len(), 4); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 197 | assert!(!messages[0].url.query_pairs().any(|(key, _)| key == "from")); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 198 | shut_down(&room.handle, &mut room.events, room.running).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 199 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 200 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 201 | #[tokio::test] |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 202 | async fn explicit_recent_start_coalesces_with_the_automatic_load() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 203 | let room_id = room_id!("!recent-coalesce:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 204 | let fresh_id = event_id!("$coalesced:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 205 | let mut room = start_room(room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 206 | room.server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 207 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 208 | .ok(RoomMessagesResponseTemplate::default() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 209 | .with_delay(Duration::from_millis(300)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 210 | .events(vec![history_event(room_id, fresh_id, "shared result")])) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 211 | .mock_once() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 212 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 213 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 214 | open_room(&mut room, room_id).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 215 | tokio::time::timeout(Duration::from_secs(5), async { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 216 | loop { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 217 | let requested = room |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 218 | .server |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 219 | .received_requests() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 220 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 221 | .unwrap() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 222 | .iter() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 223 | .any(|request| request.url.path().ends_with("/messages")); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 224 | if requested { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 225 | break; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 226 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 227 | tokio::task::yield_now().await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 228 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 229 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 230 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 231 | .expect("automatic recent request did not start"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 232 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 233 | let report = room |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 234 | .handle |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 235 | .start_recent_history(room_id.to_owned()) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 236 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 237 | .unwrap(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 238 | assert_eq!(report.pages, 1); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 239 | assert_eq!(report.events, 1); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 240 | assert_eq!( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 241 | wait_for_recent(&mut room, room_id).await, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 242 | RecentHistoryState::Loaded |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 243 | ); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 244 | let requests = room.server.received_requests().await.unwrap(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 245 | assert_eq!( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 246 | requests |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 247 | .iter() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 248 | .filter(|request| request.url.path().ends_with("/messages")) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 249 | .count(), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 250 | 1 |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 251 | ); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 252 | shut_down(&room.handle, &mut room.events, room.running).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 253 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 254 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 255 | #[cfg(feature = "mirror")] |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 256 | #[tokio::test] |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 257 | async fn fill_gap_returns_after_the_mirror_contains_the_committed_rows() { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 258 | let server = MatrixMockServer::new().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 259 | server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 260 | .mock_sliding_sync() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 261 | .ok(v5::Response::new("1".to_owned())) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 262 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 263 | .await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 264 | let client = server.client_builder().build().await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 265 | let room_id = room_id!("!fill-gap-mirror:localhost"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 266 | server.mock_room_state_encryption().plain().mount().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 267 | server.sync_joined_room(&client, room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 268 | let sync_client = client.clone(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 269 | let old_id = event_id!("$gap-old:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 270 | let fresh_id = event_id!("$gap-fresh:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 271 | let middle_id = event_id!("$gap-middle:localhost"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 272 | let (engine, handle, events) = Engine::from_client( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 273 | client, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 274 | EngineConfig::default().with_initial_history_limit(0), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 275 | ) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 276 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 277 | .unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 278 | let running = tokio::spawn(engine.run()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 279 | let (mirror, mirror_handle) = Mirror::new(handle.clone(), events); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 280 | let mirror_running = tokio::spawn(mirror.run()); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 281 | server |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 282 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 283 | .ok(RoomMessagesResponseTemplate::default() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 284 | .end_token("recent-gap") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 285 | .events(vec![history_event(room_id, fresh_id, "new window")])) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 286 | .mock_once() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 287 | .mount() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 288 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 289 | server |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 290 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 291 | .match_from("recent-gap") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 292 | .ok(RoomMessagesResponseTemplate::default().end_token("recent-gap")) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 293 | .mock_once() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 294 | .mount() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 295 | .await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 296 | server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 297 | .sync_room( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 298 | &sync_client, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 299 | JoinedRoomBuilder::new(room_id).add_timeline_event( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 300 | EventFactory::new() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 301 | .text_msg("old retained") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 302 | .event_id(old_id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 303 | .sender(&ALICE), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 304 | ), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 305 | ) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 306 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 307 | handle.open_room(room_id.to_owned()).await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 308 | let timeline_id = TimelineId::from(room_id.to_owned()); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 309 | let gap = tokio::time::timeout(Duration::from_secs(5), async { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 310 | loop { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 311 | if let Some(gap) = mirror_handle |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 312 | .snapshot() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 313 | .timelines |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 314 | .get(&timeline_id) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 315 | .and_then(|timeline| gap_in(&timeline.items)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 316 | { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 317 | break gap; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 318 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 319 | tokio::task::yield_now().await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 320 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 321 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 322 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 323 | .expect("the recent history gap did not reach the mirror"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 324 | tokio::time::timeout(Duration::from_secs(5), async { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 325 | loop { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 326 | if mirror_handle.snapshot().recent_history.get(room_id) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 327 | == Some(&RecentHistoryState::Loaded) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 328 | { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 329 | break; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 330 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 331 | tokio::task::yield_now().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 332 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 333 | }) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 334 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 335 | .expect("automatic recent loading did not finish"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 336 | server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 337 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 338 | .match_from("recent-gap") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 339 | .ok(RoomMessagesResponseTemplate::default() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 340 | .with_delay(Duration::from_millis(300)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 341 | .events(vec![ |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 342 | history_event(room_id, middle_id, "middle page"), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 343 | history_event(room_id, old_id, "old retained overlap"), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 344 | ])) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 345 | .mock_once() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 346 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 347 | .await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 348 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 349 | let fill_handle = handle.clone(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 350 | let target_gap_id = gap.id.clone(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 351 | let fill_gap_id = target_gap_id.clone(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 352 | let fill = tokio::spawn(async move { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 353 | fill_handle |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 354 | .fill_history_gap(room_id.to_owned(), fill_gap_id) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 355 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 356 | }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 357 | tokio::time::timeout(Duration::from_secs(5), async { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 358 | loop { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 359 | let snapshot = mirror_handle.snapshot(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 360 | let active_gap = snapshot.repairs.get(room_id).is_some_and(|report| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 361 | report.gap_id.as_ref() == Some(&target_gap_id) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 362 | && report.phase == HistoryRepairPhase::Bridging |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 363 | }); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 364 | if active_gap { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 365 | break; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 366 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 367 | tokio::task::yield_now().await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 368 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 369 | }) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 370 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 371 | .expect("the active repair report did not identify its target gap"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 372 | fill.await.unwrap().unwrap(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 373 | let snapshot = mirror_handle.snapshot(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 374 | let timeline = snapshot |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 375 | .timelines |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 376 | .get(&timeline_id) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 377 | .expect("the room timeline disappeared"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 378 | let event_ids = timeline |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 379 | .items |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 380 | .iter() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 381 | .filter_map(|item| match item { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 382 | TimelineItem::Event(event) => event.event_id.as_deref(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 383 | _ => None, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 384 | }) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 385 | .collect::<Vec<_>>(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 386 | assert!(event_ids.contains(&fresh_id)); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 387 | assert!(event_ids.contains(&middle_id)); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 388 | assert!(event_ids.contains(&old_id)); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 389 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 390 | handle.shutdown().await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 391 | assert_eq!(running.await.unwrap().unwrap(), Termination::ShutDown); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 392 | mirror_running.await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 393 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 394 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 395 | #[tokio::test] |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 396 | async fn cancelling_a_gap_fill_keeps_committed_progress_for_retry() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 397 | let room_id = room_id!("!cancel-gap-fill:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 398 | let old_id = event_id!("$cancel-gap-old:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 399 | let fresh_id = event_id!("$cancel-gap-fresh:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 400 | let middle_id = event_id!("$cancel-gap-middle:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 401 | let mut room = start_room(room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 402 | room.server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 403 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 404 | .ok(RoomMessagesResponseTemplate::default() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 405 | .end_token("gap-cursor-1") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 406 | .events(vec![history_event(room_id, fresh_id, "recent")])) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 407 | .mock_once() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 408 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 409 | .await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 410 | room.server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 411 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 412 | .match_from("gap-cursor-1") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 413 | .ok(RoomMessagesResponseTemplate::default().end_token("gap-cursor-1")) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 414 | .mock_once() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 415 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 416 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 417 | cache_event_before_open(&room, room_id, old_id, "old cached").await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 418 | open_room(&mut room, room_id).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 419 | let reset = receive_until(&mut room.events, |event| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 420 | matches!(event, Event::TimelineReset { timeline_id, items } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 421 | if timeline_id.room_id() == room_id && gap_in(items).is_some()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 422 | }) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 423 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 424 | let gap = match reset { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 425 | Event::TimelineReset { items, .. } => gap_in(&items).unwrap(), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 426 | _ => unreachable!(), |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 427 | }; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 428 | assert_eq!( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 429 | wait_for_recent(&mut room, room_id).await, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 430 | RecentHistoryState::Loaded |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 431 | ); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 432 | room.server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 433 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 434 | .match_from("gap-cursor-1") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 435 | .ok(RoomMessagesResponseTemplate::default() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 436 | .end_token("gap-cursor-2") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 437 | .events(vec![history_event( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 438 | room_id, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 439 | middle_id, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 440 | "committed progress", |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 441 | )])) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 442 | .mock_once() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 443 | .mount() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 444 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 445 | room.server |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 446 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 447 | .match_from("gap-cursor-2") |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 448 | .ok(RoomMessagesResponseTemplate::default() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 449 | .with_delay(Duration::from_secs(5)) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 450 | .events(vec![history_event(room_id, old_id, "old overlap")])) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 451 | .mock_once() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 452 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 453 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 454 | let fill_handle = room.handle.clone(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 455 | let gap_id = gap.id.clone(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 456 | let fill = tokio::spawn(async move { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 457 | fill_handle |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 458 | .fill_history_gap(room_id.to_owned(), gap_id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 459 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 460 | }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 461 | tokio::time::timeout(Duration::from_secs(5), async { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 462 | loop { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 463 | let requested = room |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 464 | .server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 465 | .received_requests() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 466 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 467 | .unwrap() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 468 | .iter() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 469 | .any(|request| { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 470 | request.url.path().ends_with("/messages") |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 471 | && request |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 472 | .url |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 473 | .query_pairs() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 474 | .any(|(key, value)| key == "from" && value == "gap-cursor-2") |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 475 | }); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 476 | if requested { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 477 | break; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 478 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 479 | tokio::task::yield_now().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 480 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 481 | }) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 482 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 483 | .expect("the second gap page did not start"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 484 | room.handle |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 485 | .cancel_history_repair(room_id.to_owned()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 486 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 487 | .unwrap(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 488 | let error = fill.await.unwrap().unwrap_err(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 489 | assert_eq!(error.kind, macltok::ErrorKind::Interrupted); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 490 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 491 | room.server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 492 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 493 | .match_from("gap-cursor-2") |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 494 | .ok( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 495 | RoomMessagesResponseTemplate::default().events(vec![history_event( |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 496 | room_id, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 497 | old_id, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 498 | "old overlap", |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 499 | )]), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 500 | ) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 501 | .mock_once() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 502 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 503 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 504 | room.handle |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 505 | .fill_history_gap(room_id.to_owned(), gap.id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 506 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 507 | .unwrap(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 508 | let final_reset = receive_until(&mut room.events, |event| { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 509 | matches!(event, Event::TimelineReset { timeline_id, items } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 510 | if timeline_id.room_id() == room_id |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 511 | && items.iter().any(|item| matches!(item, TimelineItem::Event(event) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 512 | if event.event_id.as_deref() == Some(middle_id))) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 513 | && items.iter().any(|item| matches!(item, TimelineItem::Event(event) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 514 | if event.event_id.as_deref() == Some(old_id)))) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 515 | }) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 516 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 517 | let ids = event_messages(&final_reset) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 518 | .iter() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 519 | .filter_map(|event| event.event_id.as_deref()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 520 | .collect::<Vec<_>>(); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 521 | assert!(ids.contains(&fresh_id)); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 522 | assert!(ids.contains(&middle_id)); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 523 | assert!(ids.contains(&old_id)); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 524 | shut_down(&room.handle, &mut room.events, room.running).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 525 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 526 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 527 | #[tokio::test] |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 528 | async fn failed_automatic_recent_load_is_public_and_keeps_the_cached_timeline() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 529 | let room_id = room_id!("!failed-recent:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 530 | let old_id = event_id!("$failed-old:localhost"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 531 | let mut room = start_room(room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 532 | room.server |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 533 | .mock_room_messages() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 534 | .respond_with(ResponseTemplate::new(403).set_body_json(serde_json::json!({ |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 535 | "errcode": "M_FORBIDDEN", |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 536 | "error": "history is unavailable", |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 537 | }))) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 538 | .mock_once() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 539 | .mount() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 540 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 541 | cache_event_before_open(&room, room_id, old_id, "kept event").await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 542 | open_room(&mut room, room_id).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 543 | let state = wait_for_recent(&mut room, room_id).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 544 | assert!(matches!(state, RecentHistoryState::Failed(_))); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 545 | let report = wait_for_report(&mut room, room_id).await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 546 | assert_eq!(report.phase, HistoryRepairPhase::Paused); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 547 | assert!(report.failure.is_some()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 548 | shut_down(&room.handle, &mut room.events, room.running).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 549 | } |