jmap-chat-server 0.6.1

JMAP Chat extension method handlers — backend-agnostic, plugs into jmap-server Dispatcher
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
//! draft-ietf-jmap-metadata-05 on Chat, Message and Space in the
//! in-memory backend opted in with `MemoryBackend::with_metadata_info`
//! (bd JMAP-s4kp.2). draft-atwood-jmap-chat-00 (lines 667, 780, 911):
//! "Implementations MAY include `Chat` [`Message`, `Space`] in that map".
//!
//! Oracle: the -05 text quoted on each test
//! (/home/mark/PROJECT/IETF/specs/draft-ietf-jmap-metadata-05.txt, the
//! revision of October 2026), with hand-written requests and expected
//! responses. The capability below allows vendor namespaces only, no
//! private metadata, and `maxDepth` 2.

mod common;

use std::collections::BTreeMap;

use common::{ACCOUNT_ID, MemoryBackend, seed_group_chat};
use jmap_chat_server::message::{
    handle_message_get, handle_message_query, handle_message_query_changes, handle_message_set,
};
use jmap_chat_server::{
    handle_chat_changes, handle_chat_get, handle_chat_query, handle_chat_query_changes,
    handle_chat_set, handle_space_get, handle_space_query, handle_space_query_changes,
    handle_space_set,
};
use jmap_metadata_types::DataTypeMetadataInfo;
use jmap_server::HandlerOutput;
use jmap_types::{Id, JmapError};
use serde_json::{Value, json};

const NS: &str = "acme.example.com";

fn using() -> Vec<String> {
    vec![
        "urn:ietf:params:jmap:core".to_owned(),
        "urn:ietf:params:jmap:chat".to_owned(),
        "urn:ietf:params:jmap:metadata".to_owned(),
    ]
}

fn using_without_metadata() -> Vec<String> {
    vec![
        "urn:ietf:params:jmap:core".to_owned(),
        "urn:ietf:params:jmap:chat".to_owned(),
    ]
}

fn info() -> DataTypeMetadataInfo {
    serde_json::from_value(json!({
        "namespaces": [],
        "supportsVendorNamespaces": true,
        "supportsPrivate": false,
        "maxDepth": 2
    }))
    .unwrap()
}

fn backend() -> MemoryBackend<()> {
    let backend = MemoryBackend::<()>::default()
        .with_metadata_info("Chat", info())
        .with_metadata_info("Message", info())
        .with_metadata_info("Space", info());
    backend.register_account(&Id::from(ACCOUNT_ID));
    backend
}

fn error_type(err: &JmapError) -> Value {
    serde_json::to_value(err).unwrap()["type"].clone()
}

fn with_account(mut args: Value) -> Value {
    args["accountId"] = json!(ACCOUNT_ID);
    args
}

async fn chat_set(backend: &MemoryBackend<()>, args: Value, using: &[String]) -> Value {
    let HandlerOutput { primary, .. } =
        handle_chat_set(backend, &(), &BTreeMap::new(), with_account(args), using)
            .await
            .expect("Chat/set must not fail at method level");
    primary
}

async fn create_chat(backend: &MemoryBackend<()>, extra: Value) -> String {
    let mut create = json!({ "kind": "group", "name": "G" });
    if let (Some(c), Some(e)) = (create.as_object_mut(), extra.as_object()) {
        c.extend(e.clone());
    }
    let resp = chat_set(backend, json!({ "create": { "c": create } }), &using()).await;
    resp["created"]["c"]["id"]
        .as_str()
        .unwrap_or_else(|| panic!("Chat/set create failed: {resp}"))
        .to_owned()
}

async fn update_chat(backend: &MemoryBackend<()>, id: &str, patch: Value) -> Value {
    let resp = chat_set(backend, json!({ "update": { id: patch } }), &using()).await;
    assert!(resp["updated"].get(id).is_some(), "{resp}");
    resp
}

async fn chat_metadata(backend: &MemoryBackend<()>, id: &str) -> Value {
    let HandlerOutput { primary, .. } =
        handle_chat_get(backend, &(), with_account(json!({ "ids": [id] })), &using())
            .await
            .expect("Chat/get");
    primary["list"][0]["metadata"].clone()
}

async fn chat_query(backend: &MemoryBackend<()>, filter: Value) -> Result<Value, JmapError> {
    handle_chat_query(
        backend,
        &(),
        with_account(json!({ "filter": filter })),
        &using(),
    )
    .await
    .map(|o| o.primary["ids"].clone())
}

async fn chat_changes(backend: &MemoryBackend<()>, args: Value, using: &[String]) -> Value {
    handle_chat_changes(backend, &(), with_account(args), using)
        .await
        .expect("Chat/changes")
        .primary
}

async fn message_set(backend: &MemoryBackend<()>, args: Value) -> Value {
    handle_message_set(backend, &(), &BTreeMap::new(), with_account(args), &using())
        .await
        .expect("Message/set must not fail at method level")
        .primary
}

