31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | mod support; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use std::time::Duration; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 5 | use macltok::{ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 6 | ConnectionState, Engine, EngineConfig, EngineError, Event, PackUsage, Retain, RoomMembership, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 7 | Termination, ThreadUnread, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 8 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | use matrix_sdk::{ |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 10 | ruma::{ |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 11 | RoomId, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 12 | api::client::sync::sync_events::v5, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 13 | event_id, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 14 | events::{ |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 15 | AnySyncStateEvent, Mentions, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 16 | receipt::{ReceiptThread, ReceiptType}, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 17 | }, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 18 | room_id, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 19 | serde::Raw, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 20 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | test_utils::mocks::MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | }; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 23 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 24 | use serde_json::json; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | use tokio::sync::watch; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | async fn rooms_changed_invalidates_room_summaries() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | let room_id = room_id!("!state:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | support::receive_until(&mut events, |event| matches!(event, Event::RoomsChanged)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | let rooms = handle.rooms().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | let summary = rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | .find(|summary| summary.room_id == room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | .expect("synced room was absent from the room summaries"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | assert_eq!(summary.membership, RoomMembership::Joined); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | assert!(!summary.name.is_empty()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | async fn connection_watch_tracks_the_engine_lifecycle() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | let mut connection = handle.connection_state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | let connection_peer = connection.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | assert_eq!(*connection.borrow(), ConnectionState::Idle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | assert_eq!(*connection_peer.borrow(), ConnectionState::Idle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | support::receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | matches!(event, Event::ConnectionChanged(ConnectionState::Connecting)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | assert_ne!(*connection.borrow(), ConnectionState::Idle); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | support::receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | matches!(event, Event::ConnectionChanged(ConnectionState::Online)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | wait_for_connection(&mut connection, ConnectionState::Online).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | assert_eq!(*connection_peer.borrow(), ConnectionState::Online); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | wait_for_connection(&mut connection, ConnectionState::Stopped).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | assert_eq!(*connection_peer.borrow(), ConnectionState::Stopped); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | #[tokio::test] |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 90 | async fn soft_logout_says_so_and_stops_the_engine() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 91 | let server = MatrixMockServer::new().await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 92 | server |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 93 | .mock_sliding_sync() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 94 | .error_unknown_token(true) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 95 | .mount() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 96 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 97 | let client = server.client_builder().build().await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 98 | let (engine, _handle, mut events) = Engine::from_client( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 99 | client, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 100 | EngineConfig::default().with_initial_history_limit(0), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 101 | ) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 102 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 103 | .unwrap(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 104 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | support::receive_until(&mut events, |event| { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 106 | matches!( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 107 | event, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 108 | Event::SessionAccessRejected { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 109 | soft_logout: Some(_) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 110 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 111 | ) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 112 | }) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 113 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | support::receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 115 | let termination = running.await.unwrap().unwrap(); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 116 | assert!(matches!(termination, Termination::SoftLoggedOut { .. })); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 117 | assert_eq!(termination.retain(), Retain::Store); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 118 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 119 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 120 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | async fn closing_the_event_stream_stops_an_idle_engine() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | let (engine, handle, mut events) = Engine::from_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | EngineConfig::default().with_initial_history_limit(0), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | support::receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | matches!(event, Event::ConnectionChanged(ConnectionState::Online)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | drop(events); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | let result = tokio::time::timeout(Duration::from_secs(2), running) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | .expect("engine did not stop after its event receiver closed") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | assert!(matches!(result, Err(EngineError::EventReceiverClosed))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | drop(handle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | async fn wait_for_connection( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | connection: &mut watch::Receiver<ConnectionState>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | expected: ConnectionState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | if *connection.borrow_and_update() == expected { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | connection |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | .changed() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | .expect("connection watch closed before the expected state"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | .expect("expected connection state did not arrive"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 167 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 168 | #[tokio::test] |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 169 | async fn image_packs_follow_room_state_and_account_data() { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 170 | let server = MatrixMockServer::new().await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 171 | server |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 172 | .mock_sliding_sync() |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 173 | .ok(v5::Response::new("1".to_owned())) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 174 | .mount() |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 175 | .await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 176 | let client = server.client_builder().build().await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 177 | let room_id = room_id!("!packs:localhost"); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 178 | server.mock_room_state_encryption().plain().mount().await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 179 | server.sync_joined_room(&client, room_id).await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 180 | let config = EngineConfig::default().with_initial_history_limit(0); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 181 | let (engine, handle, mut events) = Engine::from_client(client.clone(), config).await.unwrap(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 182 | let running = tokio::spawn(engine.run()); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 183 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 184 | let pack: Raw<AnySyncStateEvent> = serde_json::from_value(json!({ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 185 | "type": "im.ponies.room_emotes", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 186 | "state_key": "cats", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 187 | "event_id": "$pack", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 188 | "sender": "@example:localhost", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 189 | "origin_server_ts": 1, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 190 | "content": { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 191 | "pack": { "display_name": "cats", "usage": ["emoticon"] }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 192 | "images": { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 193 | "blep": { "url": "mxc://localhost/blep" }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 194 | "broken": { "url": "not an mxc" }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 195 | "sticker": { "url": "mxc://localhost/sticker", "usage": ["sticker"] }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 196 | }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 197 | }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 198 | })) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 199 | .unwrap(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 200 | server |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 201 | .mock_sync() |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 202 | .ok_and_run(&client, |builder| { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 203 | builder.add_joined_room(JoinedRoomBuilder::new(room_id).add_state_event(pack)); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 204 | builder.add_custom_global_account_data(json!({ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 205 | "type": "im.ponies.emote_rooms", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 206 | "content": { "rooms": { "!packs:localhost": { "cats": {} } } }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 207 | })); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 208 | }) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 209 | .await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 210 | support::receive_until(&mut events, |event| { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 211 | matches!(event, Event::ImagePacksChanged) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 212 | }) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 213 | .await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 214 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 215 | let packs = handle.image_packs().await.unwrap(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 216 | let offered = packs.for_room(Some(room_id)); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 217 | assert_eq!(offered.len(), 1, "an enabled room's pack was offered twice"); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 218 | let pack = offered[0]; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 219 | assert_eq!(pack.id, "room:!packs:localhost:cats"); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 220 | assert_eq!(packs.global, vec![pack.clone()]); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 221 | let images: Vec<_> = pack |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 222 | .images |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 223 | .iter() |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 224 | .map(|image| { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 225 | ( |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 226 | image.shortcode.as_str(), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 227 | image.reaction_key.as_str(), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 228 | image.usage.clone(), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 229 | ) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 230 | }) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 231 | .collect(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 232 | assert_eq!( |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 233 | images, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 234 | [ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 235 | ("blep", "mxc://localhost/blep", vec![PackUsage::Emoticon]), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 236 | ( |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 237 | "sticker", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 238 | "mxc://localhost/sticker", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 239 | vec![PackUsage::Sticker] |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 240 | ), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 241 | ] |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 242 | ); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 243 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 244 | support::shut_down(&handle, &mut events, running).await; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 245 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 246 | |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 247 | /// the sdk counts none of this once threads are on, and a threaded receipt never reaches |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 248 | /// room info, so only our own recount notices it |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 249 | #[tokio::test] |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 250 | async fn thread_unreads_follow_threaded_receipts() { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 251 | let server = MatrixMockServer::new().await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 252 | server |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 253 | .mock_sliding_sync() |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 254 | .ok(v5::Response::new("1".to_owned())) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 255 | .mount() |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 256 | .await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 257 | let client = server.client_builder().build().await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 258 | let room_id = room_id!("!threads:localhost"); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 259 | server.mock_room_state_encryption().plain().mount().await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 260 | server.sync_joined_room(&client, room_id).await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 261 | client.event_cache().subscribe().unwrap(); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 262 | let config = EngineConfig::default().with_initial_history_limit(0); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 263 | let (engine, handle, mut events) = Engine::from_client(client.clone(), config).await.unwrap(); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 264 | let running = tokio::spawn(engine.run()); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 265 | let own = client.user_id().unwrap().to_owned(); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 266 | |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 267 | let (root, first, second) = ( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 268 | event_id!("$root"), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 269 | event_id!("$first"), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 270 | event_id!("$second"), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 271 | ); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 272 | let factory = EventFactory::new().room(room_id).sender(&ALICE); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 273 | server |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 274 | .sync_room( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 275 | &client, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 276 | JoinedRoomBuilder::new(room_id) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 277 | .add_timeline_event(factory.text_msg("root").event_id(root)) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 278 | .add_timeline_event( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 279 | factory |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 280 | .text_msg("you there?") |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 281 | .in_thread(root, root) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 282 | .mentions(Mentions::with_user_ids([own.clone()])) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 283 | .event_id(first), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 284 | ) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 285 | .add_timeline_event( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 286 | factory |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 287 | .text_msg("hello?") |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 288 | .in_thread(root, first) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 289 | .event_id(second), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 290 | ), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 291 | ) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 292 | .await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 293 | let unread = thread_unread_once(&handle, &mut events, room_id, |unread| { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 294 | unread.is_some_and(|unread| unread.messages == 2) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 295 | }) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 296 | .await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 297 | assert_eq!(unread.mentions, 1); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 298 | let summary = room(&handle, room_id).await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 299 | assert!( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 300 | summary.unread_mentions >= 1, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 301 | "the thread mention left the room's count" |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 302 | ); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 303 | |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 304 | server |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 305 | .sync_room( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 306 | &client, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 307 | JoinedRoomBuilder::new(room_id).add_receipt( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 308 | factory |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 309 | .read_receipts() |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 310 | .add( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 311 | first, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 312 | &own, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 313 | ReceiptType::Read, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 314 | ReceiptThread::Thread(root.to_owned()), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 315 | ) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 316 | .into_event(), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 317 | ), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 318 | ) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 319 | .await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 320 | let unread = thread_unread_once(&handle, &mut events, room_id, |unread| { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 321 | unread.is_some_and(|unread| unread.messages == 1) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 322 | }) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 323 | .await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 324 | assert_eq!(unread.mentions, 0); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 325 | |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 326 | server |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 327 | .sync_room( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 328 | &client, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 329 | JoinedRoomBuilder::new(room_id).add_receipt( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 330 | factory |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 331 | .read_receipts() |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 332 | .add(second, &own, ReceiptType::Read, ReceiptThread::Unthreaded) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 333 | .into_event(), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 334 | ), |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 335 | ) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 336 | .await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 337 | thread_unread_once(&handle, &mut events, room_id, |unread| unread.is_none()).await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 338 | |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 339 | support::shut_down(&handle, &mut events, running).await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 340 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 341 | |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 342 | async fn room(handle: &macltok::ClientHandle, room_id: &RoomId) -> macltok::RoomSummary { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 343 | handle |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 344 | .rooms() |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 345 | .await |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 346 | .into_iter() |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 347 | .find(|summary| summary.room_id == room_id) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 348 | .expect("synced room was absent from the room summaries") |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 349 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 350 | |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 351 | /// waits out `RoomsChanged` until the root's counts satisfy `done` |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 352 | async fn thread_unread_once( |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 353 | handle: &macltok::ClientHandle, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 354 | events: &mut macltok::EventStream, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 355 | room_id: &RoomId, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 356 | done: impl Fn(Option<&ThreadUnread>) -> bool, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 357 | ) -> ThreadUnread { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 358 | let root = event_id!("$root"); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 359 | tokio::time::timeout(Duration::from_secs(10), async { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 360 | loop { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 361 | let summary = room(handle, room_id).await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 362 | let unread = summary.thread_unreads.get(root); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 363 | if done(unread) { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 364 | return unread.copied().unwrap_or_default(); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 365 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 366 | support::receive_until(events, |event| matches!(event, Event::RoomsChanged)).await; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 367 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 368 | }) |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 369 | .await |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 370 | .expect("thread unreads never settled") |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 371 | } |