use std::collections::BTreeMap;
use jmap_scene_server::{SCENE_DATA_TYPES, SceneEvent, Urgency, push_urgency};
use jmap_server::state_change_for;
use jmap_types::{Id, PushSubscription, State};
#[test]
fn the_four_types_participate_in_state_change() {
let mut types = SCENE_DATA_TYPES.to_vec();
types.sort_unstable();
assert_eq!(
types,
["SceneAsset", "SceneAvatar", "SceneObject", "SceneRegion"]
);
}
fn all_four_changed() -> BTreeMap<Id, BTreeMap<String, State>> {
serde_json::from_str(
r#"{"account-xyz":{"SceneRegion":"r41a","SceneObject":"o88f2","SceneAvatar":"av03c","SceneAsset":"as7e1"}}"#,
)
.unwrap()
}
#[test]
fn subscription_types_select_scene_types() {
let sub: PushSubscription = serde_json::from_str(
r#"{"deviceClientId":"d1","url":"https://push.example.com/endpoint/scene-abc123","types":["SceneRegion","SceneAvatar"]}"#,
)
.unwrap();
let change = state_change_for(&sub, &all_four_changed()).unwrap();
let expected: BTreeMap<Id, BTreeMap<String, State>> =
serde_json::from_str(r#"{"account-xyz":{"SceneRegion":"r41a","SceneAvatar":"av03c"}}"#)
.unwrap();
assert_eq!(change.changed, expected);
let excluded: BTreeMap<Id, BTreeMap<String, State>> =
serde_json::from_str(r#"{"account-xyz":{"SceneObject":"o88f3","SceneAsset":"as7e2"}}"#)
.unwrap();
assert!(state_change_for(&sub, &excluded).is_none());
}
#[test]
fn recommended_urgencies() {
let cases = [
(SceneEvent::RegionDestroyedWithOwnObjects, "high"),
(
SceneEvent::AvatarEnterOrLeave {
bound_chat_notified: false,
},
"normal",
),
(SceneEvent::RegionChangedForParticipant, "normal"),
(SceneEvent::ObjectDestroyedByAdmin, "low"),
(SceneEvent::AssetChanged, "very-low"),
(SceneEvent::PositionReconciliation, "very-low"),
];
for (event, header) in cases {
assert_eq!(event.urgency().as_str(), header, "{event:?}");
}
}
#[test]
fn reconciliation_is_never_high() {
assert_ne!(
push_urgency([SceneEvent::PositionReconciliation; 3]),
Some(Urgency::High)
);
}
#[test]
fn combined_payload_takes_the_highest_urgency() {
let urgency = push_urgency([
SceneEvent::AssetChanged,
SceneEvent::RegionDestroyedWithOwnObjects,
SceneEvent::ObjectDestroyedByAdmin,
]);
assert_eq!(urgency.map(Urgency::as_str), Some("high"));
let urgency = push_urgency([
SceneEvent::PositionReconciliation,
SceneEvent::ObjectDestroyedByAdmin,
]);
assert_eq!(urgency.map(Urgency::as_str), Some("low"));
assert_eq!(push_urgency([]), None);
}
#[test]
fn avatar_push_deduplicated_against_bound_chat_push() {
let deduped = SceneEvent::AvatarEnterOrLeave {
bound_chat_notified: true,
};
assert!(
!["normal", "high"].contains(&deduped.urgency().as_str()),
"{:?}",
deduped.urgency()
);
let alone = SceneEvent::AvatarEnterOrLeave {
bound_chat_notified: false,
};
assert_eq!(alone.urgency().as_str(), "normal");
let urgency = push_urgency([deduped, SceneEvent::RegionDestroyedWithOwnObjects]);
assert_eq!(urgency.map(Urgency::as_str), Some("high"));
}