async fn space_set(backend: &MemoryBackend<()>, args: Value) -> Value {
    handle_space_set(
        backend,
        &(),
        &std::collections::BTreeMap::new(),
        with_account(args),
        &using(),
    )
    .await
    .expect("Space/set must not fail at method level")
    .primary
}

// ---------------------------------------------------------------------------
// /query (-05 §3.6)
// ---------------------------------------------------------------------------

/// -05 §3.6: `metadataExists` "a value MUST be present at this path",
/// `metadataTextContains` matches a substring under `i;unicode-casemap`,
/// `metadataTextEquals` matches exactly; an object without `metadata`
/// has `{}` (-05 §2 "default: {}").
#[tokio::test]
async fn chat_query_metadata_conditions() {
    let backend = backend();
    let tagged = create_chat(
        &backend,
        json!({ "metadata": { NS: { "summary": "Vendor Invoices" } } }),
    )
    .await;
    let plain = create_chat(&backend, json!({})).await;

    assert_eq!(
        chat_query(&backend, json!({ "metadataExists": NS }))
            .await
            .unwrap(),
        json!([tagged])
    );
    assert_eq!(
        chat_query(
            &backend,
            json!({ "metadataTextContains": { "path": "acme.example.com/summary", "value": "invoice" } })
        )
        .await
        .unwrap(),
        json!([tagged])
    );
    assert_eq!(
        chat_query(
            &backend,
            json!({ "operator": "NOT", "conditions": [
                { "metadataTextEquals": { "path": "acme.example.com/summary", "value": "Vendor Invoices" } }
            ] })
        )
        .await
        .unwrap(),
        json!([plain])
    );
}

/// -05 §3.6: "Any privateMetadata* condition ... MUST be rejected with an
/// "unsupportedFilter" error if supportsPrivate is false", and "A path
/// that is not well formed ... MUST be rejected with an
/// "invalidArguments" error" (`~2` is not an RFC 6901 escape). -05
/// §1.2.1: an unlisted type "does not gain the metadata or
/// privateMetadata properties", so its metadata filter is
/// `unsupportedFilter` (RFC 8620 §5.5).
#[tokio::test]
async fn invalid_metadata_conditions_are_rejected() {
    let backend = backend();
    let err = chat_query(&backend, json!({ "privateMetadataExists": NS }))
        .await
        .unwrap_err();
    assert_eq!(error_type(&err), json!("unsupportedFilter"));
    let err = chat_query(
        &backend,
        json!({ "metadataExists": "acme.example.com/a~2" }),
    )
    .await
    .unwrap_err();
    assert_eq!(error_type(&err), json!("invalidArguments"));

    let unlisted = MemoryBackend::<()>::default();
    unlisted.register_account(&Id::from(ACCOUNT_ID));
    let err = chat_query(&unlisted, json!({ "metadataExists": NS }))
        .await
        .unwrap_err();
    assert_eq!(error_type(&err), json!("unsupportedFilter"));
}

// ---------------------------------------------------------------------------
// /set (-05 §3, §3.2)
// ---------------------------------------------------------------------------

/// -05 §2 `metadata` "default: {}", so a `metadata/<namespace>` patch
/// finds its parent; "Setting a patch value to null removes the targeted
/// key" keeps the other namespaces; and -05 §3.2 "In an update, null is
/// equivalent to {}".
#[tokio::test]
async fn chat_update_patches_metadata() {
    let backend = backend();
    let id = create_chat(&backend, json!({})).await;
    update_chat(
        &backend,
        &id,
        json!({ "metadata/acme.example.com": { "k": 1 }, "metadata/other.example.com": {} }),
    )
    .await;
    update_chat(&backend, &id, json!({ "metadata/other.example.com": null })).await;
    assert_eq!(
        chat_metadata(&backend, &id).await,
        json!({ NS: { "k": 1 } })
    );
    update_chat(&backend, &id, json!({ "metadata": null })).await;
    assert_eq!(chat_metadata(&backend, &id).await, json!({}));
}

