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 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 112 | async fn service(client: &Client, store: Option<Result<MentionsStore, Unopened>>) -> 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, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 116 | EngineConfig::default().max_context_media, |
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); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 233 | assert_eq!(before.len(), 3); |
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); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 237 | assert_eq!(after.len(), 3); |
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] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 256 | async fn a_failed_command_write_leaves_a_store_that_still_opens() { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 257 | let server = MatrixMockServer::new().await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 258 | let room_id = room_id!("!mentions:localhost"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 259 | let (client, own_user) = joined_client(&server, room_id).await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 260 | let config = config(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 261 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 262 | .await |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 263 | .unwrap(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 264 | let mut first = service(&client, Some(Ok(store))).await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 265 | first |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 266 | .batch(mention_batch( |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 267 | room_id, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 268 | &own_user, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 269 | "first", |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 270 | None, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 271 | "position-1", |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 272 | )) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 273 | .await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 274 | let key = first.data.entries.values().next().unwrap().key.clone(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 275 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 276 | let connection = Connection::open(config.mentions_location()).unwrap(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 277 | connection |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 278 | .execute_batch( |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 279 | "CREATE TRIGGER fail_mentions_update BEFORE UPDATE ON records WHEN OLD.key NOT IN ('!cipher', '!head') BEGIN SELECT RAISE(ABORT, 'injected write failure'); END;", |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 280 | ) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 281 | .unwrap(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 282 | assert!( |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 283 | first |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 284 | .command(MentionsCommand::SetRead { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 285 | keys: vec![key], |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 286 | read: true, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 287 | }) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 288 | .await |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 289 | .is_err() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 290 | ); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 291 | connection |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 292 | .execute_batch("DROP TRIGGER fail_mentions_update;") |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 293 | .unwrap(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 294 | drop(connection); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 295 | first.batch(quiet_batch("position-1", "position-2")).await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 296 | drop(first); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 297 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 298 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 299 | .await |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 300 | .unwrap(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 301 | let restarted = service(&client, Some(Ok(store))).await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 302 | assert_eq!(restarted.data.entries.len(), 1); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 303 | assert!( |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 304 | restarted |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 305 | .data |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 306 | .coverage |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 307 | .gaps |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 308 | .iter() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 309 | .all(|gap| gap.reason != MentionGapReason::StoreReset) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 310 | ); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 311 | assert_eq!( |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 312 | restarted.sync.checkpoints["mentions-connection"].pos, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 313 | "position-2" |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 314 | ); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 315 | drop(restarted); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 316 | config.delete().await.unwrap(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 317 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 318 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 319 | #[tokio::test] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 320 | async fn a_replay_from_the_previous_checkpoint_is_not_a_discontinuity() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 321 | let server = MatrixMockServer::new().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 322 | let room_id = room_id!("!mentions:localhost"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 323 | let (client, own_user) = joined_client(&server, room_id).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 324 | let config = config(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 325 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 326 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 327 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 328 | let mut first = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 329 | first |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 330 | .batch(mention_batch(room_id, &own_user, "one", None, "position-1")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 331 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 332 | first |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 333 | .batch(mention_batch( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 334 | room_id, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 335 | &own_user, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 336 | "two", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 337 | Some("position-1"), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 338 | "position-2", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 339 | )) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 340 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 341 | drop(first); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 342 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 343 | // the sdk never saved position-2, so it asks again from position-1 |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 344 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 345 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 346 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 347 | let mut restarted = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 348 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 349 | .batch(mention_batch( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 350 | room_id, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 351 | &own_user, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 352 | "three", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 353 | Some("position-1"), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 354 | "position-3", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 355 | )) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 356 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 357 | assert_eq!(discontinuities(&restarted), 0); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 358 | assert_eq!(restarted.data.entries.len(), 3); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | assert_eq!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 360 | restarted.sync.checkpoints["mentions-connection"], |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 361 | Checkpoint { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 362 | pos: "position-3".to_owned(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 363 | previous: Some("position-1".to_owned()), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 364 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 365 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 366 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 367 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 368 | .batch(quiet_batch("position-3", "position-4")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 369 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 370 | assert_eq!(discontinuities(&restarted), 0); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 371 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 372 | .batch(quiet_batch("position-1", "position-5")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 373 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 374 | assert_eq!(discontinuities(&restarted), 1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 375 | drop(restarted); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | config.delete().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 377 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 379 | #[tokio::test] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 380 | async fn an_unreadable_store_resets_behind_a_gap() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 381 | let server = MatrixMockServer::new().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 382 | let room_id = room_id!("!mentions:localhost"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 383 | let (client, own_user) = joined_client(&server, room_id).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 384 | let config = config(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 385 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 386 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 387 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 388 | let mut first = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 389 | first |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 390 | .batch(mention_batch(room_id, &own_user, "one", None, "position-1")) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 391 | .await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 392 | drop(first); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 393 | let connection = Connection::open(config.mentions_location()).unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 394 | connection |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 395 | .execute( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 396 | "UPDATE records SET value = x'00' WHERE key NOT IN ('!cipher', '!head')", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 397 | [], |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 398 | ) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 399 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 400 | drop(connection); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 401 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 402 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 403 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 404 | .unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 405 | let restarted = service(&client, Some(Ok(store))).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 406 | assert!(restarted.data.entries.is_empty()); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 407 | assert!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 408 | restarted |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 409 | .data |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 410 | .coverage |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 411 | .gaps |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 412 | .iter() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 413 | .any(|gap| gap.reason == MentionGapReason::StoreReset) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 414 | ); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 415 | drop(restarted); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 416 | config.delete().await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 417 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 418 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 419 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 420 | async fn service_recovers_notification_history_into_durable_unread_inbox() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 421 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 422 | let room_id = room_id!("!mentions:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 423 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 424 | let first = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 425 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 426 | .text_msg("history mention one") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 427 | .mentions(Mentions::with_user_ids([own_user.clone()])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 428 | .event_id(event_id!("$history-one:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 429 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 430 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 431 | let second = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 432 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 433 | .text_msg("history mention two") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | .mentions(Mentions::with_user_ids([own_user])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | .event_id(event_id!("$history-two:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 437 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 438 | let first = serde_json::from_str::<serde_json::Value>(first.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 439 | let second = serde_json::from_str::<serde_json::Value>(second.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 440 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 441 | .and(path("/_matrix/client/v3/notifications")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 442 | .and(query_param_is_missing("from")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 443 | .and(query_param("limit", "50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 444 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 445 | "notifications": [{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 446 | "actions": ["notify"], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 447 | "event": first, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 448 | "read": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 449 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 450 | "ts": 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 451 | }], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 452 | "next_token": "history-page-two", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 453 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 454 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 455 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 456 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 457 | .and(path("/_matrix/client/v3/notifications")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 458 | .and(query_param("from", "history-page-two")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 459 | .and(query_param("limit", "50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 460 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 461 | "notifications": [{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 462 | "actions": ["notify"], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 463 | "event": second, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 464 | "read": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 465 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 466 | "ts": 2, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 467 | }], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 468 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 469 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 470 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 471 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 472 | let config = config(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 473 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 474 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 475 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 476 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 477 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 478 | EngineConfig::default().max_media_bytes, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 479 | EngineConfig::default().max_context_media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 480 | )); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 481 | let (handle, service, mut updates) = Service::new( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 482 | client.clone(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 483 | media, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 484 | Some(Ok(store)), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 485 | EngineConfig::default(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 486 | ) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 487 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 488 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 489 | let service_task = tokio::spawn(service.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 490 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 491 | let discovered = tokio::time::timeout(Duration::from_secs(10), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 492 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 493 | let page = updates.borrow_and_update().clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 494 | if page.entries.len() == 2 && !page.coverage.recovering { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 495 | break page; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 496 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 497 | updates.changed().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 498 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 499 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 500 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 501 | handle.stop().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 502 | service_task.await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 503 | let discovered = discovered.expect("notification history should be recovered"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 504 | assert_eq!(discovered.entries.len(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 505 | assert!(discovered.entries.iter().all(|entry| !entry.read)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 506 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 507 | let reopened = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 508 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 509 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 510 | let persisted: Data = reopened.read("inbox").unwrap().unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 511 | let sync: SyncState = reopened.read("sync").unwrap().unwrap(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 512 | assert_eq!(sync.inbox_revision, persisted.revision); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 513 | assert_eq!(persisted.entries.len(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 514 | assert!(persisted.entries.values().all(|entry| !entry.read)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 515 | assert!(persisted.latest_cursor.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 516 | assert!(persisted.history_cursor.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 517 | drop(reopened); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 518 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 519 | config.delete().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 520 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 521 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 522 | fn message_body(item: &TimelineItem) -> Option<&str> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 523 | match item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 524 | TimelineItem::Event(event) => match &event.content { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 525 | TimelineContent::Message(message) => Some(&message.body), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 526 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 527 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 528 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 529 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 530 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 531 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 532 | fn media_registration() -> MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 533 | MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 534 | id: MediaId::new("test-scope#image".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 535 | kind: MediaKind::Image, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 536 | full: MediaSourceInfo { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 537 | source: MediaSource::Plain(ruma::mxc_uri!("mxc://mentions.example/image").to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 538 | mimetype: Some("image/png".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 539 | declared_size: Some(1234), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | thumbnail: None, |
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 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 546 | async fn live_context_survives_capture_then_edit_and_redaction() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 547 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 548 | let room_id = room_id!("!mentions:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 549 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 550 | let mention = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 551 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 552 | .text_msg("direct mention") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 553 | .mentions(Mentions::with_user_ids([own_user])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 554 | .event_id(event_id!("$live-mention:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 555 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 556 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 557 | let mut mention: serde_json::Value = serde_json::from_str(mention.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 558 | mention["origin_server_ts"] = json!(10); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 559 | let following = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 560 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 561 | .text_msg("following") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 562 | .event_id(event_id!("$live-following:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 563 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 564 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 565 | let mut following: serde_json::Value = serde_json::from_str(following.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 566 | following["origin_server_ts"] = json!(11); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 567 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 568 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 569 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 570 | EngineConfig::default().max_media_bytes, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 571 | EngineConfig::default().max_context_media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 572 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 573 | let (_handle, mut service, _updates) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 574 | Service::new(client, media, None, EngineConfig::default()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 575 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 576 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 577 | let room_id = room_id.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 578 | let mention_key = MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | event_id: event_id!("$live-mention:localhost").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 582 | let following_id = format!("{room_id}\n$live-following:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 583 | let mut data = Data::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 584 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 585 | service.observe(&mut data, &room_id, mention).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 586 | service.observe(&mut data, &room_id, following).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | let mention_identity = id(&mention_key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 589 | let entry = data.entries.get(&mention_identity).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | let revision = entry.revision; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | let target = entry.event.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | service.captured( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 593 | &mut data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 594 | mention_identity.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 595 | revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 596 | Ok(( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 597 | crate::EventContext { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 598 | events_before: Vec::new(), |
258c08ec2168 | atagen | 2026-10-25 09:15 UTC | 599 | event: target, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 600 | events_after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 601 | start_token: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 602 | end_token: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 603 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 604 | Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 605 | Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 606 | )), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 607 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 608 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 609 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 610 | data.entries[&mention_identity].capture, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 611 | MentionCaptureState::Partial |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 612 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 613 | assert!(data.events.contains_key(&following_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 614 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 615 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 616 | assert_eq!(context.events_after.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 617 | assert_eq!(message_body(&context.events_after[0]), Some("following")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 618 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 619 | let edit = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 620 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 621 | "body": "* edited following", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 622 | "m.new_content": {"body": "edited following", "msgtype": "m.text"}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 623 | "m.relates_to": {"event_id": "$live-following:localhost", "rel_type": "m.replace"}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 624 | "msgtype": "m.text" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 625 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 626 | "event_id": "$live-following-edit:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 627 | "origin_server_ts": 12, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 628 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 629 | "sender": "@bob:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 630 | "type": "m.room.message" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 631 | }); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 632 | service.observe(&mut data, &room_id, edit).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 633 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 634 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 635 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 636 | message_body(&context.events_after[0]), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 637 | Some("edited following") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 638 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 639 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 640 | let saved_following = data.events.get_mut(&following_id).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 641 | assert!(saved_following.raw.is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 642 | saved_following.media.push(media_registration()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 643 | assert_eq!(saved_following.media.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 644 | let redaction = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 645 | "content": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 646 | "event_id": "$live-following-redaction:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 647 | "origin_server_ts": 13, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 648 | "redacts": "$live-following:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 649 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 650 | "sender": "@bob:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 651 | "type": "m.room.redaction" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 652 | }); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 653 | service.observe(&mut data, &room_id, redaction).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 654 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 655 | let saved_following = data.events.get(&following_id).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 656 | assert!(matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 657 | &saved_following.item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 658 | TimelineItem::Event(event) if event.content == TimelineContent::Redacted |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 659 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 660 | assert!(saved_following.raw.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 661 | assert!(saved_following.media.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 662 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 663 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 664 | assert!(matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 665 | &context.events_after[0], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 666 | TimelineItem::Event(event) if event.content == TimelineContent::Redacted |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 667 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 668 | data.windows.remove(&mention_identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 669 | service.data = data; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 670 | let CommandOutput::MentionContext(context) = service |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 671 | .command(MentionsCommand::RetryContext(mention_key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 672 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 673 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 674 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 675 | panic!("retry should return saved context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 676 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 677 | assert_eq!(context.state, MentionCaptureState::Pending); |
258c08ec2168 | atagen | 2026-10-25 09:15 UTC | 678 | assert!(context.context.is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 679 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 680 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 681 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 682 | async fn explicit_recovery_rechecks_server_support() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 683 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 684 | let (client, _) = joined_client(&server, room_id!("!mentions:localhost")).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 685 | let config = EngineConfig::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 686 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 687 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 688 | config.max_media_bytes, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 689 | config.max_context_media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 690 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 691 | let (_, mut service, _) = Service::new(client, media, None, config).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 692 | service.data.coverage.historical_supported = Some(false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 693 | service.recovery_budget = 0; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 694 | service.command(MentionsCommand::Recover).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 695 | assert_eq!(service.data.coverage.historical_supported, None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 696 | assert!(service.data.coverage.recovering); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 697 | assert_eq!(service.recovery_budget, 20); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 698 | } |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 699 | |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 700 | #[tokio::test] |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 701 | async fn observing_again_keeps_the_profiles_a_capture_fetched() { |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 702 | let server = MatrixMockServer::new().await; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 703 | let room_id = room_id!("!mentions:localhost"); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 704 | let (client, own_user) = joined_client(&server, room_id).await; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 705 | let raw = |event: matrix_sdk::ruma::serde::Raw<_>, timestamp: u64| { |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 706 | let mut value: serde_json::Value = serde_json::from_str(event.json().get()).unwrap(); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 707 | value["origin_server_ts"] = json!(timestamp); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 708 | value |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 709 | }; |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 710 | let factory = EventFactory::new() |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 711 | .room(room_id) |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 712 | .sender(user_id!("@bob:localhost")); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 713 | let mention = raw( |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 714 | factory |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 715 | .text_msg("ping") |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 716 | .mentions(Mentions::with_user_ids([own_user])) |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 717 | .event_id(event_id!("$ping:localhost")) |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 718 | .into_raw_sync(), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 719 | 10, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 720 | ); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 721 | let following = raw( |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 722 | factory |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 723 | .text_msg("following") |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 724 | .event_id(event_id!("$following:localhost")) |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 725 | .into_raw_sync(), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 726 | 11, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 727 | ); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 728 | let service = service(&client, None).await; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 729 | let room_id = room_id.to_owned(); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 730 | let mut data = Data::default(); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 731 | service.observe(&mut data, &room_id, mention.clone()).await; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 732 | let ping = format!("{room_id}\n$ping:localhost"); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 733 | let following_id = format!("{room_id}\n$following:localhost"); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 734 | |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 735 | let profiled = |item: &TimelineItem| { |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 736 | let mut item = item.clone(); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 737 | if let TimelineItem::Event(event) = &mut item { |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 738 | event.sender.display_name = Some("Bob".to_owned()); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 739 | } |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 740 | item |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 741 | }; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 742 | let target = profiled(&data.entries[&ping].event); |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 743 | let mut neighbour = normalise_observed(&following, None, &following_id) |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 744 | .unwrap() |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 745 | .0; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 746 | neighbour = profiled(&neighbour); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 747 | let revision = data.entries[&ping].revision; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 748 | service.captured( |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 749 | &mut data, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 750 | ping.clone(), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 751 | revision, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 752 | Ok(( |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 753 | crate::EventContext { |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 754 | events_before: Vec::new(), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 755 | event: target, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 756 | events_after: vec![neighbour], |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 757 | start_token: None, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 758 | end_token: None, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 759 | }, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 760 | Vec::new(), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 761 | Vec::new(), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 762 | )), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 763 | ); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 764 | let capture = data.entries[&ping].capture.clone(); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 765 | |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 766 | service.observe(&mut data, &room_id, following).await; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 767 | service.observe(&mut data, &room_id, mention).await; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 768 | |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 769 | let name = |item: &TimelineItem| match item { |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 770 | TimelineItem::Event(event) => event.sender.display_name.clone(), |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 771 | _ => None, |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 772 | }; |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 773 | assert_eq!(name(&data.entries[&ping].event).as_deref(), Some("Bob")); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 774 | assert_eq!(name(&data.events[&ping].item).as_deref(), Some("Bob")); |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 775 | assert_eq!( |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 776 | name(&data.events[&following_id].item).as_deref(), |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 777 | Some("Bob") |
038cc6faeb14 | atagen | 2026-10-26 02:05 UTC | 778 | ); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 779 | assert_eq!(data.entries[&ping].capture, capture); |
93d4735d70cf | atagen | 2026-10-26 01:32 UTC | 780 | } |