mod common;
use common::MemoryBackend;
use jmap_server::HandlerOutput;
use jmap_tasks_server::{handle_task_get, handle_task_set};
use jmap_types::Id;
use serde_json::{Value, json};
fn backend_with_participant() -> MemoryBackend<()> {
let backend = MemoryBackend::<()>::default().with_account(&Id::from("acc1"));
backend.seed_object(
&Id::from("acc1"),
"Task",
&Id::from("t1"),
json!({
"id": "t1",
"@type": "Task",
"uid": "t1-uid",
"title": "Review",
"participants": {
"p1": {
"@type": "Participant",
"name": "Alice",
"participationStatus": "needs-action"
}
}
}),
);
backend
}
async fn set_update(backend: &MemoryBackend<()>, patch: Value) -> Value {
let args = json!({ "accountId": "acc1", "update": { "t1": patch } });
handle_task_set(backend, &(), &std::collections::BTreeMap::new(), args, &[])
.await
.expect("/set must succeed at the top level")
.primary
}
async fn get_t1(backend: &MemoryBackend<()>) -> Value {
let args = json!({ "accountId": "acc1", "ids": ["t1"] });
let HandlerOutput { primary: resp, .. } = handle_task_get(backend, &(), args, &[])
.await
.expect("/get must succeed");
resp["list"][0].clone()
}
#[tokio::test]
async fn path_key_updates_nested_member() {
let backend = backend_with_participant();
let resp = set_update(
&backend,
json!({ "participants/p1/participationStatus": "accepted" }),
)
.await;
assert!(resp["updated"].get("t1").is_some(), "t1 updated: {resp}");
let task = get_t1(&backend).await;
assert_eq!(
task["participants"]["p1"],
json!({
"@type": "Participant",
"name": "Alice",
"participationStatus": "accepted"
}),
"nested member updated, siblings kept: {task}"
);
assert!(
task.get("participants/p1/participationStatus").is_none(),
"no literal path-named property: {task}"
);
}
#[tokio::test]
async fn object_value_replaces_property_wholesale() {
let backend = backend_with_participant();
let replacement = json!({
"p1": { "@type": "Participant", "participationStatus": "declined" }
});
let resp = set_update(&backend, json!({ "participants": replacement.clone() })).await;
assert!(resp["updated"].get("t1").is_some(), "t1 updated: {resp}");
let task = get_t1(&backend).await;
assert_eq!(
task["participants"], replacement,
"participants replaced, not merged (name must be gone): {task}"
);
}
#[tokio::test]
async fn path_through_absent_parent_is_invalid_patch() {
let backend = backend_with_participant();
let before = get_t1(&backend).await;
let resp = set_update(
&backend,
json!({ "title": "Changed", "participants/p9/participationStatus": "accepted" }),
)
.await;
assert_eq!(resp["notUpdated"]["t1"]["type"], "invalidPatch", "{resp}");
assert_eq!(get_t1(&backend).await, before, "stored task unchanged");
}
#[tokio::test]
async fn prefix_conflict_is_invalid_patch() {
let backend = backend_with_participant();
let before = get_t1(&backend).await;
let resp = set_update(
&backend,
json!({
"participants": {},
"participants/p1/participationStatus": "accepted"
}),
)
.await;
assert_eq!(resp["notUpdated"]["t1"]["type"], "invalidPatch", "{resp}");
assert_eq!(get_t1(&backend).await, before, "stored task unchanged");
}
#[tokio::test]
async fn per_user_only_patch_keeps_base_updated() {
let backend = MemoryBackend::<()>::default().with_account(&Id::from("acc1"));
backend.seed_object(
&Id::from("acc1"),
"Task",
&Id::from("t1"),
json!({
"id": "t1",
"@type": "Task",
"uid": "t1-uid",
"title": "Review",
"updated": "2026-01-01T00:00:00Z"
}),
);
let resp = set_update(&backend, json!({ "keywords": { "urgent": true } })).await;
assert!(resp["updated"].get("t1").is_some(), "t1 updated: {resp}");
let task = get_t1(&backend).await;
assert_eq!(task["keywords"], json!({ "urgent": true }), "{task}");
assert_eq!(task["updated"], "2026-01-01T00:00:00Z", "{task}");
}