/// - -05 §3: without the capability, "Foo/set MUST reject any write to
///   them with an "invalidProperties" SetError".
/// - -05 §3.2: on create, "A value of null for either property MUST be
///   rejected with an "invalidProperties" SetError".
/// - rule 2: "If the path begins with privateMetadata and supportsPrivate
///   is false ..., reject with "invalidProperties"."
/// - rule 3: "If the targeted namespace is not supported on this data
///   type, reject with "invalidProperties"" (`photography` is a
///   registered name this capability does not list).
/// - rule 4: "If a namespace value would exceed maxDepth, reject with
///   "invalidProperties"" (the value below has depth 3; maxDepth is 2).
#[tokio::test]
async fn invalid_metadata_writes_are_invalid_properties() {
    let backend = backend();
    for (create, using) in [
        (
            json!({ "kind": "group", "name": "G", "metadata": { NS: {} } }),
            using_without_metadata(),
        ),
        (
            json!({ "kind": "group", "name": "G", "metadata": null }),
            using(),
        ),
    ] {
        let resp = chat_set(&backend, json!({ "create": { "c": create } }), &using).await;
        assert_eq!(
            resp["notCreated"]["c"]["type"],
            json!("invalidProperties"),
            "{resp}"
        );
        assert_eq!(
            resp["notCreated"]["c"]["properties"],
            json!(["metadata"]),
            "{resp}"
        );
    }

    let id = create_chat(&backend, json!({})).await;
    let cases: [(Value, Vec<String>, &str); 4] = [
        (
            json!({ "metadata/acme.example.com": { "k": 1 } }),
            using_without_metadata(),
            "metadata/acme.example.com",
        ),
        (
            json!({ "privateMetadata/acme.example.com": { "k": 1 } }),
            using(),
            "privateMetadata/acme.example.com",
        ),
        (
            json!({ "metadata/photography": { "k": 1 } }),
            using(),
            "metadata/photography",
        ),
        (
            json!({ "metadata/acme.example.com": { "a": { "b": { "c": 1 } } } }),
            using(),
            "metadata/acme.example.com",
        ),
    ];
    for (patch, using, property) in cases {
        let resp = chat_set(&backend, json!({ "update": { &id: patch } }), &using).await;
        assert_eq!(
            resp["notUpdated"][&id]["type"],
            json!("invalidProperties"),
            "{patch}: {resp}"
        );
        assert_eq!(
            resp["notUpdated"][&id]["properties"],
            json!([property]),
            "{patch}"
        );
    }
    // -05 §2: "When returned, its value MUST be an object ({} when no
    // metadata is set), never null".
    assert_eq!(chat_metadata(&backend, &id).await, json!({}));
}

/// -05 §1.2.1: "A type that does not appear in this object does not gain
/// the metadata or privateMetadata properties in this account".
#[tokio::test]
async fn unlisted_type_rejects_metadata_write() {
    let backend = MemoryBackend::<()>::default();
    backend.register_account(&Id::from(ACCOUNT_ID));
    let resp = chat_set(
        &backend,
        json!({ "create": { "c": { "kind": "group", "name": "G", "metadata": { NS: {} } } } }),
        &using(),
    )
    .await;
    assert_eq!(
        resp["notCreated"]["c"]["type"],
        json!("invalidProperties"),
        "{resp}"
    );
    assert_eq!(resp["notCreated"]["c"]["properties"], json!(["metadata"]));
}

/// -05 §3.2: "Patch errors that can be determined from the request alone
/// (for example, two paths where one is a prefix of the other) MUST be
/// reported with "invalidPatch" before rule 1 is evaluated", and rule 1
/// "If the user cannot read the related object, reject with
/// "notFound"" comes before rules 2 to 4.
#[tokio::test]
async fn request_only_patch_error_precedes_not_found() {
    let backend = backend();
    let resp = chat_set(
        &backend,
        json!({ "update": { "nope": {
            "metadata": { NS: {} },
            "metadata/acme.example.com/k": 1
        } } }),
        &using(),
    )
    .await;
    assert_eq!(
        resp["notUpdated"]["nope"]["type"],
        json!("invalidPatch"),
        "{resp}"
    );

    let resp = chat_set(
        &backend,
        json!({ "update": { "nope": { "metadata/photography": {} } } }),
        &using(),
    )
    .await;
    assert_eq!(
        resp["notUpdated"]["nope"]["type"],
        json!("notFound"),
        "{resp}"
    );
}

// ---------------------------------------------------------------------------
// /changes (-05 §3, §3.3)
// ---------------------------------------------------------------------------

/// -05 §3.3: `updatedProperties` "MUST be set to a list containing only
/// those property names (e.g., ["metadata"] ...)" when only metadata
/// changed; with `ignoreMetadataOnlyChanges` the server "MUST exclude from
/// the updated array any id whose only changes since sinceState ... are
/// confined to metadata", while "The state string still advances
/// normally"; and -05 §3: without the capability "Foo/changes MUST behave
/// as if ignoreMetadataOnlyChanges were true".
#[tokio::test]
async fn metadata_only_chat_change() {
    let backend = backend();
    let id = create_chat(&backend, json!({})).await;
    let resp = update_chat(
        &backend,
        &id,
        json!({ "metadata/acme.example.com": { "k": 1 } }),
    )
    .await;
    let since = resp["oldState"].clone();
    let new_state = resp["newState"].clone();

    let report = chat_changes(&backend, json!({ "sinceState": since }), &using()).await;
    assert_eq!(report["updated"], json!([id]), "{report}");
    assert_eq!(report["updatedProperties"], json!(["metadata"]));
    assert_eq!(report["newState"], new_state);

    for (args, using) in [
        (
            json!({ "sinceState": since, "ignoreMetadataOnlyChanges": true }),
            using(),
        ),
        (json!({ "sinceState": since }), using_without_metadata()),
    ] {
        let ignored = chat_changes(&backend, args, &using).await;
        assert_eq!(ignored["updated"], json!([]), "{ignored}");
        assert_eq!(ignored["updatedProperties"], Value::Null);
        assert_eq!(ignored["newState"], new_state);
    }
}

