jmap-chat-server 0.6.1

JMAP Chat extension method handlers — backend-agnostic, plugs into jmap-server Dispatcher
Documentation
//! RFC 8620 §5.3 `maxObjectsInSet` enforcement across the 9 chat-server
//! /set handlers (bd:JMAP-ayoz.41.3).
//!
//! Every test asserts the wire-format method error `requestTooLarge`
//! (description `"maxObjectsInSet"`) against a hand-built independent
//! oracle — the helper under test is never the oracle. Oversize batches use 501 entries against the
//! default `JmapAccounts::max_objects_in_set` cap of 500.

#![allow(async_fn_in_trait)]

mod common;

use common::MemoryBackend;
use jmap_chat_server::message::handle_message_set;
use jmap_chat_server::{
    handle_ban_set, handle_chat_set, handle_contact_set, handle_emoji_set, handle_invite_set,
    handle_position_set, handle_presence_set, handle_space_set,
};
use jmap_types::Id;
use serde_json::{Value, json};

const DEFAULT_CAP: usize = 500;

fn create_map_of_size(n: usize) -> Value {
    let mut map = serde_json::Map::with_capacity(n);
    for i in 0..n {
        map.insert(format!("c{i}"), json!({}));
    }
    Value::Object(map)
}

fn destroy_array_of_size(n: usize) -> Value {
    Value::Array((0..n).map(|i| json!(format!("id-{i}"))).collect())
}

/// Independent oracle for the method error `requestTooLarge` with
/// description `"maxObjectsInSet"`.
fn expected_limit_error_shape() -> (&'static str, &'static str) {
    ("requestTooLarge", "maxObjectsInSet")
}

fn one_account_backend() -> (MemoryBackend<()>, Id) {
    let backend = MemoryBackend::<()>::default();
    let account_id = Id::from("account1");
    backend.register_account(&account_id);
    (backend, account_id)
}

/// The `ids` form calls a `/set` handler that takes `created_ids`, with
/// an empty map. The `using` form calls one that takes the request's
/// `using` list, with an empty list.
macro_rules! over_limit_test {
    (ids using $name:ident, $handler:ident, $shape:expr) => {
        over_limit_test!(@ $name, $handler, $shape, [&std::collections::BTreeMap::new(),], [, &[]]);
    };
    (ids $name:ident, $handler:ident, $shape:expr) => {
        over_limit_test!(@ $name, $handler, $shape, [&std::collections::BTreeMap::new(),], []);
    };
    (using $name:ident, $handler:ident, $shape:expr) => {
        over_limit_test!(@ $name, $handler, $shape, [], [, &[]]);
    };
    ($name:ident, $handler:ident, $shape:expr) => {
        over_limit_test!(@ $name, $handler, $shape, [], []);
    };
    (@ $name:ident, $handler:ident, $shape:expr, [$($ids:tt)*], [$($using:tt)*]) => {
        #[tokio::test]
        async fn $name() {
            let (backend, account_id) = one_account_backend();
            let mut args = serde_json::Map::new();
            args.insert("accountId".into(), json!(account_id.as_ref()));
            for (k, v) in $shape {
                args.insert(k.into(), v);
            }
            let err = $handler(&backend, &(), $($ids)* Value::Object(args) $($using)*)
                .await
                .expect_err(concat!(
                    "501-entry batch must trip maxObjectsInSet cap (",
                    stringify!($handler),
                    ")"
                ));
            let (etype, edesc) = expected_limit_error_shape();
            assert_eq!(err.error_type.to_string(), etype);
            assert_eq!(err.description.as_deref(), Some(edesc));
        }
    };
}

over_limit_test!(
    ids using chat_set_over_limit,
    handle_chat_set,
    [("create", create_map_of_size(DEFAULT_CAP + 1))]
);

over_limit_test!(
    ids using message_set_over_limit,
    handle_message_set,
    [("create", create_map_of_size(DEFAULT_CAP + 1))]
);

over_limit_test!(
    presence_set_over_limit,
    handle_presence_set,
    [("create", create_map_of_size(DEFAULT_CAP + 1))]
);

over_limit_test!(
    ids position_set_over_limit,
    handle_position_set,
    [("create", create_map_of_size(DEFAULT_CAP + 1))]
);

over_limit_test!(
    contact_set_over_limit,
    handle_contact_set,
    [("create", create_map_of_size(DEFAULT_CAP + 1))]
);

over_limit_test!(
    ids using space_set_over_limit,
    handle_space_set,
    [("create", create_map_of_size(DEFAULT_CAP + 1))]
);

over_limit_test!(
    ids emoji_set_over_limit,
    handle_emoji_set,
    [("create", create_map_of_size(DEFAULT_CAP + 1))]
);

// Invite/set uses destroy (not create — invites are typically destroyed
// after consumption rather than re-created in bulk). Cap counts apply
// uniformly across create/update/destroy.
over_limit_test!(
    ids invite_set_over_limit,
    handle_invite_set,
    [("destroy", destroy_array_of_size(DEFAULT_CAP + 1))]
);

over_limit_test!(
    ids ban_set_over_limit,
    handle_ban_set,
    [("destroy", destroy_array_of_size(DEFAULT_CAP + 1))]
);