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