/// -05 §3.3: "If any other property ... of any id in the array may also
/// have changed ... this argument MUST be null", and "Ids with any other
/// change remain in updated".
#[tokio::test]
async fn mixed_change_is_reported_without_updated_properties() {
    let backend = backend();
    let id = create_chat(&backend, json!({})).await;
    let resp = update_chat(
        &backend,
        &id,
        json!({ "metadata/acme.example.com": { "k": 1 } }),
    )
    .await;
    let since = resp["oldState"].clone();
    update_chat(&backend, &id, json!({ "muted": true })).await;

    for ignore in [false, true] {
        let resp = chat_changes(
            &backend,
            json!({ "sinceState": since, "ignoreMetadataOnlyChanges": ignore }),
            &using(),
        )
        .await;
        assert_eq!(resp["updated"], json!([id]), "{resp}");
        assert_eq!(resp["updatedProperties"], Value::Null, "{resp}");
    }
}

/// -05 §3.3: "Excluded ids do not count toward maxChanges." Two changes
/// fall in the window, one metadata-only; with `maxChanges` 1 the
/// request succeeds only if the excluded one is not counted.
#[tokio::test]
async fn excluded_ids_do_not_count_toward_max_changes() {
    let backend = backend();
    let quiet = create_chat(&backend, json!({})).await;
    let loud = create_chat(&backend, json!({})).await;
    let resp = update_chat(
        &backend,
        &quiet,
        json!({ "metadata/acme.example.com": { "k": 1 } }),
    )
    .await;
    let since = resp["oldState"].clone();
    update_chat(&backend, &loud, json!({ "muted": true })).await;

    let resp = chat_changes(
        &backend,
        json!({ "sinceState": since, "maxChanges": 1, "ignoreMetadataOnlyChanges": true }),
        &using(),
    )
    .await;
    assert_eq!(resp["updated"], json!([loud]), "{resp}");
}

// ---------------------------------------------------------------------------
// Message and Space
// ---------------------------------------------------------------------------

/// draft-atwood-jmap-chat-00 §Message/set lists the only client updates;
/// -05 §3.2 adds `metadata` written by patch path, and §3.6 the
/// conditions, here inside a §Message/query `chatId` filter.
#[tokio::test]
async fn message_metadata_update_and_query() {
    let backend = backend();
    seed_group_chat(&backend, "g1");
    let resp = message_set(
        &backend,
        json!({ "create": {
            "m1": { "chatId": "g1", "body": "a", "bodyType": "text/plain", "sentAt": "2026-01-01T00:00:00Z" },
            "m2": { "chatId": "g1", "body": "b", "bodyType": "text/plain", "sentAt": "2026-01-01T00:00:01Z" }
        } }),
    )
    .await;
    let m1 = resp["created"]["m1"]["id"]
        .as_str()
        .unwrap_or_else(|| panic!("{resp}"))
        .to_owned();

    let resp = message_set(
        &backend,
        json!({ "update": { &m1: { "metadata/acme.example.com": { "moderationNote": "ok" } } } }),
    )
    .await;
    assert!(resp["updated"].get(&m1).is_some(), "{resp}");

    let ids = handle_message_query(
        &backend,
        &(),
        with_account(json!({ "filter": { "chatId": "g1", "metadataExists": "acme.example.com/moderationNote" } })), &using())
    .await
    .expect("Message/query")
    .primary["ids"]
        .clone();
    assert_eq!(ids, json!([m1]));
}

