mod common;
use common::{ACCOUNT_ID, InjectableBackend};
use jmap_chat_server::message::handle_message_set;
use jmap_chat_server::{ChatBackend, handle_chat_set, handle_position_get, handle_position_set};
use jmap_server::HandlerOutput;
use serde_json::{Value, json};
async fn create_group<B: ChatBackend<CallerCtx = ()>>(backend: &B) -> String {
let HandlerOutput { primary: resp, .. } = handle_chat_set(
backend,
&(),
&std::collections::BTreeMap::new(),
json!({
"accountId": ACCOUNT_ID,
"create": { "g": { "kind": "group", "name": "G", "memberIds": ["contact-bob"] } }
}),
&[],
)
.await
.expect("Chat/set");
resp["created"]["g"]["id"]
.as_str()
.expect("created group id")
.to_owned()
}
async fn send<B: ChatBackend<CallerCtx = ()>>(backend: &B, chat_id: &str, key: &str) {
let HandlerOutput { primary: resp, .. } = handle_message_set(
backend,
&(),
&std::collections::BTreeMap::new(),
json!({
"accountId": ACCOUNT_ID,
"create": { key: { "chatId": chat_id, "body": "hi", "sentAt": "2024-01-01T00:00:00Z" } }
}),
&[],
)
.await
.expect("Message/set");
assert!(resp["created"][key].is_object(), "{resp}");
}
async fn positions<B: ChatBackend<CallerCtx = ()>>(backend: &B) -> Vec<Value> {
let HandlerOutput { primary: resp, .. } =
handle_position_get(backend, &(), json!({ "accountId": ACCOUNT_ID }))
.await
.expect("ReadPosition/get");
resp["list"].as_array().expect("list").clone()
}
#[tokio::test]
async fn first_message_in_group_creates_one_read_position() {
let backend = common::account_backend();
let chat_id = create_group(&backend).await;
assert!(positions(&backend).await.is_empty());
send(&backend, &chat_id, "m1").await;
let list = positions(&backend).await;
assert_eq!(list.len(), 1, "{list:?}");
assert_eq!(list[0]["chatId"], chat_id.as_str());
assert!(list[0].get("lastReadMessageId").is_none(), "{list:?}");
send(&backend, &chat_id, "m2").await;
assert_eq!(positions(&backend).await.len(), 1);
}
#[tokio::test]
async fn read_position_create_failure_does_not_fail_message_set() {
let backend = InjectableBackend::new();
let chat_id = create_group(&backend).await;
backend.inject("ReadPosition", "create");
send(&backend, &chat_id, "m1").await;
assert!(positions(&backend).await.is_empty());
send(&backend, &chat_id, "m2").await;
let list = positions(&backend).await;
assert_eq!(list.len(), 1, "{list:?}");
assert_eq!(list[0]["chatId"], chat_id.as_str());
}
#[tokio::test]
async fn read_position_set_create_and_destroy_forbidden() {
let backend = common::account_backend();
let rp_id = "rp1".to_owned();
backend.insert_object_for_test(
"ReadPosition",
ACCOUNT_ID,
&rp_id,
json!({ "id": rp_id, "chatId": "c1" }),
);
let HandlerOutput { primary: resp, .. } = handle_position_set(
&backend,
&(),
&std::collections::BTreeMap::new(),
json!({
"accountId": ACCOUNT_ID,
"create": { "rp0": { "chatId": "c-other" } },
"destroy": [rp_id]
}),
)
.await
.expect("ReadPosition/set");
assert_eq!(resp["notCreated"]["rp0"]["type"], "forbidden", "{resp}");
assert_eq!(resp["notDestroyed"][&rp_id]["type"], "forbidden", "{resp}");
let list = positions(&backend).await;
assert_eq!(list.len(), 1, "{list:?}");
assert_eq!(list[0]["id"], rp_id.as_str());
}