cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | mod support; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | use std::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | collections::{BTreeMap, HashSet}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | sync::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | Arc, Mutex, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | atomic::{AtomicUsize, Ordering}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | time::Duration, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | #[cfg(feature = "sqlite")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 13 | use macltok::{EncryptedSessionStore, PasswordLogin, Restore, Session, SessionStore, StoreConfig}; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 14 | use macltok::{Engine, EngineConfig, Event, MentionReason, MentionsPage}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | use matrix_sdk::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | Client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 17 | ruma::{OwnedRoomId, OwnedUserId, event_id, events::Mentions, room_id, user_id}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | test_utils::mocks::{MatrixMockServer, RoomContextResponseTemplate}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 21 | use ruma::push::{ |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 22 | Action, EventMatchConditionData, HighlightTweakValue, NewConditionalPushRule, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 23 | NewPatternedPushRule, PushCondition, Ruleset, SoundTweakValue, Tweak, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 24 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | use serde_json::{Value, json}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | #[cfg(feature = "sqlite")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 27 | use support::receive_until; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 28 | use support::shut_down; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 29 | use wiremock::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | Mock, Request, Respond, ResponseTemplate, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 31 | matchers::{body_string_contains, method, path, path_regex, query_param}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 32 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 33 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | #[cfg(feature = "serde")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 35 | use macltok::{MentionCoverage, MentionsBoundary, MentionsCommand}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | const SLIDING_SYNC_PATH: &str = "/_matrix/client/unstable/org.matrix.simplified_msc3575/sync"; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | struct SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | rooms: Arc<Mutex<BTreeMap<OwnedRoomId, Vec<Value>>>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | required_state: Arc<Mutex<BTreeMap<OwnedRoomId, Vec<Value>>>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | requests: Arc<AtomicUsize>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 43 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 45 | impl SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 46 | fn new(room_id: OwnedRoomId, events: Vec<Value>) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 47 | Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 48 | rooms: Arc::new(Mutex::new(BTreeMap::from([(room_id, events)]))), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 49 | required_state: Arc::new(Mutex::new(BTreeMap::new())), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 50 | requests: Arc::new(AtomicUsize::new(0)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 51 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 52 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 54 | fn replace(&self, room_id: &ruma::RoomId, events: Vec<Value>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 55 | self.rooms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 56 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | .insert(room_id.to_owned(), events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | fn add_room(&self, room_id: &ruma::RoomId, events: Vec<Value>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 62 | self.rooms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | .insert(room_id.to_owned(), events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | fn set_required_state(&self, room_id: &ruma::RoomId, events: Vec<Value>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | self.required_state |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | .insert(room_id.to_owned(), events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 74 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | async fn mount(&self, server: &MatrixMockServer) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | Mock::given(method("POST")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | .and(path(SLIDING_SYNC_PATH)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | .respond_with(self.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | impl Clone for SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | fn clone(&self) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | rooms: self.rooms.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | required_state: self.required_state.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | requests: self.requests.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 91 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | impl Respond for SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | fn respond(&self, _request: &Request) -> ResponseTemplate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | let request = self.requests.fetch_add(1, Ordering::AcqRel); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | let required_state = self.required_state.lock().unwrap().clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | let rooms: serde_json::Map<String, Value> = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | .rooms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | .map(|(room_id, events)| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | room_id.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | "initial": request == 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | "required_state": required_state |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | .get(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | .cloned() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | .unwrap_or_default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | "timeline": events, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | }), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | ResponseTemplate::new(200) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | .set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | "txn_id": null, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | "pos": format!("mentions-{request}"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | "lists": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | "rooms": rooms, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | "extensions": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | .set_delay(Duration::from_millis(20)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | async fn joined_client(server: &MatrixMockServer, room_id: &ruma::RoomId) -> (Client, OwnedUserId) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | let client = server.client_builder().build().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | let own_user = client.user_id().unwrap().to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | let room_id = room_id.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | .mock_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 135 | .ok_and_run(&client, { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | let own_user = own_user.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | move |builder| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | let factory = EventFactory::new().room(&room_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | builder.add_global_account_data( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | factory.push_rules(Ruleset::server_default(&own_user)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | builder.add_joined_room( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | JoinedRoomBuilder::new(&room_id).add_state_event(factory.member(&own_user)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 144 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 145 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 146 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | (client, own_user) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | fn message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | event_id: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | sender: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | timestamp: u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | body: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | mentioned: Option<&str>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | ) -> Value { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | let mut content = json!({ "msgtype": "m.text", "body": body }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | if let Some(mentioned) = mentioned { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | content["m.mentions"] = json!({ "user_ids": [mentioned] }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | "type": "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | "event_id": event_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 166 | "origin_server_ts": timestamp, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 167 | "content": content, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 168 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 169 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 170 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 171 | fn room_mention(event_id: &str, sender: &str, timestamp: u64) -> Value { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 172 | let mut event = message(event_id, sender, timestamp, "room mention", None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 173 | event["content"]["m.mentions"] = json!({ "room": true }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 174 | event |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 175 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 176 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 177 | async fn mentions_page( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 178 | events: &mut macltok::EventStream, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 179 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 180 | predicate: impl Fn(&MentionsPage) -> bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 181 | ) -> MentionsPage { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 182 | mentions_page_with_diagnostics(events, room_id, predicate, None).await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 183 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | async fn mentions_page_with_diagnostics( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 186 | events: &mut macltok::EventStream, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 188 | predicate: impl Fn(&MentionsPage) -> bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 189 | diagnostic_server: Option<&MatrixMockServer>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 190 | ) -> MentionsPage { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | let mut last_page = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 192 | let result = tokio::time::timeout(Duration::from_secs(5), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 193 | while let Some(event) = events.recv().await { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 194 | match event { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 195 | Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 196 | panic!("the unopened room produced a timeline reset"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 197 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 198 | Event::MentionsChanged(update) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 199 | let snapshot = update.snapshot; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 200 | let matched = predicate(&snapshot); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | last_page = Some(snapshot.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 202 | if matched { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 203 | return snapshot; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 204 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 205 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 206 | _ => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 207 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 208 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 209 | panic!("event stream closed before the expected mentions page"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 210 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 211 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 212 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 213 | match result { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 214 | Ok(page) => page, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 215 | Err(_) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 216 | let last_state = last_page.map(|page: MentionsPage| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 217 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 218 | page.revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 219 | page.unread_count, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 220 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 221 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 222 | .map(|entry| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 223 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 224 | entry.key.room_id.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 225 | entry.key.event_id.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 226 | entry.capture.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 227 | entry.revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 228 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 229 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 230 | .collect::<Vec<_>>(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 231 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 232 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 233 | let requests = if let Some(server) = diagnostic_server { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 234 | server.server().received_requests().await.map(|requests| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 235 | requests |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 236 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 237 | .filter(|request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 238 | let path = request.url.path(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 239 | path.contains(room_id.as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 240 | && (path.contains("/context/") || path.ends_with("/messages")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 241 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 242 | .map(|request| format!("{} {}", request.method, request.url)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 243 | .collect::<Vec<_>>() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 244 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 245 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 246 | None |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 247 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 248 | panic!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 249 | "expected mentions update did not arrive, last state {last_state:?}, requests {requests:?}" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 250 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 251 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 252 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 253 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 254 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 255 | fn entry_ids(page: &MentionsPage) -> HashSet<String> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 256 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 257 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 258 | .map(|entry| entry.key.event_id.to_string()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 259 | .collect() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 260 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 261 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 262 | async fn wait_for_requests(feed: &SlidingFeed, minimum: usize) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 263 | tokio::time::timeout(Duration::from_secs(5), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 264 | while feed.requests.load(Ordering::Acquire) < minimum { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 265 | tokio::task::yield_now().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 266 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 267 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 268 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 269 | .expect("the sliding sync feed did not receive another request"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 270 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 271 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 272 | async fn mount_messages_page( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 273 | server: &MatrixMockServer, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 274 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 275 | direction: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 276 | token: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 277 | limit: u16, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 278 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 279 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 280 | .and(path_regex(format!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 281 | r"^/_matrix/client/v3/rooms/{room_id}/messages$" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 282 | ))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 283 | .and(query_param("dir", direction)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 284 | .and(query_param("from", token)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 285 | .and(move |request: &Request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 286 | request |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 287 | .url |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 288 | .query_pairs() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 289 | .find(|(key, _)| key == "limit") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 290 | .map_or(limit == 10, |(_, value)| value == limit.to_string()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 291 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 292 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 293 | "start": token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 294 | "end": format!("{token}-next"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 295 | "chunk": [], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 296 | "state": [], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 297 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 298 | .expect(1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 299 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 300 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 301 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 302 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 303 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 304 | async fn unopened_room_mentions_are_deduplicated_and_read_through_uses_a_revision_boundary() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 305 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 306 | let room_id = room_id!("!mentions-inbox:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 307 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 308 | let sender = user_id!("@alice:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 309 | let initial = vec![ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 310 | message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 311 | "$mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 312 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 313 | 2_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 314 | "direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 315 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 316 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 317 | message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 318 | "$own-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 319 | own_user.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 320 | 2_100, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 321 | "own event", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 322 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 323 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 324 | message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 325 | "$unrelated", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 326 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 327 | 2_200, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 328 | "ordinary message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 329 | None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 330 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 331 | ]; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 332 | let feed = SlidingFeed::new(room_id.to_owned(), initial.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 333 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 334 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 335 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 336 | client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 337 | EngineConfig::default().with_initial_history_limit(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 338 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 339 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 340 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 341 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 342 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 343 | let first = mentions_page(&mut events, room_id, |page| !page.entries.is_empty()).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 344 | assert_eq!(entry_ids(&first), HashSet::from(["$mention".to_owned()])); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 345 | assert_eq!(first.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 346 | let first_boundary = first.boundary.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 347 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 348 | wait_for_requests(&feed, 3).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 349 | let replayed = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 350 | assert_eq!(entry_ids(&replayed), HashSet::from(["$mention".to_owned()])); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 351 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 352 | let key = replayed.entries[0].key.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 353 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 354 | .set_mentions_read(vec![key.clone()], true) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 355 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 356 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 357 | let read = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 358 | assert_eq!(read.unread_count, 0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 360 | feed.replace( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 361 | room_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 362 | [ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 363 | initial, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 364 | vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 365 | "$older-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 366 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 367 | 1_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 368 | "older direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 369 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 370 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 371 | ] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 372 | .concat(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 373 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 374 | let updated = mentions_page(&mut events, room_id, |page| page.entries.len() == 2).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 375 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | entry_ids(&updated), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 377 | HashSet::from(["$mention".to_owned(), "$older-mention".to_owned(),]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 379 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 380 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 381 | .mark_mentions_read_through(first_boundary) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 382 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 383 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 384 | let after_boundary = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 385 | let read_states: Vec<_> = after_boundary |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 386 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 387 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 388 | .map(|entry| (entry.key.event_id.to_string(), entry.read)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 389 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 390 | assert!(read_states.contains(&("$mention".to_owned(), true))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 391 | assert!(read_states.contains(&("$older-mention".to_owned(), false))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 392 | assert_eq!(after_boundary.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 393 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 394 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 395 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 396 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 397 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 398 | async fn edits_can_remove_and_restore_a_mention_until_redaction_removes_its_target() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 399 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 400 | let room_id = room_id!("!mentions-edits:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 401 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 402 | let sender = user_id!("@alice:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 403 | let original = message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 404 | "$edited-target", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 405 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 406 | 1_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 407 | "original mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 408 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 409 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 410 | let removal = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 411 | "type": "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 412 | "event_id": "$remove-edit", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 413 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 414 | "origin_server_ts": 2_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 415 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 416 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 417 | "body": "* edited without a mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 418 | "m.relates_to": { "rel_type": "m.replace", "event_id": "$edited-target" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 419 | "m.new_content": { "msgtype": "m.text", "body": "edited without a mention" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 420 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 421 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 422 | let addition = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 423 | "type": "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 424 | "event_id": "$add-edit", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 425 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 426 | "origin_server_ts": 3_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 427 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 428 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 429 | "body": "* edited to mention you", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 430 | "m.relates_to": { "rel_type": "m.replace", "event_id": "$edited-target" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 431 | "m.new_content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 432 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 433 | "body": "edited to mention you", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | "m.mentions": { "user_ids": [own_user.as_str()] }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 437 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 438 | let redaction = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 439 | "type": "m.room.redaction", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 440 | "event_id": "$redaction", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 441 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 442 | "origin_server_ts": 4_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 443 | "redacts": "$edited-target", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 444 | "content": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 445 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 446 | let feed = SlidingFeed::new(room_id.to_owned(), vec![original.clone()]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 447 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 448 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 449 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 450 | client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 451 | EngineConfig::default().with_initial_history_limit(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 452 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 453 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 454 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 455 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 456 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 457 | let discovered = mentions_page(&mut events, room_id, |page| !page.entries.is_empty()).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 458 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 459 | entry_ids(&discovered), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 460 | HashSet::from(["$edited-target".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 461 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 462 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 463 | feed.replace(room_id, vec![original.clone(), removal.clone()]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 464 | let removed = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 465 | page.revision > discovered.revision && page.entries.is_empty() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 466 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 467 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 468 | assert!(removed.entries.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 469 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 470 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 471 | .mention_context(room_id, ruma::event_id!("$edited-target")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 472 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 473 | .unwrap_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 474 | .kind, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 475 | macltok::ErrorKind::EventNotFound |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 476 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 477 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 478 | feed.replace(room_id, vec![original.clone(), removal, addition]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 479 | let restored = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 480 | page.revision > removed.revision && entry_ids(page).contains("$edited-target") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 481 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 482 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 483 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 484 | entry_ids(&restored), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 485 | HashSet::from(["$edited-target".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 486 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 487 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 488 | feed.replace(room_id, vec![original, redaction]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 489 | let redacted = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 490 | page.revision > restored.revision && page.entries.is_empty() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 491 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 492 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 493 | assert!(redacted.entries.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 494 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 495 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 496 | .mention_context(room_id, ruma::event_id!("$edited-target")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 497 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 498 | .unwrap_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 499 | .kind, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 500 | macltok::ErrorKind::EventNotFound |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 501 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 502 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 503 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 504 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 505 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 506 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 507 | async fn room_mentions_require_power_while_muted_direct_mentions_remain_and_ignored_senders_drop() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 508 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 509 | let client = server.client_builder().build().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 510 | let own_user = client.user_id().unwrap().to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 511 | let room_mention_room = room_id!("!room-mention-policy:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 512 | let muted_room = room_id!("!muted-mention-policy:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 513 | let ignored_sender = user_id!("@ignored:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 514 | let sender = user_id!("@alice:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 515 | let mut rules = Ruleset::server_default(&own_user); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 516 | rules.override_.shift_insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 517 | 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 518 | NewConditionalPushRule::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 519 | "mute-inbox-room".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 520 | vec![PushCondition::EventMatch(EventMatchConditionData::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 521 | "room_id".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 522 | muted_room.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 523 | ))], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 524 | Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 525 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 526 | .into(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 527 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 528 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 529 | .mock_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 530 | .ok_and_run(&client, { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 531 | let own_user = own_user.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 532 | let ignored_sender = ignored_sender.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 533 | move |builder| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 534 | let factory = EventFactory::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 535 | builder.add_global_account_data(factory.push_rules(rules)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 536 | builder.add_global_account_data(factory.ignored_user_list([ignored_sender])); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 537 | builder.add_joined_room( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 538 | JoinedRoomBuilder::new(room_mention_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 539 | .add_state_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | .room(room_mention_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 542 | .member(&own_user), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 543 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 544 | .add_state_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 545 | EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 546 | .room(room_mention_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 547 | .default_power_levels() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 548 | .sender(&own_user), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 549 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 550 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 551 | builder.add_joined_room( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 552 | JoinedRoomBuilder::new(muted_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 553 | .add_state_event(EventFactory::new().room(muted_room).member(&own_user)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 554 | .add_state_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 555 | EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 556 | .room(muted_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 557 | .default_power_levels() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 558 | .sender(&own_user), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 559 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 560 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 561 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 562 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 563 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 564 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 565 | let feed = SlidingFeed::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 566 | room_mention_room.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 567 | vec![room_mention( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 568 | "$unauthorized-room-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 569 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 570 | 3_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 571 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 572 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 573 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 574 | let muted_events = vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 575 | "$muted-direct-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 576 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 577 | 2_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 578 | "muted direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | )]; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | let ignored = message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 582 | "$ignored-direct-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 583 | ignored_sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 584 | 1_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 585 | "ignored direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 586 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | let mut muted_events = muted_events; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 589 | muted_events.push(ignored); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | feed.add_room(muted_room, muted_events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 593 | client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 594 | EngineConfig::default().with_initial_history_limit(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 595 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 596 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 597 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 598 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 599 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 600 | let page = mentions_page(&mut events, room_mention_room, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 601 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 602 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 603 | .any(|entry| entry.key.event_id.as_str() == "$muted-direct-mention") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 604 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 605 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 606 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 607 | entry_ids(&page), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 608 | HashSet::from(["$muted-direct-mention".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 609 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 610 | assert!(page.entries[0].notification_suppressed); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 611 | assert!(!entry_ids(&page).contains("$unauthorized-room-mention")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 612 | assert!(!entry_ids(&page).contains("$ignored-direct-mention")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 613 | let room_ids: HashSet<_> = page |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 614 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 615 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 616 | .map(|entry| entry.key.room_id.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 617 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 618 | assert!(room_ids.contains(&muted_room.to_owned())); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 619 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 620 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 621 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 622 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 623 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 624 | async fn event_context_uses_independent_direction_limits_and_saved_mention_context_is_offline() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 625 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 626 | let room_id = room_id!("!mentions-context:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 627 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 628 | let mention_id = event_id!("$mention-context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 629 | let context_id = event_id!("$manual-context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 630 | let mention_event = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 631 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 632 | .text_msg("mention with saved context") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 633 | .mentions(Mentions::with_user_ids([own_user.clone()])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 634 | .event_id(mention_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 635 | .sender(&ALICE) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 636 | .into_event(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 637 | let context_event = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 638 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 639 | .text_msg("manually fetched context") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 640 | .event_id(context_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 641 | .sender(&ALICE) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 642 | .into_event(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 643 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 644 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 645 | .mock_room_event_context() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 646 | .room(room_id.to_owned()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 647 | .match_event_id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 648 | .ok(RoomContextResponseTemplate::new(mention_event.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 649 | .start("mention-before") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 650 | .end("mention-after")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 651 | .expect(1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 652 | .mount() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 653 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 654 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 655 | .mock_room_event_context() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 656 | .room(room_id.to_owned()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 657 | .match_event_id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 658 | .ok(RoomContextResponseTemplate::new(context_event) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 659 | .start("manual-before") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 660 | .end("manual-after")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 661 | .expect(1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 662 | .mount() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 663 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 664 | mount_messages_page(&server, room_id, "b", "mention-before", 10).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 665 | mount_messages_page(&server, room_id, "f", "mention-after", 10).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 666 | mount_messages_page(&server, room_id, "b", "manual-before", 2).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 667 | mount_messages_page(&server, room_id, "f", "manual-after", 3).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 668 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 669 | let feed = SlidingFeed::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 670 | room_id.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 671 | vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 672 | mention_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 673 | ALICE.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 674 | 5_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 675 | "mention with saved context", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 676 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 677 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 678 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 679 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 680 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 681 | client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 682 | EngineConfig::default().with_initial_history_limit(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 683 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 684 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 685 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 686 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 687 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 688 | let captured = mentions_page_with_diagnostics( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 689 | &mut events, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 690 | room_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 691 | |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 692 | page.entries.iter().any(|entry| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 693 | entry.key.event_id == mention_id |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 694 | && matches!(&entry.capture, macltok::MentionCaptureState::Partial) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 695 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 696 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 697 | Some(&server), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 698 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 699 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 700 | let saved = handle.mention_context(room_id, mention_id).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 701 | assert!(saved.context.is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 702 | assert!(matches!(saved.state, macltok::MentionCaptureState::Partial)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 703 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 704 | let before_saved_request_count = server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 705 | .server() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 706 | .received_requests() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 707 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 708 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 709 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 710 | .filter(|request| request.url.path().contains("/context/$mention-context")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 711 | .count(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 712 | let _ = handle.mention_context(room_id, mention_id).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 713 | let after_saved_request_count = server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 714 | .server() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 715 | .received_requests() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 716 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 717 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 718 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 719 | .filter(|request| request.url.path().contains("/context/$mention-context")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 720 | .count(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 721 | assert_eq!(after_saved_request_count, before_saved_request_count); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 722 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 723 | let session = handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 724 | .event_context(room_id, context_id, 2, 3) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 725 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 726 | .unwrap(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 727 | let context = &session.context; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 728 | assert_eq!(context.start_token.as_deref(), Some("manual-before-next")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 729 | assert_eq!(context.end_token.as_deref(), Some("manual-after-next")); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 730 | handle |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 731 | .close_event_context(&session.context_id) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 732 | .await |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 733 | .unwrap(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 734 | let closed = handle |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 735 | .event_context_before(&session.context_id, "manual-before-next", 5) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 736 | .await |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 737 | .unwrap_err(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 738 | assert_eq!(closed.kind, macltok::ErrorKind::EventContextNotOpen); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 739 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 740 | let requests = server.server().received_requests().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 741 | let context_limits: Vec<_> = requests |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 742 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 743 | .filter(|request| request.url.path().contains("/context/")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 744 | .map(|request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 745 | request |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 746 | .url |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 747 | .query_pairs() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 748 | .find(|(key, _)| key == "limit") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 749 | .map(|(_, value)| value.into_owned()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 750 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 751 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 752 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 753 | assert_eq!(context_limits, ["0", "0"]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 754 | let queries: Vec<_> = requests |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 755 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 756 | .filter(|request| request.url.path().ends_with("/messages")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 757 | .map(|request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 758 | let query: std::collections::HashMap<_, _> = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 759 | request.url.query_pairs().into_owned().collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 760 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 761 | query.get("dir").cloned().unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 762 | query.get("from").cloned().unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 763 | query |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 764 | .get("limit") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 765 | .cloned() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 766 | .unwrap_or_else(|| "10".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 767 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 768 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 769 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 770 | assert!(queries.contains(&("b".to_owned(), "mention-before".to_owned(), "10".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 771 | assert!(queries.contains(&("f".to_owned(), "mention-after".to_owned(), "10".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 772 | assert!(queries.contains(&("b".to_owned(), "manual-before".to_owned(), "2".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 773 | assert!(queries.contains(&("f".to_owned(), "manual-after".to_owned(), "3".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 774 | assert_eq!(captured.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 775 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 776 | shut_down(&handle, &mut events, running).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 777 | server.server().verify().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 778 | } |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 779 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 780 | #[tokio::test] |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 781 | async fn highlighting_keywords_are_mentions_and_quiet_ones_are_not() { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 782 | let server = MatrixMockServer::new().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 783 | let client = server.client_builder().build().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 784 | let own_user = client.user_id().unwrap().to_owned(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 785 | let room_id = room_id!("!keyword-policy:localhost"); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 786 | let sender = user_id!("@alice:localhost"); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 787 | let mut rules = Ruleset::server_default(&own_user); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 788 | let notify = [ |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 789 | Action::Notify, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 790 | Action::SetTweak(Tweak::Sound(SoundTweakValue::Default)), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 791 | ]; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 792 | let mut loud = notify.to_vec(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 793 | loud.push(Action::SetTweak(Tweak::Highlight(HighlightTweakValue::Yes))); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 794 | rules |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 795 | .content |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 796 | .insert(NewPatternedPushRule::new("deploy".to_owned(), "deploy".to_owned(), loud).into()); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 797 | rules.content.insert( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 798 | NewPatternedPushRule::new("lunch".to_owned(), "lunch".to_owned(), notify.to_vec()).into(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 799 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 800 | server |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 801 | .mock_sync() |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 802 | .ok_and_run(&client, { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 803 | let own_user = own_user.clone(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 804 | move |builder| { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 805 | let factory = EventFactory::new().room(room_id); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 806 | builder.add_global_account_data(factory.push_rules(rules)); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 807 | builder.add_joined_room( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 808 | JoinedRoomBuilder::new(room_id).add_state_event(factory.member(&own_user)), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 809 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 810 | } |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 811 | }) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 812 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 813 | let feed = SlidingFeed::new( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 814 | room_id.to_owned(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 815 | vec![ |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 816 | message( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 817 | "$keyword", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 818 | sender.as_str(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 819 | 1_000, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 820 | "the deploy is done", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 821 | None, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 822 | ), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 823 | message("$quiet", sender.as_str(), 2_000, "lunch anyone", None), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 824 | message("$plain", sender.as_str(), 3_000, "nothing to see", None), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 825 | message( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 826 | "$keyword-direct", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 827 | sender.as_str(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 828 | 4_000, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 829 | "deploy now", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 830 | Some(own_user.as_str()), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 831 | ), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 832 | message("$own-keyword", own_user.as_str(), 5_000, "my deploy", None), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 833 | ], |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 834 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 835 | feed.mount(&server).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 836 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 837 | let (engine, handle, mut events) = Engine::from_client( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 838 | client, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 839 | EngineConfig::default().with_initial_history_limit(0), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 840 | ) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 841 | .await |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 842 | .unwrap(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 843 | let running = tokio::spawn(engine.run()); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 844 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 845 | let page = mentions_page(&mut events, room_id, |page| page.entries.len() >= 2).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 846 | assert_eq!( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 847 | entry_ids(&page), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 848 | HashSet::from(["$keyword".to_owned(), "$keyword-direct".to_owned()]) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 849 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 850 | let reasons: BTreeMap<_, _> = page |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 851 | .entries |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 852 | .iter() |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 853 | .map(|entry| (entry.key.event_id.to_string(), entry.reasons.clone())) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 854 | .collect(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 855 | assert_eq!(reasons["$keyword"], vec![MentionReason::Keyword]); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 856 | assert_eq!(reasons["$keyword-direct"], vec![MentionReason::Direct]); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 857 | assert_eq!(page.unread_count, 2); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 858 | assert!( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 859 | page.entries |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 860 | .iter() |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 861 | .all(|entry| !entry.notification_suppressed) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 862 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 863 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 864 | shut_down(&handle, &mut events, running).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 865 | } |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 866 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 867 | #[tokio::test] |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 868 | async fn adding_a_keyword_gives_its_rule_a_highlight() { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 869 | let server = MatrixMockServer::new().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 870 | let room_id = room_id!("!keyword-rule:localhost"); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 871 | let (client, _) = joined_client(&server, room_id).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 872 | Mock::given(method("PUT")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 873 | .and(path("/_matrix/client/v3/pushrules/global/content/deploy")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 874 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 875 | .expect(1) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 876 | .mount(server.server()) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 877 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 878 | Mock::given(method("PUT")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 879 | .and(path( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 880 | "/_matrix/client/v3/pushrules/global/content/deploy/actions", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 881 | )) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 882 | .and(body_string_contains("highlight")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 883 | .and(body_string_contains("notify")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 884 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 885 | .expect(1) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 886 | .mount(server.server()) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 887 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 888 | SlidingFeed::new(room_id.to_owned(), Vec::new()) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 889 | .mount(&server) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 890 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 891 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 892 | let (engine, handle, mut events) = Engine::from_client(client, EngineConfig::default()) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 893 | .await |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 894 | .unwrap(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 895 | let running = tokio::spawn(engine.run()); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 896 | handle.add_notification_keyword("deploy").await.unwrap(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 897 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 898 | server.server().verify().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 899 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 900 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 901 | #[cfg(feature = "sqlite")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 902 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 903 | async fn sqlite_mentions_survive_engine_shutdown_and_session_resume() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 904 | static NEXT_TEMPORARY_PATH: AtomicUsize = AtomicUsize::new(0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 905 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 906 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 907 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 908 | .mock_versions() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 909 | .with_simplified_sliding_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 910 | .ok() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 911 | .mount() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 912 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 913 | server.mock_login().ok().mount().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 914 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 915 | let root = std::env::temp_dir().join(format!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 916 | "macltok-mentions-inbox-{}-{}", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 917 | std::process::id(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 918 | NEXT_TEMPORARY_PATH.fetch_add(1, Ordering::Relaxed) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 919 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 920 | let store_path = root.join("store"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 921 | let sessions_path = root.join("records").join("session"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 922 | let sessions = Arc::new(EncryptedSessionStore::file( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 923 | &sessions_path, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 924 | StoreConfig::sqlite(&store_path, "mentions inbox test passphrase"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 925 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 926 | let room_id = room_id!("!mentions-resume:matrix.org"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 927 | let own_user = user_id!("@cheeky_monkey:matrix.org"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 928 | let feed = SlidingFeed::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 929 | room_id.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 930 | vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 931 | "$durable-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 932 | "@alice:matrix.org", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 933 | 7_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 934 | "persist this mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 935 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 936 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 937 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 938 | feed.set_required_state( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 939 | room_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 940 | vec![json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 941 | "type": "m.room.member", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 942 | "event_id": "$joined-member", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 943 | "sender": own_user, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 944 | "state_key": own_user, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 945 | "origin_server_ts": 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 946 | "content": { "membership": "join" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 947 | })], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 948 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 949 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 950 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 951 | let config = EngineConfig::default().with_initial_history_limit(0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 952 | let session = Session::login_password( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 953 | PasswordLogin::new(server.uri(), "example", "wordpass").allow_insecure_loopback(true), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 954 | sessions.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 955 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 956 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 957 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 958 | let (engine, handle, mut events) = session.build(config.clone()).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 959 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 960 | let first = mentions_page(&mut events, room_id, |page| !page.entries.is_empty()).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 961 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 962 | entry_ids(&first), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 963 | HashSet::from(["$durable-mention".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 964 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 965 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 966 | drop(handle); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 967 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 968 | let Some(Restore::Resumed(session)) = Session::restore(sessions.clone()).await.unwrap() else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 969 | panic!("the saved session did not resume"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 970 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 971 | let (engine, handle, mut events) = session.build(config).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 972 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 973 | receive_until(&mut events, |event| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 974 | matches!(event, Event::Authenticated { .. }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 975 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 976 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 977 | let resumed = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 978 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 979 | entry_ids(&resumed), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 980 | HashSet::from(["$durable-mention".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 981 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 982 | assert_eq!(resumed.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 983 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 984 | drop(handle); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 985 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 986 | sessions.discard().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 987 | std::fs::remove_dir_all(root).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 988 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 989 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 990 | #[cfg(feature = "serde")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 991 | #[test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 992 | fn mentions_command_and_page_round_trip_through_serde() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 993 | let command = MentionsCommand::ReadThrough(MentionsBoundary { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 994 | account: user_id!("@alice:example.org").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 995 | revision: 7, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 996 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 997 | let encoded_command = serde_json::to_value(&command).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 998 | let decoded_command: MentionsCommand = serde_json::from_value(encoded_command).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 999 | assert_eq!(decoded_command, command); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1000 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1001 | let page = MentionsPage { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1002 | entries: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1003 | next_cursor: Some("opaque cursor".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1004 | end_reached: false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1005 | coverage: MentionCoverage::default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1006 | unread_count: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1007 | revision: 7, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1008 | boundary: MentionsBoundary { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1009 | account: user_id!("@alice:example.org").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1010 | revision: 7, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1011 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1012 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1013 | let encoded_page = serde_json::to_value(&page).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1014 | let decoded_page: MentionsPage = serde_json::from_value(encoded_page).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1015 | assert_eq!(decoded_page, page); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1016 | } |