/// -05 §3.2 on Space: a create stores the `metadata` it carries, and an
/// update is an RFC 8620 §5.3 PatchObject ("metadata/<namespace>/..."
/// replaces only the targeted key), alongside a top-level field.
#[tokio::test]
async fn space_metadata_create_update_and_query() {
    let backend = backend();
    let resp = space_set(
        &backend,
        json!({ "create": { "s": { "name": "S", "metadata": { NS: { "welcomeMessage": "Hello", "guidelines": "Be kind" } } } } }),
    )
    .await;
    let id = resp["created"]["s"]["id"]
        .as_str()
        .unwrap_or_else(|| panic!("{resp}"))
        .to_owned();

    let resp = space_set(
        &backend,
        json!({ "update": { &id: { "metadata/acme.example.com/welcomeMessage": "Hi", "description": "d" } } }),
    )
    .await;
    assert!(resp["updated"].get(&id).is_some(), "{resp}");

    let got = handle_space_get(
        &backend,
        &(),
        with_account(json!({ "ids": [&id] })),
        &using(),
    )
    .await
    .expect("Space/get")
    .primary;
    assert_eq!(
        got["list"][0]["metadata"],
        json!({ NS: { "welcomeMessage": "Hi", "guidelines": "Be kind" } })
    );
    assert_eq!(got["list"][0]["description"], json!("d"));

    let ids = handle_space_query(
        &backend,
        &(),
        with_account(json!({ "filter": { "metadataTextEquals": { "path": "acme.example.com/welcomeMessage", "value": "Hi" } } })), &using())
    .await
    .expect("Space/query")
    .primary["ids"]
        .clone();
    assert_eq!(ids, json!([id]));
}

/// -05 §2: privateMetadata "MUST" be filtered per user; this backend has
/// no viewer identity, so it refuses `supportsPrivate: true`, and it
/// supports only the types draft-atwood-jmap-chat-00 names.
#[test]
#[should_panic(expected = "supportsPrivate")]
fn private_metadata_capability_is_refused() {
    let mut private = info();
    private.supports_private = true;
    let _ = MemoryBackend::<()>::default().with_metadata_info("Chat", private);
}

#[test]
#[should_panic(expected = "ChatContact")]
fn unnamed_type_is_refused() {
    let _ = MemoryBackend::<()>::default().with_metadata_info("ChatContact", info());
}

// ---------------------------------------------------------------------------
// /get and the /query capability gate (-05 §2, §3, §3.1; bd JMAP-s4kp.9)
// ---------------------------------------------------------------------------

/// A second vendor namespace for the subselector tests.
const NS2: &str = "other.example.org";

/// The `type` of a method-level error.
fn method_error_type(err: &JmapError) -> Value {
    serde_json::to_value(err).unwrap()["type"].clone()
}

/// Run Chat/get with `properties` on `id` and return its one list entry.
async fn chat_get_one(
    b: &MemoryBackend<()>,
    id: &str,
    properties: Value,
    using: &[String],
) -> Result<Value, JmapError> {
    let args = json!({ "accountId": ACCOUNT_ID, "ids": [id], "properties": properties });
    handle_chat_get(b, &(), args, using)
        .await
        .map(|o| o.primary["list"][0].clone())
}

/// -05 §3.1 (L524-526): "when properties is omitted or null, the server
/// MUST include metadata and, where supportsPrivate is true,
/// privateMetadata"; §2 (L296-297): "When returned, its value MUST be an
/// object ({} when no metadata is set), never null"; §2 (L306-307): "When
/// supportsPrivate is false for the data type, this property MUST NOT be
/// returned"; §3 (L510-512): for other requests "metadata and
/// privateMetadata are not default properties of Foo/get (although they
/// are still returned when requested by name)".
#[tokio::test]
async fn chat_get_default_properties_follow_capability() {
    let b = backend();
    let bare = create_chat(&b, json!({})).await;
    let tagged = create_chat(&b, json!({ "metadata": json!({ NS: { "k": 1 } }) })).await;

    let got = chat_get_one(&b, &bare, Value::Null, &using())
        .await
        .unwrap();
    assert_eq!(got["metadata"], json!({}), "{got}");
    assert!(got.get("privateMetadata").is_none(), "{got}");
    let got = chat_get_one(&b, &tagged, Value::Null, &using())
        .await
        .unwrap();
    assert_eq!(got["metadata"], json!({ NS: { "k": 1 } }), "{got}");

    let without = using_without_metadata();
    let got = chat_get_one(&b, &tagged, Value::Null, &without)
        .await
        .unwrap();
    assert!(got.get("metadata").is_none(), "{got}");
    let props = json!(["id", "metadata", "privateMetadata"]);
    let got = chat_get_one(&b, &tagged, props, &without).await.unwrap();
    assert_eq!(got["metadata"], json!({ NS: { "k": 1 } }), "{got}");
    assert!(got.get("privateMetadata").is_none(), "{got}");
}

