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