refactor(tests): make more use of the MatrixMockServer in ui tests

This commit is contained in:
Benjamin Bouvier
2026-04-09 15:33:29 +02:00
parent 2da262e56c
commit c02d0f1d01
4 changed files with 11 additions and 52 deletions
@@ -23,14 +23,9 @@ use ruma::{
owned_mxc_uri, room_id,
time::{Duration, Instant},
};
use serde_json::json;
use stream_assert::{assert_next_matches, assert_pending};
use tempfile::TempDir;
use tokio::{spawn, sync::Barrier, task::yield_now, time::sleep};
use wiremock::{
Mock, ResponseTemplate,
matchers::{header, method, path},
};
use crate::timeline::sliding_sync::{assert_timeline_stream, timeline_event};
@@ -2680,14 +2675,7 @@ async fn test_room_empty_timeline() {
let (client, server, room_list) = new_room_list_service().await.unwrap();
server.mock_room_state_encryption().plain().mount().await;
Mock::given(method("POST"))
.and(path("_matrix/client/v3/createRoom"))
.and(header("authorization", "Bearer 1234"))
.respond_with(
ResponseTemplate::new(200).set_body_json(json!({ "room_id": "!example:localhost"})),
)
.mount(server.server())
.await;
server.mock_create_room().ok().mount().await;
let room = client.create_room(CreateRoomRequest::default()).await.unwrap();
let room_id = room.room_id().to_owned();
@@ -213,11 +213,7 @@ async fn test_sync_service_offline_mode() {
let sync_service = SyncService::builder(client).with_offline_mode().build().await.unwrap();
let mut states = sync_service.state();
Mock::given(SlidingSyncMatcher)
.respond_with(ResponseTemplate::new(404))
.expect(1..)
.mount(mock_server.server())
.await;
mock_server.mock_sliding_sync().error_unrecognized().expect(1..).mount().await;
{
let _versions_guard = mock_server.mock_versions().error500().mount_as_scoped().await;
@@ -240,11 +236,7 @@ async fn test_sync_service_offline_mode_stopping() {
let sync_service = SyncService::builder(client).with_offline_mode().build().await.unwrap();
let mut states = sync_service.state();
Mock::given(SlidingSyncMatcher)
.respond_with(ResponseTemplate::new(404))
.expect(1..)
.mount(mock_server.server())
.await;
mock_server.mock_sliding_sync().error_unrecognized().expect(1..).mount().await;
mock_server.mock_versions().error500().mount().await;
sync_service.start().await;
@@ -263,10 +255,7 @@ async fn test_sync_service_offline_mode_restarting() {
let sync_service = SyncService::builder(client).with_offline_mode().build().await.unwrap();
let mut states = sync_service.state();
Mock::given(SlidingSyncMatcher)
.respond_with(ResponseTemplate::new(404))
.mount(mock_server.server())
.await;
mock_server.mock_sliding_sync().error_unrecognized().expect(1..).mount().await;
mock_server.mock_versions().error500().mount().await;
sync_service.start().await;
@@ -26,10 +26,8 @@ use ruma::{
events::room::message::{MessageType, RoomMessageEventContent},
room_id, user_id,
};
use serde_json::json;
use stream_assert::{assert_next_matches, assert_pending};
use tokio::task::yield_now;
use wiremock::ResponseTemplate;
#[async_test]
async fn test_echo() {
@@ -227,14 +225,10 @@ async fn test_dedup_by_event_id_late() {
server
.mock_room_send()
.respond_with(
ResponseTemplate::new(200)
.set_body_json(json!({ "event_id": event_id }))
// Not great to use a timer for this, but it's what wiremock gives us right now.
// Ideally we'd wait on a channel to produce a value or sth. like that, but
// wiremock doesn't allow to handle multiple queries at the same time.
.set_delay(Duration::from_millis(500)),
)
// Not great to use a timer for this, but it's what wiremock gives us right now.
// Ideally we'd wait on a channel to produce a value or sth. like that, but
// wiremock doesn't allow to handle multiple queries at the same time.
.ok_with_delay(event_id, Duration::from_millis(500))
.mount()
.await;
@@ -45,7 +45,7 @@ use ruma::{
MediaSource,
message::{MessageType, TextMessageEventContent},
},
room_id, uint,
mxc_uri, room_id, uint,
};
use serde_json::json;
use stream_assert::assert_pending;
@@ -385,22 +385,10 @@ async fn test_send_media_with_thumbnail() -> TestResult {
let thumbnail_data = b"hello world".to_vec();
// A mock to upload the thumbnail.
mock.mock_upload()
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"content_uri": "mxc://sdk.rs/thumbnail"
})))
.mock_once()
.mount()
.await;
mock.mock_upload().ok(mxc_uri!("mxc://sdk.rs/thumbnail")).mock_once().mount().await;
// A mock to upload the media file.
mock.mock_upload()
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"content_uri": "mxc://sdk.rs/media"
})))
.mock_once()
.mount()
.await;
mock.mock_upload().ok(mxc_uri!("mxc://sdk.rs/media")).mock_once().mount().await;
// A mock for sending the media event.
mock.mock_room_send().ok(event_id!("$media")).mock_once().mount().await;