/// -05 §3.1 (L542-549): "Multiple subselectors with the same root combine
/// by union" and "An explicit root entry supersedes subselectors with the
/// same root"; (L551-554) a namespace that is not supported "MUST be
/// silently omitted from the response, unless the object has data stored
/// under that namespace"; (L537-540) "Any other properties entry beginning
/// with metadata/ or privateMetadata/ MUST be rejected with an
/// "invalidArguments" method-level error"; §3 (L509-510) subselectors are
/// part of the extension, so without the capability they name no
/// property.
#[tokio::test]
async fn chat_get_metadata_subselectors() {
    let b = backend();
    let id = create_chat(
        &b,
        json!({ "metadata": json!({ NS: { "k": 1 }, NS2: { "k": 2 } }) }),
    )
    .await;
    let cases = [
        (
            json!(["metadata/acme.example.com"]),
            json!({ NS: { "k": 1 } }),
        ),
        (
            json!(["metadata/acme.example.com", "metadata/other.example.org"]),
            json!({ NS: { "k": 1 }, NS2: { "k": 2 } }),
        ),
        (
            json!(["metadata", "metadata/acme.example.com"]),
            json!({ NS: { "k": 1 }, NS2: { "k": 2 } }),
        ),
        (json!(["metadata/unlisted.example.net"]), json!({})),
    ];
    for (props, want) in cases {
        let got = chat_get_one(&b, &id, props.clone(), &using())
            .await
            .unwrap();
        assert_eq!(got["metadata"], want, "{props}: {got}");
    }

    let props = json!(["metadata/acme.example.com"]);
    let got = chat_get_one(&b, &id, props, &using_without_metadata())
        .await
        .unwrap();
    assert!(got.get("metadata").is_none(), "{got}");

    for props in [
        json!(["metadata/"]),
        json!(["metadata/acme.example.com/k"]),
        json!(["privateMetadata/"]),
    ] {
        let err = chat_get_one(&b, &id, props.clone(), &using())
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("invalidArguments"),
            "{props}"
        );
    }
}

/// -05 §3 (L509-510): the extensions, the §3.6 filter conditions among
/// them, "apply to requests whose using includes
/// urn:ietf:params:jmap:metadata"; RFC 8620 §5.5 `unsupportedFilter`: "The
/// filter is syntactically valid, but the server cannot process it".
#[tokio::test]
async fn chat_metadata_filter_requires_capability() {
    let b = backend();
    let without = using_without_metadata();
    for filter in [
        json!({ "metadataExists": NS }),
        json!({ "operator": "NOT", "conditions": [json!({ "metadataExists": NS })] }),
    ] {
        let args = json!({ "accountId": ACCOUNT_ID, "filter": filter.clone() });
        let err = handle_chat_query(&b, &(), args, &without)
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("unsupportedFilter"),
            "{filter}"
        );
        let args =
            json!({ "accountId": ACCOUNT_ID, "filter": filter.clone(), "sinceQueryState": "0" });
        let err = handle_chat_query_changes(&b, &(), args, &without)
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("unsupportedFilter"),
            "{filter}"
        );
    }
    let args = json!({ "accountId": ACCOUNT_ID, "filter": json!({ "metadataExists": NS }) });
    assert!(handle_chat_query(&b, &(), args, &using()).await.is_ok());
}

/// Run Message/get with `properties` on `id` and return its one list entry.
async fn message_get_one(
    b: &MemoryBackend<()>,
    id: &str,
    properties: Value,
    using: &[String],
) -> Result<Value, JmapError> {
    let args = json!({ "accountId": ACCOUNT_ID, "ids": [id], "properties": properties });
    handle_message_get(b, &(), args, using)
        .await
        .map(|o| o.primary["list"][0].clone())
}

/// -05 §3.1 (L524-526): "when properties is omitted or null, the server
/// MUST include metadata and, where supportsPrivate is true,
/// privateMetadata"; §2 (L296-297): "When returned, its value MUST be an
/// object ({} when no metadata is set), never null"; §2 (L306-307): "When
/// supportsPrivate is false for the data type, this property MUST NOT be
/// returned"; §3 (L510-512): for other requests "metadata and
/// privateMetadata are not default properties of Foo/get (although they
/// are still returned when requested by name)".
#[tokio::test]
async fn message_get_default_properties_follow_capability() {
    let b = backend();
    seed_group_chat(&b, "g1");
    let bare = create_message(&b, json!({})).await;
    let tagged = create_message(&b, json!({ NS: { "k": 1 } })).await;

    let got = message_get_one(&b, &bare, Value::Null, &using())
        .await
        .unwrap();
    assert_eq!(got["metadata"], json!({}), "{got}");
    assert!(got.get("privateMetadata").is_none(), "{got}");
    let got = message_get_one(&b, &tagged, Value::Null, &using())
        .await
        .unwrap();
    assert_eq!(got["metadata"], json!({ NS: { "k": 1 } }), "{got}");

    let without = using_without_metadata();
    let got = message_get_one(&b, &tagged, Value::Null, &without)
        .await
        .unwrap();
    assert!(got.get("metadata").is_none(), "{got}");
    let props = json!(["id", "metadata", "privateMetadata"]);
    let got = message_get_one(&b, &tagged, props, &without).await.unwrap();
    assert_eq!(got["metadata"], json!({ NS: { "k": 1 } }), "{got}");
    assert!(got.get("privateMetadata").is_none(), "{got}");
}

