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::{ |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 6 | ConnectionState, Engine, EngineConfig, EngineError, Event, Retain, RoomMembership, Termination, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 7 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | ruma::{api::client::sync::sync_events::v5, room_id}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | test_utils::mocks::MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | use tokio::sync::watch; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | async fn rooms_changed_invalidates_room_summaries() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | let room_id = room_id!("!state:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | support::receive_until(&mut events, |event| matches!(event, Event::RoomsChanged)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | let rooms = handle.rooms().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | let summary = rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | .find(|summary| summary.room_id == room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | .expect("synced room was absent from the room summaries"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | assert_eq!(summary.membership, RoomMembership::Joined); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | assert!(!summary.name.is_empty()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | async fn connection_watch_tracks_the_engine_lifecycle() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | let mut connection = handle.connection_state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | let connection_peer = connection.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | assert_eq!(*connection.borrow(), ConnectionState::Idle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | assert_eq!(*connection_peer.borrow(), ConnectionState::Idle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | support::receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | matches!(event, Event::ConnectionChanged(ConnectionState::Connecting)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | assert_ne!(*connection.borrow(), ConnectionState::Idle); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | support::receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | matches!(event, Event::ConnectionChanged(ConnectionState::Online)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | wait_for_connection(&mut connection, ConnectionState::Online).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | assert_eq!(*connection_peer.borrow(), ConnectionState::Online); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | wait_for_connection(&mut connection, ConnectionState::Stopped).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | assert_eq!(*connection_peer.borrow(), ConnectionState::Stopped); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | #[tokio::test] |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 77 | async fn soft_logout_says_so_and_stops_the_engine() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 78 | let server = MatrixMockServer::new().await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 79 | server |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 80 | .mock_sliding_sync() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 81 | .error_unknown_token(true) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 82 | .mount() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 83 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 84 | let client = server.client_builder().build().await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 85 | let (engine, _handle, mut events) = Engine::from_client( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 86 | client, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 87 | EngineConfig::default().with_initial_history_limit(0), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 88 | ) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 89 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 90 | .unwrap(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 91 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | support::receive_until(&mut events, |event| { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 93 | matches!( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 94 | event, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 95 | Event::SessionAccessRejected { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 96 | soft_logout: Some(_) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 97 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 98 | ) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 99 | }) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 100 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | support::receive_until(&mut events, |event| matches!(event, Event::Stopped)).await; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 102 | let termination = running.await.unwrap().unwrap(); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 103 | assert!(matches!(termination, Termination::SoftLoggedOut { .. })); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 104 | assert_eq!(termination.retain(), Retain::Store); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 105 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 106 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 107 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | async fn closing_the_event_stream_stops_an_idle_engine() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | let (engine, handle, mut events) = Engine::from_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | EngineConfig::default().with_initial_history_limit(0), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | support::receive_until(&mut events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | matches!(event, Event::ConnectionChanged(ConnectionState::Online)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | drop(events); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | let result = tokio::time::timeout(Duration::from_secs(2), running) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | .expect("engine did not stop after its event receiver closed") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | assert!(matches!(result, Err(EngineError::EventReceiverClosed))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | drop(handle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | async fn wait_for_connection( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | connection: &mut watch::Receiver<ConnectionState>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | expected: ConnectionState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | if *connection.borrow_and_update() == expected { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | connection |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | .changed() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | .expect("connection watch closed before the expected state"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | .expect("expected connection state did not arrive"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | } |