fix(sdk): Test that invited member events get emitted

This commit is contained in:
Damir Jelić 2021-09-13 10:51:20 +02:00
parent 140630745f
commit 500bb6e940
2 changed files with 121 additions and 58 deletions

View file

@ -2134,7 +2134,10 @@ impl Client {
self.process_sync(response).await
}
async fn process_sync(&self, response: sync_events::Response) -> Result<SyncResponse> {
pub(crate) async fn process_sync(
&self,
response: sync_events::Response,
) -> Result<SyncResponse> {
let response = self.base_client.receive_sync_response(response).await?;
let SyncResponse {
next_batch: _,
@ -3183,14 +3186,12 @@ impl Client {
}
#[cfg(test)]
mod test {
pub(crate) mod test {
use std::{
collections::BTreeMap,
convert::{TryFrom, TryInto},
future,
io::Cursor,
str::FromStr,
sync::Arc,
time::Duration,
};
@ -3220,20 +3221,19 @@ mod test {
event_id,
events::{
room::{
member::MemberEventContent,
message::{ImageMessageEventContent, MessageEventContent},
ImageInfo,
},
AnyMessageEventContent, AnySyncStateEvent, EventType, SyncStateEvent,
AnyMessageEventContent, AnySyncStateEvent, EventType,
},
mxc_uri, room_id, thirdparty, uint, user_id, UserId,
};
use serde_json::json;
use super::{Client, Session, SyncSettings, Url};
use crate::{room, ClientConfig, HttpError, RequestConfig, RoomMember};
use crate::{ClientConfig, HttpError, RequestConfig, RoomMember};
async fn logged_in_client() -> Client {
pub(crate) async fn logged_in_client() -> Client {
let session = Session {
access_token: "1234".to_owned(),
user_id: user_id!("@example:localhost"),
@ -3491,56 +3491,6 @@ mod test {
// assert_eq!(1, ignored_users.len())
}
#[tokio::test]
async fn event_handler() {
use std::sync::atomic::{AtomicU8, Ordering::SeqCst};
let client = logged_in_client().await;
let member_count = Arc::new(AtomicU8::new(0));
let typing_count = Arc::new(AtomicU8::new(0));
let power_levels_count = Arc::new(AtomicU8::new(0));
client
.register_event_handler({
let member_count = member_count.clone();
move |_ev: SyncStateEvent<MemberEventContent>, _room: room::Room| {
member_count.fetch_add(1, SeqCst);
future::ready(())
}
})
.await
.register_event_handler({
let typing_count = typing_count.clone();
move |_ev: SyncStateEvent<MemberEventContent>| {
typing_count.fetch_add(1, SeqCst);
future::ready(())
}
})
.await
.register_event_handler({
let power_levels_count = power_levels_count.clone();
move |_ev: SyncStateEvent<MemberEventContent>,
_client: Client,
_room: room::Room| {
power_levels_count.fetch_add(1, SeqCst);
future::ready(())
}
})
.await;
let response = EventBuilder::default()
.add_room_event(EventsJson::Member)
.add_ephemeral(EventsJson::Typing)
.add_state_event(EventsJson::PowerLevels)
.build_sync_response();
client.process_sync(response).await.unwrap();
assert_eq!(member_count.load(SeqCst), 1);
assert_eq!(typing_count.load(SeqCst), 1);
assert_eq!(power_levels_count.load(SeqCst), 1);
}
#[tokio::test]
async fn room_creation() {
let client = logged_in_client().await;

View file

@ -433,3 +433,116 @@ mod static_events {
const ID: (EventKind, &'static str) = (EventKind::State { redacted: true }, C::TYPE);
}
}
#[cfg(test)]
mod test {
use std::{future, sync::Arc};
use matrix_sdk_test::{EventBuilder, EventsJson};
use ruma::{
events::{room::member::MemberEventContent, StrippedStateEvent, SyncStateEvent},
room_id,
};
use serde_json::json;
use crate::{room, Client};
#[tokio::test]
async fn event_handler() -> crate::Result<()> {
use std::sync::atomic::{AtomicU8, Ordering::SeqCst};
let client = crate::client::test::logged_in_client().await;
let member_count = Arc::new(AtomicU8::new(0));
let typing_count = Arc::new(AtomicU8::new(0));
let power_levels_count = Arc::new(AtomicU8::new(0));
let invited_member_count = Arc::new(AtomicU8::new(0));
client
.register_event_handler({
let member_count = member_count.clone();
move |_ev: SyncStateEvent<MemberEventContent>, _room: room::Room| {
member_count.fetch_add(1, SeqCst);
future::ready(())
}
})
.await
.register_event_handler({
let typing_count = typing_count.clone();
move |_ev: SyncStateEvent<MemberEventContent>| {
typing_count.fetch_add(1, SeqCst);
future::ready(())
}
})
.await
.register_event_handler({
let power_levels_count = power_levels_count.clone();
move |_ev: SyncStateEvent<MemberEventContent>,
_client: Client,
_room: room::Room| {
power_levels_count.fetch_add(1, SeqCst);
future::ready(())
}
})
.await
.register_event_handler({
let invited_member_count = invited_member_count.clone();
move |_ev: StrippedStateEvent<MemberEventContent>| {
invited_member_count.fetch_add(1, SeqCst);
future::ready(())
}
})
.await;
let response = EventBuilder::default()
.add_room_event(EventsJson::Member)
.add_ephemeral(EventsJson::Typing)
.add_state_event(EventsJson::PowerLevels)
.add_custom_invited_event(
&room_id!("!test_invited:example.org"),
json!({
"content": {
"avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
"displayname": "Alice",
"membership": "invite",
},
"event_id": "$143273582443PhrSn:example.org",
"origin_server_ts": 1432735824653u64,
"room_id": "!jEsUZKDJdhlrceRyVU:example.org",
"sender": "@example:example.org",
"state_key": "@alice:example.org",
"type": "m.room.member",
"unsigned": {
"age": 1234,
"invite_room_state": [
{
"content": {
"name": "Example Room"
},
"sender": "@bob:example.org",
"state_key": "",
"type": "m.room.name"
},
{
"content": {
"join_rule": "invite"
},
"sender": "@bob:example.org",
"state_key": "",
"type": "m.room.join_rules"
}
]
}
}),
)
.build_sync_response();
client.process_sync(response).await?;
assert_eq!(member_count.load(SeqCst), 1);
assert_eq!(typing_count.load(SeqCst), 1);
assert_eq!(power_levels_count.load(SeqCst), 1);
assert_eq!(invited_member_count.load(SeqCst), 1);
Ok(())
}
}