cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | use std::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | sync::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | Arc, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | atomic::{AtomicUsize, Ordering}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | time::Duration, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | use matrix_sdk::ruma::events::room::MediaSource; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | use matrix_sdk::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | ruma::{event_id, events::Mentions, push::Ruleset, room_id, user_id}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | test_utils::mocks::MatrixMockServer, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 13 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 14 | use matrix_sdk_test::{JoinedRoomBuilder, event_factory::EventFactory}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | use rusqlite::Connection; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | use serde_json::json; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 17 | use wiremock::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | Mock, ResponseTemplate, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | matchers::{method, path, query_param, query_param_is_missing}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 21 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 22 | use super::super::store::{Account, MentionsStore}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 23 | use super::*; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 24 | use crate::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | MediaId, MediaKind, StoreConfig, TimelineContent, TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | normalise::{MediaRegistration, MediaSourceInfo}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 27 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 28 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 29 | static STORES: AtomicUsize = AtomicUsize::new(0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | const MAX_BYTES: usize = 64 * 1024 * 1024; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 31 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 32 | fn config() -> StoreConfig { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 33 | let number = STORES.fetch_add(1, Ordering::Relaxed); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | StoreConfig::sqlite( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 35 | std::env::temp_dir().join(format!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | "macltok-mentions-service-{}-{number}", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | std::process::id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | )), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | "service commit test passphrase", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 43 | fn account(client: &Client) -> Account { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | Account { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 45 | homeserver: client.homeserver().to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 46 | user_id: client.user_id().unwrap().to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 47 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 48 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 49 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 50 | async fn joined_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 51 | server: &MatrixMockServer, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 52 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | ) -> (Client, ruma::OwnedUserId) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 54 | let client = server.client_builder().build().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 55 | let own_user = client.user_id().unwrap().to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 56 | let room_id = room_id.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | .mock_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | .ok_and_run(&client, { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | let own_user = own_user.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | move |builder| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 62 | let factory = EventFactory::new().room(&room_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | builder.add_global_account_data( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | factory.push_rules(Ruleset::server_default(&own_user)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | builder.add_joined_room( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | JoinedRoomBuilder::new(&room_id).add_state_event(factory.member(&own_user)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | (client, own_user) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 74 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 75 | fn mention_batch( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 76 | room_id: &ruma::RoomId, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 77 | own_user: &ruma::UserId, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 78 | event: &str, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 79 | previous_pos: Option<&str>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 80 | next_pos: &str, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 81 | ) -> ResponseCommitBatch { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | let event = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | .room(room_id) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 84 | .text_msg(event) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 85 | .mentions(Mentions::with_user_ids([own_user.to_owned()])) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 86 | .event_id(&ruma::EventId::parse(format!("${event}:localhost")).unwrap()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | let raw_room = serde_json::from_value(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | "timeline": [serde_json::to_value(event).unwrap()], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 91 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 93 | ResponseCommitBatch { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | connection_id: "mentions-connection".to_owned(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 95 | previous_pos: previous_pos.map(str::to_owned), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 96 | next_pos: next_pos.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | response: Default::default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | raw_rooms: [(room_id.to_owned(), raw_room)].into(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 99 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 100 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 101 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 102 | fn quiet_batch(previous_pos: &str, next_pos: &str) -> ResponseCommitBatch { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 103 | ResponseCommitBatch { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 104 | connection_id: "mentions-connection".to_owned(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 105 | previous_pos: Some(previous_pos.to_owned()), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 106 | next_pos: next_pos.to_owned(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 107 | response: Default::default(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 108 | raw_rooms: Default::default(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 109 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 110 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 111 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 112 | async fn service(client: &Client, store: Option<Result<MentionsStore, Error>>) -> Service { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | EngineConfig::default().max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | EngineConfig::default().max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | )); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 118 | let (_, mut service, _) = Service::new(client.clone(), media, store, EngineConfig::default()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 119 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 120 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 121 | service.recovery_budget = 0; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 122 | service.latest_scan = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 123 | service |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 124 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 125 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 126 | fn rows(connection: &Connection) -> BTreeMap<String, Vec<u8>> { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 127 | let mut statement = connection |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 128 | .prepare("SELECT key, value FROM records WHERE key NOT IN ('!cipher', '!head')") |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 129 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 130 | statement |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 131 | .query_map([], |row| Ok((row.get(0)?, row.get(1)?))) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 132 | .unwrap() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 133 | .map(Result::unwrap) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 134 | .collect() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 135 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 136 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 137 | fn discontinuities(service: &Service) -> usize { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 138 | service |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 139 | .data |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 140 | .coverage |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 141 | .gaps |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 142 | .iter() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 143 | .filter(|gap| gap.reason == MentionGapReason::SyncDiscontinuity) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 144 | .count() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 145 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 146 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 147 | #[tokio::test] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 148 | async fn failed_write_acks_and_keeps_the_inbox_in_memory_until_a_later_write() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 149 | let server = MatrixMockServer::new().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 150 | let room_id = room_id!("!mentions:localhost"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 151 | let (client, own_user) = joined_client(&server, room_id).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 152 | let config = config(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 153 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 154 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 155 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 156 | let mut service = service(&client, Some(Ok(store))).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | let connection = Connection::open(config.mentions_location()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | connection |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | .execute_batch( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | "CREATE TRIGGER fail_mentions_inbox_insert BEFORE INSERT ON records WHEN NEW.key NOT IN ('!cipher', '!head') BEGIN SELECT RAISE(ABORT, 'injected inbox write failure'); END; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | CREATE TRIGGER fail_mentions_inbox_update BEFORE UPDATE ON records WHEN OLD.key NOT IN ('!cipher', '!head') BEGIN SELECT RAISE(ABORT, 'injected inbox write failure'); END;", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 166 | service |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 167 | .batch(mention_batch( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 168 | room_id, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 169 | &own_user, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 170 | "first", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 171 | None, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 172 | "position-1", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 173 | )) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 174 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 175 | assert!(service.dirty); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 176 | assert_eq!(service.data.entries.len(), 1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 177 | assert!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 178 | service |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 179 | .data |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 180 | .coverage |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 181 | .gaps |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 182 | .iter() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 183 | .any(|gap| gap.reason == MentionGapReason::StoreFailed) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 184 | ); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 185 | assert_eq!(service.updates.borrow().entries.len(), 1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 186 | assert!(rows(&connection).is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 188 | connection |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 189 | .execute_batch( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 190 | "DROP TRIGGER fail_mentions_inbox_insert; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | DROP TRIGGER fail_mentions_inbox_update;", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 192 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 193 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 194 | service.flush_after = 0; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 195 | service.batch(quiet_batch("position-1", "position-2")).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 196 | assert!(!service.dirty); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 197 | drop(service); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 198 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 199 | let reopened = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 200 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 201 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 202 | let persisted: Data = reopened.read("inbox").unwrap().unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 203 | let sync: SyncState = reopened.read("sync").unwrap().unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 204 | assert_eq!(persisted.entries.len(), 1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 205 | assert_eq!(sync.inbox_revision, persisted.revision); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 206 | assert_eq!(sync.checkpoints["mentions-connection"].pos, "position-2"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 207 | drop(reopened); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 208 | drop(connection); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 209 | config.delete().await.unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 210 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 211 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 212 | #[tokio::test] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 213 | async fn a_quiet_response_writes_only_the_checkpoint() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 214 | let server = MatrixMockServer::new().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 215 | let room_id = room_id!("!mentions:localhost"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 216 | let (client, own_user) = joined_client(&server, room_id).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 217 | let config = config(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 218 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 219 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 220 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 221 | let mut service = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 222 | service |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 223 | .batch(mention_batch( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 224 | room_id, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 225 | &own_user, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 226 | "first", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 227 | None, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 228 | "position-1", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 229 | )) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 230 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 231 | let connection = Connection::open(config.mentions_location()).unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 232 | let before = rows(&connection); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 233 | assert_eq!(before.len(), 2); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 234 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 235 | service.batch(quiet_batch("position-1", "position-2")).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 236 | let after = rows(&connection); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 237 | assert_eq!(after.len(), 2); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 238 | assert_eq!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 239 | before |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 240 | .iter() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 241 | .filter(|(key, value)| after.get(*key) != Some(value)) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 242 | .count(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 243 | 1 |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 244 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 245 | assert_eq!(service.data.revision, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 246 | assert_eq!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 247 | service.sync.checkpoints["mentions-connection"].pos, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 248 | "position-2" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 249 | ); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 250 | drop(service); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 251 | drop(connection); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 252 | config.delete().await.unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 253 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 254 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 255 | #[tokio::test] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 256 | async fn a_replay_from_the_previous_checkpoint_is_not_a_discontinuity() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 257 | let server = MatrixMockServer::new().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 258 | let room_id = room_id!("!mentions:localhost"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 259 | let (client, own_user) = joined_client(&server, room_id).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 260 | let config = config(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 261 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 262 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 263 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 264 | let mut first = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 265 | first |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 266 | .batch(mention_batch(room_id, &own_user, "one", None, "position-1")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 267 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 268 | first |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 269 | .batch(mention_batch( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 270 | room_id, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 271 | &own_user, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 272 | "two", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 273 | Some("position-1"), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 274 | "position-2", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 275 | )) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 276 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 277 | drop(first); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 278 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 279 | // the sdk never saved position-2, so it asks again from position-1 |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 280 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 281 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 282 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 283 | let mut restarted = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 284 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 285 | .batch(mention_batch( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 286 | room_id, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 287 | &own_user, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 288 | "three", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 289 | Some("position-1"), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 290 | "position-3", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 291 | )) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 292 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 293 | assert_eq!(discontinuities(&restarted), 0); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 294 | assert_eq!(restarted.data.entries.len(), 3); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 295 | assert_eq!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 296 | restarted.sync.checkpoints["mentions-connection"], |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 297 | Checkpoint { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 298 | pos: "position-3".to_owned(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 299 | previous: Some("position-1".to_owned()), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 300 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 301 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 302 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 303 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 304 | .batch(quiet_batch("position-3", "position-4")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 305 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 306 | assert_eq!(discontinuities(&restarted), 0); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 307 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 308 | .batch(quiet_batch("position-1", "position-5")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 309 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 310 | assert_eq!(discontinuities(&restarted), 1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 311 | drop(restarted); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 312 | config.delete().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 313 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 314 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 315 | #[tokio::test] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 316 | async fn an_unreadable_store_resets_behind_a_gap() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 317 | let server = MatrixMockServer::new().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 318 | let room_id = room_id!("!mentions:localhost"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 319 | let (client, own_user) = joined_client(&server, room_id).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 320 | let config = config(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 321 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 322 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 323 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 324 | let mut first = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 325 | first |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 326 | .batch(mention_batch(room_id, &own_user, "one", None, "position-1")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 327 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 328 | drop(first); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 329 | let connection = Connection::open(config.mentions_location()).unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 330 | connection |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 331 | .execute( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 332 | "UPDATE records SET value = x'00' WHERE key NOT IN ('!cipher', '!head')", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 333 | [], |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 334 | ) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 335 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 336 | drop(connection); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 337 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 338 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 339 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 340 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 341 | let restarted = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 342 | assert!(restarted.data.entries.is_empty()); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 343 | assert!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 344 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 345 | .data |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 346 | .coverage |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 347 | .gaps |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 348 | .iter() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 349 | .any(|gap| gap.reason == MentionGapReason::StoreReset) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 350 | ); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 351 | drop(restarted); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 352 | config.delete().await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 353 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 354 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 355 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 356 | async fn service_recovers_notification_history_into_durable_unread_inbox() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 357 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 358 | let room_id = room_id!("!mentions:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 360 | let first = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 361 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 362 | .text_msg("history mention one") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 363 | .mentions(Mentions::with_user_ids([own_user.clone()])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 364 | .event_id(event_id!("$history-one:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 365 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 366 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 367 | let second = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 368 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 369 | .text_msg("history mention two") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 370 | .mentions(Mentions::with_user_ids([own_user])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 371 | .event_id(event_id!("$history-two:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 372 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 373 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 374 | let first = serde_json::from_str::<serde_json::Value>(first.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 375 | let second = serde_json::from_str::<serde_json::Value>(second.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 377 | .and(path("/_matrix/client/v3/notifications")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | .and(query_param_is_missing("from")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 379 | .and(query_param("limit", "50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 380 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 381 | "notifications": [{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 382 | "actions": ["notify"], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 383 | "event": first, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 384 | "read": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 385 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 386 | "ts": 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 387 | }], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 388 | "next_token": "history-page-two", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 389 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 390 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 391 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 392 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 393 | .and(path("/_matrix/client/v3/notifications")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 394 | .and(query_param("from", "history-page-two")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 395 | .and(query_param("limit", "50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 396 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 397 | "notifications": [{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 398 | "actions": ["notify"], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 399 | "event": second, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 400 | "read": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 401 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 402 | "ts": 2, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 403 | }], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 404 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 405 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 406 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 407 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 408 | let config = config(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 409 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 410 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 411 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 412 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 413 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 414 | EngineConfig::default().max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 415 | EngineConfig::default().max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 416 | )); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 417 | let (handle, service, mut updates) = Service::new( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 418 | client.clone(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 419 | media, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 420 | Some(Ok(store)), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 421 | EngineConfig::default(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 422 | ) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 423 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 424 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 425 | let service_task = tokio::spawn(service.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 426 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 427 | let discovered = tokio::time::timeout(Duration::from_secs(10), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 428 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 429 | let page = updates.borrow_and_update().clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 430 | if page.entries.len() == 2 && !page.coverage.recovering { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 431 | break page; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 432 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 433 | updates.changed().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 437 | handle.stop().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 438 | service_task.await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 439 | let discovered = discovered.expect("notification history should be recovered"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 440 | assert_eq!(discovered.entries.len(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 441 | assert!(discovered.entries.iter().all(|entry| !entry.read)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 442 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 443 | let reopened = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 444 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 445 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 446 | let persisted: Data = reopened.read("inbox").unwrap().unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 447 | let sync: SyncState = reopened.read("sync").unwrap().unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 448 | assert_eq!(sync.inbox_revision, persisted.revision); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 449 | assert_eq!(persisted.entries.len(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 450 | assert!(persisted.entries.values().all(|entry| !entry.read)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 451 | assert!(persisted.latest_cursor.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 452 | assert!(persisted.history_cursor.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 453 | drop(reopened); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 454 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 455 | config.delete().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 456 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 457 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 458 | fn message_body(item: &TimelineItem) -> Option<&str> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 459 | match item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 460 | TimelineItem::Event(event) => match &event.content { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 461 | TimelineContent::Message(message) => Some(&message.body), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 462 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 463 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 464 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 465 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 466 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 467 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 468 | fn media_registration() -> MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 469 | MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 470 | id: MediaId::new("test-scope#image".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 471 | kind: MediaKind::Image, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 472 | full: MediaSourceInfo { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 473 | source: MediaSource::Plain(ruma::mxc_uri!("mxc://mentions.example/image").to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 474 | mimetype: Some("image/png".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 475 | declared_size: Some(1234), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 476 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 477 | thumbnail: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 478 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 479 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 480 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 481 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 482 | async fn live_context_survives_capture_then_edit_and_redaction() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 483 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 484 | let room_id = room_id!("!mentions:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 485 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 486 | let mention = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 487 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 488 | .text_msg("direct mention") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 489 | .mentions(Mentions::with_user_ids([own_user])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 490 | .event_id(event_id!("$live-mention:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 491 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 492 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 493 | let mut mention: serde_json::Value = serde_json::from_str(mention.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 494 | mention["origin_server_ts"] = json!(10); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 495 | let following = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 496 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 497 | .text_msg("following") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 498 | .event_id(event_id!("$live-following:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 499 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 500 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 501 | let mut following: serde_json::Value = serde_json::from_str(following.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 502 | following["origin_server_ts"] = json!(11); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 503 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 504 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 505 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 506 | EngineConfig::default().max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 507 | EngineConfig::default().max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 508 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 509 | let (_handle, mut service, _updates) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 510 | Service::new(client, media, None, EngineConfig::default()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 511 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 512 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 513 | let room_id = room_id.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 514 | let mention_key = MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 515 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 516 | event_id: event_id!("$live-mention:localhost").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 517 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 518 | let following_id = format!("{room_id}\n$live-following:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 519 | let mut data = Data::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 520 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 521 | service.observe(&mut data, &room_id, mention).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 522 | service.observe(&mut data, &room_id, following).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 523 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 524 | let mention_identity = id(&mention_key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 525 | let entry = data.entries.get(&mention_identity).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 526 | let revision = entry.revision; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 527 | let target = entry.event.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 528 | service.captured( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 529 | &mut data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 530 | mention_identity.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 531 | revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 532 | Ok(( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 533 | crate::EventContext { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 534 | events_before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 535 | event: Some(target), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 536 | events_after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 537 | start_token: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 538 | end_token: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 539 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 542 | )), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 543 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 544 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 545 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 546 | data.entries[&mention_identity].capture, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 547 | MentionCaptureState::Partial |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 548 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 549 | assert!(data.events.contains_key(&following_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 550 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 551 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 552 | assert_eq!(context.events_after.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 553 | assert_eq!(message_body(&context.events_after[0]), Some("following")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 554 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 555 | let edit = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 556 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 557 | "body": "* edited following", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 558 | "m.new_content": {"body": "edited following", "msgtype": "m.text"}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 559 | "m.relates_to": {"event_id": "$live-following:localhost", "rel_type": "m.replace"}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 560 | "msgtype": "m.text" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 561 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 562 | "event_id": "$live-following-edit:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 563 | "origin_server_ts": 12, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 564 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 565 | "sender": "@bob:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 566 | "type": "m.room.message" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 567 | }); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 568 | service.observe(&mut data, &room_id, edit).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 569 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 570 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 571 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 572 | message_body(&context.events_after[0]), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 573 | Some("edited following") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 574 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 575 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 576 | let saved_following = data.events.get_mut(&following_id).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 577 | assert!(saved_following.raw.is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 578 | saved_following.media.push(media_registration()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | assert_eq!(saved_following.media.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | let redaction = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | "content": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 582 | "event_id": "$live-following-redaction:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 583 | "origin_server_ts": 13, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 584 | "redacts": "$live-following:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 585 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 586 | "sender": "@bob:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | "type": "m.room.redaction" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | }); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 589 | service.observe(&mut data, &room_id, redaction).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | let saved_following = data.events.get(&following_id).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | assert!(matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 593 | &saved_following.item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 594 | TimelineItem::Event(event) if event.content == TimelineContent::Redacted |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 595 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 596 | assert!(saved_following.raw.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 597 | assert!(saved_following.media.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 598 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 599 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 600 | assert!(matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 601 | &context.events_after[0], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 602 | TimelineItem::Event(event) if event.content == TimelineContent::Redacted |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 603 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 604 | data.windows.remove(&mention_identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 605 | service.data = data; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 606 | let CommandOutput::MentionContext(context) = service |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 607 | .command(MentionsCommand::RetryContext(mention_key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 608 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 609 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 610 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 611 | panic!("retry should return saved context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 612 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 613 | assert_eq!(context.state, MentionCaptureState::Pending); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 614 | assert!(context.context.unwrap().event.is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 615 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 616 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 617 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 618 | async fn explicit_recovery_rechecks_server_support() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 619 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 620 | let (client, _) = joined_client(&server, room_id!("!mentions:localhost")).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 621 | let config = EngineConfig::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 622 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 623 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 624 | config.max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 625 | config.max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 626 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 627 | let (_, mut service, _) = Service::new(client, media, None, config).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 628 | service.data.coverage.historical_supported = Some(false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 629 | service.recovery_budget = 0; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 630 | service.command(MentionsCommand::Recover).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 631 | assert_eq!(service.data.coverage.historical_supported, None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 632 | assert!(service.data.coverage.recovering); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 633 | assert_eq!(service.recovery_budget, 20); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 634 | } |