mod common;
use common::{
ACCOUNT_ID, INJECTABLE_BACKEND_CANARY, IdentityBackend, InjectableBackend, SPACE_ID, seed_space,
};
use jmap_chat_server::{handle_invite_changes, handle_invite_set};
use jmap_server::HandlerOutput;
use jmap_types::Id;
use serde_json::{Value, json};
use std::collections::BTreeMap;
const INVITE_ID: &str = "inv1";
fn seeded() -> IdentityBackend {
let backend = IdentityBackend::new();
seed_space(
&backend,
json!([{ "id": "r-mm", "name": "Mod", "permissions": ["manage_members"], "position": 10 }]),
json!([{ "id": ACCOUNT_ID, "roleIds": ["r-mm"], "joinedAt": "2026-01-01T00:00:00Z" }]),
);
backend.inner().insert_object_for_test(
"SpaceInvite",
ACCOUNT_ID,
INVITE_ID,
json!({
"id": INVITE_ID,
"code": "code1",
"spaceId": SPACE_ID,
"createdBy": ACCOUNT_ID,
"uses": 0,
"createdAt": "2026-01-01T00:00:00Z",
}),
);
backend
}
fn with_account(body: Value) -> Value {
let mut args = json!({ "accountId": ACCOUNT_ID });
let (Value::Object(args_map), Value::Object(body_map)) = (&mut args, body) else {
unreachable!("both are object literals");
};
args_map.extend(body_map);
args
}
async fn set(backend: &IdentityBackend, body: Value) -> Result<Value, jmap_types::JmapError> {
handle_invite_set(
backend,
&Id::from(ACCOUNT_ID),
&BTreeMap::new(),
with_account(body),
)
.await
.map(|HandlerOutput { primary, .. }| primary)
}
async fn set_injected(backend: &InjectableBackend, body: Value) -> Value {
let HandlerOutput { primary, .. } =
handle_invite_set(backend, &(), &BTreeMap::new(), with_account(body))
.await
.expect("SpaceInvite/set");
primary
}
#[tokio::test]
async fn changes_without_since_state_is_invalid_arguments() {
let err = handle_invite_changes(&seeded(), &Id::from(ACCOUNT_ID), with_account(json!({})))
.await
.expect_err("missing sinceState");
assert_eq!(err.error_type.to_string(), "invalidArguments", "{err:?}");
}
#[tokio::test]
async fn set_with_stale_if_in_state_is_state_mismatch() {
let err = set(
&seeded(),
json!({ "ifInState": "not-the-state", "destroy": [INVITE_ID] }),
)
.await
.expect_err("stale ifInState");
assert_eq!(err.error_type.to_string(), "stateMismatch", "{err:?}");
}
#[tokio::test]
async fn create_with_unknown_creation_reference_is_invalid_properties() {
let resp = set(
&seeded(),
json!({ "create": { "i0": { "spaceId": "#nope" } } }),
)
.await
.expect("SpaceInvite/set");
assert_eq!(resp["created"], Value::Null, "{resp:?}");
assert_eq!(
resp["notCreated"]["i0"]["type"], "invalidProperties",
"{resp:?}"
);
assert_eq!(
resp["notCreated"]["i0"]["properties"],
json!(["spaceId"]),
"{resp:?}"
);
}
#[tokio::test]
async fn create_supplying_code_is_invalid_properties() {
let resp = set(
&seeded(),
json!({ "create": { "i0": { "spaceId": SPACE_ID, "code": "mine" } } }),
)
.await
.expect("SpaceInvite/set");
assert_eq!(resp["created"], Value::Null, "{resp:?}");
assert_eq!(
resp["notCreated"]["i0"]["type"], "invalidProperties",
"{resp:?}"
);
assert_eq!(
resp["notCreated"]["i0"]["properties"],
json!(["code"]),
"{resp:?}"
);
}
#[tokio::test]
async fn create_stores_default_channel_and_expiry() {
let resp = set(
&seeded(),
json!({ "create": { "i0": {
"spaceId": SPACE_ID,
"defaultChannelId": "ch1",
"expiresAt": "2030-01-01T00:00:00Z",
} } }),
)
.await
.expect("SpaceInvite/set");
assert_eq!(resp["notCreated"], Value::Null, "{resp:?}");
let created = &resp["created"]["i0"];
assert_eq!(created["defaultChannelId"], "ch1", "{resp:?}");
assert_eq!(created["expiresAt"], "2030-01-01T00:00:00Z", "{resp:?}");
}
#[tokio::test]
async fn destroy_with_non_string_element_is_invalid_arguments() {
let err = set(&seeded(), json!({ "destroy": [42] }))
.await
.expect_err("non-string destroy element");
assert_eq!(err.error_type.to_string(), "invalidArguments", "{err:?}");
}
#[tokio::test]
async fn destroy_with_malformed_id_is_invalid_properties() {
let resp = set(&seeded(), json!({ "destroy": ["bad id!"] }))
.await
.expect("SpaceInvite/set");
assert_eq!(resp["destroyed"], Value::Null, "{resp:?}");
assert_eq!(
resp["notDestroyed"]["bad id!"]["type"], "invalidProperties",
"{resp:?}"
);
}
#[tokio::test]
async fn destroy_of_unknown_invite_is_not_found() {
let resp = set(&seeded(), json!({ "destroy": ["missing"] }))
.await
.expect("SpaceInvite/set");
assert_eq!(resp["destroyed"], Value::Null, "{resp:?}");
assert_eq!(
resp["notDestroyed"]["missing"]["type"], "notFound",
"{resp:?}"
);
}
#[tokio::test]
async fn backend_faults_fail_the_target_with_server_fail() {
let cases: [(&str, &str, Value, &str, &str); 4] = [
(
"Space",
"get",
json!({ "create": { "i0": { "spaceId": SPACE_ID } } }),
"notCreated",
"i0",
),
(
"SpaceInvite",
"create",
json!({ "create": { "i0": { "spaceId": SPACE_ID } } }),
"notCreated",
"i0",
),
(
"SpaceInvite",
"get",
json!({ "destroy": [INVITE_ID] }),
"notDestroyed",
INVITE_ID,
),
(
"SpaceInvite",
"destroy",
json!({ "destroy": [INVITE_ID] }),
"notDestroyed",
INVITE_ID,
),
];
for (type_name, op, body, map, key) in cases {
let backend = InjectableBackend::new();
backend.inner.insert_object_for_test(
"SpaceInvite",
ACCOUNT_ID,
INVITE_ID,
json!({
"id": INVITE_ID,
"code": "code1",
"spaceId": SPACE_ID,
"createdBy": ACCOUNT_ID,
"uses": 0,
"createdAt": "2026-01-01T00:00:00Z",
}),
);
backend.inject(type_name, op);
let resp = set_injected(&backend, body).await;
assert_eq!(
resp[map][key]["type"], "serverFail",
"{type_name}/{op}: {resp:?}"
);
assert!(
!resp.to_string().contains(INJECTABLE_BACKEND_CANARY),
"{type_name}/{op} leaked backend text: {resp:?}"
);
}
}