diff --git a/src/room.rs b/src/room.rs index 2bbec9e..948d060 100644 --- a/src/room.rs +++ b/src/room.rs @@ -915,4 +915,65 @@ mod tests { .await .unwrap(); } + + #[tokio::test] + async fn test_occupant_update_presence() { + let presence_louise1 = Presence::new(PresenceType::None) + .with_from(LOUISE_FULL1.clone()) + .with_to(LOUISE_ROOM1_PART.clone()); + let presence_louise2 = Presence::new(PresenceType::None) + .with_from(LOUISE_FULL2.clone()) + .with_to(LOUISE_ROOM1_PART.clone()); + + let mut occupant = Occupant::new(presence_louise1).unwrap(); + occupant.add_session(presence_louise2).unwrap(); + + let presence1 = Presence::new(PresenceType::None) + .with_from(LOUISE_FULL1.clone()) + .with_to(ROOM1_BARE.clone()) + .with_show(PresenceShow::Away) + .with_payloads(vec![ + Muc::new().into(), + MucUser { + status: Vec::new(), + items: vec![MucItem::new(Affiliation::Owner, Role::Moderator)], + } + .into(), + ]); + match occupant.update_presence(presence1.clone()) { + Ok(()) => (), + err => panic!("Err: {:?}", err), + } + assert_eq!(occupant.sessions[0].presence, presence1); + + let presence2 = Presence::new(PresenceType::None) + .with_from(LOUISE_FULL2.clone()) + .with_to(ROOM1_BARE.clone()) + .with_show(PresenceShow::Xa); + + match occupant.update_presence(presence2.clone()) { + Err(Error::SecondarySession(session)) if session.real == *LOUISE_FULL2 => (), + err => panic!( + "Should return Error::SecondarySession(Session {{ {:?} }}), returned: {:?}", + *LOUISE_FULL2, err, + ), + } + assert_eq!( + occupant.sessions[1], + Session::with_nick(presence2.clone(), LOUISE_NICK.clone()).unwrap() + ); + + let presence_leave_louise2 = Presence::new(PresenceType::Unavailable) + .with_from(LOUISE_FULL2.clone()) + .with_to(LOUISE_ROOM1_PART.clone()); + occupant.remove_session(presence_leave_louise2).unwrap(); + + match occupant.update_presence(presence2) { + Err(Error::NonexistantSession(session)) if session.real == *LOUISE_FULL2 => (), + err => panic!( + "Should return Error::SecondarySession(Session {{ {:?} }}), returned: {:?}", + *LOUISE_FULL2, err, + ), + } + } }