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