Skip to main content

jmap_base_client/
request.rs

1//! Base JMAP request and session types: [`JmapRequestBuilder`], [`Session`],
2//! [`AccountInfo`], [`WebSocketCapability`].
3//!
4//! Types that belong to the base JMAP client layer (RFC 8620 §2, §3.3, RFC 8887).
5//! Chat-specific and Mail-specific types live in their own crates.
6//!
7//! Types already in `jmap-types` and NOT redefined here:
8//! `Id`, `UTCDate`, `State`, `Date`, `JmapRequest`, `JmapResponse`, `Invocation`,
9//! `ResultReference`.
10
11use std::collections::HashMap;
12use std::collections::HashSet;
13
14use serde::{Deserialize, Serialize};
15
16use jmap_types::{Invocation, JmapRequest, State};
17
18use crate::error::ClientError;
19
20// ---------------------------------------------------------------------------
21// JmapUrl / JmapUrlTemplate (bd:JMAP-6r7c.40)
22// ---------------------------------------------------------------------------
23
24/// A plain JMAP URL — no RFC 6570 template variables expected.
25///
26/// This is the typed counterpart to [`JmapUrlTemplate`] (which requires
27/// expansion before use). The Session document distinguishes the two at
28/// the type level so callers cannot accidentally pass an unexpanded
29/// template (e.g. `https://server/download/{accountId}/{blobId}/{name}`)
30/// to a function that wants a plain URL.
31///
32/// Construct via [`JmapUrl::new`]. The string is taken as-is — no URL
33/// parsing or validation; downstream consumers (reqwest, http crate)
34/// validate at the actual request site. Borrow the inner string via
35/// [`as_str`](Self::as_str) for `&str`-accepting APIs.
36///
37/// Deliberately does NOT implement `Deref<Target = str>`. Auto-coercion
38/// would defeat the type distinction with [`JmapUrlTemplate`]: both
39/// would coerce to `&str` and pass any `&str`-accepting function. Use
40/// `.as_str()` at the call site so the type transition is visible in
41/// code review.
42#[non_exhaustive]
43#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
44#[serde(transparent)]
45pub struct JmapUrl(String);
46
47impl JmapUrl {
48    /// Wrap a string as a plain JMAP URL.
49    pub fn new(url: impl Into<String>) -> Self {
50        Self(url.into())
51    }
52
53    /// Borrow the inner URL string.
54    pub fn as_str(&self) -> &str {
55        &self.0
56    }
57
58    /// Consume the wrapper and return the inner `String`.
59    pub fn into_inner(self) -> String {
60        self.0
61    }
62}
63
64impl AsRef<str> for JmapUrl {
65    fn as_ref(&self) -> &str {
66        &self.0
67    }
68}
69
70impl std::fmt::Display for JmapUrl {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        f.write_str(&self.0)
73    }
74}
75
76impl PartialEq<str> for JmapUrl {
77    fn eq(&self, other: &str) -> bool {
78        self.0 == other
79    }
80}
81
82impl PartialEq<&str> for JmapUrl {
83    fn eq(&self, other: &&str) -> bool {
84        self.0 == *other
85    }
86}
87
88impl PartialEq<JmapUrl> for str {
89    fn eq(&self, other: &JmapUrl) -> bool {
90        self == other.0
91    }
92}
93
94impl PartialEq<JmapUrl> for &str {
95    fn eq(&self, other: &JmapUrl) -> bool {
96        *self == other.0
97    }
98}
99
100/// An RFC 6570 Level-1 URI template — requires variable substitution
101/// before use as a request URL.
102///
103/// Typed counterpart to [`JmapUrl`]. The template carries placeholders
104/// like `{accountId}` or `{blobId}` that must be expanded via
105/// [`expand_url_template`](crate::expand_url_template) before the result
106/// can be sent to an HTTP client. Passing the unexpanded template
107/// verbatim would produce a request URL containing literal `{...}`
108/// braces, which reqwest percent-encodes to `%7B...%7D` and the server
109/// rejects.
110///
111/// Construct via [`JmapUrlTemplate::new`]. See [`JmapUrl`] for the
112/// rationale behind not implementing `Deref<Target = str>`.
113#[non_exhaustive]
114#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
115#[serde(transparent)]
116pub struct JmapUrlTemplate(String);
117
118impl JmapUrlTemplate {
119    /// Wrap a string as a JMAP URL template.
120    pub fn new(template: impl Into<String>) -> Self {
121        Self(template.into())
122    }
123
124    /// Borrow the inner template string.
125    pub fn as_str(&self) -> &str {
126        &self.0
127    }
128
129    /// Consume the wrapper and return the inner `String`.
130    pub fn into_inner(self) -> String {
131        self.0
132    }
133}
134
135impl AsRef<str> for JmapUrlTemplate {
136    fn as_ref(&self) -> &str {
137        &self.0
138    }
139}
140
141impl std::fmt::Display for JmapUrlTemplate {
142    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143        f.write_str(&self.0)
144    }
145}
146
147impl PartialEq<str> for JmapUrlTemplate {
148    fn eq(&self, other: &str) -> bool {
149        self.0 == other
150    }
151}
152
153impl PartialEq<&str> for JmapUrlTemplate {
154    fn eq(&self, other: &&str) -> bool {
155        self.0 == *other
156    }
157}
158
159impl PartialEq<JmapUrlTemplate> for str {
160    fn eq(&self, other: &JmapUrlTemplate) -> bool {
161        self == other.0
162    }
163}
164
165impl PartialEq<JmapUrlTemplate> for &str {
166    fn eq(&self, other: &JmapUrlTemplate) -> bool {
167        *self == other.0
168    }
169}
170
171// ---------------------------------------------------------------------------
172// Username / AccountName (bd:JMAP-6r7c.63)
173// ---------------------------------------------------------------------------
174
175/// The authenticated user's username (RFC 8620 §2 `username` field).
176///
177/// Typically an email address, and therefore PII under GDPR / CCPA.
178/// The wrapper exists to centralise PII handling at the type level:
179///
180/// - **`Display` redacts to `"[REDACTED]"`.** `println!("{}", username)`,
181///   `format!("{username}")`, and `tracing::info!(user = %username, ...)`
182///   all hit `Display` and therefore the redaction. The pre-bd:JMAP-6r7c.63
183///   shape (`pub username: String`) leaked the raw value through every
184///   `Display`-bearing path.
185/// - **`Debug` redacts to `Username("[REDACTED]")`.** Pre-existing
186///   `Session::Debug` already redacted explicitly; keeping the wrapper's
187///   own redaction makes the field safe to print even outside the
188///   `Session::Debug` path (e.g. if a caller stores the `Username` in
189///   a struct of their own and derives `Debug` on it).
190/// - **`Serialize` emits the raw value verbatim** because the wire
191///   format requires it for round-trip. Callers who want to scrub the
192///   field before serialising MUST do so explicitly.
193/// - **[`expose_unredacted`](Self::expose_unredacted)** is the only path
194///   to the raw string. The accessor name is deliberately explicit so
195///   the intent is visible at the call site in code review.
196///
197/// Construct via [`Username::new`] or via deserialize.
198#[non_exhaustive]
199#[derive(Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
200#[serde(transparent)]
201pub struct Username(String);
202
203impl Username {
204    /// Wrap a string as a [`Username`].
205    pub fn new(username: impl Into<String>) -> Self {
206        Self(username.into())
207    }
208
209    /// Return the raw, un-redacted username string.
210    ///
211    /// **Do not log this return value.** This is the only path to the
212    /// raw PII; the explicit accessor name surfaces the intent in code
213    /// review. Use only when the wire format requires the raw value
214    /// (e.g. constructing an `Authorization` header that re-uses the
215    /// username, building an audit log under a separate PII-handling
216    /// policy).
217    pub fn expose_unredacted(&self) -> &str {
218        &self.0
219    }
220
221    /// Consume the wrapper and return the inner `String`.
222    ///
223    /// Same handling guidance as
224    /// [`expose_unredacted`](Self::expose_unredacted) — the caller now
225    /// owns a raw `String` and must handle PII manually from that
226    /// point on.
227    pub fn into_inner(self) -> String {
228        self.0
229    }
230}
231
232impl std::fmt::Display for Username {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        f.write_str("[REDACTED]")
235    }
236}
237
238impl std::fmt::Debug for Username {
239    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
240        f.debug_tuple("Username").field(&"[REDACTED]").finish()
241    }
242}
243
244impl PartialEq<str> for Username {
245    fn eq(&self, other: &str) -> bool {
246        self.0 == other
247    }
248}
249
250impl PartialEq<&str> for Username {
251    fn eq(&self, other: &&str) -> bool {
252        self.0 == *other
253    }
254}
255
256impl PartialEq<Username> for str {
257    fn eq(&self, other: &Username) -> bool {
258        self == other.0
259    }
260}
261
262impl PartialEq<Username> for &str {
263    fn eq(&self, other: &Username) -> bool {
264        *self == other.0
265    }
266}
267
268/// The human-readable account name (RFC 8620 §2 `name` field on the
269/// per-account object).
270///
271/// Typically the owner's email address, and therefore PII. Shape
272/// mirrors [`Username`]; see that type for the PII-handling rationale.
273#[non_exhaustive]
274#[derive(Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
275#[serde(transparent)]
276pub struct AccountName(String);
277
278impl AccountName {
279    /// Wrap a string as an [`AccountName`].
280    pub fn new(name: impl Into<String>) -> Self {
281        Self(name.into())
282    }
283
284    /// Return the raw, un-redacted account name. Same handling
285    /// guidance as [`Username::expose_unredacted`].
286    pub fn expose_unredacted(&self) -> &str {
287        &self.0
288    }
289
290    /// Consume the wrapper and return the inner `String`. Same
291    /// handling guidance as [`Username::into_inner`].
292    pub fn into_inner(self) -> String {
293        self.0
294    }
295}
296
297impl std::fmt::Display for AccountName {
298    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
299        f.write_str("[REDACTED]")
300    }
301}
302
303impl std::fmt::Debug for AccountName {
304    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
305        f.debug_tuple("AccountName").field(&"[REDACTED]").finish()
306    }
307}
308
309impl PartialEq<str> for AccountName {
310    fn eq(&self, other: &str) -> bool {
311        self.0 == other
312    }
313}
314
315impl PartialEq<&str> for AccountName {
316    fn eq(&self, other: &&str) -> bool {
317        self.0 == *other
318    }
319}
320
321impl PartialEq<AccountName> for str {
322    fn eq(&self, other: &AccountName) -> bool {
323        self == other.0
324    }
325}
326
327impl PartialEq<AccountName> for &str {
328    fn eq(&self, other: &AccountName) -> bool {
329        *self == other.0
330    }
331}
332
333// ---------------------------------------------------------------------------
334// JmapRequestBuilder (RFC 8620 §3.3)
335// ---------------------------------------------------------------------------
336
337/// Fluent builder for multi-method [`JmapRequest`] objects.
338///
339/// Collects method calls and produces a [`JmapRequest`] ready for dispatch.
340///
341/// The `using` capability URIs passed to `new` apply to the whole request;
342/// callers must include every capability required by the methods they add.
343///
344/// Spec: RFC 8620 §3.3
345#[derive(Debug)]
346pub struct JmapRequestBuilder {
347    using: Vec<String>,
348    method_calls: Vec<Invocation>,
349    call_ids: HashSet<String>,
350}
351
352impl JmapRequestBuilder {
353    /// Create a new builder with the given capability URIs.
354    ///
355    /// The `using` list MUST include `"urn:ietf:params:jmap:core"` (always
356    /// required by RFC 8620 §3.3) plus every capability URI needed by the
357    /// methods added via [`add_call`](JmapRequestBuilder::add_call). An
358    /// incorrect or empty `using` list will cause the server to return an
359    /// `"unknownCapability"` error — the builder does not validate it.
360    pub fn new(using: &[&str]) -> Self {
361        Self {
362            using: using.iter().map(|&s| s.to_owned()).collect(),
363            method_calls: Vec::new(),
364            call_ids: HashSet::new(),
365        }
366    }
367
368    /// Add one method call to the request.
369    ///
370    /// `call_id` must be unique within this request; callers use it to match
371    /// responses back to the originating call.
372    ///
373    /// Returns `Err(ClientError::InvalidArgument)` if `call_id` has already
374    /// been used in this builder. Duplicate call IDs violate RFC 8620 §3.5.
375    pub fn add_call(
376        &mut self,
377        method: impl Into<String>,
378        args: serde_json::Value,
379        call_id: impl Into<String>,
380    ) -> Result<&mut Self, ClientError> {
381        let call_id = call_id.into();
382        if !self.call_ids.insert(call_id.clone()) {
383            return Err(ClientError::InvalidArgument(format!(
384                "JmapRequestBuilder: duplicate call_id {call_id:?}"
385            )));
386        }
387        self.method_calls.push((method.into(), args, call_id));
388        Ok(self)
389    }
390
391    /// Consume the builder and produce the [`JmapRequest`].
392    ///
393    /// Returns `Err(ClientError::InvalidArgument)` if no method calls have
394    /// been added. An empty `methodCalls` array is invalid per RFC 8620 §3.3.
395    pub fn build(self) -> Result<JmapRequest, ClientError> {
396        if self.method_calls.is_empty() {
397            return Err(ClientError::InvalidArgument("no method calls added".into()));
398        }
399        Ok(JmapRequest::new(self.using, self.method_calls, None))
400    }
401}
402
403// ---------------------------------------------------------------------------
404// Session (RFC 8620 §2)
405// ---------------------------------------------------------------------------
406
407/// JMAP Session object returned by `GET /.well-known/jmap` (RFC 8620 §2).
408///
409/// Contains only the base RFC 8620 fields. Extension-specific fields
410/// (e.g. JMAP Chat `ownerUserId`) are surfaced by extension crates that
411/// parse the `capabilities` and `accounts` maps.
412///
413/// # `extra` equality is feature-flag-dependent (bd:JMAP-6r7c.43)
414///
415/// The derived `PartialEq` / `Eq` impl's behaviour on the `extra` field
416/// depends on the global `serde_json/preserve_order` feature flag — see
417/// the [crate-level note](crate#extra-field-equality-and-the-serde_jsonpreserve_order-feature-bdjmap-6r7c43)
418/// for the canonical statement.
419#[non_exhaustive]
420#[derive(Clone, PartialEq, Eq, Deserialize)]
421#[serde(rename_all = "camelCase")]
422pub struct Session {
423    /// Map of capability URI → capability object (RFC 8620 §2).
424    ///
425    /// Values are kept as raw JSON so callers can extract extension-specific
426    /// capability objects without this crate knowing their schema.
427    pub capabilities: HashMap<String, serde_json::Value>,
428
429    /// Map of account ID → [`AccountInfo`] (RFC 8620 §2).
430    pub accounts: HashMap<String, AccountInfo>,
431
432    /// Map of capability URI → primary account ID (RFC 8620 §2).
433    pub primary_accounts: HashMap<String, String>,
434
435    /// Username associated with the current credentials (RFC 8620 §2).
436    ///
437    /// # ⚠ PII — handle with the same care as a credential (bd:JMAP-6r7c.35, bd:JMAP-6r7c.63)
438    ///
439    /// This field is typically an email address and is therefore PII
440    /// under GDPR / CCPA. The [`Username`] wrapper redacts both
441    /// `Display` and `Debug`:
442    ///
443    /// - `println!("User: {}", session.username)` — `Display` renders
444    ///   `"[REDACTED]"` (per [`Username`]'s impl).
445    /// - `format!("hello {}", session.username)` — same.
446    /// - `tracing::info!(user = %session.username, ...)` — same.
447    /// - `format!("{:?}", session.username)` — `Debug` renders
448    ///   `Username("[REDACTED]")`.
449    ///
450    /// Two paths still expose the raw value, both deliberately
451    /// explicit at the call site:
452    ///
453    /// - [`Username::expose_unredacted`] returns `&str`. Use only when
454    ///   the wire requires it (constructing an `Authorization` header
455    ///   that re-uses the username, audit logging under a separate
456    ///   policy).
457    /// - `serde_json::to_string(&session)?` — `Session` derives
458    ///   `Serialize` for wire round-trip and emits the raw value
459    ///   verbatim. Callers who want to scrub PII before serialising
460    ///   MUST replace or clear the field first.
461    ///
462    /// If you need a non-PII session-scoped identifier, prefer
463    /// [`primary_accounts`](Session::primary_accounts) account IDs
464    /// (RFC 8620 §2's `accountId` is server-opaque and is not PII).
465    pub username: Username,
466
467    /// URL for JMAP API POST requests (RFC 8620 §2).
468    ///
469    /// Typed as [`JmapUrl`] (plain URL — no template variables) to
470    /// distinguish from the template-shaped URL fields below
471    /// (bd:JMAP-6r7c.40). Borrow as `&str` via
472    /// [`JmapUrl::as_str`](crate::JmapUrl::as_str) when calling
473    /// `&str`-accepting APIs.
474    pub api_url: JmapUrl,
475
476    /// URL template for blob downloads (RFC 8620 §2).
477    ///
478    /// URI Template (level 1) containing variables `accountId`, `blobId`,
479    /// `type`, and `name`. Typed as [`JmapUrlTemplate`] so it cannot be
480    /// confused with [`api_url`](Self::api_url) at the type level
481    /// (bd:JMAP-6r7c.40); expand via
482    /// [`expand_url_template`](crate::expand_url_template) before use.
483    pub download_url: JmapUrlTemplate,
484
485    /// URL template for blob uploads (RFC 8620 §2).
486    ///
487    /// URI Template (level 1) containing variable `accountId`. Typed
488    /// as [`JmapUrlTemplate`] (bd:JMAP-6r7c.40); see
489    /// [`download_url`](Self::download_url) for the type-distinction
490    /// rationale.
491    pub upload_url: JmapUrlTemplate,
492
493    /// URL template for SSE push event stream (RFC 8620 §2, §7.3).
494    ///
495    /// URI Template (level 1) containing variables `types`, `closeafter`,
496    /// and `ping`. Typed as [`JmapUrlTemplate`] (bd:JMAP-6r7c.40); see
497    /// [`download_url`](Self::download_url) for the type-distinction
498    /// rationale.
499    pub event_source_url: JmapUrlTemplate,
500
501    /// Opaque session state token (RFC 8620 §2).
502    ///
503    /// Changes whenever any session property changes. Returned in every API
504    /// response as `sessionState`; clients compare to detect staleness.
505    pub state: State,
506
507    /// Catch-all for vendor / site / private extension fields not covered
508    /// by the typed fields above. Preserves unknown fields across
509    /// deserialize/serialize round-trip per workspace extras-preservation
510    /// policy (see workspace AGENTS.md).
511    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
512    pub extra: serde_json::Map<String, serde_json::Value>,
513}
514
515impl Session {
516    /// Returns the primary account ID for the given capability URI, if set.
517    ///
518    /// Example: `session.primary_account_id("urn:ietf:params:jmap:mail")`
519    pub fn primary_account_id(&self, capability: &str) -> Option<&str> {
520        self.primary_accounts.get(capability).map(String::as_str)
521    }
522
523    /// Returns the parsed [`WebSocketCapability`] for the JMAP WebSocket
524    /// transport, if advertised (RFC 8887).
525    ///
526    /// - `Ok(None)` — server does not advertise JMAP WebSocket support.
527    /// - `Ok(Some(...))` — WebSocket is supported; use `result.url` to connect.
528    /// - `Err` — capability key is present but the value is malformed.
529    pub fn websocket_capability(&self) -> Result<Option<WebSocketCapability>, ClientError> {
530        self.extension_capability("urn:ietf:params:jmap:websocket")
531    }
532
533    /// Returns the parsed extension-capability object for `capability_uri`,
534    /// deserialized into the caller-supplied type `T` (bd:JMAP-6r7c.22).
535    ///
536    /// Use this when an extension defines a typed capability struct (the
537    /// way `urn:ietf:params:jmap:websocket` maps to [`WebSocketCapability`])
538    /// and you want a typed view instead of poking at the raw
539    /// `serde_json::Value` in [`Session::capabilities`]. Each extension
540    /// `*-client` crate should expose a typed `XxxCapability` struct and
541    /// a thin wrapper like:
542    ///
543    /// ```rust,ignore
544    /// pub fn mail_capability(session: &Session) -> Result<Option<MailCapability>, ClientError> {
545    ///     session.extension_capability("urn:ietf:params:jmap:mail")
546    /// }
547    /// ```
548    ///
549    /// # Returns
550    ///
551    /// - `Ok(None)` — server does not advertise this capability.
552    /// - `Ok(Some(_))` — capability is advertised AND the value parsed into `T`.
553    /// - `Err(ClientError::Parse)` — capability is advertised but the value
554    ///   could not be deserialised into `T`. Indicates either a server bug,
555    ///   a schema-version mismatch, or a `T` type that does not match the
556    ///   spec for `capability_uri`.
557    ///
558    /// The function only inspects the value when the key is present; an
559    /// absent key always returns `Ok(None)` regardless of `T`.
560    pub fn extension_capability<T>(&self, capability_uri: &str) -> Result<Option<T>, ClientError>
561    where
562        T: serde::de::DeserializeOwned,
563    {
564        let Some(raw) = self.capabilities.get(capability_uri) else {
565            return Ok(None);
566        };
567        T::deserialize(raw)
568            .map(Some)
569            .map_err(ClientError::from_parse)
570    }
571
572    /// Returns `true` if the server advertises the JMAP Blob Content
573    /// Identifiers extension (draft-atwood-jmap-cid-00).
574    ///
575    /// Checks for presence of `capabilities["urn:ietf:params:jmap:cid"]`.
576    /// The capability value object is empty per the draft (§2: "no
577    /// capability fields defined at this time"), so the presence of the
578    /// key is sufficient — no value-shape check is required.
579    ///
580    /// When `true`, the server commits to including a `sha256` field
581    /// (the 64-character lowercase-hex SHA-256 digest of the uploaded
582    /// content) on Blob upload responses, and on FileNode objects when
583    /// the JMAP FileNode extension is also supported. See
584    /// [`jmap_cid_types::Sha256`] for the typed wire shape.
585    ///
586    /// Mirrors the `supports_*` capability-probe pattern established by
587    /// `ChatSessionExt::supports_quotas` and
588    /// `ChatSessionExt::supports_refplus` in `jmap-chat-client`.
589    ///
590    /// [`jmap_cid_types::Sha256`]: https://docs.rs/jmap-cid-types
591    pub fn supports_cid(&self) -> bool {
592        self.capabilities.contains_key("urn:ietf:params:jmap:cid")
593    }
594}
595
596/// Manual `Debug` impl that redacts privacy-sensitive fields (bd:JMAP-sc1b.99).
597///
598/// `Session.username` is the authenticated user's identifier — typically a
599/// full email address, which is PII under GDPR/CCPA. `Session.state` is the
600/// opaque RFC 8620 §2 session-state token; it is not an auth credential, but
601/// it uniquely identifies the client's session and is the same shape of leak
602/// as logging a session cookie. Both are replaced with `"[REDACTED]"` /
603/// `"[opaque]"` in the Debug output.
604///
605/// All other URL/map fields are surfaced — they are deployment metadata and
606/// not credential-grade. `AccountInfo.name` is redacted by `AccountInfo`'s
607/// own manual `Debug` impl, so the `accounts` map below does not leak
608/// owner emails transitively (bd:JMAP-sc1b.104).
609impl std::fmt::Debug for Session {
610    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
611        f.debug_struct("Session")
612            .field("capabilities", &self.capabilities)
613            .field("accounts", &self.accounts)
614            .field("primary_accounts", &self.primary_accounts)
615            .field("username", &"[REDACTED]")
616            .field("api_url", &self.api_url)
617            .field("download_url", &self.download_url)
618            .field("upload_url", &self.upload_url)
619            .field("event_source_url", &self.event_source_url)
620            .field("state", &"[opaque]")
621            .field("extra", &self.extra)
622            .finish()
623    }
624}
625
626// ---------------------------------------------------------------------------
627// AccountInfo (RFC 8620 §2 Account object)
628// ---------------------------------------------------------------------------
629
630/// Per-account metadata in a JMAP Session (RFC 8620 §2).
631///
632/// `Debug` is hand-written to redact `name` because the field's own
633/// definition identifies it as "typically the owner's email address"
634/// (PII under GDPR/CCPA). The other fields are non-credential metadata
635/// and are surfaced directly. See bd:JMAP-sc1b.104.
636///
637/// # `extra` equality is feature-flag-dependent (bd:JMAP-6r7c.43)
638///
639/// The derived `PartialEq` / `Eq` impl's behaviour on the `extra` field
640/// depends on the global `serde_json/preserve_order` feature flag — see
641/// the [crate-level note](crate#extra-field-equality-and-the-serde_jsonpreserve_order-feature-bdjmap-6r7c43)
642/// for the canonical statement.
643#[non_exhaustive]
644#[derive(Clone, PartialEq, Eq, Deserialize)]
645#[serde(rename_all = "camelCase")]
646pub struct AccountInfo {
647    /// Human-readable account name (e.g. the owner's email address).
648    ///
649    /// # ⚠ PII — same handling rules as [`Session::username`] (bd:JMAP-6r7c.35, bd:JMAP-6r7c.63)
650    ///
651    /// Typed as [`AccountName`] (PII wrapper) so `Display`, `Debug`,
652    /// `format!`, and `tracing::*` paths all redact to `"[REDACTED]"`
653    /// rather than leaking the raw value. The only paths that surface
654    /// the raw string are [`AccountName::expose_unredacted`] (explicit
655    /// caller intent) and `serde_json::to_string(&account)?` (wire
656    /// round-trip).
657    ///
658    /// See [`Session::username`] for the full PII discussion and
659    /// recommended non-PII replacement identifiers.
660    pub name: AccountName,
661
662    /// `true` if this is the authenticated user's own personal account.
663    pub is_personal: bool,
664
665    /// `true` if the entire account is read-only for the current user.
666    pub is_read_only: bool,
667
668    /// Map of capability URI → capability object for this account.
669    ///
670    /// Values are kept as raw JSON so extension crates can extract
671    /// their own capability objects.
672    pub account_capabilities: HashMap<String, serde_json::Value>,
673
674    /// Catch-all for vendor / site / private extension fields not covered
675    /// by the typed fields above. Preserves unknown fields across
676    /// deserialize/serialize round-trip per workspace extras-preservation
677    /// policy (see workspace AGENTS.md).
678    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
679    pub extra: serde_json::Map<String, serde_json::Value>,
680}
681
682impl AccountInfo {
683    /// Returns the parsed per-account extension-capability object for
684    /// `capability_uri`, deserialized into the caller-supplied type `T`
685    /// (bd:JMAP-6r7c.22).
686    ///
687    /// Per-account counterpart of [`Session::extension_capability`]. Used
688    /// when an extension defines an account-scoped capability shape (e.g.
689    /// per-account quotas, per-account folder roots) rather than a
690    /// server-wide one.
691    ///
692    /// # Returns
693    ///
694    /// - `Ok(None)` — this account does not advertise this capability.
695    /// - `Ok(Some(_))` — capability is advertised AND the value parsed into `T`.
696    /// - `Err(ClientError::Parse)` — capability is advertised but the value
697    ///   could not be deserialised into `T`.
698    pub fn account_extension_capability<T>(
699        &self,
700        capability_uri: &str,
701    ) -> Result<Option<T>, ClientError>
702    where
703        T: serde::de::DeserializeOwned,
704    {
705        let Some(raw) = self.account_capabilities.get(capability_uri) else {
706            return Ok(None);
707        };
708        T::deserialize(raw)
709            .map(Some)
710            .map_err(ClientError::from_parse)
711    }
712}
713
714/// Manual `Debug` impl that redacts `name` (bd:JMAP-sc1b.104).
715///
716/// `AccountInfo.name` is typically the owner's email address, which is
717/// PII under GDPR/CCPA. The other fields (`is_personal`, `is_read_only`,
718/// `account_capabilities`) are non-credential metadata and are surfaced
719/// directly so `{:?}` output remains useful for debugging.
720///
721/// This redaction closes the transitive leak through `Session.accounts`
722/// — `Session`'s own Debug impl (bd:JMAP-sc1b.99) only redacted
723/// `username` and `state` directly and was silent about the accounts
724/// map. With `AccountInfo` redacting itself, any `{:?}` of a `Session`
725/// is now safe with respect to the canonical email-shaped PII.
726impl std::fmt::Debug for AccountInfo {
727    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
728        f.debug_struct("AccountInfo")
729            .field("name", &"[REDACTED]")
730            .field("is_personal", &self.is_personal)
731            .field("is_read_only", &self.is_read_only)
732            .field("account_capabilities", &self.account_capabilities)
733            .field("extra", &self.extra)
734            .finish()
735    }
736}
737
738// ---------------------------------------------------------------------------
739// WebSocketCapability (RFC 8887)
740// ---------------------------------------------------------------------------
741
742/// Capability object for `"urn:ietf:params:jmap:websocket"` (RFC 8887).
743///
744/// Advertised in `Session.capabilities` when the server supports JMAP over
745/// WebSocket. The `url` field is the `wss://` endpoint to connect to.
746///
747/// # `extra` equality is feature-flag-dependent (bd:JMAP-6r7c.43)
748///
749/// The derived `PartialEq` / `Eq` impl's behaviour on the `extra` field
750/// depends on the global `serde_json/preserve_order` feature flag — see
751/// the [crate-level note](crate#extra-field-equality-and-the-serde_jsonpreserve_order-feature-bdjmap-6r7c43)
752/// for the canonical statement.
753#[non_exhaustive]
754#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
755#[serde(rename_all = "camelCase")]
756pub struct WebSocketCapability {
757    /// The WebSocket endpoint URL (`wss://`).
758    pub url: String,
759
760    /// Whether the server supports push notifications over this WebSocket.
761    #[serde(default)]
762    pub supports_push: bool,
763
764    /// Catch-all for vendor / site / private extension fields not covered
765    /// by the typed fields above. Preserves unknown fields across
766    /// deserialize/serialize round-trip per workspace extras-preservation
767    /// policy (see workspace AGENTS.md).
768    #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
769    pub extra: serde_json::Map<String, serde_json::Value>,
770}
771
772// ---------------------------------------------------------------------------
773// Tests
774// ---------------------------------------------------------------------------
775
776#[cfg(test)]
777mod tests {
778    use super::*;
779    use serde_json::json;
780
781    // -----------------------------------------------------------------------
782    // JmapRequestBuilder
783    // -----------------------------------------------------------------------
784
785    /// Oracle: RFC 8620 §3.3 — a request with two method calls serializes to
786    /// a JSON object with a "methodCalls" array containing two 3-element arrays.
787    /// The expected JSON shape is derived directly from the RFC §3.3 example.
788    #[test]
789    fn builder_two_calls_serializes_correctly() {
790        let mut builder =
791            JmapRequestBuilder::new(&["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"]);
792        builder
793            .add_call(
794                "Mailbox/get",
795                json!({"accountId": "A13824", "ids": null}),
796                "r1",
797            )
798            .expect("add_call r1 must succeed");
799        builder
800            .add_call(
801                "Email/get",
802                json!({"accountId": "A13824", "ids": ["e001"]}),
803                "r2",
804            )
805            .expect("add_call r2 must succeed");
806        let req = builder.build().expect("build must succeed with two calls");
807
808        let v = serde_json::to_value(&req).expect("serialize JmapRequest");
809
810        // Oracle: RFC 8620 §3.3 — "using" must be present
811        assert!(v.get("using").is_some(), "must have 'using' field");
812        let using = v["using"].as_array().expect("using must be array");
813        assert_eq!(using.len(), 2);
814        assert!(using.contains(&json!("urn:ietf:params:jmap:core")));
815        assert!(using.contains(&json!("urn:ietf:params:jmap:mail")));
816
817        // Oracle: RFC 8620 §3.3 — "methodCalls" must be present
818        let calls = v["methodCalls"]
819            .as_array()
820            .expect("methodCalls must be array");
821        assert_eq!(calls.len(), 2, "must have exactly 2 method calls");
822
823        // Oracle: RFC 8620 §3.2 — each invocation is [methodName, args, callId]
824        assert_eq!(calls[0][0], json!("Mailbox/get"));
825        assert_eq!(calls[0][2], json!("r1"));
826        assert_eq!(calls[1][0], json!("Email/get"));
827        assert_eq!(calls[1][2], json!("r2"));
828    }
829
830    /// Oracle: RFC 8620 §3.3 — build() with no method calls is invalid;
831    /// must return Err(InvalidArgument) rather than produce an empty batch.
832    #[test]
833    fn builder_returns_err_on_empty_build() {
834        let result = JmapRequestBuilder::new(&["urn:ietf:params:jmap:core"]).build();
835        assert!(
836            matches!(result, Err(ClientError::InvalidArgument(_))),
837            "empty build must return Err(InvalidArgument), got {result:?}"
838        );
839    }
840
841    /// Oracle: RFC 8620 §3.5 — call IDs must be unique within a request.
842    /// Duplicate call ID returns Err(ClientError::InvalidArgument).
843    #[test]
844    fn builder_returns_err_on_duplicate_call_id() {
845        let mut builder = JmapRequestBuilder::new(&["urn:ietf:params:jmap:core"]);
846        builder
847            .add_call("Foo/get", json!({}), "r1")
848            .expect("first add_call must succeed");
849        let result = builder.add_call("Bar/get", json!({}), "r1"); // duplicate
850        assert!(
851            matches!(result, Err(ClientError::InvalidArgument(_))),
852            "duplicate call_id must return Err(InvalidArgument), got {result:?}"
853        );
854    }
855
856    // -----------------------------------------------------------------------
857    // Session
858    // -----------------------------------------------------------------------
859
860    /// Oracle: RFC 8620 §2.1 example Session JSON, transcribed from the RFC text.
861    /// All field names and values come from the RFC, not from the code under test.
862    #[test]
863    fn session_deserializes_rfc8620_example() {
864        // RFC 8620 §2.1 example — hand-transcribed from spec text.
865        let raw = r#"{
866            "capabilities": {
867                "urn:ietf:params:jmap:core": {
868                    "maxSizeUpload": 50000000,
869                    "maxConcurrentUpload": 8,
870                    "maxSizeRequest": 10000000,
871                    "maxConcurrentRequest": 8,
872                    "maxCallsInRequest": 32,
873                    "maxObjectsInGet": 256,
874                    "maxObjectsInSet": 128,
875                    "collationAlgorithms": [
876                        "i;ascii-numeric",
877                        "i;ascii-casemap",
878                        "i;unicode-casemap"
879                    ]
880                },
881                "urn:ietf:params:jmap:mail": {},
882                "urn:ietf:params:jmap:contacts": {},
883                "https://example.com/apis/foobar": {
884                    "maxFoosFinangled": 42
885                }
886            },
887            "accounts": {
888                "A13824": {
889                    "name": "john@example.com",
890                    "isPersonal": true,
891                    "isReadOnly": false,
892                    "accountCapabilities": {
893                        "urn:ietf:params:jmap:mail": {
894                            "maxMailboxesPerEmail": null,
895                            "maxMailboxDepth": 10
896                        },
897                        "urn:ietf:params:jmap:contacts": {}
898                    }
899                },
900                "A97813": {
901                    "name": "jane@example.com",
902                    "isPersonal": false,
903                    "isReadOnly": true,
904                    "accountCapabilities": {
905                        "urn:ietf:params:jmap:mail": {
906                            "maxMailboxesPerEmail": 1,
907                            "maxMailboxDepth": 10
908                        }
909                    }
910                }
911            },
912            "primaryAccounts": {
913                "urn:ietf:params:jmap:mail": "A13824",
914                "urn:ietf:params:jmap:contacts": "A13824"
915            },
916            "username": "john@example.com",
917            "apiUrl": "https://jmap.example.com/api/",
918            "downloadUrl": "https://jmap.example.com/download/{accountId}/{blobId}/{name}?accept={type}",
919            "uploadUrl": "https://jmap.example.com/upload/{accountId}/",
920            "eventSourceUrl": "https://jmap.example.com/eventsource/?types={types}&closeafter={closeafter}&ping={ping}",
921            "state": "75128aab4b1b"
922        }"#;
923
924        let session: Session =
925            serde_json::from_str(raw).expect("RFC 8620 §2.1 example must deserialize");
926
927        // Oracle: RFC 8620 §2.1
928        assert_eq!(session.username, "john@example.com");
929        assert_eq!(session.api_url, "https://jmap.example.com/api/");
930        assert_eq!(
931            session.upload_url,
932            "https://jmap.example.com/upload/{accountId}/"
933        );
934        assert_eq!(
935            session.download_url,
936            "https://jmap.example.com/download/{accountId}/{blobId}/{name}?accept={type}"
937        );
938        assert_eq!(
939            session.event_source_url,
940            "https://jmap.example.com/eventsource/?types={types}&closeafter={closeafter}&ping={ping}"
941        );
942        assert_eq!(session.state, "75128aab4b1b");
943
944        // Oracle: RFC 8620 §2.1 — capabilities map
945        assert!(
946            session
947                .capabilities
948                .contains_key("urn:ietf:params:jmap:core"),
949            "must have core capability"
950        );
951        assert!(
952            session
953                .capabilities
954                .contains_key("urn:ietf:params:jmap:mail"),
955            "must have mail capability"
956        );
957        assert!(
958            session
959                .capabilities
960                .contains_key("https://example.com/apis/foobar"),
961            "must have vendor capability"
962        );
963
964        // Oracle: RFC 8620 §2.1 — accounts map
965        assert!(
966            session.accounts.contains_key("A13824"),
967            "must have account A13824"
968        );
969        assert!(
970            session.accounts.contains_key("A97813"),
971            "must have account A97813"
972        );
973
974        // Oracle: RFC 8620 §2.1 — primaryAccounts
975        assert_eq!(
976            session.primary_account_id("urn:ietf:params:jmap:mail"),
977            Some("A13824")
978        );
979        assert_eq!(
980            session.primary_account_id("urn:ietf:params:jmap:contacts"),
981            Some("A13824")
982        );
983        assert_eq!(
984            session.primary_account_id("urn:ietf:params:jmap:core"),
985            None
986        );
987    }
988
989    // -----------------------------------------------------------------------
990    // AccountInfo
991    // -----------------------------------------------------------------------
992
993    /// Oracle: RFC 8620 §2.1 example — account A13824 (john@example.com).
994    /// Field names and values transcribed directly from the RFC.
995    #[test]
996    fn account_info_deserializes_rfc8620_example() {
997        // RFC 8620 §2.1 example account entry
998        let raw = r#"{
999            "name": "john@example.com",
1000            "isPersonal": true,
1001            "isReadOnly": false,
1002            "accountCapabilities": {
1003                "urn:ietf:params:jmap:mail": {
1004                    "maxMailboxesPerEmail": null,
1005                    "maxMailboxDepth": 10
1006                },
1007                "urn:ietf:params:jmap:contacts": {}
1008            }
1009        }"#;
1010
1011        let account: AccountInfo =
1012            serde_json::from_str(raw).expect("RFC 8620 §2.1 AccountInfo must deserialize");
1013
1014        // Oracle: RFC 8620 §2 Account object fields
1015        assert_eq!(account.name, "john@example.com");
1016        assert!(account.is_personal, "isPersonal must be true");
1017        assert!(!account.is_read_only, "isReadOnly must be false");
1018        assert!(
1019            account
1020                .account_capabilities
1021                .contains_key("urn:ietf:params:jmap:mail"),
1022            "must have mail capability"
1023        );
1024        assert!(
1025            account
1026                .account_capabilities
1027                .contains_key("urn:ietf:params:jmap:contacts"),
1028            "must have contacts capability"
1029        );
1030
1031        // Oracle: RFC 8620 §2.1 — read-only account (A97813 / jane@example.com)
1032        let raw2 = r#"{
1033            "name": "jane@example.com",
1034            "isPersonal": false,
1035            "isReadOnly": true,
1036            "accountCapabilities": {
1037                "urn:ietf:params:jmap:mail": {
1038                    "maxMailboxesPerEmail": 1,
1039                    "maxMailboxDepth": 10
1040                }
1041            }
1042        }"#;
1043        let account2: AccountInfo = serde_json::from_str(raw2)
1044            .expect("RFC 8620 §2.1 read-only AccountInfo must deserialize");
1045
1046        assert_eq!(account2.name, "jane@example.com");
1047        assert!(!account2.is_personal, "isPersonal must be false");
1048        assert!(account2.is_read_only, "isReadOnly must be true");
1049    }
1050
1051    // -----------------------------------------------------------------------
1052    // WebSocketCapability
1053    // -----------------------------------------------------------------------
1054
1055    /// Oracle: RFC 8887 §3 — WebSocketCapability has url and supportsPush fields.
1056    /// Transcribed from the RFC 8887 capability object definition.
1057    #[test]
1058    fn websocket_capability_deserializes() {
1059        let raw = r#"{"url": "wss://jmap.example.com/ws", "supportsPush": true}"#;
1060        let cap: WebSocketCapability =
1061            serde_json::from_str(raw).expect("WebSocketCapability must deserialize");
1062        assert_eq!(cap.url, "wss://jmap.example.com/ws");
1063        assert!(cap.supports_push);
1064    }
1065
1066    /// Oracle: RFC 8887 §3 — supportsPush defaults to false when absent.
1067    #[test]
1068    fn websocket_capability_supports_push_defaults_false() {
1069        let raw = r#"{"url": "wss://jmap.example.com/ws"}"#;
1070        let cap: WebSocketCapability =
1071            serde_json::from_str(raw).expect("WebSocketCapability must deserialize");
1072        assert_eq!(cap.url, "wss://jmap.example.com/ws");
1073        assert!(!cap.supports_push, "supportsPush must default to false");
1074    }
1075
1076    /// Oracle: Session.websocket_capability() returns Ok(None) when key absent.
1077    #[test]
1078    fn session_websocket_capability_absent_returns_ok_none() {
1079        let raw = r#"{
1080            "capabilities": {},
1081            "accounts": {},
1082            "primaryAccounts": {},
1083            "username": "u@example.com",
1084            "apiUrl": "https://jmap.example.com/api/",
1085            "downloadUrl": "https://jmap.example.com/dl/{accountId}/{blobId}/{name}?accept={type}",
1086            "uploadUrl": "https://jmap.example.com/ul/{accountId}/",
1087            "eventSourceUrl": "https://jmap.example.com/sse/?types={types}&closeafter={closeafter}&ping={ping}",
1088            "state": "s1"
1089        }"#;
1090        let session: Session = serde_json::from_str(raw).expect("Session must deserialize");
1091        let result = session.websocket_capability();
1092        assert!(
1093            matches!(result, Ok(None)),
1094            "expected Ok(None), got {result:?}"
1095        );
1096    }
1097
1098    /// Oracle: Session.websocket_capability() returns Ok(Some) when key present and valid.
1099    #[test]
1100    fn session_websocket_capability_present_and_valid() {
1101        let raw = r#"{
1102            "capabilities": {
1103                "urn:ietf:params:jmap:websocket": {
1104                    "url": "wss://jmap.example.com/ws",
1105                    "supportsPush": true
1106                }
1107            },
1108            "accounts": {},
1109            "primaryAccounts": {},
1110            "username": "u@example.com",
1111            "apiUrl": "https://jmap.example.com/api/",
1112            "downloadUrl": "https://jmap.example.com/dl/{accountId}/{blobId}/{name}?accept={type}",
1113            "uploadUrl": "https://jmap.example.com/ul/{accountId}/",
1114            "eventSourceUrl": "https://jmap.example.com/sse/?types={types}&closeafter={closeafter}&ping={ping}",
1115            "state": "s1"
1116        }"#;
1117        let session: Session = serde_json::from_str(raw).expect("Session must deserialize");
1118        let ws = session
1119            .websocket_capability()
1120            .expect("must not error")
1121            .expect("websocket capability must be present");
1122        assert_eq!(ws.url, "wss://jmap.example.com/ws");
1123        assert!(ws.supports_push);
1124    }
1125
1126    /// Oracle: `Session::supports_cid()` returns `false` when the JMAP
1127    /// CID capability URI is not present in the capabilities map
1128    /// (bd:JMAP-v9py.14).
1129    ///
1130    /// Mirrors the absent-key precedent of
1131    /// `session_websocket_capability_absent_returns_ok_none`. The test
1132    /// fixture has an empty capabilities map; the negative answer must
1133    /// be `false`, not `Err` or panic.
1134    #[test]
1135    fn supports_cid_returns_false_when_capability_absent() {
1136        let raw = r#"{
1137            "capabilities": {},
1138            "accounts": {},
1139            "primaryAccounts": {},
1140            "username": "u@example.com",
1141            "apiUrl": "https://jmap.example.com/api/",
1142            "downloadUrl": "https://jmap.example.com/dl/{accountId}/{blobId}/{name}?accept={type}",
1143            "uploadUrl": "https://jmap.example.com/ul/{accountId}/",
1144            "eventSourceUrl": "https://jmap.example.com/sse/?types={types}&closeafter={closeafter}&ping={ping}",
1145            "state": "s1"
1146        }"#;
1147        let session: Session = serde_json::from_str(raw).expect("Session must deserialize");
1148        assert!(!session.supports_cid());
1149    }
1150
1151    /// Oracle: `Session::supports_cid()` returns `true` when the JMAP
1152    /// CID capability URI is present in the capabilities map, even
1153    /// though the value object is empty per draft-atwood-jmap-cid-00
1154    /// §2 ("no capability fields defined at this time")
1155    /// (bd:JMAP-v9py.14).
1156    #[test]
1157    fn supports_cid_returns_true_when_capability_present_empty_value() {
1158        let raw = r#"{
1159            "capabilities": {
1160                "urn:ietf:params:jmap:cid": {}
1161            },
1162            "accounts": {},
1163            "primaryAccounts": {},
1164            "username": "u@example.com",
1165            "apiUrl": "https://jmap.example.com/api/",
1166            "downloadUrl": "https://jmap.example.com/dl/{accountId}/{blobId}/{name}?accept={type}",
1167            "uploadUrl": "https://jmap.example.com/ul/{accountId}/",
1168            "eventSourceUrl": "https://jmap.example.com/sse/?types={types}&closeafter={closeafter}&ping={ping}",
1169            "state": "s1"
1170        }"#;
1171        let session: Session = serde_json::from_str(raw).expect("Session must deserialize");
1172        assert!(session.supports_cid());
1173    }
1174
1175    /// Oracle: `Session::supports_cid()` checks only for the URI key —
1176    /// presence with a non-empty value object (vendor extras inside the
1177    /// CID capability) still returns `true`. The draft reserves the
1178    /// shape of the capability value but does not currently define any
1179    /// fields; a server that pre-populates vendor fields under the URI
1180    /// must still be detected as supporting CID.
1181    #[test]
1182    fn supports_cid_returns_true_when_capability_present_with_extra_fields() {
1183        let raw = r#"{
1184            "capabilities": {
1185                "urn:ietf:params:jmap:cid": {
1186                    "x-vendor-flag": "future-shape"
1187                }
1188            },
1189            "accounts": {},
1190            "primaryAccounts": {},
1191            "username": "u@example.com",
1192            "apiUrl": "https://jmap.example.com/api/",
1193            "downloadUrl": "https://jmap.example.com/dl/{accountId}/{blobId}/{name}?accept={type}",
1194            "uploadUrl": "https://jmap.example.com/ul/{accountId}/",
1195            "eventSourceUrl": "https://jmap.example.com/sse/?types={types}&closeafter={closeafter}&ping={ping}",
1196            "state": "s1"
1197        }"#;
1198        let session: Session = serde_json::from_str(raw).expect("Session must deserialize");
1199        assert!(session.supports_cid());
1200    }
1201
1202    /// Oracle: Session's manual Debug impl never reveals the authenticated
1203    /// `username` or the opaque `state` token (bd:JMAP-sc1b.99), AND the
1204    /// `accounts` map does not transitively leak `AccountInfo.name`
1205    /// (bd:JMAP-sc1b.104). Mirrors the canary tripwire pattern used by
1206    /// `bearer_auth_debug_does_not_leak_token` and
1207    /// `basic_auth_debug_does_not_leak_credentials` in auth.rs.
1208    ///
1209    /// The canary literals are independent of the Session's internal state —
1210    /// the test is the oracle, not the code under test. A regression that
1211    /// re-derives `Debug` on `Session` or `AccountInfo`, or that prints the
1212    /// username/state/name via a manual impl, would fail the assertion.
1213    ///
1214    /// We deliberately reuse `CANARY_USER` in two distinct locations
1215    /// (`username` and `accounts["a1"].name`) so a single negative
1216    /// `assert!(!dbg.contains(...))` catches a leak from either path —
1217    /// the same kind of email-shaped PII surfacing through either field
1218    /// is the failure we want to fail loudly.
1219    #[test]
1220    fn session_debug_does_not_leak_username_or_state() {
1221        const CANARY_USER: &str = "CANARY-USERNAME-DO-NOT-LEAK@example.com";
1222        const CANARY_STATE: &str = "CANARY-STATE-TOKEN-DO-NOT-LEAK";
1223        let raw = format!(
1224            r#"{{
1225                "capabilities": {{}},
1226                "accounts": {{
1227                    "a1": {{
1228                        "name": "{CANARY_USER}",
1229                        "isPersonal": true,
1230                        "isReadOnly": false,
1231                        "accountCapabilities": {{}}
1232                    }}
1233                }},
1234                "primaryAccounts": {{}},
1235                "username": "{CANARY_USER}",
1236                "apiUrl": "https://jmap.example.com/api/",
1237                "downloadUrl": "https://jmap.example.com/dl/{{accountId}}/",
1238                "uploadUrl": "https://jmap.example.com/ul/{{accountId}}/",
1239                "eventSourceUrl": "https://jmap.example.com/sse/",
1240                "state": "{CANARY_STATE}"
1241            }}"#
1242        );
1243        let session: Session = serde_json::from_str(&raw).expect("Session must deserialize");
1244
1245        // Sanity-check: the canary really did land in the AccountInfo —
1246        // otherwise an empty accounts map would silently make the
1247        // transitive-leak assertion below tautologically pass.
1248        let account = session
1249            .accounts
1250            .get("a1")
1251            .expect("accounts['a1'] must deserialize");
1252        assert_eq!(account.name, CANARY_USER);
1253
1254        let dbg = format!("{session:?}");
1255        assert!(
1256            !dbg.contains(CANARY_USER),
1257            "Session Debug must not contain the raw username or AccountInfo.name; got: {dbg}"
1258        );
1259        assert!(
1260            !dbg.contains(CANARY_STATE),
1261            "Session Debug must not contain the raw state token; got: {dbg}"
1262        );
1263    }
1264
1265    /// Oracle: AccountInfo's manual Debug impl never reveals the raw
1266    /// `name` field (bd:JMAP-sc1b.104). Independent of the Session-level
1267    /// test above: a regression on AccountInfo alone (e.g. re-deriving
1268    /// `#[derive(Debug)]`) would be caught here without needing the
1269    /// Session wrapper.
1270    #[test]
1271    fn account_info_debug_does_not_leak_name() {
1272        const CANARY_NAME: &str = "CANARY-ACCOUNT-NAME-DO-NOT-LEAK@example.com";
1273        let raw = format!(
1274            r#"{{
1275                "name": "{CANARY_NAME}",
1276                "isPersonal": true,
1277                "isReadOnly": false,
1278                "accountCapabilities": {{}}
1279            }}"#
1280        );
1281        let account: AccountInfo =
1282            serde_json::from_str(&raw).expect("AccountInfo must deserialize");
1283        // Sanity-check that the canary really did populate `name`.
1284        assert_eq!(account.name, CANARY_NAME);
1285
1286        let dbg = format!("{account:?}");
1287        assert!(
1288            !dbg.contains(CANARY_NAME),
1289            "AccountInfo Debug must not contain the raw name; got: {dbg}"
1290        );
1291    }
1292
1293    // ── Extras-preservation policy tests (JMAP-lbdy.9) ─────────────────
1294    //
1295    // Synthetic `acmeCorp*` vendor keys cannot collide with any RFC 8620 /
1296    // RFC 8887 typed field, so the tests are independent of the code under
1297    // test (workspace test-integrity rule).
1298
1299    /// `Session.extra` captures unknown fields on deserialize.
1300    #[test]
1301    fn session_preserves_vendor_extras() {
1302        let raw = json!({
1303            "capabilities": {},
1304            "accounts": {},
1305            "primaryAccounts": {},
1306            "username": "u@example.com",
1307            "apiUrl": "https://jmap.example.com/api/",
1308            "downloadUrl": "https://jmap.example.com/dl/{accountId}/{blobId}/{name}?accept={type}",
1309            "uploadUrl": "https://jmap.example.com/ul/{accountId}/",
1310            "eventSourceUrl": "https://jmap.example.com/sse/?types={types}&closeafter={closeafter}&ping={ping}",
1311            "state": "s1",
1312            "acmeCorpDeployment": "prod-eu-west-1"
1313        });
1314        let obj: Session = serde_json::from_value(raw).expect("Session must deserialize");
1315        assert_eq!(
1316            obj.extra.get("acmeCorpDeployment").and_then(|v| v.as_str()),
1317            Some("prod-eu-west-1")
1318        );
1319    }
1320
1321    /// `AccountInfo.extra` captures unknown fields on deserialize.
1322    #[test]
1323    fn account_info_preserves_vendor_extras() {
1324        let raw = json!({
1325            "name": "u@example.com",
1326            "isPersonal": true,
1327            "isReadOnly": false,
1328            "accountCapabilities": {},
1329            "acmeCorpQuotaTier": "gold"
1330        });
1331        let obj: AccountInfo = serde_json::from_value(raw).expect("AccountInfo must deserialize");
1332        assert_eq!(
1333            obj.extra.get("acmeCorpQuotaTier").and_then(|v| v.as_str()),
1334            Some("gold")
1335        );
1336    }
1337
1338    /// `WebSocketCapability.extra` captures unknown fields on deserialize.
1339    #[test]
1340    fn websocket_capability_preserves_vendor_extras() {
1341        let raw = json!({
1342            "url": "wss://jmap.example.com/ws",
1343            "supportsPush": true,
1344            "acmeCorpHeartbeatMs": 30000
1345        });
1346        let obj: WebSocketCapability =
1347            serde_json::from_value(raw).expect("WebSocketCapability must deserialize");
1348        assert_eq!(
1349            obj.extra
1350                .get("acmeCorpHeartbeatMs")
1351                .and_then(|v| v.as_u64()),
1352            Some(30000)
1353        );
1354    }
1355
1356    // -----------------------------------------------------------------------
1357    // Session::extension_capability / AccountInfo::account_extension_capability
1358    // (bd:JMAP-6r7c.22)
1359    // -----------------------------------------------------------------------
1360
1361    /// Hand-written capability struct standing in for any future extension
1362    /// (e.g. JMAP Mail / Calendars / Tasks). Has the same shape as
1363    /// `WebSocketCapability` deliberately — the helper is generic, the
1364    /// caller supplies the schema.
1365    #[derive(Debug, Deserialize, PartialEq)]
1366    #[serde(rename_all = "camelCase")]
1367    struct FakeMailCapability {
1368        max_size_upload: u64,
1369        max_size_request: u64,
1370    }
1371
1372    fn build_session_with_capability(uri: &str, value: serde_json::Value) -> Session {
1373        let raw = json!({
1374            "capabilities": { uri: value },
1375            "accounts": {},
1376            "primaryAccounts": {},
1377            "username": "u@example.com",
1378            "apiUrl": "https://jmap.example.com/api/",
1379            "downloadUrl": "https://jmap.example.com/dl/{accountId}/{blobId}/{name}?accept={type}",
1380            "uploadUrl": "https://jmap.example.com/ul/{accountId}/",
1381            "eventSourceUrl": "https://jmap.example.com/sse/?types={types}&closeafter={closeafter}&ping={ping}",
1382            "state": "s1",
1383        });
1384        serde_json::from_value(raw).expect("Session must deserialize")
1385    }
1386
1387    /// Oracle: `Session::extension_capability::<T>` returns `Ok(None)` when
1388    /// the capability key is absent, regardless of `T`.
1389    #[test]
1390    fn extension_capability_absent_returns_ok_none() {
1391        let session = build_session_with_capability(
1392            "urn:ietf:params:jmap:other",
1393            json!({"unrelated": "value"}),
1394        );
1395        let result: Result<Option<FakeMailCapability>, _> =
1396            session.extension_capability("urn:ietf:params:jmap:mail");
1397        assert!(
1398            matches!(result, Ok(None)),
1399            "absent capability key must return Ok(None), got {result:?}"
1400        );
1401    }
1402
1403    /// Oracle: `Session::extension_capability::<T>` returns `Ok(Some(T))`
1404    /// when the capability is present and the value matches `T`.
1405    #[test]
1406    fn extension_capability_present_and_valid_returns_ok_some() {
1407        let session = build_session_with_capability(
1408            "urn:ietf:params:jmap:mail",
1409            json!({"maxSizeUpload": 50000000, "maxSizeRequest": 10000000}),
1410        );
1411        let cap: FakeMailCapability = session
1412            .extension_capability("urn:ietf:params:jmap:mail")
1413            .expect("must not error")
1414            .expect("capability must be present");
1415        assert_eq!(cap.max_size_upload, 50_000_000);
1416        assert_eq!(cap.max_size_request, 10_000_000);
1417    }
1418
1419    /// Oracle: `Session::extension_capability::<T>` returns
1420    /// `Err(ClientError::Parse)` when the capability is present but the
1421    /// value cannot deserialise into `T` (server bug or schema mismatch).
1422    #[test]
1423    fn extension_capability_present_but_malformed_returns_parse_err() {
1424        let session = build_session_with_capability(
1425            "urn:ietf:params:jmap:mail",
1426            // Wrong shape — missing required maxSizeRequest field.
1427            json!({"maxSizeUpload": 50000000}),
1428        );
1429        let result: Result<Option<FakeMailCapability>, _> =
1430            session.extension_capability("urn:ietf:params:jmap:mail");
1431        assert!(
1432            matches!(result, Err(ClientError::Parse(_))),
1433            "malformed capability value must surface as ClientError::Parse, got {result:?}"
1434        );
1435    }
1436
1437    /// Oracle: `Session::websocket_capability()` delegates to
1438    /// `extension_capability` and the existing semantics are preserved
1439    /// (regression test for the refactor).
1440    #[test]
1441    fn websocket_capability_still_works_after_refactor() {
1442        let session = build_session_with_capability(
1443            "urn:ietf:params:jmap:websocket",
1444            json!({"url": "wss://jmap.example.com/ws", "supportsPush": true}),
1445        );
1446        let ws = session
1447            .websocket_capability()
1448            .expect("must not error")
1449            .expect("websocket capability must be present");
1450        assert_eq!(ws.url, "wss://jmap.example.com/ws");
1451        assert!(ws.supports_push);
1452    }
1453
1454    /// Oracle: `AccountInfo::account_extension_capability::<T>` returns
1455    /// `Ok(None)` when the per-account capability key is absent.
1456    #[test]
1457    fn account_extension_capability_absent_returns_ok_none() {
1458        let raw = json!({
1459            "name": "alice@example.com",
1460            "isPersonal": true,
1461            "isReadOnly": false,
1462            "accountCapabilities": {},
1463        });
1464        let acct: AccountInfo = serde_json::from_value(raw).expect("AccountInfo must deserialize");
1465        let result: Result<Option<FakeMailCapability>, _> =
1466            acct.account_extension_capability("urn:ietf:params:jmap:mail");
1467        assert!(
1468            matches!(result, Ok(None)),
1469            "absent per-account capability must return Ok(None), got {result:?}"
1470        );
1471    }
1472
1473    /// Oracle: `AccountInfo::account_extension_capability::<T>` returns
1474    /// `Ok(Some(T))` when the per-account capability is present and valid.
1475    #[test]
1476    fn account_extension_capability_present_and_valid() {
1477        let raw = json!({
1478            "name": "alice@example.com",
1479            "isPersonal": true,
1480            "isReadOnly": false,
1481            "accountCapabilities": {
1482                "urn:ietf:params:jmap:mail": {
1483                    "maxSizeUpload": 50000000,
1484                    "maxSizeRequest": 10000000,
1485                },
1486            },
1487        });
1488        let acct: AccountInfo = serde_json::from_value(raw).expect("AccountInfo must deserialize");
1489        let cap: FakeMailCapability = acct
1490            .account_extension_capability("urn:ietf:params:jmap:mail")
1491            .expect("must not error")
1492            .expect("capability must be present");
1493        assert_eq!(cap.max_size_upload, 50_000_000);
1494    }
1495
1496    // bd:JMAP-6r7c.40 — Typed URL wrappers (JmapUrl, JmapUrlTemplate)
1497
1498    /// `JmapUrl` and `JmapUrlTemplate` are distinct types at the type
1499    /// level. A function that takes `&JmapUrlTemplate` MUST refuse a
1500    /// `&JmapUrl` argument and vice versa. This is the compile-time
1501    /// guard that prevents callers from accidentally passing
1502    /// `session.api_url` (a plain URL) to a function expecting a
1503    /// template, or `session.upload_url` (a template) to a function
1504    /// expecting a plain URL.
1505    ///
1506    /// Implemented as compile-time witness: the function bodies do
1507    /// nothing useful; if either signature compiled with the other
1508    /// type, the test would break the type-distinction invariant.
1509    #[test]
1510    fn jmap_url_and_template_are_distinct_types() {
1511        fn _takes_plain_url(_u: &JmapUrl) {}
1512        fn _takes_template(_t: &JmapUrlTemplate) {}
1513
1514        let plain = JmapUrl::new("https://example.com/api/");
1515        let template = JmapUrlTemplate::new("https://example.com/upload/{accountId}/");
1516
1517        _takes_plain_url(&plain);
1518        _takes_template(&template);
1519
1520        // The interesting non-compilation cases:
1521        //   _takes_plain_url(&template);     // FAILS: expected JmapUrl, got JmapUrlTemplate
1522        //   _takes_template(&plain);          // FAILS: expected JmapUrlTemplate, got JmapUrl
1523        // These cannot be expressed as runtime assertions; the test's
1524        // value is locking in the distinct-types invariant so a future
1525        // refactor that accidentally collapses the wrappers (e.g. a
1526        // `type JmapUrlTemplate = JmapUrl;` alias) breaks the function
1527        // signatures above and the build fails.
1528    }
1529
1530    /// `JmapUrl` round-trips through serde_json as a transparent
1531    /// string. Oracle: hand-written JSON containing a quoted string.
1532    #[test]
1533    fn jmap_url_serde_round_trip() {
1534        let original = JmapUrl::new("https://example.com/api/");
1535        let json = serde_json::to_value(&original).expect("must serialise");
1536        assert_eq!(json, serde_json::json!("https://example.com/api/"));
1537        let restored: JmapUrl = serde_json::from_value(json).expect("must deserialise");
1538        assert_eq!(restored, original);
1539    }
1540
1541    /// `JmapUrlTemplate` round-trips through serde_json as a transparent
1542    /// string.
1543    #[test]
1544    fn jmap_url_template_serde_round_trip() {
1545        let original = JmapUrlTemplate::new("https://example.com/upload/{accountId}/");
1546        let json = serde_json::to_value(&original).expect("must serialise");
1547        assert_eq!(
1548            json,
1549            serde_json::json!("https://example.com/upload/{accountId}/")
1550        );
1551        let restored: JmapUrlTemplate = serde_json::from_value(json).expect("must deserialise");
1552        assert_eq!(restored, original);
1553    }
1554
1555    /// `PartialEq<&str>` and `PartialEq<str>` ergonomics for
1556    /// `assert_eq!(session.api_url, "...")` style assertions in
1557    /// downstream tests.
1558    #[test]
1559    fn jmap_url_partial_eq_str() {
1560        let url = JmapUrl::new("https://example.com/api/");
1561        assert_eq!(url, "https://example.com/api/");
1562        assert_eq!("https://example.com/api/", url);
1563        assert_ne!(url, "https://other.example.com/api/");
1564    }
1565}