mod common;
use std::collections::BTreeMap;
use common::{ACCOUNT_ID, INJECTABLE_BACKEND_CANARY, InjectableBackend, account_backend};
use jmap_chat_server::memory::MemoryBackend;
use jmap_chat_server::{
ChatBackend, handle_position_changes, handle_position_get, handle_position_set,
};
use jmap_server::HandlerOutput;
use jmap_types::JmapError;
use serde_json::{Value, json};
const RP_ID: &str = "rp1";
fn seeded() -> MemoryBackend<()> {
let backend = account_backend();
seed(&backend);
backend
}
fn seed(backend: &MemoryBackend<()>) {
backend.insert_object_for_test(
"ReadPosition",
ACCOUNT_ID,
RP_ID,
json!({ "id": RP_ID, "chatId": "c1" }),
);
}
async fn set<B: ChatBackend<CallerCtx = ()>>(backend: &B, body: Value) -> Result<Value, JmapError> {
let mut args = body;
args["accountId"] = json!(ACCOUNT_ID);
handle_position_set(backend, &(), &BTreeMap::new(), args)
.await
.map(|HandlerOutput { primary, .. }| primary)
}
async fn stored<B: ChatBackend<CallerCtx = ()>>(backend: &B) -> Value {
let HandlerOutput { primary, .. } =
handle_position_get(backend, &(), json!({ "accountId": ACCOUNT_ID }))
.await
.expect("ReadPosition/get");
primary["list"][0].clone()
}
fn assert_error_type(result: Result<Value, JmapError>, expected: &str) {
let err = result.expect_err(expected);
assert_eq!(err.error_type.to_string(), expected, "{err:?}");
}
#[tokio::test]
async fn changes_without_since_state_is_invalid_arguments() {
let result = handle_position_changes(&seeded(), &(), json!({ "accountId": ACCOUNT_ID }))
.await
.map(|HandlerOutput { primary, .. }| primary);
assert_error_type(result, "invalidArguments");
}
#[tokio::test]
async fn set_with_stale_if_in_state_is_state_mismatch() {
let backend = seeded();
let result = set(
&backend,
json!({
"ifInState": "not-the-current-state",
"update": { RP_ID: { "lastReadMessageId": "m1" } }
}),
)
.await;
assert_error_type(result, "stateMismatch");
assert!(stored(&backend).await.get("lastReadMessageId").is_none());
}
#[tokio::test]
async fn update_with_malformed_id_is_invalid_properties() {
let resp = set(
&seeded(),
json!({ "update": { "bad id": { "lastReadMessageId": "m1" } } }),
)
.await
.expect("ReadPosition/set");
assert_eq!(resp["updated"], Value::Null, "{resp}");
assert_eq!(
resp["notUpdated"]["bad id"]["type"], "invalidProperties",
"{resp}"
);
assert_eq!(resp["notUpdated"]["bad id"]["properties"], json!(["id"]));
}
#[tokio::test]
async fn update_with_unknown_creation_ref_is_invalid_properties() {
let backend = seeded();
let resp = set(
&backend,
json!({ "update": { RP_ID: { "lastReadMessageId": "#nope" } } }),
)
.await
.expect("ReadPosition/set");
assert_eq!(resp["updated"], Value::Null, "{resp}");
assert_eq!(
resp["notUpdated"][RP_ID]["type"], "invalidProperties",
"{resp}"
);
assert_eq!(
resp["notUpdated"][RP_ID]["properties"],
json!(["lastReadMessageId"])
);
assert!(stored(&backend).await.get("lastReadMessageId").is_none());
}
#[tokio::test]
async fn update_with_non_object_patch_is_invalid_patch() {
let resp = set(&seeded(), json!({ "update": { RP_ID: "m1" } }))
.await
.expect("ReadPosition/set");
assert_eq!(resp["updated"], Value::Null, "{resp}");
assert_eq!(resp["notUpdated"][RP_ID]["type"], "invalidPatch", "{resp}");
}
#[tokio::test]
async fn update_backend_failure_is_server_fail_without_backend_text() {
let backend = InjectableBackend::new();
seed(&backend.inner);
backend.inject("ReadPosition", "update");
let resp = set(
&backend,
json!({ "update": { RP_ID: { "lastReadMessageId": "m1" } } }),
)
.await
.expect("ReadPosition/set");
assert_eq!(resp["updated"], Value::Null, "{resp}");
assert_eq!(resp["notUpdated"][RP_ID]["type"], "serverFail", "{resp}");
assert!(
!resp.to_string().contains(INJECTABLE_BACKEND_CANARY),
"leaked backend text: {resp}"
);
assert!(stored(&backend).await.get("lastReadMessageId").is_none());
}
#[tokio::test]
async fn destroy_with_non_string_element_is_invalid_arguments() {
assert_error_type(
set(&seeded(), json!({ "destroy": [RP_ID, 7] })).await,
"invalidArguments",
);
}
#[tokio::test]
async fn update_with_identical_read_only_values_is_updated() {
let backend = seeded();
let resp = set(
&backend,
json!({ "update": { RP_ID: { "id": RP_ID, "chatId": "c1", "lastReadMessageId": "m1" } } }),
)
.await
.expect("ReadPosition/set");
assert_eq!(resp["notUpdated"], Value::Null, "{resp}");
assert!(resp["updated"].get(RP_ID).is_some(), "{resp}");
assert_eq!(stored(&backend).await["lastReadMessageId"], json!("m1"));
}