/// -05 §3.1 (L542-549): "Multiple subselectors with the same root combine
/// by union" and "An explicit root entry supersedes subselectors with the
/// same root"; (L551-554) a namespace that is not supported "MUST be
/// silently omitted from the response, unless the object has data stored
/// under that namespace"; (L537-540) "Any other properties entry beginning
/// with metadata/ or privateMetadata/ MUST be rejected with an
/// "invalidArguments" method-level error"; §3 (L509-510) subselectors are
/// part of the extension, so without the capability they name no
/// property.
#[tokio::test]
async fn message_get_metadata_subselectors() {
    let b = backend();
    seed_group_chat(&b, "g1");
    let id = create_message(&b, json!({ NS: { "k": 1 }, NS2: { "k": 2 } })).await;
    let cases = [
        (
            json!(["metadata/acme.example.com"]),
            json!({ NS: { "k": 1 } }),
        ),
        (
            json!(["metadata/acme.example.com", "metadata/other.example.org"]),
            json!({ NS: { "k": 1 }, NS2: { "k": 2 } }),
        ),
        (
            json!(["metadata", "metadata/acme.example.com"]),
            json!({ NS: { "k": 1 }, NS2: { "k": 2 } }),
        ),
        (json!(["metadata/unlisted.example.net"]), json!({})),
    ];
    for (props, want) in cases {
        let got = message_get_one(&b, &id, props.clone(), &using())
            .await
            .unwrap();
        assert_eq!(got["metadata"], want, "{props}: {got}");
    }

    let props = json!(["metadata/acme.example.com"]);
    let got = message_get_one(&b, &id, props, &using_without_metadata())
        .await
        .unwrap();
    assert!(got.get("metadata").is_none(), "{got}");

    for props in [
        json!(["metadata/"]),
        json!(["metadata/acme.example.com/k"]),
        json!(["privateMetadata/"]),
    ] {
        let err = message_get_one(&b, &id, props.clone(), &using())
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("invalidArguments"),
            "{props}"
        );
    }
}

/// -05 §3 (L509-510): the extensions, the §3.6 filter conditions among
/// them, "apply to requests whose using includes
/// urn:ietf:params:jmap:metadata"; RFC 8620 §5.5 `unsupportedFilter`: "The
/// filter is syntactically valid, but the server cannot process it".
#[tokio::test]
async fn message_metadata_filter_requires_capability() {
    let b = backend();
    seed_group_chat(&b, "g1");
    let without = using_without_metadata();
    for filter in [
        json!({ "chatId": "g1", "metadataExists": NS }),
        json!({ "operator": "NOT", "conditions": [json!({ "chatId": "g1", "metadataExists": NS })] }),
    ] {
        let args = json!({ "accountId": ACCOUNT_ID, "filter": filter.clone() });
        let err = handle_message_query(&b, &(), args, &without)
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("unsupportedFilter"),
            "{filter}"
        );
        let args =
            json!({ "accountId": ACCOUNT_ID, "filter": filter.clone(), "sinceQueryState": "0" });
        let err = handle_message_query_changes(&b, &(), args, &without)
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("unsupportedFilter"),
            "{filter}"
        );
    }
    let args = json!({ "accountId": ACCOUNT_ID, "filter": json!({ "chatId": "g1", "metadataExists": NS }) });
    assert!(handle_message_query(&b, &(), args, &using()).await.is_ok());
}

/// Run Space/get with `properties` on `id` and return its one list entry.
async fn space_get_one(
    b: &MemoryBackend<()>,
    id: &str,
    properties: Value,
    using: &[String],
) -> Result<Value, JmapError> {
    let args = json!({ "accountId": ACCOUNT_ID, "ids": [id], "properties": properties });
    handle_space_get(b, &(), args, using)
        .await
        .map(|o| o.primary["list"][0].clone())
}

/// -05 §3.1 (L524-526): "when properties is omitted or null, the server
/// MUST include metadata and, where supportsPrivate is true,
/// privateMetadata"; §2 (L296-297): "When returned, its value MUST be an
/// object ({} when no metadata is set), never null"; §2 (L306-307): "When
/// supportsPrivate is false for the data type, this property MUST NOT be
/// returned"; §3 (L510-512): for other requests "metadata and
/// privateMetadata are not default properties of Foo/get (although they
/// are still returned when requested by name)".
#[tokio::test]
async fn space_get_default_properties_follow_capability() {
    let b = backend();
    let bare = create_space(&b, json!({})).await;
    let tagged = create_space(&b, json!({ NS: { "k": 1 } })).await;

    let got = space_get_one(&b, &bare, Value::Null, &using())
        .await
        .unwrap();
    assert_eq!(got["metadata"], json!({}), "{got}");
    assert!(got.get("privateMetadata").is_none(), "{got}");
    let got = space_get_one(&b, &tagged, Value::Null, &using())
        .await
        .unwrap();
    assert_eq!(got["metadata"], json!({ NS: { "k": 1 } }), "{got}");

    let without = using_without_metadata();
    let got = space_get_one(&b, &tagged, Value::Null, &without)
        .await
        .unwrap();
    assert!(got.get("metadata").is_none(), "{got}");
    let props = json!(["id", "metadata", "privateMetadata"]);
    let got = space_get_one(&b, &tagged, props, &without).await.unwrap();
    assert_eq!(got["metadata"], json!({ NS: { "k": 1 } }), "{got}");
    assert!(got.get("privateMetadata").is_none(), "{got}");
}

