fix(sdk): Test that invited member events get emitted
This commit is contained in:
parent
140630745f
commit
500bb6e940
2 changed files with 121 additions and 58 deletions
|
@ -2134,7 +2134,10 @@ impl Client {
|
||||||
self.process_sync(response).await
|
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 response = self.base_client.receive_sync_response(response).await?;
|
||||||
let SyncResponse {
|
let SyncResponse {
|
||||||
next_batch: _,
|
next_batch: _,
|
||||||
|
@ -3183,14 +3186,12 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
pub(crate) mod test {
|
||||||
use std::{
|
use std::{
|
||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
future,
|
|
||||||
io::Cursor,
|
io::Cursor,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
sync::Arc,
|
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3220,20 +3221,19 @@ mod test {
|
||||||
event_id,
|
event_id,
|
||||||
events::{
|
events::{
|
||||||
room::{
|
room::{
|
||||||
member::MemberEventContent,
|
|
||||||
message::{ImageMessageEventContent, MessageEventContent},
|
message::{ImageMessageEventContent, MessageEventContent},
|
||||||
ImageInfo,
|
ImageInfo,
|
||||||
},
|
},
|
||||||
AnyMessageEventContent, AnySyncStateEvent, EventType, SyncStateEvent,
|
AnyMessageEventContent, AnySyncStateEvent, EventType,
|
||||||
},
|
},
|
||||||
mxc_uri, room_id, thirdparty, uint, user_id, UserId,
|
mxc_uri, room_id, thirdparty, uint, user_id, UserId,
|
||||||
};
|
};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use super::{Client, Session, SyncSettings, Url};
|
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 {
|
let session = Session {
|
||||||
access_token: "1234".to_owned(),
|
access_token: "1234".to_owned(),
|
||||||
user_id: user_id!("@example:localhost"),
|
user_id: user_id!("@example:localhost"),
|
||||||
|
@ -3491,56 +3491,6 @@ mod test {
|
||||||
// assert_eq!(1, ignored_users.len())
|
// 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]
|
#[tokio::test]
|
||||||
async fn room_creation() {
|
async fn room_creation() {
|
||||||
let client = logged_in_client().await;
|
let client = logged_in_client().await;
|
||||||
|
|
|
@ -433,3 +433,116 @@ mod static_events {
|
||||||
const ID: (EventKind, &'static str) = (EventKind::State { redacted: true }, C::TYPE);
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue