use std::collections::{BTreeMap, HashMap, HashSet};
use jmap_chat_server::{chat_state_change_for, strip_unsupported_chat_push, validate_chat_push};
use jmap_chat_types::ChatPushCapability;
use jmap_server::SetErrorType;
use jmap_types::{Id, PushSubscription, State};
use serde_json::{Map, Value, json};
const PUSH_ACCOUNT: &str = "acct-push";
const PLAIN_ACCOUNT: &str = "acct-plain";
fn id(s: &str) -> Id {
serde_json::from_value(json!(s)).expect("valid Id")
}
fn state(s: &str) -> State {
serde_json::from_value(json!(s)).expect("valid State")
}
fn caps() -> HashMap<Id, ChatPushCapability> {
let cap: ChatPushCapability = serde_json::from_value(json!({
"maxSnippetBytes": 256,
"supportedUrgencyValues": ["normal", "high"],
}))
.expect("valid ChatPushCapability");
HashMap::from([(id(PUSH_ACCOUNT), cap)])
}
fn extra_with(chat_push: Value) -> Map<String, Value> {
match json!({ "chatPush": chat_push }) {
Value::Object(m) => m,
_ => unreachable!(),
}
}
fn assert_invalid(extra: &Map<String, Value>, property: &str) {
let err = validate_chat_push(extra, &caps()).expect_err("config must be rejected");
assert_eq!(err.error_type, SetErrorType::InvalidProperties);
assert_eq!(err.properties, Some(vec![property.to_owned()]));
}
#[test]
fn valid_config_is_accepted() {
let extra = extra_with(json!({
PUSH_ACCOUNT: {
"kinds": ["direct", "group", "channel"],
"properties": ["messageId", "chatId", "hasMention"],
"urgency": "normal",
"mentionUrgency": "high",
}
}));
validate_chat_push(&extra, &caps()).expect("valid config is accepted");
}
#[test]
fn chat_push_for_account_without_capability_is_rejected() {
let extra = extra_with(json!({ PLAIN_ACCOUNT: { "urgency": "normal" } }));
let err = validate_chat_push(&extra, &caps()).expect_err("entry must be rejected");
assert_eq!(err.error_type, SetErrorType::InvalidProperties);
let props = err.properties.expect("offending property is named");
assert!(
props.iter().any(|p| p.starts_with("chatPush")),
"properties {props:?} do not name chatPush"
);
}
#[test]
fn urgency_outside_supported_values_is_rejected() {
for bad in ["low", "urgent"] {
let extra = extra_with(json!({ PUSH_ACCOUNT: { "urgency": bad } }));
assert_invalid(&extra, &format!("chatPush/{PUSH_ACCOUNT}/urgency"));
}
}
#[test]
fn mention_urgency_outside_supported_values_is_rejected() {
for bad in ["very-low", "urgent"] {
let extra = extra_with(json!({
PUSH_ACCOUNT: { "urgency": "normal", "mentionUrgency": bad }
}));
assert_invalid(&extra, &format!("chatPush/{PUSH_ACCOUNT}/mentionUrgency"));
}
}
#[test]
fn unknown_kind_is_rejected() {
let extra = extra_with(json!({ PUSH_ACCOUNT: { "kinds": ["direct", "forum"] } }));
assert_invalid(&extra, &format!("chatPush/{PUSH_ACCOUNT}/kinds"));
}
#[test]
fn unknown_entry_property_is_rejected() {
let extra = extra_with(json!({
PUSH_ACCOUNT: { "properties": ["messageId", "notAField"] }
}));
assert_invalid(&extra, &format!("chatPush/{PUSH_ACCOUNT}/properties"));
}
#[test]
fn strip_removes_entries_for_accounts_without_capability() {
let mut extra = extra_with(json!({
PUSH_ACCOUNT: { "urgency": "high" },
PLAIN_ACCOUNT: { "urgency": "normal" },
}));
strip_unsupported_chat_push(&mut extra, &caps());
assert_eq!(
Value::Object(extra),
json!({ "chatPush": { PUSH_ACCOUNT: { "urgency": "high" } } })
);
}
#[test]
fn state_change_drops_message_for_chat_push_account() {
let sub: PushSubscription = serde_json::from_value(json!({
"deviceClientId": "device-1",
"url": "https://push.example.com/endpoint/1",
"types": null,
"chatPush": { PUSH_ACCOUNT: {} },
}))
.expect("valid PushSubscription");
let changed = BTreeMap::from([
(
id(PUSH_ACCOUNT),
BTreeMap::from([
("Message".to_owned(), state("m1")),
("Chat".to_owned(), state("c1")),
]),
),
(
id(PLAIN_ACCOUNT),
BTreeMap::from([("Message".to_owned(), state("m2"))]),
),
]);
let carried = HashSet::from([id(PUSH_ACCOUNT)]);
let sc = chat_state_change_for(&sub, &changed, &carried).expect("other types still change");
let expected = BTreeMap::from([
(
id(PUSH_ACCOUNT),
BTreeMap::from([("Chat".to_owned(), state("c1"))]),
),
(
id(PLAIN_ACCOUNT),
BTreeMap::from([("Message".to_owned(), state("m2"))]),
),
]);
assert_eq!(sc.changed, expected);
}
#[test]
fn state_change_is_none_when_only_message_changed_in_chat_push_account() {
let sub: PushSubscription = serde_json::from_value(json!({
"deviceClientId": "device-1",
"url": "https://push.example.com/endpoint/1",
"types": null,
"chatPush": { PUSH_ACCOUNT: {} },
}))
.expect("valid PushSubscription");
let changed = BTreeMap::from([(
id(PUSH_ACCOUNT),
BTreeMap::from([("Message".to_owned(), state("m1"))]),
)]);
let carried = HashSet::from([id(PUSH_ACCOUNT)]);
assert!(chat_state_change_for(&sub, &changed, &carried).is_none());
}
#[test]
fn state_change_keeps_uncarried_message_for_chat_push_account() {
let sub: PushSubscription = serde_json::from_value(json!({
"deviceClientId": "device-1",
"url": "https://push.example.com/endpoint/1",
"types": null,
"chatPush": { PUSH_ACCOUNT: {} },
}))
.expect("valid PushSubscription");
let changed = BTreeMap::from([(
id(PUSH_ACCOUNT),
BTreeMap::from([("Message".to_owned(), state("m1"))]),
)]);
let sc = chat_state_change_for(&sub, &changed, &HashSet::new())
.expect("uncarried Message change is announced");
assert_eq!(sc.changed, changed);
}