/// -05 §3.1 (L542-549): "Multiple subselectors with the same root combine
/// by union" and "An explicit root entry supersedes subselectors with the
/// same root"; (L551-554) a namespace that is not supported "MUST be
/// silently omitted from the response, unless the object has data stored
/// under that namespace"; (L537-540) "Any other properties entry beginning
/// with metadata/ or privateMetadata/ MUST be rejected with an
/// "invalidArguments" method-level error"; §3 (L509-510) subselectors are
/// part of the extension, so without the capability they name no
/// property.
#[tokio::test]
async fn space_get_metadata_subselectors() {
    let b = backend();
    let id = create_space(&b, json!({ NS: { "k": 1 }, NS2: { "k": 2 } })).await;
    let cases = [
        (
            json!(["metadata/acme.example.com"]),
            json!({ NS: { "k": 1 } }),
        ),
        (
            json!(["metadata/acme.example.com", "metadata/other.example.org"]),
            json!({ NS: { "k": 1 }, NS2: { "k": 2 } }),
        ),
        (
            json!(["metadata", "metadata/acme.example.com"]),
            json!({ NS: { "k": 1 }, NS2: { "k": 2 } }),
        ),
        (json!(["metadata/unlisted.example.net"]), json!({})),
    ];
    for (props, want) in cases {
        let got = space_get_one(&b, &id, props.clone(), &using())
            .await
            .unwrap();
        assert_eq!(got["metadata"], want, "{props}: {got}");
    }

    let props = json!(["metadata/acme.example.com"]);
    let got = space_get_one(&b, &id, props, &using_without_metadata())
        .await
        .unwrap();
    assert!(got.get("metadata").is_none(), "{got}");

    for props in [
        json!(["metadata/"]),
        json!(["metadata/acme.example.com/k"]),
        json!(["privateMetadata/"]),
    ] {
        let err = space_get_one(&b, &id, props.clone(), &using())
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("invalidArguments"),
            "{props}"
        );
    }
}

/// -05 §3 (L509-510): the extensions, the §3.6 filter conditions among
/// them, "apply to requests whose using includes
/// urn:ietf:params:jmap:metadata"; RFC 8620 §5.5 `unsupportedFilter`: "The
/// filter is syntactically valid, but the server cannot process it".
#[tokio::test]
async fn space_metadata_filter_requires_capability() {
    let b = backend();
    let without = using_without_metadata();
    for filter in [
        json!({ "metadataExists": NS }),
        json!({ "operator": "NOT", "conditions": [json!({ "metadataExists": NS })] }),
    ] {
        let args = json!({ "accountId": ACCOUNT_ID, "filter": filter.clone() });
        let err = handle_space_query(&b, &(), args, &without)
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("unsupportedFilter"),
            "{filter}"
        );
        let args =
            json!({ "accountId": ACCOUNT_ID, "filter": filter.clone(), "sinceQueryState": "0" });
        let err = handle_space_query_changes(&b, &(), args, &without)
            .await
            .unwrap_err();
        assert_eq!(
            method_error_type(&err),
            json!("unsupportedFilter"),
            "{filter}"
        );
    }
    let args = json!({ "accountId": ACCOUNT_ID, "filter": json!({ "metadataExists": NS }) });
    assert!(handle_space_query(&b, &(), args, &using()).await.is_ok());
}

/// Create a Message in chat "g1" carrying `metadata`.
async fn create_message(backend: &MemoryBackend<()>, metadata: Value) -> String {
    let create = json!({
        "chatId": "g1",
        "body": "b",
        "bodyType": "text/plain",
        "sentAt": "2026-01-01T00:00:00Z",
        "metadata": metadata,
    });
    let resp = message_set(backend, json!({ "create": { "m": create } })).await;
    resp["created"]["m"]["id"]
        .as_str()
        .unwrap_or_else(|| panic!("Message/set create failed: {resp}"))
        .to_owned()
}

/// Create a Space, owned by the caller, carrying `metadata`.
async fn create_space(backend: &MemoryBackend<()>, metadata: Value) -> String {
    let resp = space_set(
        backend,
        json!({ "create": { "s": { "name": "S", "metadata": metadata } } }),
    )
    .await;
    resp["created"]["s"]["id"]
        .as_str()
        .unwrap_or_else(|| panic!("Space/set create failed: {resp}"))
        .to_owned()
}