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 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | async fn failed_batch_keeps_inbox_and_checkpoint_at_the_same_commit_boundary() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | let room_id = room_id!("!mentions:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | let event = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | .text_msg("commit boundary") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | .mentions(Mentions::with_user_ids([own_user.clone()])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | .event_id(event_id!("$commit-boundary:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | let raw_room = serde_json::from_value(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | "initial": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | "timeline": [serde_json::to_value(event).unwrap()], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 91 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | let batch = ResponseCommitBatch { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | connection_id: "mentions-connection".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | previous_pos: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | next_pos: "mentions-position-1".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | response: Default::default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | raw_rooms: [(room_id.to_owned(), raw_room)].into(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | let config = config(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | EngineConfig::default().max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | EngineConfig::default().max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | let (_handle, mut service, updates) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | Service::new(client, media, Some(store), EngineConfig::default()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | let connection = Connection::open(config.mentions_location()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | connection |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | .execute_batch( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | "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 | 117 | 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 | 118 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | assert!(service.batch(batch.clone()).await.is_err()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | assert_eq!(service.data.revision, 0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | assert!(service.data.entries.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | assert!(service.data.checkpoints.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | assert_eq!(updates.borrow().revision, 0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | assert!(updates.borrow().entries.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | let reopened = MentionsStore::open(&config, account(&service.client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | assert_eq!(reopened.revision(), 0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | assert!(reopened.read::<Data>("inbox").unwrap().is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | drop(reopened); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 135 | connection |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | .execute_batch( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | "DROP TRIGGER fail_mentions_inbox_insert; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | DROP TRIGGER fail_mentions_inbox_update;", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | drop(connection); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | service.batch(batch.clone()).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 144 | assert_eq!(service.data.revision, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 145 | assert_eq!(service.data.entries.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 146 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | service.data.checkpoints.get("mentions-connection"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | Some(&batch.next_pos) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | assert_eq!(updates.borrow().revision, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | assert_eq!(updates.borrow().entries.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | let reopened = MentionsStore::open(&config, account(&service.client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | let persisted: Data = reopened.read("inbox").unwrap().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | assert_eq!(reopened.revision(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | assert_eq!(persisted.revision, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | assert_eq!(persisted.entries.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | persisted.checkpoints.get("mentions-connection"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | Some(&batch.next_pos) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | drop(reopened); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 166 | service.batch(batch).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 167 | assert_eq!(service.data.revision, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 168 | assert_eq!(service.store.as_ref().unwrap().revision(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 169 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 170 | drop(service); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 171 | config.delete().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 172 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 173 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 174 | #[test] |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 175 | fn repair_fields_default_when_loading_an_older_inbox() { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 176 | let mut inbox = serde_json::to_value(Data::default()).unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 177 | inbox.as_object_mut().unwrap().remove("repair_policy"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 178 | inbox.as_object_mut().unwrap().remove("repair_coverage"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 179 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 180 | let restored: Data = serde_json::from_value(inbox).unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 181 | assert_eq!(restored.repair_policy, HistoryRepairPolicy::default()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 182 | assert!(restored.repair_coverage.is_empty()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 183 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 184 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 185 | #[tokio::test] |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 186 | async fn repair_policy_and_coverage_roundtrip_through_the_inbox() { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 187 | let server = MatrixMockServer::new().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 188 | let room_id = room_id!("!repair-coverage:localhost"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 189 | let (client, _) = joined_client(&server, room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 190 | let config = config(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 191 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 192 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 193 | .unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 194 | let media = Arc::new(MediaService::new( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 195 | client.clone(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 196 | EngineConfig::default().max_media_bytes, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 197 | EngineConfig::default().max_media_sources, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 198 | )); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 199 | let (handle, mut service, _) = |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 200 | Service::new(client.clone(), media, Some(store), EngineConfig::default()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 201 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 202 | .unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 203 | service.recovery_budget = 0; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 204 | service.latest_scan = false; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 205 | let service_task = tokio::spawn(service.run()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 206 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 207 | let default_policy = HistoryRepairPolicy::default(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 208 | assert_eq!(handle.repair_policy().await.unwrap(), default_policy); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 209 | let stored_policy = HistoryRepairPolicy::Bridge; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 210 | handle |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 211 | .set_repair_policy(stored_policy.clone()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 212 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 213 | .unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 214 | handle.repair_resolved(room_id.to_owned()).await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 215 | let coverage = handle.repair_coverage(room_id.to_owned()).await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 216 | assert!(coverage.last_confirmed_ms.is_some()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 217 | assert!(!coverage.reliable); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 218 | assert_eq!(coverage.duration_ms(), None); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 219 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 220 | handle.stop().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 221 | service_task.await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 222 | drop(handle); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 223 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 224 | let reopened = MentionsStore::open(&config, account(&client), MAX_BYTES) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 225 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 226 | .unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 227 | let persisted: Data = reopened.read("inbox").unwrap().unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 228 | assert_eq!(persisted.repair_policy, stored_policy); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 229 | let coverage = persisted.repair_coverage.get(room_id).unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 230 | assert!(coverage.last_confirmed_ms.is_some()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 231 | assert!(coverage.boundary_ms.is_none()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 232 | assert!(!coverage.unresolved); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 233 | assert!(!coverage.reliable); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 234 | drop(reopened); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 235 | config.delete().await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 236 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 237 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 238 | #[tokio::test] |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 239 | async fn repair_limited_sync_freezes_the_last_room_checkpoint() { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 240 | let server = MatrixMockServer::new().await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 241 | let room_id = room_id!("!repair-boundary:localhost"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 242 | let (client, _) = joined_client(&server, room_id).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 243 | let media = Arc::new(MediaService::new( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 244 | client.clone(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 245 | EngineConfig::default().max_media_bytes, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 246 | EngineConfig::default().max_media_sources, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 247 | )); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 248 | let (_handle, mut service, _) = Service::new(client, media, None, EngineConfig::default()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 249 | .await |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 250 | .unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 251 | service.data.checkpoints.insert( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 252 | "mentions-connection".to_owned(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 253 | "position-before".to_owned(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 254 | ); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 255 | service.data.observed_rooms.insert(room_id.to_owned()); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 256 | service |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 257 | .data |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 258 | .repair_coverage |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 259 | .entry(room_id.to_owned()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 260 | .or_default() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 261 | .last_confirmed_ms = Some(1_000); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 262 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 263 | let events = [ |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 264 | EventFactory::new() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 265 | .room(room_id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 266 | .text_msg("older event") |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 267 | .sender(user_id!("@sender:localhost")) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 268 | .event_id(event_id!("$repair-older:localhost")) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 269 | .into_raw_sync(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 270 | EventFactory::new() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 271 | .room(room_id) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 272 | .text_msg("latest event") |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 273 | .sender(user_id!("@sender:localhost")) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 274 | .event_id(event_id!("$repair-latest:localhost")) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 275 | .into_raw_sync(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 276 | ]; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 277 | let timeline: Vec<_> = events |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 278 | .iter() |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 279 | .map(|event| serde_json::to_value(event).unwrap()) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 280 | .collect(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 281 | let raw_room = serde_json::from_value(json!({ |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 282 | "limited": true, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 283 | "prev_batch": "repair-gap-token", |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 284 | "timeline": timeline, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 285 | })) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 286 | .unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 287 | let batch = ResponseCommitBatch { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 288 | connection_id: "mentions-connection".to_owned(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 289 | previous_pos: Some("position-before".to_owned()), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 290 | next_pos: "position-after".to_owned(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 291 | response: Default::default(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 292 | raw_rooms: [(room_id.to_owned(), raw_room)].into(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 293 | }; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 294 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 295 | service.batch(batch).await.unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 296 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 297 | let coverage = service.data.repair_coverage.get(room_id).unwrap(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 298 | assert_eq!(coverage.last_confirmed_ms, Some(1_000)); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 299 | assert!( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 300 | coverage |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 301 | .boundary_ms |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 302 | .is_some_and(|boundary| boundary > 1_000) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 303 | ); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 304 | assert!(coverage.unresolved); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 305 | assert!(!coverage.reliable); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 306 | assert_eq!(coverage.duration_ms(), None); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 307 | assert_eq!( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 308 | coverage.latest_anchor.as_deref(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 309 | Some(event_id!("$repair-latest:localhost")) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 310 | ); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 311 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 312 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 313 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 314 | async fn service_recovers_notification_history_into_durable_unread_inbox() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 315 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 316 | let room_id = room_id!("!mentions:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 317 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 318 | let first = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 319 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 320 | .text_msg("history mention one") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 321 | .mentions(Mentions::with_user_ids([own_user.clone()])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 322 | .event_id(event_id!("$history-one:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 323 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 324 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 325 | let second = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 326 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 327 | .text_msg("history mention two") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 328 | .mentions(Mentions::with_user_ids([own_user])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 329 | .event_id(event_id!("$history-two:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 330 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 331 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 332 | let first = serde_json::from_str::<serde_json::Value>(first.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 333 | let second = serde_json::from_str::<serde_json::Value>(second.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 334 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 335 | .and(path("/_matrix/client/v3/notifications")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 336 | .and(query_param_is_missing("from")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 337 | .and(query_param("limit", "50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 338 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 339 | "notifications": [{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 340 | "actions": ["notify"], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 341 | "event": first, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 342 | "read": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 343 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 344 | "ts": 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 345 | }], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 346 | "next_token": "history-page-two", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 347 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 348 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 349 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 350 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 351 | .and(path("/_matrix/client/v3/notifications")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 352 | .and(query_param("from", "history-page-two")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 353 | .and(query_param("limit", "50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 354 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 355 | "notifications": [{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 356 | "actions": ["notify"], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 357 | "event": second, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 358 | "read": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 360 | "ts": 2, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 361 | }], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 362 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 363 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 364 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 365 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 366 | let config = config(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 367 | let store = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 368 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 369 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 370 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 371 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 372 | EngineConfig::default().max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 373 | EngineConfig::default().max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 374 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 375 | let (handle, service, mut updates) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | Service::new(client.clone(), media, Some(store), EngineConfig::default()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 377 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 379 | let service_task = tokio::spawn(service.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 380 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 381 | let discovered = tokio::time::timeout(Duration::from_secs(10), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 382 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 383 | let page = updates.borrow_and_update().clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 384 | if page.entries.len() == 2 && !page.coverage.recovering { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 385 | break page; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 386 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 387 | updates.changed().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 388 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 389 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 390 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 391 | handle.stop().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 392 | service_task.await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 393 | let discovered = discovered.expect("notification history should be recovered"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 394 | assert_eq!(discovered.entries.len(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 395 | assert!(discovered.entries.iter().all(|entry| !entry.read)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 396 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 397 | let reopened = MentionsStore::open(&config, account(&client), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 398 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 399 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 400 | let persisted: Data = reopened.read("inbox").unwrap().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 401 | assert_eq!(reopened.revision(), persisted.revision); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 402 | assert_eq!(persisted.entries.len(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 403 | assert!(persisted.entries.values().all(|entry| !entry.read)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 404 | assert!(persisted.latest_cursor.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 405 | assert!(persisted.history_cursor.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 406 | drop(reopened); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 407 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 408 | config.delete().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 409 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 410 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 411 | fn message_body(item: &TimelineItem) -> Option<&str> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 412 | match item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 413 | TimelineItem::Event(event) => match &event.content { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 414 | TimelineContent::Message(message) => Some(&message.body), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 415 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 416 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 417 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 418 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 419 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 420 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 421 | fn media_registration() -> MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 422 | MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 423 | id: MediaId::new("test-scope#image".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 424 | kind: MediaKind::Image, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 425 | full: MediaSourceInfo { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 426 | source: MediaSource::Plain(ruma::mxc_uri!("mxc://mentions.example/image").to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 427 | mimetype: Some("image/png".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 428 | declared_size: Some(1234), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 429 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 430 | thumbnail: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 431 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 432 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 433 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | async fn live_context_survives_capture_then_edit_and_redaction() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 437 | let room_id = room_id!("!mentions:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 438 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 439 | let mention = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 440 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 441 | .text_msg("direct mention") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 442 | .mentions(Mentions::with_user_ids([own_user])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 443 | .event_id(event_id!("$live-mention:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 444 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 445 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 446 | let mut mention: serde_json::Value = serde_json::from_str(mention.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 447 | mention["origin_server_ts"] = json!(10); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 448 | let following = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 449 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 450 | .text_msg("following") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 451 | .event_id(event_id!("$live-following:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 452 | .sender(user_id!("@bob:localhost")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 453 | .into_raw_sync(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 454 | let mut following: serde_json::Value = serde_json::from_str(following.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 455 | following["origin_server_ts"] = json!(11); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 456 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 457 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 458 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 459 | EngineConfig::default().max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 460 | EngineConfig::default().max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 461 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 462 | let (_handle, mut service, _updates) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 463 | Service::new(client, media, None, EngineConfig::default()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 464 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 465 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 466 | let room_id = room_id.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 467 | let mention_key = MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 468 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 469 | event_id: event_id!("$live-mention:localhost").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 470 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 471 | let following_id = format!("{room_id}\n$live-following:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 472 | let mut data = Data::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 473 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 474 | service.observe(&mut data, &room_id, mention).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 475 | service |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 476 | .observe(&mut data, &room_id, following) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 477 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 478 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 479 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 480 | let mention_identity = id(&mention_key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 481 | let entry = data.entries.get(&mention_identity).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 482 | let revision = entry.revision; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 483 | let target = entry.event.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 484 | service.captured( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 485 | &mut data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 486 | mention_identity.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 487 | revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 488 | Ok(( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 489 | crate::EventContext { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 490 | events_before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 491 | event: Some(target), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 492 | events_after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 493 | start_token: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 494 | end_token: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 495 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 496 | Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 497 | Vec::new(), |
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 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 501 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 502 | data.entries[&mention_identity].capture, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 503 | MentionCaptureState::Partial |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 504 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 505 | assert!(data.events.contains_key(&following_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 506 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 507 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 508 | assert_eq!(context.events_after.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 509 | assert_eq!(message_body(&context.events_after[0]), Some("following")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 510 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 511 | let edit = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 512 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 513 | "body": "* edited following", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 514 | "m.new_content": {"body": "edited following", "msgtype": "m.text"}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 515 | "m.relates_to": {"event_id": "$live-following:localhost", "rel_type": "m.replace"}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 516 | "msgtype": "m.text" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 517 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 518 | "event_id": "$live-following-edit:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 519 | "origin_server_ts": 12, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 520 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 521 | "sender": "@bob:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 522 | "type": "m.room.message" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 523 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 524 | service.observe(&mut data, &room_id, edit).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 525 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 526 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 527 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 528 | message_body(&context.events_after[0]), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 529 | Some("edited following") |
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 | let saved_following = data.events.get_mut(&following_id).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 533 | assert!(saved_following.raw.is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 534 | saved_following.media.push(media_registration()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 535 | assert_eq!(saved_following.media.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 536 | let redaction = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 537 | "content": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 538 | "event_id": "$live-following-redaction:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 539 | "origin_server_ts": 13, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | "redacts": "$live-following:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | "room_id": room_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 542 | "sender": "@bob:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 543 | "type": "m.room.redaction" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 544 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 545 | service |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 546 | .observe(&mut data, &room_id, redaction) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 547 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 548 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 549 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 550 | let saved_following = data.events.get(&following_id).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 551 | assert!(matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 552 | &saved_following.item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 553 | TimelineItem::Event(event) if event.content == TimelineContent::Redacted |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 554 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 555 | assert!(saved_following.raw.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 556 | assert!(saved_following.media.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 557 | let (saved, _) = data.context(&mention_key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 558 | let context = saved.context.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 559 | assert!(matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 560 | &context.events_after[0], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 561 | TimelineItem::Event(event) if event.content == TimelineContent::Redacted |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 562 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 563 | data.windows.remove(&mention_identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 564 | service.data = data; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 565 | let CommandOutput::MentionContext(context) = service |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 566 | .command(MentionsCommand::RetryContext(mention_key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 567 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 568 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 569 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 570 | panic!("retry should return saved context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 571 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 572 | assert_eq!(context.state, MentionCaptureState::Pending); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 573 | assert!(context.context.unwrap().event.is_some()); |
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 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 577 | async fn explicit_recovery_rechecks_server_support() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 578 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | let (client, _) = joined_client(&server, room_id!("!mentions:localhost")).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | let config = EngineConfig::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 582 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 583 | config.max_media_bytes, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 584 | config.max_media_sources, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 585 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 586 | let (_, mut service, _) = Service::new(client, media, None, config).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | service.data.coverage.historical_supported = Some(false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | service.recovery_budget = 0; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 589 | service.command(MentionsCommand::Recover).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | assert_eq!(service.data.coverage.historical_supported, None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | assert!(service.data.coverage.recovering); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | assert_eq!(service.recovery_budget, 20); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 593 | } |