Skip to content

Commit

Permalink
test: comprehensive beacon_info testing
Browse files Browse the repository at this point in the history
  • Loading branch information
torrybr committed Jul 24, 2024
1 parent d77d387 commit 431bd15
Showing 1 changed file with 98 additions and 33 deletions.
131 changes: 98 additions & 33 deletions crates/matrix-sdk/tests/integration/room/joined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
};

use futures_util::future::join_all;
use js_int::uint;
use matrix_sdk::{
config::SyncSettings,
room::{edit::EditedContent, Receipts, ReportedContentScore, RoomMemberRole},
Expand All @@ -14,6 +15,7 @@ use matrix_sdk_test::{
async_test, test_json, test_json::sync::CUSTOM_ROOM_POWER_LEVELS, EphemeralTestEvent,
GlobalAccountDataTestEvent, JoinedRoomBuilder, SyncResponseBuilder, DEFAULT_TEST_ROOM_ID,
};
use ruma::events::location::AssetType;
use ruma::{
api::client::{membership::Invite3pidInit, receipt::create_receipt::v3::ReceiptType},
assign, event_id,
Expand All @@ -22,7 +24,8 @@ use ruma::{
room::message::{RoomMessageEventContent, RoomMessageEventContentWithoutRelation},
AnySyncStateEvent, StateEventType, TimelineEventType,
},
int, mxc_uri, owned_event_id, room_id, thirdparty, user_id, OwnedUserId, TransactionId,
int, mxc_uri, owned_event_id, room_id, thirdparty, user_id, MilliSecondsSinceUnixEpoch,
OwnedUserId, TransactionId,
};
use serde_json::{json, Value};
use wiremock::{
Expand Down Expand Up @@ -780,19 +783,17 @@ async fn test_start_live_location_share_for_room() {
.mount(&server)
.await;

// Initial Sync with no beacon information
mock_sync(&server, &*test_json::SYNC, None).await;

let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

mock_sync(&server, &*test_json::SYNC, None).await;

let _response = client.sync_once(sync_settings.clone()).await.unwrap();

let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();

let response =
room.start_live_location_share(3000, Some("Live Share".to_owned())).await.unwrap();

// Verify the event is correctly sent.
assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id);
server.reset().await;

Expand All @@ -809,12 +810,12 @@ async fn test_start_live_location_share_for_room() {
"content": {
"description": "Live Share",
"live": true,
"org.matrix.msc3488.ts": 1436829458432_u64,
"org.matrix.msc3488.ts": 1_636_829_458,
"timeout": 3000,
"org.matrix.msc3488.asset": { "type": "m.self" }
},
"event_id": "$15139375514XsgmR:localhost",
"origin_server_ts": 151393755000000_u64,
"origin_server_ts": 1_636_829_458,
"sender": "@example:localhost",
"state_key": "@example:localhost",
"type": "org.matrix.msc3672.beacon_info",
Expand All @@ -836,29 +837,30 @@ async fn test_start_live_location_share_for_room() {
let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

// Verify the event is correctly processed in the state store.
let state_events = room.get_state_events(StateEventType::BeaconInfo).await.unwrap();
assert_eq!(state_events.len(), 1);

if let Some(raw_event) = state_events.first() {
match raw_event.deserialize() {
Ok(AnySyncOrStrippedState::Sync(AnySyncStateEvent::BeaconInfo(e))) => {
let content = e.as_original().unwrap().content.clone();
assert_eq!(e.sender(), room.own_user_id());
assert_eq!(e.state_key(), "@example:localhost");
assert_eq!(e.event_id(), event_id!("$15139375514XsgmR:localhost"));

assert_eq!(content.description, Some("Live Share".to_owned()));
assert_eq!(content.timeout, Duration::from_millis(3000));
assert!(content.live);
}
_ => {
panic!("Expected a BeaconInfo event");
}
}
} else {
panic!("There should be a beacon_info state event");
}
let raw_event = state_events.first().expect("There should be a beacon_info state event");

let ev = match raw_event.deserialize().expect("Failed to deserialize event") {
AnySyncOrStrippedState::Sync(AnySyncStateEvent::BeaconInfo(ev)) => ev,
_ => panic!("Expected a BeaconInfo event"),
};

let content = ev.as_original().unwrap().content.clone();

assert_eq!(ev.sender(), room.own_user_id());
assert_eq!(ev.state_key(), "@example:localhost");
assert_eq!(ev.event_id(), event_id!("$15139375514XsgmR:localhost"));
assert_eq!(ev.event_type(), StateEventType::BeaconInfo);
assert_eq!(ev.origin_server_ts(), MilliSecondsSinceUnixEpoch(uint!(1_636_829_458)));

assert_eq!(content.description, Some("Live Share".to_owned()));
assert_eq!(content.timeout, Duration::from_millis(3000));
assert_eq!(content.ts, MilliSecondsSinceUnixEpoch(uint!(1_636_829_458)));
assert_eq!(content.asset.type_, AssetType::Self_);

assert!(content.live);
}

#[async_test]
Expand All @@ -872,6 +874,8 @@ async fn test_stop_sharing_live_location() {
.mount(&server)
.await;

let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

mock_sync(
&server,
json!({
Expand All @@ -885,12 +889,12 @@ async fn test_stop_sharing_live_location() {
"content": {
"description": "Live Share",
"live": true,
"org.matrix.msc3488.ts": 1436829458432_u64,
"timeout": 86400000,
"org.matrix.msc3488.ts": 1_636_829_458,
"timeout": 3000,
"org.matrix.msc3488.asset": { "type": "m.self" }
},
"event_id": "$15139375514XsgmR:localhost",
"origin_server_ts": 151393755000000_u64,
"origin_server_ts": 1_636_829_458,
"sender": "@example:localhost",
"state_key": "@example:localhost",
"type": "org.matrix.msc3672.beacon_info",
Expand All @@ -909,16 +913,77 @@ async fn test_stop_sharing_live_location() {
)
.await;

let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

let _response = client.sync_once(sync_settings).await.unwrap();
let _response = client.sync_once(sync_settings.clone()).await.unwrap();

let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();

let response = room.stop_live_location_share().await.unwrap();

assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id);
server.reset().await;

mock_sync(
&server,
json!({
"next_batch": "s526_47314_1_7_1_1_1_1_1",
"rooms": {
"join": {
*DEFAULT_TEST_ROOM_ID: {
"state": {
"events": [
{
"content": {
"description": "Live Share",
"live": false,
"org.matrix.msc3488.ts": 1_636_829_458,
"timeout": 3000,
"org.matrix.msc3488.asset": { "type": "m.self" }
},
"event_id": "$15139375514XsgmR:localhost",
"origin_server_ts": 1_636_829_458,
"sender": "@example:localhost",
"state_key": "@example:localhost",
"type": "org.matrix.msc3672.beacon_info",
"unsigned": {
"age": 7034220
}
},
]
}
}
}
}

}),
None,
)
.await;

let _response = client.sync_once(sync_settings.clone()).await.unwrap();
server.reset().await;

let state_events = room.get_state_events(StateEventType::BeaconInfo).await.unwrap();
assert_eq!(state_events.len(), 1);

let raw_event = state_events.first().expect("There should be a beacon_info state event");

let ev = match raw_event.deserialize().expect("Failed to deserialize event") {
AnySyncOrStrippedState::Sync(AnySyncStateEvent::BeaconInfo(ev)) => ev,
_ => panic!("Expected a BeaconInfo event"),
};

let content = ev.as_original().unwrap().content.clone();

assert_eq!(ev.sender(), room.own_user_id());
assert_eq!(ev.state_key(), "@example:localhost");
assert_eq!(ev.event_id(), event_id!("$15139375514XsgmR:localhost"));
assert_eq!(ev.event_type(), StateEventType::BeaconInfo);
assert_eq!(ev.origin_server_ts(), MilliSecondsSinceUnixEpoch(uint!(1_636_829_458)));

assert_eq!(content.description, Some("Live Share".to_owned()));
assert_eq!(content.timeout, Duration::from_millis(3000));
assert_eq!(content.ts, MilliSecondsSinceUnixEpoch(uint!(1_636_829_458)));
assert_eq!(content.asset.type_, AssetType::Self_);

assert!(!content.live);
}

0 comments on commit 431bd15

Please sign in to comment.