~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
3424 lines · 120.8 KB Raw History
//! The spaces each account is the authority of (`spec/SPACES-POLICIES.md`):
//! creating, changing, describing and deleting them, a memberspace's
//! member list, taking an account out of the writer set, publishing the
//! policy and members to readers, and a space's host-side state as an
//! archive. Who a policy authorizes is `space_authz`.
//!
//! A space's state is its authority's: kept with the account's repository,
//! under its lock, each change one frame of its `repo.log` (`ENTRIES`). A
//! deleted space is remembered as deleted.

use crate::oauth::space_scope::SpaceOp;
use crate::space_authz::{Asking, SpaceState};
use crate::space_repo::Staged;
use crate::{
    Account, Call, Handler, Resp, Service, Shared, Store, XrpcError, durable, err, input, invalid,
    invalid_token, read, sessions, write,
};
use pds_lexicon::Method;
use pds_repo::data::{self, V};
use pds_space::{Commit, SpaceRef, star};
use serde_json::{Map, Value as Json, json};
use std::collections::BTreeMap;
use std::ops::Bound;
use std::sync::Arc;

/// The kinds of `repo.log` entry this module keeps, each naming its space
/// by `space`. The writer set's (`spaceWriter`, `spaceWriterRemoved`) are
/// logged by the notifications' part (`space_notify`).
pub(crate) const ENTRIES: &[&str] = &[
    "spaceCreate",
    "spaceUpdate",
    "spaceDelete",
    "spaceMember",
    "spaceUnmember",
    "spaceImport",
    "spaceWriter",
    "spaceWriterRemoved",
];

pub(crate) const PERSONAL: &str = "com.atproto.personalspace.policy";
pub(crate) const MEMBER: &str = "com.atproto.memberspace.policy";
pub(crate) const MANAGED: &str = "com.atproto.managedspace.policy";

/// The published records' collections.
pub(crate) const POLICY_RECORD: &str = "com.atproto.space.policy";
pub(crate) const MEMBER_RECORD: &str = "com.atproto.memberspace.member";

/// A space meta archive's profile.
const META_TYPE: &str = "atproto-space-meta";
const WRITER: &str = "writer/";

// ---- Policies ---------------------------------------------------------------

/// A space's policy, as stored: defaults filled in, and nothing its lexicon
/// does not define.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Policy {
    Personal,
    Member {
        public_read: bool,
        public_write: bool,
        publish_policy: bool,
        publish_members: bool,
        allowed_apps: Option<Vec<String>>,
    },
    Managed {
        managing_app: String,
        publish_policy: bool,
    },
}

impl Policy {
    /// A policy as stored or logged: `UnsupportedPolicy` for a `$type` not
    /// one of the three.
    pub fn read(p: &Json) -> Result<Policy, XrpcError> {
        let flag = |k: &str| p.get(k).and_then(Json::as_bool).unwrap_or(false);
        let t = p["$type"].as_str().unwrap_or_default();
        match t.strip_suffix("#main").unwrap_or(t) {
            PERSONAL => Ok(Policy::Personal),
            MEMBER => Ok(Policy::Member {
                public_read: flag("publicRead"),
                public_write: flag("publicWrite"),
                publish_policy: flag("publishPolicy"),
                publish_members: flag("publishMembers"),
                allowed_apps: p.get("allowedApps").and_then(Json::as_array).map(|a| {
                    a.iter()
                        .filter_map(Json::as_str)
                        .map(str::to_string)
                        .collect()
                }),
            }),
            MANAGED => Ok(Policy::Managed {
                managing_app: p["managingApp"].as_str().unwrap_or_default().to_string(),
                publish_policy: flag("publishPolicy"),
            }),
            _ => err(400, "UnsupportedPolicy", format!("Unsupported policy: {t}")),
        }
    }

    /// A policy given to `createSpace`, `updateSpace` or `importSpaceMeta`,
    /// once its lexicon took it: read, then `InvalidRequest` for a
    /// `managingApp` that is no service reference (`service_ref`).
    fn given(p: &Json) -> Result<Policy, XrpcError> {
        let policy = Policy::read(p)?;
        match &policy {
            Policy::Managed { managing_app, .. } if !service_ref(managing_app) => {
                invalid("managingApp must be a service reference, an atproto DID and a #fragment")
            }
            _ => Ok(policy),
        }
    }

    /// Whether the policy's lexicon takes `p`: an object with a `$type`, its
    /// fields of their types. A `$type` not one of the three is taken here.
    fn lexicon(p: &Json) -> Result<(), String> {
        let union = json!({ "type": "union", "refs": [PERSONAL, MEMBER, MANAGED] });
        pds_lexicon::validate("com.atproto.space.createSpace", &union, p, "policy").map_err(|e| e.0)
    }

    /// A policy a checkpoint keeps: its lexicon's, then as given.
    fn restored(p: &Json) -> Result<Policy, String> {
        Policy::lexicon(p)?;
        Policy::given(p).map_err(|e| e.message)
    }

    /// An archive's policy (`spec/SPACES-POLICIES.md`, "importSpaceMeta",
    /// step 5): one its lexicon refuses is `InvalidSpaceMeta`; then as given.
    fn imported(p: &Json) -> Result<Policy, XrpcError> {
        Policy::lexicon(p).or_else(|why| bad_meta(format!("Invalid archive policy: {why}")))?;
        Policy::given(p)
    }

    pub fn to_json(&self) -> Json {
        match self {
            Policy::Personal => json!({ "$type": PERSONAL }),
            Policy::Member {
                public_read,
                public_write,
                publish_policy,
                publish_members,
                allowed_apps,
            } => {
                let mut p = json!({
                    "$type": MEMBER, "publicRead": public_read, "publicWrite": public_write,
                    "publishPolicy": publish_policy, "publishMembers": publish_members,
                });
                if let Some(apps) = allowed_apps {
                    p["allowedApps"] = json!(apps);
                }
                p
            }
            Policy::Managed {
                managing_app,
                publish_policy,
            } => {
                json!({ "$type": MANAGED, "managingApp": managing_app, "publishPolicy": publish_policy })
            }
        }
    }

    fn to_drisl(&self) -> Vec<u8> {
        data::encode(&data::from_json(&self.to_json()).expect("a policy is in the data model"))
    }

    fn type_of(&self) -> &'static str {
        match self {
            Policy::Personal => PERSONAL,
            Policy::Member { .. } => MEMBER,
            Policy::Managed { .. } => MANAGED,
        }
    }

    fn publishes_policy(&self) -> bool {
        matches!(
            self,
            Policy::Member {
                publish_policy: true,
                ..
            } | Policy::Managed {
                publish_policy: true,
                ..
            }
        )
    }

    fn publishes_members(&self) -> bool {
        matches!(
            self,
            Policy::Member {
                publish_members: true,
                ..
            }
        )
    }
}

/// Whether `app` is a service reference, as a `managingApp` must be
/// (`spec/SPACES-POLICIES.md`, "Policies"): `<did>#<fragment>`, with one
/// `#`; the DID a `did:plc` of 24 characters of `a-z` and `2-7`, or a
/// `did:web` with no path and no port but on `localhost`; the fragment not
/// empty, of the characters RFC 3986 allows in one, `%` followed by two hex
/// digits.
fn service_ref(app: &str) -> bool {
    let Some((did, fragment)) = app.split_once('#') else {
        return false;
    };
    let b = fragment.as_bytes();
    let mut i = 0;
    while i < b.len() {
        let c = b[i];
        if c == b'%' {
            if !(i + 2 < b.len() && b[i + 1].is_ascii_hexdigit() && b[i + 2].is_ascii_hexdigit()) {
                return false;
            }
            i += 3;
        } else if c.is_ascii_alphanumeric() || b"-._~!$&'()*+,;=:@/?".contains(&c) {
            i += 1;
        } else {
            return false;
        }
    }
    let did_ok = match did.strip_prefix("did:plc:") {
        Some(id) => {
            id.len() == 24
                && id
                    .bytes()
                    .all(|c| c.is_ascii_lowercase() || (b'2'..=b'7').contains(&c))
        }
        None => did.starts_with("did:web:") && crate::outbound::atproto_did_web(did),
    };
    !b.is_empty() && did_ok
}

fn bad_meta<T>(message: impl Into<String>) -> Result<T, XrpcError> {
    err(400, "InvalidSpaceMeta", message)
}

/// A member's record key: the DID with its first `%3A` written as `:`, so
/// that a `did:web` with a port is a record key.
pub(crate) fn member_rkey(did: &str) -> String {
    did.replacen("%3A", ":", 1)
}

/// The DID a member's record key is, read back by writing the `:` before a
/// `did:web:<host>:<port>`'s port as `%3A`; None if that is no DID.
fn member_did(rkey: &str) -> Option<String> {
    let did = match rkey
        .strip_prefix("did:web:")
        .and_then(|r| r.split_once(':'))
    {
        Some((host, port)) if !port.is_empty() && port.bytes().all(|c| c.is_ascii_digit()) => {
            format!("did:web:{host}%3A{port}")
        }
        _ => rkey.to_string(),
    };
    (data::valid_rkey(rkey) && data::valid_did(&did)).then_some(did)
}

fn member_record(a: Access) -> Json {
    json!({ "$type": MEMBER_RECORD, "read": a.read, "write": a.write })
}

fn policy_record(p: &Policy) -> Json {
    json!({ "$type": POLICY_RECORD, "policy": p.to_json() })
}

// ---- The state ----------------------------------------------------------------

/// A member's entry.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Access {
    pub read: bool,
    pub write: bool,
}

/// An entry of the writer set (`spec/SPACES-SYNC.md`, "The writer set"):
/// the writer's repo in the space as last notified, the `spaceRev` the
/// entry was given when it last changed, and whether the writer was taken
/// out of the set since.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Writer {
    pub rev: String,
    pub data: Vec<u8>,
    pub space_rev: String,
    pub removed: bool,
}

/// A live space: its policy, its member list (a memberspace's alone), and
/// its writer set, each by DID.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Space {
    pub policy: Policy,
    pub members: BTreeMap<String, Access>,
    pub writers: BTreeMap<String, Writer>,
}

impl Space {
    fn new(policy: Policy) -> Space {
        Space {
            policy,
            members: BTreeMap::new(),
            writers: BTreeMap::new(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum Held {
    Live(Space),
    Deleted,
}

/// The spaces an account is the authority of, live or deleted, by URI.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Authority {
    spaces: BTreeMap<String, Held>,
    /// The bytes all of it is counted as in the cache.
    cost: u64,
}

impl Authority {
    pub fn live(&self, uri: &str) -> Option<&Space> {
        match self.spaces.get(uri) {
            Some(Held::Live(s)) => Some(s),
            _ => None,
        }
    }

    pub fn state(&self, uri: &str) -> SpaceState {
        match self.spaces.get(uri) {
            Some(Held::Live(_)) => SpaceState::Live,
            Some(Held::Deleted) => SpaceState::Deleted,
            None => SpaceState::Unknown,
        }
    }

    /// The URIs of the live spaces in `range`, in ascending order.
    pub fn live_spaces(&self, range: (Bound<String>, Bound<String>)) -> impl Iterator<Item = &str> {
        self.spaces
            .range::<String, _>(range)
            .filter(|(_, h)| matches!(h, Held::Live(_)))
            .map(|(uri, _)| uri.as_str())
    }

    fn live_mut(&mut self, uri: &str) -> Result<&mut Space, String> {
        match self.spaces.get_mut(uri) {
            Some(Held::Live(s)) => Ok(s),
            _ => Err(format!("a change to {uri}, which is not live")),
        }
    }

    /// Replay an entry of `repo.log` of the kinds in `ENTRIES`.
    pub fn replay(&mut self, e: &Json) -> Result<(), String> {
        self.apply(e).map_err(|why| format!("{why}: {e}"))
    }

    /// About the bytes it holds in memory: a total kept as its spaces change.
    pub(crate) fn cost(&self) -> u64 {
        self.cost
    }

    /// About the bytes the space `uri` takes in memory, by how many
    /// members, writers and allowed apps it has: `0` if it holds none of it.
    fn cost_of(&self, uri: &str) -> u64 {
        let held = match self.spaces.get(uri) {
            None => return 0,
            Some(Held::Deleted) => 0,
            Some(Held::Live(s)) => {
                let policy = match &s.policy {
                    Policy::Member {
                        allowed_apps: Some(apps),
                        ..
                    } => apps.len() * 96,
                    Policy::Managed { managing_app, .. } => managing_app.len(),
                    _ => 0,
                };
                160 + policy + s.members.len() * 144 + s.writers.len() * 256
            }
        };
        (uri.len() + 96 + held) as u64
    }

    /// Each space as a checkpoint keeps it, in ascending order of URI: a
    /// live one's archive, as `exportSpaceMeta` answers it but with the
    /// writer set's removals, or None for one deleted.
    pub(crate) fn saved(&self) -> impl Iterator<Item = (&str, Option<Vec<u8>>)> {
        self.spaces.iter().map(|(uri, h)| {
            (
                uri.as_str(),
                match h {
                    Held::Live(s) => Some(archive(uri, s, true)),
                    Held::Deleted => None,
                },
            )
        })
    }

    /// Restore a space of `did`'s a checkpoint keeps: from its archive, read
    /// as `importSpaceMeta` reads one, its policy, members and writers, the
    /// removed ones too (`Of::Checkpoint`); or deleted, with none.
    pub(crate) fn restore(
        &mut self,
        did: &str,
        uri: &str,
        archive: Option<&[u8]>,
    ) -> Result<(), String> {
        let bad = |why: String| format!("space {uri}: {why}");
        if SpaceRef::parse(uri).is_none_or(|s| s.authority != did) || self.spaces.contains_key(uri)
        {
            return Err(bad("not a space of the account's, or twice".into()));
        }
        let held = match archive {
            None => Held::Deleted,
            Some(bytes) => {
                let r = read_archive(bytes, Of::Checkpoint).map_err(|e| bad(e.message))?;
                if r.space.uri() != uri {
                    return Err(bad(format!("the archive of {}", r.space.uri())));
                }
                let policy =
                    Policy::restored(&r.policy).map_err(|e| bad(format!("its policy: {e}")))?;
                let members = members_of(&policy, &r.state).map_err(|e| bad(e.message))?;
                Held::Live(Space {
                    policy,
                    members,
                    writers: r.writers,
                })
            }
        };
        self.spaces.insert(uri.to_string(), held);
        self.cost += self.cost_of(uri);
        Ok(())
    }

    /// Make the change an entry records, and count what it leaves of its
    /// space. A change is checked before it is logged, so one logged
    /// applies as it did.
    pub(crate) fn apply(&mut self, e: &Json) -> Result<(), String> {
        let uri = e["space"].as_str().unwrap_or_default();
        let before = self.cost_of(uri);
        let done = self.change(e);
        self.cost = self.cost - before + self.cost_of(uri);
        done
    }

    fn change(&mut self, e: &Json) -> Result<(), String> {
        let text = |k: &str| e[k].as_str().ok_or_else(|| format!("no {k}"));
        let flag = |k: &str| e[k].as_bool().ok_or_else(|| format!("no {k}"));
        let policy =
            || Policy::read(&e["policy"]).map_err(|x| format!("a policy refused: {}", x.message));
        let uri = text("space")?;
        if SpaceRef::parse(uri).is_none() {
            return Err("a space that is no space-ref".into());
        }
        match text("t")? {
            "spaceCreate" => {
                if self.live(uri).is_some() {
                    return Err("a space created that exists".into());
                }
                self.spaces
                    .insert(uri.to_string(), Held::Live(Space::new(policy()?)));
            }
            "spaceUpdate" => {
                let p = policy()?;
                let s = self.live_mut(uri)?;
                // Another type migrates the space: its member list goes.
                if s.policy.type_of() != p.type_of() {
                    s.members.clear();
                }
                s.policy = p;
            }
            "spaceDelete" => {
                self.live_mut(uri)?;
                self.spaces.insert(uri.to_string(), Held::Deleted);
            }
            "spaceMember" => {
                let (did, a) = (
                    text("did")?,
                    Access {
                        read: flag("read")?,
                        write: flag("write")?,
                    },
                );
                let s = self.live_mut(uri)?;
                if !matches!(s.policy, Policy::Member { .. }) {
                    return Err("a member of a space that is no memberspace".into());
                }
                s.members.insert(did.to_string(), a);
            }
            "spaceUnmember" => {
                let did = text("did")?;
                self.live_mut(uri)?.members.remove(did);
            }
            "spaceWriter" => {
                let (did, w) = (text("did")?, writer_of(e)?);
                self.live_mut(uri)?.writers.insert(did.to_string(), w);
            }
            "spaceWriterRemoved" => {
                let (did, space_rev) = (text("did")?, text("spaceRev")?);
                let w = self
                    .live_mut(uri)?
                    .writers
                    .get_mut(did)
                    .ok_or("a writer removed that is not in the set")?;
                w.removed = true;
                w.space_rev = space_rev.to_string();
            }
            "spaceImport" => {
                let mut s = Space::new(policy()?);
                for m in e["members"].as_array().ok_or("no members")? {
                    let (Some(did), Some(read), Some(write)) =
                        (m["did"].as_str(), m["read"].as_bool(), m["write"].as_bool())
                    else {
                        return Err(format!("a member {m}"));
                    };
                    s.members.insert(did.to_string(), Access { read, write });
                }
                for w in e["writers"].as_array().ok_or("no writers")? {
                    let did = w["did"].as_str().ok_or_else(|| format!("a writer {w}"))?;
                    s.writers.insert(did.to_string(), writer_of(w)?);
                }
                self.spaces.insert(uri.to_string(), Held::Live(s));
            }
            t => return Err(format!("an entry of no known type {t}")),
        }
        Ok(())
    }
}

/// A writer set's entry in a log entry, not removed.
fn writer_entry(did: &str, w: &Writer) -> Json {
    json!({ "did": did, "rev": w.rev, "data": hex::encode(&w.data), "spaceRev": w.space_rev })
}

fn writer_of(e: &Json) -> Result<Writer, String> {
    let (Some(rev), Some(data), Some(space_rev)) = (
        e["rev"].as_str(),
        e["data"].as_str().and_then(|d| hex::decode(d).ok()),
        e["spaceRev"].as_str(),
    ) else {
        return Err(format!("a writer {e}"));
    };
    Ok(Writer {
        rev: rev.to_string(),
        data,
        space_rev: space_rev.to_string(),
        removed: false,
    })
}

/// The entry that tracks `did`'s write to the space `uri` in its writer
/// set, as `w`, not removed: logged by the notifications' part for a
/// notification it takes (`space_notify`), applied with `Authority::apply`.
pub(crate) fn writer_tracked(uri: &str, did: &str, w: &Writer) -> Json {
    let mut e = writer_entry(did, w);
    e["t"] = json!("spaceWriter");
    e["space"] = json!(uri);
    e
}

/// The entry that marks `did`'s entry in the writer set of the space `uri`
/// removed, at `space_rev`, keeping its `repoRev` and `data`: logged as
/// `space_notify::remove_writers` makes it.
pub(crate) fn writer_removed(uri: &str, did: &str, space_rev: &str) -> Json {
    json!({ "t": "spaceWriterRemoved", "space": uri, "did": did, "spaceRev": space_rev })
}

/// One change to an authority's spaces: the entries it logs, all in one
/// frame, made only once that frame is written (a change the log could not
/// take leaves nothing of itself); the commits of the authority's space
/// repos it makes, heard of (`space_repo::Hooks`) once the frame is on
/// disk; and what it owes the syncers, sent then.
#[derive(Default)]
struct Frame {
    entries: Vec<Json>,
    /// The entries of the spaces' own state, made in order once written.
    changes: Vec<Json>,
    staged: Vec<Staged>,
    /// The notifications' entries (`space_notify`), made once written.
    notices: Vec<Json>,
    commits: Vec<Commit>,
    sends: Vec<crate::space_notify::Sends>,
}

/// Log the change `e` records in the change's frame, made once the frame is
/// written: each method checks a change before it logs it.
fn log(frame: &mut Frame, e: Json) {
    frame.entries.push(e.clone());
    frame.changes.push(e);
}

/// Track `did`'s write to `space` as `w`, as the notifications' part does
/// for a notification it takes. Called under the authority's account lock;
/// false if the space is not live.
#[cfg(test)]
pub(crate) fn track_writer(st: &mut Store, space: &SpaceRef, did: &str, w: &Writer) -> bool {
    let uri = space.uri();
    if st.authority.live(&uri).is_none() {
        return false;
    }
    let e = writer_tracked(&uri, did, w);
    if st.wal.append(std::slice::from_ref(&e)).is_err() {
        return false;
    }
    st.authority
        .apply(&e)
        .expect("a change checked before it is logged");
    true
}

// ---- Publishing ---------------------------------------------------------------

/// A change to a published record in the authority's own repo in a space.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Publication {
    Put {
        collection: &'static str,
        rkey: String,
        record: Json,
    },
    Delete {
        collection: &'static str,
        rkey: String,
    },
}

/// The space repo's write path, as publishing uses it: each call is made
/// under the authority's account lock, with its store, and what it logs goes
/// in the change's frame.
pub(crate) trait Publisher {
    /// The record keys of `collection` in the authority's repo in `space`.
    fn rkeys(&self, st: &Store, space: &SpaceRef, collection: &str) -> Vec<String>;
    /// Whether the authority's repo in `space` holds a record of
    /// `collection` at `rkey`.
    fn holds(&self, st: &Store, space: &SpaceRef, collection: &str, rkey: &str) -> bool;
    /// One commit of the authority's repo in `space`, making these changes
    /// in order, as any write of that repo is made: its log entry, and the
    /// commit. Never called with none.
    fn commit(
        &self,
        st: &mut Store,
        space: &SpaceRef,
        ops: Vec<Publication>,
    ) -> (Json, Commit, Staged);
    /// Delete the authority's repo in `space`: its records, commit and
    /// operation log. Its log entry, if it had a repo there.
    fn drop_repo(&self, st: &mut Store, space: &SpaceRef) -> Option<(Json, Staged)>;
}

/// What the spaces' authority shares across accounts.
#[derive(Default)]
pub(crate) struct Host {
    /// The space repo's write path for the published records.
    pub publisher: crate::space_repo::Publish,
    /// The managing apps being asked.
    pub asking: Asking,
}

/// Write `ops` as one commit of the authority's repo in `space`; none, no commit.
fn publish(
    svc: &Service,
    st: &mut Store,
    frame: &mut Frame,
    space: &SpaceRef,
    ops: Vec<Publication>,
) {
    if !ops.is_empty() {
        let (entry, commit, staged) = svc.space_authority.publisher.commit(st, space, ops);
        frame.entries.push(entry);
        frame.staged.push(staged);
        frame.commits.push(commit);
    }
}

fn put_policy(p: &Policy) -> Publication {
    Publication::Put {
        collection: POLICY_RECORD,
        rkey: "self".into(),
        record: policy_record(p),
    }
}

fn put_member(did: &str, a: Access) -> Publication {
    Publication::Put {
        collection: MEMBER_RECORD,
        rkey: member_rkey(did),
        record: member_record(a),
    }
}

// ---- Who may call -------------------------------------------------------------

/// What an account deactivated or taken down, or a session, may do: each
/// method is one of these.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Kind {
    /// `createSpace`, `updateSpace`, `deleteSpace`, `putMembers`, `removeMembers`, `removeWriter`.
    Manage,
    /// `importSpaceMeta`, and `importRepo` (`space_repo`).
    Import,
    /// `getSpace`, `listMembers`.
    Read,
    Export,
}

/// An account's status (`spec/SPACES-POLICIES.md`, "Takedown and deactivation").
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Status {
    Active,
    Deactivated,
    TakenDown,
}

/// An account's status, as `spec/LIFECYCLE.md` keeps it: taken down before
/// deactivated.
pub(crate) fn status(a: &Account) -> Status {
    let state = crate::read(&a.state);
    if state.takedown.is_some() {
        Status::TakenDown
    } else if state.deactivated {
        Status::Deactivated
    } else {
        Status::Active
    }
}

/// The session a token is of.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Session {
    Password,
    AppPassword,
    /// From `createSession` with `allowTakendown`.
    Takendown,
}

/// The session a token is of: an OAuth session's is its account's, as a
/// password session's, its scope asked by each method
/// (`spec/OAUTH-SCOPES.md`, "Spaces").
fn session_of(t: &sessions::Token) -> Session {
    if t.oauth {
        return Session::Password;
    }
    match t.scope.as_str() {
        sessions::ACCESS => Session::Password,
        sessions::TAKENDOWN => Session::Takendown,
        _ => Session::AppPassword,
    }
}

/// Whether a method of `kind` takes a session of `session`, of an account of `status`.
fn admit(kind: Kind, session: Session, status: Status) -> Result<(), XrpcError> {
    match (kind, session) {
        (Kind::Export, _)
        | (Kind::Manage | Kind::Read, Session::Password | Session::AppPassword)
        | (Kind::Import, Session::Password) => {}
        _ => return invalid_token(),
    }
    match (kind, status) {
        (Kind::Manage, Status::Deactivated) => {
            err(401, "AccountDeactivated", "Account is deactivated")
        }
        (Kind::Manage | Kind::Import, Status::TakenDown) => {
            err(401, "AccountTakedown", "Account has been taken down")
        }
        _ => Ok(()),
    }
}

/// The caller's account, from its token, if a method of `kind` takes it.
pub(crate) fn caller(svc: &Service, call: &Call, kind: Kind) -> Result<Arc<Account>, XrpcError> {
    let t = sessions::takendown_ok_token(svc, call.bearer)?;
    let Some(a) = svc.account(&t.did) else {
        return invalid_token();
    };
    admit(kind, session_of(&t), status(&a))?;
    Ok(a)
}

/// Once the call's token and input are checked, it pays its rate limits,
/// and a change checks the free space (`limits::guard`; `LIMITS.md`).
fn pay(svc: &Service, call: &Call, a: &Account) -> Result<(), XrpcError> {
    crate::limits::guard(svc, call, Some(&a.did), None)
}

/// A space the call names, the call's scope asked for `op` on it
/// (`spec/OAUTH-SCOPES.md`, "What each method needs"), and the caller its
/// authority, else `NotAuthorized`.
fn owned(call: &Call, a: &Account, space: &str, op: SpaceOp) -> Result<SpaceRef, XrpcError> {
    let Some(sr) = SpaceRef::parse(space) else {
        return invalid("space must be a valid space-ref");
    };
    crate::scopes::may_in(call.bearer, &sr, op)?;
    if sr.authority != a.did {
        return not_authorized();
    }
    Ok(sr)
}

fn not_authorized<T>() -> Result<T, XrpcError> {
    err(400, "NotAuthorized", "Not the authority of the space")
}

fn not_found<T>() -> Result<T, XrpcError> {
    err(400, "SpaceNotFound", "Space not found")
}

fn mismatch<T>() -> Result<T, XrpcError> {
    err(
        400,
        "PolicyMismatch",
        "The space is not governed by the memberspace policy",
    )
}

fn text<'a>(b: &'a Map<String, Json>, k: &str) -> &'a str {
    b.get(k).and_then(Json::as_str).unwrap_or_default()
}

fn params(call: &Call, m: &Method) -> Result<Map<String, Json>, XrpcError> {
    m.parse_params(call.params.iter().map(|(k, v)| (k.as_str(), v.as_str())))
        .or_else(|e| invalid(e.0))
}

/// Make a change to the caller's spaces, under its account's lock, logged
/// in one frame; `f` refuses before it logs anything. Answered once what the
/// log has then is on disk and the commits it made are heard of.
async fn change<T>(
    svc: &Shared,
    a: &Account,
    f: impl FnOnce(&mut Store, &mut Frame) -> Result<T, XrpcError>,
) -> Result<T, XrpcError> {
    let mut frame = Frame::default();
    let (out, wal, written, doomed) = {
        let mut st = write(&a.store);
        let out = f(&mut st, &mut frame)?;
        if !frame.entries.is_empty() {
            let pos = match crate::blobstore::mark_for(&st)
                .and_then(|()| crate::space_repo::append(&st, &frame.entries))
            {
                Ok(pos) => pos,
                Err(e) => {
                    crate::ops::failed_for(
                        crate::ops::Level::Error,
                        "a change to spaces failed",
                        &a.did,
                        e,
                    );
                    return crate::internal();
                }
            };
            for c in &frame.changes {
                st.authority
                    .apply(c)
                    .expect("a change checked before it is logged");
            }
            for s in std::mem::take(&mut frame.staged) {
                s.apply(&mut st, pos);
            }
            for e in &frame.notices {
                st.notify
                    .apply(e)
                    .expect("a change checked before it is logged");
            }
        }
        // A dropped repo's records may have held blobs.
        (
            out,
            st.wal.clone(),
            st.wal.written(),
            st.blobs.take_doomed(),
        )
    };
    // The frame is in the log: the rest must happen even if the caller goes away.
    let (hearing, commits, sends) = (svc.clone(), frame.commits, frame.sends);
    let finish = crate::ops::spawn(async move {
        let synced = durable(wal, written).await;
        if synced.is_ok() {
            for s in sends {
                s.go(&hearing);
            }
            for c in &commits {
                hearing.space_hooks.committed(&hearing, c);
            }
        }
        synced
    });
    finish.await.unwrap_or_else(|_| crate::internal())?;
    crate::blobs::forget(svc, a, doomed);
    Ok(out)
}

/// Read the caller's spaces; answered once what they show is on disk.
async fn look<T>(
    a: &Account,
    f: impl FnOnce(&Store) -> Result<T, XrpcError>,
) -> Result<T, XrpcError> {
    let (out, wal) = {
        let st = read(&a.store);
        (f(&st)?, st.wal.clone())
    };
    durable(wal.clone(), wal.written()).await?;
    Ok(out)
}

// ---- The methods ----------------------------------------------------------------

pub(crate) fn handler(nsid: &str) -> Option<Handler> {
    Some(match nsid {
        "com.atproto.space.createSpace" => Handler::Call(|s, c, m| Box::pin(create_space(s, c, m))),
        "com.atproto.space.getSpace" => Handler::Call(|s, c, m| Box::pin(get_space(s, c, m))),
        "com.atproto.space.updateSpace" => Handler::Call(|s, c, m| Box::pin(update_space(s, c, m))),
        "com.atproto.space.deleteSpace" => Handler::Call(|s, c, m| Box::pin(delete_space(s, c, m))),
        "com.atproto.space.removeWriter" => {
            Handler::Call(|s, c, m| Box::pin(remove_writer(s, c, m)))
        }
        "com.atproto.space.exportSpaceMeta" => {
            Handler::Call(|s, c, m| Box::pin(export_space_meta(s, c, m)))
        }
        "com.atproto.space.importSpaceMeta" => {
            Handler::Call(|s, c, _| Box::pin(import_space_meta(s, c)))
        }
        "com.atproto.memberspace.putMembers" => {
            Handler::Call(|s, c, m| Box::pin(put_members(s, c, m)))
        }
        "com.atproto.memberspace.removeMembers" => {
            Handler::Call(|s, c, m| Box::pin(remove_members(s, c, m)))
        }
        "com.atproto.memberspace.listMembers" => {
            Handler::Call(|s, c, m| Box::pin(list_members(s, c, m)))
        }
        _ => return None,
    })
}

async fn create_space(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let a = caller(svc, call, Kind::Manage)?;
    let b = input(call, m)?;
    pay(svc, call, &a)?;
    // The key given, or one made for it now, which the scope is asked of.
    let skey = match b.get("skey").and_then(Json::as_str) {
        Some(k) => k.to_string(),
        None => write(&a.store).repo.tids.next_tid(),
    };
    let typ = text(&b, "spaceType").to_string();
    crate::scopes::may_space(call.bearer, &a.did, &typ, &skey, SpaceOp::Manage("create"))?;
    let policy = Policy::given(&b["policy"])?;
    let uri = change(svc, &a, |st, frame| {
        let sr = SpaceRef {
            authority: a.did.clone(),
            typ,
            skey,
        };
        let uri = sr.uri();
        if st.authority.live(&uri).is_some() {
            return err(
                400,
                "SpaceAlreadyExists",
                "A space with this URI already exists",
            );
        }
        log(
            frame,
            json!({ "t": "spaceCreate", "space": uri, "policy": policy.to_json() }),
        );
        let ops = if policy.publishes_policy() {
            vec![put_policy(&policy)]
        } else {
            vec![]
        };
        publish(svc, st, frame, &sr, ops);
        Ok(uri)
    })
    .await?;
    Ok(Resp::Json(json!({ "space": uri })))
}

async fn get_space(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let p = params(call, m)?;
    let a = caller(svc, call, Kind::Read)?;
    pay(svc, call, &a)?;
    let asked = text(&p, "space");
    let uri = owned(call, &a, asked, SpaceOp::Read { whole: false })?.uri();
    let policy = look(&a, |st| {
        st.authority
            .live(&uri)
            .map_or_else(not_found, |s| Ok(s.policy.to_json()))
    })
    .await?;
    Ok(Resp::Json(json!({ "space": asked, "policy": policy })))
}

async fn update_space(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let a = caller(svc, call, Kind::Manage)?;
    let b = input(call, m)?;
    pay(svc, call, &a)?;
    let sr = owned(call, &a, text(&b, "space"), SpaceOp::Manage("update"))?;
    let policy = Policy::given(&b["policy"])?;
    change(svc, &a, |st, frame| {
        let uri = sr.uri();
        let Some(old) = st.authority.live(&uri) else {
            return not_found();
        };
        let members_before = old.policy.publishes_members();
        log(
            frame,
            json!({ "t": "spaceUpdate", "space": uri, "policy": policy.to_json() }),
        );
        let publisher = &svc.space_authority.publisher;
        let mut ops = Vec::new();
        if policy.publishes_policy() {
            ops.push(put_policy(&policy));
        } else if publisher.holds(st, &sr, POLICY_RECORD, "self") {
            ops.push(Publication::Delete {
                collection: POLICY_RECORD,
                rkey: "self".into(),
            });
        }
        match (members_before, policy.publishes_members()) {
            (false, true) => {
                let s = st.authority.live(&uri).expect("updated");
                ops.extend(s.members.iter().map(|(did, a)| put_member(did, *a)));
            }
            (true, false) => {
                ops.extend(
                    publisher
                        .rkeys(st, &sr, MEMBER_RECORD)
                        .into_iter()
                        .map(|rkey| Publication::Delete {
                            collection: MEMBER_RECORD,
                            rkey,
                        }),
                );
            }
            _ => {}
        }
        publish(svc, st, frame, &sr, ops);
        Ok(())
    })
    .await?;
    Ok(Resp::Nothing)
}

async fn delete_space(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let a = caller(svc, call, Kind::Manage)?;
    let b = input(call, m)?;
    pay(svc, call, &a)?;
    let sr = owned(call, &a, text(&b, "space"), SpaceOp::Manage("delete"))?;
    change(svc, &a, |st, frame| {
        let uri = sr.uri();
        match st.authority.state(&uri) {
            SpaceState::Unknown => return not_found(),
            SpaceState::Deleted => {}
            SpaceState::Live => {
                log(frame, json!({ "t": "spaceDelete", "space": uri }));
                if let Some((entry, staged)) = svc.space_authority.publisher.drop_repo(st, &sr) {
                    frame.entries.push(entry);
                    frame.staged.push(staged);
                }
                // Its registrations are dropped, each owed a deletion notice.
                let notices = crate::space_notify::space_deleted(svc, st, &uri)?;
                frame.entries.extend(notices.iter().cloned());
                frame.notices.extend(notices);
            }
        }
        Ok(())
    })
    .await?;
    crate::space_notify::poke(svc, &a.did);
    Ok(Resp::Nothing)
}

async fn remove_writer(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let a = caller(svc, call, Kind::Manage)?;
    let b = input(call, m)?;
    pay(svc, call, &a)?;
    let sr = owned(call, &a, text(&b, "space"), SpaceOp::Manage("update"))?;
    let did = text(&b, "repo");
    change(svc, &a, |st, frame| {
        let uri = sr.uri();
        if st.authority.live(&uri).is_none() {
            return not_found();
        }
        take_out_writers(svc, st, frame, &uri, [did]);
        Ok(())
    })
    .await?;
    Ok(Resp::Nothing)
}

/// `putMembers`: each DID given takes its last entry, a `write: false` one
/// out of the writer set too; their records, when members are published,
/// written in one commit, each once. An empty list changes nothing.
async fn put_members(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let a = caller(svc, call, Kind::Manage)?;
    let b = input(call, m)?;
    pay(svc, call, &a)?;
    let sr = owned(call, &a, text(&b, "space"), SpaceOp::Manage("update"))?;
    let given: BTreeMap<String, Access> = b["members"]
        .as_array()
        .into_iter()
        .flatten()
        .map(|e| {
            (
                e["did"].as_str().unwrap_or_default().to_string(),
                Access {
                    read: e["read"] == true,
                    write: e["write"] == true,
                },
            )
        })
        .collect();
    change(svc, &a, |st, frame| {
        let uri = sr.uri();
        let Some(s) = st.authority.live(&uri) else {
            return not_found();
        };
        if !matches!(s.policy, Policy::Member { .. }) {
            return mismatch();
        }
        let publishing = s.policy.publishes_members();
        // Removals first, each its own entry (`take_out_writers`).
        let unwriting = given.iter().filter(|(_, a)| !a.write);
        take_out_writers(svc, st, frame, &uri, unwriting.map(|(did, _)| did.as_str()));
        for (did, access) in &given {
            log(
                frame,
                json!({ "t": "spaceMember", "space": uri, "did": did, "read": access.read, "write": access.write }),
            );
        }
        if publishing {
            let ops = given.iter().map(|(did, a)| put_member(did, *a)).collect();
            publish(svc, st, frame, &sr, ops);
        }
        Ok(())
    })
    .await?;
    Ok(Resp::Nothing)
}

/// `removeMembers`: each DID given that is a member, off the list and out
/// of the writer set, its record deleted if there is one, all in one
/// commit; a DID that is no member is left as it is.
async fn remove_members(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let a = caller(svc, call, Kind::Manage)?;
    let b = input(call, m)?;
    pay(svc, call, &a)?;
    let sr = owned(call, &a, text(&b, "space"), SpaceOp::Manage("update"))?;
    let dids: std::collections::BTreeSet<&str> = b["dids"]
        .as_array()
        .into_iter()
        .flatten()
        .filter_map(Json::as_str)
        .collect();
    change(svc, &a, |st, frame| {
        let uri = sr.uri();
        let Some(s) = st.authority.live(&uri) else {
            return not_found();
        };
        if !matches!(s.policy, Policy::Member { .. }) {
            return mismatch();
        }
        let removed: Vec<&str> = dids
            .iter()
            .copied()
            .filter(|did| s.members.contains_key(*did))
            .collect();
        // Removals first, as `putMembers` logs them.
        take_out_writers(svc, st, frame, &uri, removed.iter().copied());
        for did in &removed {
            log(
                frame,
                json!({ "t": "spaceUnmember", "space": uri, "did": did }),
            );
        }
        let publisher = &svc.space_authority.publisher;
        let ops = removed
            .iter()
            .map(|did| member_rkey(did))
            .filter(|rkey| publisher.holds(st, &sr, MEMBER_RECORD, rkey))
            .map(|rkey| Publication::Delete {
                collection: MEMBER_RECORD,
                rkey,
            })
            .collect();
        publish(svc, st, frame, &sr, ops);
        Ok(())
    })
    .await?;
    Ok(Resp::Nothing)
}

/// Take `dids` out of the writer set of the live space `uri`
/// (`space_notify::remove_writers`): each in it and not removed is a
/// removal (`spec/SPACES-POLICIES.md`, "The writer set"), logged in the
/// change's frame, and sent to the space's syncers once it is on disk.
/// `removeWriter`, `putMembers` with `write: false` and `removeMembers` of
/// a member each remove a writer through here alone.
fn take_out_writers<'a>(
    svc: &Service,
    st: &Store,
    frame: &mut Frame,
    uri: &str,
    dids: impl IntoIterator<Item = &'a str>,
) {
    let r = crate::space_notify::remove_writers(svc, st, uri, dids);
    for c in r.changes {
        log(frame, c);
    }
    frame.entries.extend(r.entries.iter().cloned());
    frame.notices.extend(r.entries);
    frame.sends.push(r.sends);
}

async fn list_members(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let p = params(call, m)?;
    let a = caller(svc, call, Kind::Read)?;
    pay(svc, call, &a)?;
    let uri = owned(call, &a, text(&p, "space"), SpaceOp::Read { whole: false })?.uri();
    let limit = p.get("limit").and_then(Json::as_u64).unwrap_or(100) as usize;
    // An empty cursor is as none; any other must be a DID, checked last.
    let cursor = p
        .get("cursor")
        .and_then(Json::as_str)
        .filter(|c| !c.is_empty());
    let members: Vec<Json> = look(&a, |st| {
        let Some(s) = st.authority.live(&uri) else {
            return not_found();
        };
        if !matches!(s.policy, Policy::Member { .. }) {
            return mismatch();
        }
        let from = match cursor {
            None => Bound::Unbounded,
            Some(c) if data::valid_did(c) => Bound::Excluded(c.to_string()),
            Some(_) => return err(400, "InvalidCursor", "The cursor must be a DID"),
        };
        Ok(s.members
            .range((from, Bound::Unbounded))
            .take(limit)
            .map(|(did, a)| json!({ "did": did, "read": a.read, "write": a.write }))
            .collect())
    })
    .await?;
    let mut out = json!({ "members": members });
    if let Some(last) = members.last() {
        out["cursor"] = last["did"].clone();
    }
    Ok(Resp::Json(out))
}

// ---- Space meta archives --------------------------------------------------------

async fn export_space_meta(svc: &Shared, call: &Call<'_>, m: &Method) -> Result<Resp, XrpcError> {
    let p = params(call, m)?;
    let a = caller(svc, call, Kind::Export)?;
    pay(svc, call, &a)?;
    let uri = owned(call, &a, text(&p, "space"), SpaceOp::Read { whole: false })?.uri();
    let bytes = look(&a, |st| {
        st.authority
            .live(&uri)
            .map_or_else(not_found, |s| Ok(archive(&uri, s, false)))
    })
    .await?;
    Ok(Resp::Star(bytes))
}

/// The archive of a space's state: its policy, a memberspace's members,
/// then its writer set, each section in ascending order of its keys' bytes.
/// The writer set's removals are in it only with `removals`, as a
/// checkpoint keeps them (`"removed": true`); `exportSpaceMeta` writes the
/// entries not removed.
fn archive(uri: &str, s: &Space, removals: bool) -> Vec<u8> {
    let meta = V::Map(vec![
        ("$type".into(), V::Str(META_TYPE.into())),
        ("ver".into(), V::Int(1)),
        ("space".into(), V::Str(uri.into())),
    ]);
    let mut pairs: Vec<(Vec<u8>, Vec<u8>)> = vec![(b"policy".to_vec(), s.policy.to_drisl())];
    if matches!(s.policy, Policy::Member { .. }) {
        let mut members: Vec<(Vec<u8>, Vec<u8>)> = s
            .members
            .iter()
            .map(|(did, a)| {
                let v = data::from_json(&member_record(*a))
                    .expect("a member record is in the data model");
                (
                    format!("{MEMBER_RECORD}/{}", member_rkey(did)).into_bytes(),
                    data::encode(&v),
                )
            })
            .collect();
        members.sort();
        pairs.extend(members);
    }
    for (did, w) in s.writers.iter().filter(|(_, w)| removals || !w.removed) {
        let mut v = vec![
            ("repoRev".into(), V::Str(w.rev.clone())),
            ("data".into(), V::Bytes(w.data.clone())),
            ("spaceRev".into(), V::Str(w.space_rev.clone())),
        ];
        if w.removed {
            v.push(("removed".into(), V::Bool(true)));
        }
        pairs.push((
            format!("{WRITER}{did}").into_bytes(),
            data::encode(&V::Map(v)),
        ));
    }
    star::write(
        &meta,
        pairs.iter().map(|(k, v)| (k.as_slice(), v.as_slice())),
    )
}

/// A map's value of key `k`.
fn field<'a>(m: &'a V, k: &str) -> Option<&'a V> {
    match m {
        V::Map(m) => m.iter().find(|(key, _)| key == k).map(|(_, v)| v),
        _ => None,
    }
}

/// An archive read as far as it can be without knowing who sent it: its
/// space, its policy, the policy state's pairs, and the writer set.
struct Read<'a> {
    space: SpaceRef,
    policy: Json,
    state: Vec<(&'a [u8], &'a [u8])>,
    writers: BTreeMap<String, Writer>,
}

/// What an archive is read as. Either way a writer's repo rev is its
/// `repoRev`, as `exportSpaceMeta` writes it.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Of {
    /// As `importSpaceMeta` takes one: none removed, the set is taken with
    /// no removals.
    Import,
    /// As a checkpoint keeps one: a writer `"removed": true` removed.
    Checkpoint,
}

/// Read an archive (`spec/SPACES-POLICIES.md`, "importSpaceMeta", steps
/// 1-3), each fault `InvalidSpaceMeta`: STAR's faults and the limits, then
/// the metadata, then the pairs as they come, each value a DRISL map, in
/// their sections and order, and the writer set.
fn read_archive(bytes: &[u8], of: Of) -> Result<Read<'_>, XrpcError> {
    let a = star::read(bytes, &star::REPO_LIMITS)
        .or_else(|e| bad_meta(format!("Invalid archive: {e}")))?;
    let space = match (
        field(&a.meta, "$type"),
        field(&a.meta, "ver"),
        field(&a.meta, "space"),
    ) {
        (Some(V::Str(t)), Some(V::Int(1)), Some(V::Str(space))) if t == META_TYPE => {
            SpaceRef::parse(space)
        }
        _ => None,
    };
    let Some(space) = space else {
        return bad_meta("Invalid archive metadata");
    };
    let (mut policy, mut state, mut writers) = (None, Vec::new(), BTreeMap::new());
    let mut last: Option<&[u8]> = None;
    for (i, (k, v)) in a.pairs.iter().enumerate() {
        let value = match data::decode(v) {
            Ok(value @ V::Map(_)) => value,
            _ => return bad_meta("An archive whose value is not a DRISL map"),
        };
        if i == 0 {
            if *k != b"policy" {
                return bad_meta("An archive whose first pair is not its policy");
            }
            policy = Some(data::to_json(&value));
            continue;
        }
        let is_writer = k.starts_with(WRITER.as_bytes());
        if *k == b"policy" || (!is_writer && !writers.is_empty()) {
            return bad_meta("An archive's sections out of order");
        }
        if is_writer && writers.is_empty() {
            last = None;
        }
        if last.is_some_and(|l| l >= *k) {
            return bad_meta("An archive's keys out of order or repeated");
        }
        last = Some(*k);
        if !is_writer {
            state.push((*k, *v));
            continue;
        }
        let did = std::str::from_utf8(&k[WRITER.len()..])
            .ok()
            .filter(|d| data::valid_did(d));
        let tid = |k: &str| match field(&value, k) {
            Some(V::Str(t)) if pds_repo::valid_tid(t) => Some(t.clone()),
            _ => None,
        };
        let removed = of == Of::Checkpoint && field(&value, "removed") == Some(&V::Bool(true));
        let w = match (field(&value, "data"), tid("repoRev"), tid("spaceRev")) {
            (Some(V::Bytes(data)), Some(rev), Some(space_rev)) => Some(Writer {
                rev,
                data: data.clone(),
                space_rev,
                removed,
            }),
            _ => None,
        };
        let (Some(did), Some(w)) = (did, w) else {
            return bad_meta("An archive with a bad writer");
        };
        writers.insert(did.to_string(), w);
    }
    let Some(policy) = policy else {
        return bad_meta("An archive with no policy");
    };
    Ok(Read {
        space,
        policy,
        state,
        writers,
    })
}

/// The policy state's pairs, under `policy` (`spec/SPACES-POLICIES.md`,
/// "importSpaceMeta", step 6): a memberspace's members, each a member
/// record keyed by a DID; no pair under any other policy. Else
/// `InvalidSpaceMeta`.
fn members_of(
    policy: &Policy,
    state: &[(&[u8], &[u8])],
) -> Result<BTreeMap<String, Access>, XrpcError> {
    let mut members = BTreeMap::new();
    let prefix = format!("{MEMBER_RECORD}/");
    for (k, v) in state {
        let did = std::str::from_utf8(k)
            .ok()
            .and_then(|k| k.strip_prefix(&prefix))
            .and_then(member_did);
        let (Some(did), Policy::Member { .. }) = (did, policy) else {
            return bad_meta("An archive with policy state its policy does not keep");
        };
        let value = data::decode(v).ok();
        let f = |k: &str| value.as_ref().and_then(|v| field(v, k));
        match (f("$type"), f("read"), f("write")) {
            (Some(V::Str(t)), Some(V::Bool(read)), Some(V::Bool(write))) if t == MEMBER_RECORD => {
                members.insert(
                    did,
                    Access {
                        read: *read,
                        write: *write,
                    },
                )
            }
            _ => return bad_meta("An archive with a bad member"),
        };
    }
    Ok(members)
}

/// `importSpaceMeta`: its body's `Content-Type` with the token, before the
/// body is read; then the archive, ownership, its policy and the policy
/// state, in that order. It replaces the space whole, its writer set with
/// no removals, and publishes nothing.
async fn import_space_meta(svc: &Shared, call: &Call<'_>) -> Result<Resp, XrpcError> {
    let a = caller(svc, call, Kind::Import)?;
    let media = call
        .headers
        .get(axum::http::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .map(|t| t.split(';').next().unwrap_or_default().trim());
    if !media.is_some_and(|t| t.eq_ignore_ascii_case(crate::STAR_TYPE)) {
        return invalid(format!(
            "Wrong request encoding (Content-Type): expected {}",
            crate::STAR_TYPE
        ));
    }
    pay(svc, call, &a)?;
    let r = read_archive(call.body, Of::Import)?;
    for m in ["create", "update"] {
        crate::scopes::may_in(call.bearer, &r.space, SpaceOp::Manage(m))?;
    }
    if r.space.authority != a.did {
        return not_authorized();
    }
    let policy = Policy::imported(&r.policy)?;
    let members = members_of(&policy, &r.state)?;
    let entry = json!({
        "t": "spaceImport", "space": r.space.uri(), "policy": policy.to_json(),
        "members": members.iter().map(|(did, a)| json!({ "did": did, "read": a.read, "write": a.write })).collect::<Vec<_>>(),
        "writers": r.writers.iter().map(|(did, w)| writer_entry(did, w)).collect::<Vec<_>>(),
    });
    change(svc, &a, |_, frame| {
        log(frame, entry);
        Ok(())
    })
    .await?;
    Ok(Resp::Nothing)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::*;
    use axum::http::{HeaderMap, HeaderValue, Method as Http, header};

    const T: &str = "com.example.forum";
    const TID: &str = "3jzfcijpj2z2a";
    const TID2: &str = "3jzfcijpj2z2b";
    const CREATE: &str = "com.atproto.space.createSpace";
    const UPDATE: &str = "com.atproto.space.updateSpace";
    const DELETE: &str = "com.atproto.space.deleteSpace";
    const GET: &str = "com.atproto.space.getSpace";
    const REMOVE_WRITER: &str = "com.atproto.space.removeWriter";
    const PUT: &str = "com.atproto.memberspace.putMembers";
    const REMOVE: &str = "com.atproto.memberspace.removeMembers";
    const LIST: &str = "com.atproto.memberspace.listMembers";

    fn sp(did: &str, key: &str) -> String {
        format!("at://{did}/space/{T}/{key}")
    }

    fn err_of<T>(name: &str) -> Result<T, String> {
        Err(name.to_string())
    }

    /// A call as sent, with its `Content-Type`.
    async fn send(
        s: &Shared,
        nsid: &str,
        token: Option<&str>,
        params: &[(&str, &str)],
        body: &[u8],
        ctype: Option<&str>,
    ) -> Result<Resp, String> {
        let mut headers = HeaderMap::new();
        if let Some(t) = ctype {
            headers.insert(header::CONTENT_TYPE, HeaderValue::from_str(t).unwrap());
        }
        let params: Vec<(String, String)> = params
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        let method = if pds_lexicon::method(nsid).unwrap().kind == pds_lexicon::Kind::Query {
            Http::GET
        } else {
            Http::POST
        };
        let bearer = token.map_or(sessions::Bearer::None, sessions::Bearer::Token);
        crate::answer(
            s,
            &Call {
                nsid,
                method: &method,
                params: &params,
                body,
                bearer,
                headers: &headers,
                client: "127.0.0.1".into(),
                charged: Default::default(),
                http: None,
            },
        )
        .await
        .map_err(|e| e.name.to_string())
    }

    async fn query(
        s: &Shared,
        nsid: &str,
        token: Option<&str>,
        params: &[(&str, &str)],
    ) -> Result<Json, String> {
        match send(s, nsid, token, params, b"", None).await? {
            Resp::Json(j) | Resp::Headed(j, _) => Ok(j),
            _ => panic!("{nsid} answered no JSON"),
        }
    }

    async fn export(s: &Shared, token: &str, space: &str) -> Result<Vec<u8>, String> {
        match send(
            s,
            "com.atproto.space.exportSpaceMeta",
            Some(token),
            &[("space", space)],
            b"",
            None,
        )
        .await?
        {
            Resp::Star(b) => Ok(b),
            _ => panic!("exportSpaceMeta answered no archive"),
        }
    }

    async fn import_as(
        s: &Shared,
        token: Option<&str>,
        ctype: Option<&str>,
        body: &[u8],
    ) -> Result<(), String> {
        match send(
            s,
            "com.atproto.space.importSpaceMeta",
            token,
            &[],
            body,
            ctype,
        )
        .await?
        {
            Resp::Nothing => Ok(()),
            _ => panic!("importSpaceMeta answered something"),
        }
    }

    async fn import(s: &Shared, token: &str, body: &[u8]) -> Result<(), String> {
        import_as(s, Some(token), Some(crate::STAR_TYPE), body).await
    }

    async fn create_space(s: &Shared, token: &str, key: &str, policy: Json) -> String {
        let got = call(
            s,
            CREATE,
            Some(token),
            json!({ "spaceType": T, "skey": key, "policy": policy }),
        )
        .await
        .unwrap();
        got["space"].as_str().unwrap().to_string()
    }

    async fn policy_of(s: &Shared, token: &str, space: &str) -> Json {
        let got = query(s, GET, Some(token), &[("space", space)])
            .await
            .unwrap();
        assert_eq!(got["space"], json!(space));
        got["policy"].clone()
    }

    /// putMembers of one member.
    async fn put(s: &Shared, token: &str, space: &str, did: &str, read: bool, write: bool) {
        put_all(s, token, space, &[(did, read, write)]).await;
    }

    /// putMembers of a batch, each `(did, read, write)`.
    async fn put_all(s: &Shared, token: &str, space: &str, batch: &[(&str, bool, bool)]) {
        let members: Vec<Json> = batch
            .iter()
            .map(|(did, read, write)| json!({ "did": did, "read": read, "write": write }))
            .collect();
        assert_eq!(
            call(
                s,
                PUT,
                Some(token),
                json!({ "space": space, "members": members })
            )
            .await,
            Ok(Json::Null)
        );
    }

    /// removeMembers of a batch.
    async fn remove(s: &Shared, token: &str, space: &str, dids: &[&str]) {
        assert_eq!(
            call(
                s,
                REMOVE,
                Some(token),
                json!({ "space": space, "dids": dids })
            )
            .await,
            Ok(Json::Null)
        );
    }

    async fn members(s: &Shared, token: &str, space: &str) -> Json {
        query(s, LIST, Some(token), &[("space", space), ("limit", "1000")])
            .await
            .unwrap()["members"]
            .clone()
    }

    fn writer(n: u8) -> Writer {
        Writer {
            rev: TID.into(),
            data: vec![n; 32],
            space_rev: TID2.into(),
            removed: false,
        }
    }

    /// Track `did`'s write to `space`, as the write notifications' part would.
    fn track(s: &Service, authority: &str, space: &str, did: &str, n: u8) -> bool {
        let a = s.account(authority).unwrap();
        let mut st = write(&a.store);
        track_writer(&mut st, &SpaceRef::parse(space).unwrap(), did, &writer(n))
    }

    /// The writers in the set, removed ones left out.
    fn writers(s: &Service, authority: &str, space: &str) -> Vec<String> {
        read(&s.account(authority).unwrap().store)
            .authority
            .live(space)
            .map(|x| {
                x.writers
                    .iter()
                    .filter(|(_, w)| !w.removed)
                    .map(|(did, _)| did.clone())
                    .collect()
            })
            .unwrap_or_default()
    }

    async fn app_password_session(s: &Shared, name: &str, token: &str) -> String {
        let p = call(
            s,
            "com.atproto.server.createAppPassword",
            Some(token),
            json!({ "name": "spaces" }),
        )
        .await
        .unwrap();
        let body = json!({ "identifier": format!("{name}.test"), "password": p["password"] });
        call(s, "com.atproto.server.createSession", None, body)
            .await
            .unwrap()["accessJwt"]
            .as_str()
            .unwrap()
            .to_string()
    }

    /// Every method's refusals, each in the order the spec checks them.
    #[tokio::test]
    async fn refusals_in_order() {
        let plc = plc().await;
        let dir = scratch("spaces-refusals");
        let s = open(&dir, &plc);
        let (alice, ta) = create(&s, "alice").await;
        let (bob, tb) = create(&s, "bob").await;
        let ms = json!({ "$type": MEMBER });
        let unsupported = json!({ "$type": "com.example.policy" });
        let bad_app = json!({ "$type": MANAGED, "managingApp": "web:example.com#forum" });
        let new =
            |key: &str, policy: &Json| json!({ "spaceType": T, "skey": key, "policy": policy });
        // createSpace: the token, however wrong the input; then the input; then the policy; then whether it exists.
        assert_eq!(
            call(&s, CREATE, None, json!({ "spaceType": "nope" })).await,
            err_of("AuthMissing")
        );
        assert_eq!(
            call(&s, CREATE, Some("abc.def.ghi"), new("u", &ms)).await,
            err_of("InvalidToken")
        );
        for bad in [
            json!({ "spaceType": "nope", "policy": ms }),
            json!({ "spaceType": T, "skey": "a b", "policy": ms }),
            json!({ "spaceType": T, "skey": "k".repeat(513), "policy": ms }),
            json!({ "spaceType": T }),
            json!({ "spaceType": T, "policy": 5 }),
            json!({ "spaceType": T, "policy": {} }),
            json!({ "spaceType": T, "policy": { "$type": MEMBER, "publicRead": "yes" } }),
            json!({ "spaceType": T, "policy": { "$type": MEMBER, "allowedApps": "x" } }),
            json!({ "spaceType": T, "policy": { "$type": MANAGED } }),
            json!({ "spaceType": T, "policy": { "$type": MANAGED, "managingApp": 5 } }),
            new("u", &bad_app),
        ] {
            assert_eq!(
                call(&s, CREATE, Some(&ta), bad.clone()).await,
                err_of("InvalidRequest"),
                "{bad}"
            );
        }
        assert_eq!(
            call(&s, CREATE, Some(&ta), new("u", &unsupported)).await,
            err_of("UnsupportedPolicy")
        );
        let got = call(
            &s,
            CREATE,
            Some(&ta),
            json!({ "spaceType": T, "policy": ms }),
        )
        .await
        .unwrap();
        // It answers the space's URI alone.
        assert_eq!(
            got.as_object().unwrap().keys().collect::<Vec<_>>(),
            ["space"]
        );
        let tid = SpaceRef::parse(got["space"].as_str().unwrap()).unwrap();
        assert!(
            tid.authority == alice && tid.typ == T && pds_repo::valid_tid(&tid.skey),
            "{got}"
        );
        let m1 = create_space(&s, &ta, "m1", ms.clone()).await;
        assert_eq!(m1, sp(&alice, "m1"));
        assert_eq!(
            call(
                &s,
                CREATE,
                Some(&ta),
                new("m1", &json!({ "$type": PERSONAL }))
            )
            .await,
            err_of("SpaceAlreadyExists")
        );
        assert_eq!(
            call(&s, CREATE, Some(&ta), new("m1", &unsupported)).await,
            err_of("UnsupportedPolicy")
        );
        // A space is its whole URI: another type, or another authority, is another space.
        let other_type = format!("at://{alice}/space/com.example.other/m1");
        call(
            &s,
            CREATE,
            Some(&ta),
            json!({ "spaceType": "com.example.other", "skey": "m1", "policy": ms }),
        )
        .await
        .unwrap();
        create_space(&s, &tb, "m1", json!({ "$type": PERSONAL })).await;
        // getSpace: its parameters, then the token, then ownership, then the space.
        assert_eq!(
            query(&s, GET, None, &[("space", "nope")]).await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            query(
                &s,
                GET,
                Some(&ta),
                &[("space", &format!("at://{alice}/space/{T}"))]
            )
            .await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            query(&s, GET, None, &[("space", &m1)]).await,
            err_of("AuthMissing")
        );
        assert_eq!(
            query(&s, GET, Some("abc.def.ghi"), &[("space", &m1)]).await,
            err_of("InvalidToken")
        );
        assert_eq!(
            query(&s, GET, Some(&tb), &[("space", &m1)]).await,
            err_of("NotAuthorized")
        );
        assert_eq!(
            query(
                &s,
                GET,
                Some(&tb),
                &[("space", &sp("did:plc:nobody", "m1"))]
            )
            .await,
            err_of("NotAuthorized")
        );
        assert_eq!(
            query(&s, GET, Some(&ta), &[("space", &sp(&alice, "never"))]).await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            policy_of(&s, &ta, &other_type).await,
            policy_of(&s, &ta, &m1).await
        );
        assert_eq!(
            policy_of(&s, &tb, &sp(&bob, "m1")).await,
            json!({ "$type": PERSONAL })
        );
        // updateSpace: the token, the input, ownership, the policy, then the space.
        let upd = |space: &str, policy: &Json| json!({ "space": space, "policy": policy });
        let never = sp(&alice, "never");
        assert_eq!(
            call(&s, UPDATE, None, json!({})).await,
            err_of("AuthMissing")
        );
        assert_eq!(
            call(&s, UPDATE, Some(&ta), upd("nope", &ms)).await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            call(
                &s,
                UPDATE,
                Some(&ta),
                upd(&m1, &json!({ "$type": MEMBER, "publishMembers": 1 }))
            )
            .await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            call(&s, UPDATE, Some(&tb), upd(&m1, &unsupported)).await,
            err_of("NotAuthorized")
        );
        assert_eq!(
            call(&s, UPDATE, Some(&ta), upd(&never, &unsupported)).await,
            err_of("UnsupportedPolicy")
        );
        assert_eq!(
            call(&s, UPDATE, Some(&ta), upd(&never, &bad_app)).await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            call(&s, UPDATE, Some(&ta), upd(&never, &ms)).await,
            err_of("SpaceNotFound")
        );
        // The member methods: ownership, the space, then its policy's type.
        let pers = create_space(&s, &ta, "p", json!({ "$type": PERSONAL })).await;
        for (nsid, body, malformed) in [
            (
                PUT,
                json!({ "members": [{ "did": bob, "read": true, "write": true }] }),
                vec![
                    json!({ "members": [{ "did": "not a did", "read": true, "write": true }] }),
                    json!({ "members": [{ "did": bob, "read": true }] }),
                    json!({ "did": bob, "read": true, "write": true }),
                    json!({ "members": (0..101).map(|i| json!({ "did": format!("did:plc:m{i}"), "read": true, "write": true })).collect::<Vec<_>>() }),
                ],
            ),
            (
                REMOVE,
                json!({ "dids": [bob] }),
                vec![
                    json!({ "dids": ["not a did"] }),
                    json!({ "did": bob }),
                    json!({ "dids": (0..101).map(|i| format!("did:plc:m{i}")).collect::<Vec<_>>() }),
                ],
            ),
        ] {
            let with = |space: &str, body: &Json| {
                let mut b = body.clone();
                b["space"] = json!(space);
                b
            };
            assert_eq!(
                call(&s, nsid, None, with(&pers, &body)).await,
                err_of("AuthMissing"),
                "{nsid}"
            );
            assert_eq!(
                call(&s, nsid, Some(&ta), with("nope", &body)).await,
                err_of("InvalidRequest"),
                "{nsid}"
            );
            // Input the lexicon refuses, before whose space it is.
            for bad in malformed {
                assert_eq!(
                    call(&s, nsid, Some(&tb), with(&pers, &bad)).await,
                    err_of("InvalidRequest"),
                    "{nsid} {bad}"
                );
            }
            assert_eq!(
                call(&s, nsid, Some(&tb), with(&pers, &body)).await,
                err_of("NotAuthorized"),
                "{nsid}"
            );
            assert_eq!(
                call(&s, nsid, Some(&ta), with(&never, &body)).await,
                err_of("SpaceNotFound"),
                "{nsid}"
            );
            assert_eq!(
                call(&s, nsid, Some(&ta), with(&pers, &body)).await,
                err_of("PolicyMismatch"),
                "{nsid}"
            );
        }
        assert_eq!(
            query(&s, LIST, Some(&ta), &[("space", "nope")]).await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            query(&s, LIST, None, &[("space", &pers)]).await,
            err_of("AuthMissing")
        );
        assert_eq!(
            query(&s, LIST, Some(&tb), &[("space", &pers)]).await,
            err_of("NotAuthorized")
        );
        assert_eq!(
            query(&s, LIST, Some(&ta), &[("space", &never)]).await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            query(&s, LIST, Some(&ta), &[("space", &pers)]).await,
            err_of("PolicyMismatch")
        );
        // A cursor that is no DID, checked last.
        for (token, space, want) in [
            (&tb, &pers, "NotAuthorized"),
            (&ta, &never, "SpaceNotFound"),
            (&ta, &pers, "PolicyMismatch"),
            (&ta, &m1, "InvalidCursor"),
        ] {
            assert_eq!(
                query(&s, LIST, Some(token), &[("space", space), ("cursor", "zz")]).await,
                err_of(want),
                "{space}"
            );
        }
        // removeWriter, under any policy.
        assert_eq!(
            call(
                &s,
                REMOVE_WRITER,
                Some(&ta),
                json!({ "space": pers, "repo": "nope" })
            )
            .await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            call(
                &s,
                REMOVE_WRITER,
                Some(&ta),
                json!({ "space": pers, "did": bob })
            )
            .await,
            err_of("InvalidRequest")
        );
        assert_eq!(
            call(
                &s,
                REMOVE_WRITER,
                Some(&tb),
                json!({ "space": pers, "repo": bob })
            )
            .await,
            err_of("NotAuthorized")
        );
        assert_eq!(
            call(
                &s,
                REMOVE_WRITER,
                Some(&ta),
                json!({ "space": never, "repo": bob })
            )
            .await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            call(
                &s,
                REMOVE_WRITER,
                Some(&ta),
                json!({ "space": pers, "repo": bob })
            )
            .await,
            Ok(Json::Null)
        );
        // exportSpaceMeta.
        assert_eq!(export(&s, &tb, &pers).await, err_of("NotAuthorized"));
        assert_eq!(export(&s, &ta, &never).await, err_of("SpaceNotFound"));
        // deleteSpace: one never created is not found; deleting again changes nothing.
        assert_eq!(
            call(&s, DELETE, Some(&tb), json!({ "space": m1 })).await,
            err_of("NotAuthorized")
        );
        assert_eq!(
            call(&s, DELETE, Some(&ta), json!({ "space": never })).await,
            err_of("SpaceNotFound")
        );
        put(&s, &ta, &m1, &bob, true, true).await;
        assert!(track(&s, &alice, &m1, &bob, 1));
        assert_eq!(
            call(&s, DELETE, Some(&ta), json!({ "space": m1 })).await,
            Ok(Json::Null)
        );
        assert_eq!(
            call(&s, DELETE, Some(&ta), json!({ "space": m1 })).await,
            Ok(Json::Null)
        );
        assert!(!track(&s, &alice, &m1, &bob, 1));
        assert_eq!(
            query(&s, GET, Some(&ta), &[("space", &m1)]).await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            call(&s, UPDATE, Some(&ta), upd(&m1, &ms)).await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            call(
                &s,
                PUT,
                Some(&ta),
                json!({ "space": m1, "members": [{ "did": bob, "read": true, "write": true }] })
            )
            .await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            call(&s, REMOVE, Some(&ta), json!({ "space": m1, "dids": [bob] })).await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            query(&s, LIST, Some(&ta), &[("space", &m1)]).await,
            err_of("SpaceNotFound")
        );
        assert_eq!(
            call(
                &s,
                REMOVE_WRITER,
                Some(&ta),
                json!({ "space": m1, "repo": bob })
            )
            .await,
            err_of("SpaceNotFound")
        );
        assert_eq!(export(&s, &ta, &m1).await, err_of("SpaceNotFound"));
        // Created again, it starts afresh: the new policy, no members, no writers.
        create_space(&s, &ta, "m1", ms.clone()).await;
        assert_eq!(members(&s, &ta, &m1).await, json!([]));
        assert!(writers(&s, &alice, &m1).is_empty());
        // An app-password session manages spaces as a password session does.
        let app = app_password_session(&s, "alice", &ta).await;
        let ap = create_space(&s, &app, "ap", ms.clone()).await;
        put(&s, &app, &ap, &bob, true, true).await;
        assert_eq!(
            members(&s, &app, &ap).await,
            json!([{ "did": bob, "read": true, "write": true }])
        );
        assert_eq!(policy_of(&s, &app, &ap).await["$type"], MEMBER);
        assert_eq!(
            call(
                &s,
                REMOVE_WRITER,
                Some(&app),
                json!({ "space": ap, "repo": bob })
            )
            .await,
            Ok(Json::Null)
        );
        assert_eq!(
            call(&s, UPDATE, Some(&app), upd(&ap, &ms)).await,
            Ok(Json::Null)
        );
        assert!(export(&s, &app, &ap).await.is_ok());
        assert_eq!(
            call(&s, DELETE, Some(&app), json!({ "space": ap })).await,
            Ok(Json::Null)
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// The policy stored, as getSpace answers it: its defaults filled in and
    /// every field its lexicon does not define dropped; replaced wholesale by
    /// updateSpace, a migration dropping the member list.
    #[tokio::test]
    async fn policies_are_stored_with_defaults() {
        let plc = plc().await;
        let dir = scratch("spaces-policies");
        let s = open(&dir, &plc);
        let (alice, ta) = create(&s, "alice").await;
        let (bob, _) = create(&s, "bob").await;
        let defaults = json!({ "$type": MEMBER, "publicRead": false, "publicWrite": false, "publishPolicy": false, "publishMembers": false });
        for (key, given, stored) in [
            (
                "p",
                json!({ "$type": PERSONAL, "x": 1, "publicRead": true }),
                json!({ "$type": PERSONAL }),
            ),
            (
                "pm",
                json!({ "$type": format!("{PERSONAL}#main") }),
                json!({ "$type": PERSONAL }),
            ),
            ("m", json!({ "$type": MEMBER }), defaults.clone()),
            (
                "m2",
                json!({ "$type": MEMBER, "publicRead": true, "publishMembers": true, "allowedApps": [], "junk": { "a": 1 } }),
                json!({ "$type": MEMBER, "publicRead": true, "publicWrite": false, "publishPolicy": false, "publishMembers": true, "allowedApps": [] }),
            ),
            (
                "m3",
                json!({ "$type": MEMBER, "allowedApps": ["https://a.example/c", "https://b.example/"] }),
                {
                    let mut p = defaults.clone();
                    p["allowedApps"] = json!(["https://a.example/c", "https://b.example/"]);
                    p
                },
            ),
            (
                "g",
                json!({ "$type": MANAGED, "managingApp": "did:web:app.example#forum", "x": 2 }),
                json!({ "$type": MANAGED, "managingApp": "did:web:app.example#forum", "publishPolicy": false }),
            ),
            (
                "g2",
                json!({ "$type": MANAGED, "managingApp": "did:web:localhost%3A2583#x", "publishPolicy": true }),
                json!({ "$type": MANAGED, "managingApp": "did:web:localhost%3A2583#x", "publishPolicy": true }),
            ),
        ] {
            let uri = create_space(&s, &ta, key, given).await;
            assert_eq!(policy_of(&s, &ta, &uri).await, stored, "{key}");
        }
        // A managingApp of a form refused, at create and at update.
        let g = sp(&alice, "g");
        for app in ["did:", "did:web:app.example", "x#y"] {
            let p = json!({ "$type": MANAGED, "managingApp": app });
            assert_eq!(
                call(
                    &s,
                    CREATE,
                    Some(&ta),
                    json!({ "spaceType": T, "skey": "bad", "policy": p })
                )
                .await,
                err_of("InvalidRequest"),
                "{app}"
            );
            assert_eq!(
                call(&s, UPDATE, Some(&ta), json!({ "space": g, "policy": p })).await,
                err_of("InvalidRequest"),
                "{app}"
            );
        }
        // Replaced wholesale: a field not given takes its default, not its old value.
        let m2 = sp(&alice, "m2");
        put(&s, &ta, &m2, &bob, true, false).await;
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": m2, "policy": { "$type": MEMBER, "publicWrite": true } }),
        )
        .await
        .unwrap();
        let mut want = defaults.clone();
        want["publicWrite"] = json!(true);
        assert_eq!(policy_of(&s, &ta, &m2).await, want);
        // The same type keeps the member list; another drops it, and coming back does not restore it.
        assert_eq!(
            members(&s, &ta, &m2).await,
            json!([{ "did": bob, "read": true, "write": false }])
        );
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": m2, "policy": { "$type": PERSONAL } }),
        )
        .await
        .unwrap();
        assert_eq!(policy_of(&s, &ta, &m2).await, json!({ "$type": PERSONAL }));
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": m2, "policy": { "$type": MEMBER } }),
        )
        .await
        .unwrap();
        assert_eq!(members(&s, &ta, &m2).await, json!([]));
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// putMembers and removeMembers in batches, and listMembers' pages.
    #[tokio::test]
    async fn the_member_list() {
        let plc = plc().await;
        let dir = scratch("spaces-members");
        let s = open(&dir, &plc);
        let (alice, ta) = create(&s, "alice").await;
        let m = create_space(&s, &ta, "m", json!({ "$type": MEMBER })).await;
        let dids = [
            "did:web:localhost%3A2583",
            "did:plc:bbbb",
            "did:plc:aaaa",
            "did:web:example.com",
            "did:plc:cccc",
        ];
        // In batches, and one at a time.
        let given: Vec<(&str, bool, bool)> = dids
            .iter()
            .enumerate()
            .map(|(i, did)| (*did, i % 2 == 0, i % 3 == 0))
            .collect();
        put_all(&s, &ta, &m, &given[..3]).await;
        for (did, read, write) in &given[3..] {
            put(&s, &ta, &m, did, *read, *write).await;
        }
        // Replacing both booleans, a DID given twice taking its last entry;
        // the authority may be a member; an empty batch changes nothing.
        put_all(
            &s,
            &ta,
            &m,
            &[
                ("did:plc:bbbb", false, false),
                (alice.as_str(), false, false),
                ("did:plc:bbbb", true, true),
            ],
        )
        .await;
        put_all(&s, &ta, &m, &[]).await;
        let mut all: Vec<&str> = dids.to_vec();
        all.push(&alice);
        all.sort();
        let listed = members(&s, &ta, &m).await;
        assert_eq!(
            listed
                .as_array()
                .unwrap()
                .iter()
                .map(|x| x["did"].as_str().unwrap())
                .collect::<Vec<_>>(),
            all
        );
        assert_eq!(
            listed[all.iter().position(|d| *d == "did:plc:bbbb").unwrap()],
            json!({ "did": "did:plc:bbbb", "read": true, "write": true })
        );
        // Pages: the cursor is the last member's DID, on the last page too, and absent from an empty one.
        let page = |limit: &'static str, cursor: Option<String>| {
            let s = s.clone();
            let (ta, m) = (ta.clone(), m.clone());
            async move {
                let mut p = vec![("space", m.as_str()), ("limit", limit)];
                if let Some(c) = &cursor {
                    p.push(("cursor", c.as_str()));
                }
                query(&s, LIST, Some(&ta), &p).await
            }
        };
        let mut cursor = None;
        let mut seen = Vec::new();
        loop {
            let got = page("4", cursor.clone()).await.unwrap();
            let dids: Vec<String> = got["members"]
                .as_array()
                .unwrap()
                .iter()
                .map(|x| x["did"].as_str().unwrap().to_string())
                .collect();
            if dids.is_empty() {
                assert!(got.get("cursor").is_none(), "{got}");
                break;
            }
            assert_eq!(got["cursor"], json!(dids.last().unwrap()));
            seen.extend(dids);
            cursor = got["cursor"].as_str().map(str::to_string);
        }
        assert_eq!(seen, all);
        assert_eq!(
            page("100", Some("did:zzz:z".into())).await.unwrap(),
            json!({ "members": [] })
        );
        assert_eq!(
            page("100", Some("did:a:a".into())).await.unwrap()["members"],
            listed
        );
        // An empty cursor is as none; any other must be a DID.
        assert_eq!(
            page("100", Some(String::new())).await.unwrap()["members"],
            listed
        );
        for bad in ["zzzz", "a", "did:plc:", "did:PLC:abc"] {
            assert_eq!(
                page("100", Some(bad.into())).await,
                err_of("InvalidCursor"),
                "{bad}"
            );
        }
        assert_eq!(page("1000", None).await.unwrap()["members"], listed);
        assert_eq!(
            query(&s, LIST, Some(&ta), &[("space", &m)]).await.unwrap()["members"],
            listed
        );
        for bad in ["0", "1001", "x"] {
            assert_eq!(page(bad, None).await, err_of("InvalidRequest"), "{bad}");
        }
        // removeMembers: a listed DID given twice, and one not listed.
        remove(
            &s,
            &ta,
            &m,
            &["did:plc:aaaa", "did:plc:never", "did:plc:aaaa"],
        )
        .await;
        remove(&s, &ta, &m, &[]).await;
        all.retain(|d| *d != "did:plc:aaaa");
        let listed = members(&s, &ta, &m).await;
        assert_eq!(
            listed
                .as_array()
                .unwrap()
                .iter()
                .map(|x| x["did"].as_str().unwrap())
                .collect::<Vec<_>>(),
            all
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// What this part changes of the writer set, and what it leaves.
    #[tokio::test]
    async fn the_writer_set() {
        let plc = plc().await;
        let dir = scratch("spaces-writers");
        let s = open(&dir, &plc);
        let (alice, ta) = create(&s, "alice").await;
        let (bob, _) = create(&s, "bob").await;
        let (carol, dave) = ("did:plc:carol", "did:plc:dave");
        let m = create_space(
            &s,
            &ta,
            "m",
            json!({ "$type": MEMBER, "publicWrite": true }),
        )
        .await;
        for (i, did) in [&alice, &bob, carol, dave].into_iter().enumerate() {
            assert!(track(&s, &alice, &m, did, i as u8));
        }
        let mut want = vec![
            alice.clone(),
            bob.clone(),
            carol.to_string(),
            dave.to_string(),
        ];
        want.sort();
        assert_eq!(writers(&s, &alice, &m), want);
        // removeWriter: in it or not, the authority's own DID too.
        for did in [&bob, &alice, &"did:plc:never".to_string()] {
            assert_eq!(
                call(
                    &s,
                    REMOVE_WRITER,
                    Some(&ta),
                    json!({ "space": m, "repo": did })
                )
                .await,
                Ok(Json::Null)
            );
        }
        want.retain(|d| *d != bob && *d != alice);
        assert_eq!(writers(&s, &alice, &m), want);
        // Tracked again by its next write.
        assert!(track(&s, &alice, &m, &bob, 9));
        // putMembers with write: true leaves it; with write: false takes it
        // out; removeMembers takes out a member, and leaves a DID not listed.
        put(&s, &ta, &m, &bob, true, true).await;
        assert!(writers(&s, &alice, &m).contains(&bob));
        put(&s, &ta, &m, &bob, true, false).await;
        assert!(!writers(&s, &alice, &m).contains(&bob));
        put(&s, &ta, &m, carol, true, true).await;
        remove(&s, &ta, &m, &[carol, dave]).await;
        assert_eq!(writers(&s, &alice, &m), [dave]);
        call(
            &s,
            REMOVE_WRITER,
            Some(&ta),
            json!({ "space": m, "repo": dave }),
        )
        .await
        .unwrap();
        assert!(writers(&s, &alice, &m).is_empty());
        // updateSpace leaves it, a migration too.
        assert!(track(&s, &alice, &m, carol, 3));
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": m, "policy": { "$type": PERSONAL } }),
        )
        .await
        .unwrap();
        assert_eq!(writers(&s, &alice, &m), [carol]);
        // deleteSpace empties it; created again, it is empty.
        call(&s, DELETE, Some(&ta), json!({ "space": m }))
            .await
            .unwrap();
        create_space(&s, &ta, "m", json!({ "$type": PERSONAL })).await;
        assert!(writers(&s, &alice, &m).is_empty());
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// An operation of a commit, as `listRepoOps` lists it: its path, and
    /// the CID of the record it left (None: it deleted one).
    type Op = (String, Option<String>);

    fn put_op(collection: &str, rkey: &str, record: Json) -> Op {
        let cid = pds_repo::make_record(data::from_json(&record).unwrap()).cid;
        (format!("{collection}/{rkey}"), Some(data::cid_string(&cid)))
    }

    fn del_op(collection: &str, rkey: &str) -> Op {
        (format!("{collection}/{rkey}"), None)
    }

    /// The records each change writes and deletes in the authority's space
    /// repo, each change one commit, which `on_commit` hears of.
    #[tokio::test]
    async fn publishing() {
        let plc = plc().await;
        let dir = scratch("spaces-publishing");
        let s = open(&dir, &plc);
        let (alice, ta) = create(&s, "alice").await;
        let (bob, _) = create(&s, "bob").await;
        let heard = Arc::new(std::sync::Mutex::new(Vec::<Commit>::new()));
        let h = heard.clone();
        s.space_hooks
            .on_commit(Box::new(move |_, c| h.lock().unwrap().push(c.clone())));
        // Each commit heard since last asked: its space, and its operations.
        let commits = || {
            let heard = std::mem::take(&mut *heard.lock().unwrap());
            let (s, ta, alice) = (s.clone(), ta.clone(), alice.clone());
            async move {
                let mut out = Vec::new();
                for c in heard {
                    assert_eq!(c.repo, alice);
                    let got = query(
                        &s,
                        "com.atproto.space.listRepoOps",
                        Some(&ta),
                        &[("space", &c.space), ("repo", &alice), ("limit", "1000")],
                    )
                    .await
                    .unwrap();
                    let ops: Vec<Op> = got["ops"]
                        .as_array()
                        .unwrap()
                        .iter()
                        .filter(|o| o["rev"] == json!(c.rev))
                        .map(|o| {
                            (
                                format!(
                                    "{}/{}",
                                    o["collection"].as_str().unwrap(),
                                    o["rkey"].as_str().unwrap()
                                ),
                                o["cid"].as_str().map(str::to_string),
                            )
                        })
                        .collect();
                    out.push((c.space, ops));
                }
                out
            }
        };
        let records = |space: &str| {
            let a = s.account(&alice).unwrap();
            let st = crate::read(&a.store);
            st.space_values(space)
        };
        let web = "did:web:localhost%3A2583";
        let record = |read: bool, write: bool| json!({ "$type": MEMBER_RECORD, "read": read, "write": write });
        let policy = |p: Json| json!({ "$type": POLICY_RECORD, "policy": p });
        // createSpace writes the policy record if it publishes it; nothing otherwise.
        let quiet = create_space(&s, &ta, "quiet", json!({ "$type": MEMBER })).await;
        put(&s, &ta, &quiet, &bob, true, true).await;
        remove(&s, &ta, &quiet, &[bob.as_str()]).await;
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": quiet, "policy": { "$type": PERSONAL } }),
        )
        .await
        .unwrap();
        assert_eq!(commits().await, []);
        let p = json!({ "$type": MEMBER, "publishPolicy": true, "publishMembers": true });
        let m = create_space(&s, &ta, "m", p.clone()).await;
        let stored = policy_of(&s, &ta, &m).await;
        assert_eq!(
            commits().await,
            [(
                m.clone(),
                vec![put_op(POLICY_RECORD, "self", policy(stored.clone()))]
            )]
        );
        // putMembers writes each given DID's record, once however often it is
        // given, in one commit, even one already so; keyed with a did:web's
        // port after a `:`. An empty batch makes no commit.
        put(&s, &ta, &m, &bob, true, false).await;
        put_all(
            &s,
            &ta,
            &m,
            &[
                (bob.as_str(), false, false),
                (web, false, true),
                (bob.as_str(), true, false),
            ],
        )
        .await;
        put_all(&s, &ta, &m, &[]).await;
        let mut batch = vec![
            put_op(MEMBER_RECORD, &bob, record(true, false)),
            put_op(MEMBER_RECORD, "did:web:localhost:2583", record(false, true)),
        ];
        batch.sort();
        assert_eq!(
            commits().await,
            [
                (
                    m.clone(),
                    vec![put_op(MEMBER_RECORD, &bob, record(true, false))]
                ),
                (m.clone(), batch)
            ]
        );
        // removeMembers deletes each removed member's record, in one commit;
        // one that removes no member makes none.
        remove(&s, &ta, &m, &[web, "did:plc:never", web]).await;
        remove(&s, &ta, &m, &["did:plc:never"]).await;
        assert_eq!(
            commits().await,
            [(
                m.clone(),
                vec![del_op(MEMBER_RECORD, "did:web:localhost:2583")]
            )]
        );
        // updateSpace: the policy record, a new version even unchanged.
        call(&s, UPDATE, Some(&ta), json!({ "space": m, "policy": p }))
            .await
            .unwrap();
        assert_eq!(
            commits().await,
            [(
                m.clone(),
                vec![put_op(POLICY_RECORD, "self", policy(stored.clone()))]
            )]
        );
        // Members no longer published: each record deleted, in the same commit as the policy.
        put(&s, &ta, &m, web, true, true).await;
        commits().await;
        let p2 = json!({ "$type": MEMBER, "publishPolicy": true });
        call(&s, UPDATE, Some(&ta), json!({ "space": m, "policy": p2 }))
            .await
            .unwrap();
        let stored2 = policy_of(&s, &ta, &m).await;
        let mut rkeys = [bob.clone(), "did:web:localhost:2583".to_string()];
        rkeys.sort();
        let mut want = vec![put_op(POLICY_RECORD, "self", policy(stored2))];
        want.extend(rkeys.iter().map(|k| del_op(MEMBER_RECORD, k)));
        assert_eq!(commits().await, [(m.clone(), want)]);
        // ... and published again: a record for each member.
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": m, "policy": { "$type": MEMBER, "publishMembers": true } }),
        )
        .await
        .unwrap();
        let mut want = vec![del_op(POLICY_RECORD, "self")];
        want.push(put_op(MEMBER_RECORD, &bob, record(true, false)));
        want.push(put_op(
            MEMBER_RECORD,
            "did:web:localhost:2583",
            record(true, true),
        ));
        assert_eq!(commits().await, [(m.clone(), want)]);
        // An update that writes and deletes nothing makes no commit.
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": m, "policy": { "$type": MEMBER, "publishMembers": true } }),
        )
        .await
        .unwrap();
        assert_eq!(commits().await, []);
        // A migration away from memberspace deletes the members' records.
        let managed = json!({ "$type": MANAGED, "managingApp": "did:plc:appappappappappappappapp#forum", "publishPolicy": true });
        call(
            &s,
            UPDATE,
            Some(&ta),
            json!({ "space": m, "policy": managed }),
        )
        .await
        .unwrap();
        let stored3 = policy_of(&s, &ta, &m).await;
        let want = vec![
            put_op(POLICY_RECORD, "self", policy(stored3.clone())),
            del_op(MEMBER_RECORD, &bob),
            del_op(MEMBER_RECORD, "did:web:localhost:2583"),
        ];
        assert_eq!(commits().await, [(m.clone(), want)]);
        assert_eq!(
            records(&m),
            Some(BTreeMap::from([(
                format!("{POLICY_RECORD}/self"),
                policy(stored3)
            )]))
        );
        // importSpaceMeta writes nothing; deleteSpace drops the repo, and so it stays after a restart.
        let archive = export(&s, &ta, &m).await.unwrap();
        import(&s, &ta, &archive).await.unwrap();
        assert_eq!(commits().await, []);
        call(&s, DELETE, Some(&ta), json!({ "space": m }))
            .await
            .unwrap();
        assert_eq!(commits().await, []);
        assert_eq!(records(&m), None);
        drop(s);
        let s = open(&dir, &plc);
        let a = s.account(&alice).unwrap();
        assert!(crate::read(&a.store).spaces.get(&m).is_none());
        assert!(crate::read(&a.store).spaces.get(&quiet).is_none());
        let _ = std::fs::remove_dir_all(&dir);
    }

    fn v(j: Json) -> V {
        data::from_json(&j).unwrap()
    }

    fn star_of(meta: Json, pairs: &[(&str, V)]) -> Vec<u8> {
        let pairs: Vec<(Vec<u8>, Vec<u8>)> = pairs
            .iter()
            .map(|(k, x)| (k.as_bytes().to_vec(), data::encode(x)))
            .collect();
        star::write(
            &v(meta),
            pairs.iter().map(|(k, x)| (k.as_slice(), x.as_slice())),
        )
    }

    fn writer_v(rev: &str, bytes: &[u8], space_rev: &str) -> V {
        V::Map(vec![
            ("data".into(), V::Bytes(bytes.to_vec())),
            ("repoRev".into(), V::Str(rev.into())),
            ("spaceRev".into(), V::Str(space_rev.into())),
        ])
    }

    /// exportSpaceMeta's archive, read as the spec writes it; importSpaceMeta
    /// of it answered back byte for byte; and each fault refused, in order.
    #[tokio::test]
    async fn meta_archives() {
        let plc = plc().await;
        let dir = scratch("spaces-archives");
        let s = open(&dir, &plc);
        let (alice, ta) = create(&s, "alice").await;
        let (bob, tb) = create(&s, "bob").await;
        let m = create_space(
            &s,
            &ta,
            "m",
            json!({ "$type": MEMBER, "allowedApps": ["https://a.example/c"] }),
        )
        .await;
        let web = "did:web:localhost%3A2583";
        put(&s, &ta, &m, &bob, true, false).await;
        put(&s, &ta, &m, web, false, true).await;
        put(&s, &ta, &m, "did:web:localhost", true, true).await;
        assert!(track(&s, &alice, &m, &bob, 7) && track(&s, &alice, &m, "did:plc:zed", 8));
        let bytes = export(&s, &ta, &m).await.unwrap();
        let a = star::read(&bytes, &star::REPO_LIMITS).unwrap();
        assert_eq!(
            data::to_json(&a.meta),
            json!({ "$type": "atproto-space-meta", "ver": 1, "space": m })
        );
        let keys: Vec<String> = a
            .pairs
            .iter()
            .map(|(k, _)| String::from_utf8(k.to_vec()).unwrap())
            .collect();
        let mut member_keys = vec![
            format!("{MEMBER_RECORD}/{bob}"),
            format!("{MEMBER_RECORD}/did:web:localhost:2583"),
            format!("{MEMBER_RECORD}/did:web:localhost"),
        ];
        member_keys.sort();
        let mut want = vec!["policy".to_string()];
        want.extend(member_keys);
        let mut writer_keys = vec![format!("writer/{bob}"), "writer/did:plc:zed".to_string()];
        writer_keys.sort();
        want.extend(writer_keys);
        assert_eq!(keys, want);
        let value = |k: &str| {
            data::to_json(
                &data::decode(a.pairs[keys.iter().position(|x| x == k).unwrap()].1).unwrap(),
            )
        };
        assert_eq!(value("policy"), policy_of(&s, &ta, &m).await);
        assert_eq!(
            value(&format!("{MEMBER_RECORD}/did:web:localhost:2583")),
            json!({ "$type": MEMBER_RECORD, "read": false, "write": true })
        );
        assert_eq!(
            data::decode(
                a.pairs[keys
                    .iter()
                    .position(|x| *x == format!("writer/{bob}"))
                    .unwrap()]
                .1
            )
            .unwrap(),
            writer_v(TID, &[7; 32], TID2)
        );
        // A personalspace's archive: its policy and writers alone.
        let p = create_space(&s, &ta, "p", json!({ "$type": PERSONAL })).await;
        let pa = export(&s, &ta, &p).await.unwrap();
        assert_eq!(
            pa,
            star_of(
                json!({ "$type": "atproto-space-meta", "ver": 1, "space": p }),
                &[("policy", v(json!({ "$type": PERSONAL })))]
            )
        );
        // Imported, deleted or not, it is the space again, exported byte for byte.
        call(&s, DELETE, Some(&ta), json!({ "space": m }))
            .await
            .unwrap();
        import(&s, &ta, &bytes).await.unwrap();
        assert_eq!(export(&s, &ta, &m).await.unwrap(), bytes);
        let mut want_writers = [bob.clone(), "did:plc:zed".to_string()];
        want_writers.sort();
        assert_eq!(writers(&s, &alice, &m), want_writers);
        // It replaces whatever the server holds, all at once.
        put(&s, &ta, &m, "did:plc:extra", true, true).await;
        assert!(track(&s, &alice, &m, "did:plc:extra", 1));
        import(&s, &ta, &bytes).await.unwrap();
        assert_eq!(export(&s, &ta, &m).await.unwrap(), bytes);
        // A space never created here, with fields to drop.
        let fresh = sp(&alice, "fresh");
        let meta =
            json!({ "$type": "atproto-space-meta", "ver": 1, "space": fresh, "other": "ignored" });
        let member =
            |read: bool| v(json!({ "$type": MEMBER_RECORD, "read": read, "write": false, "x": 1 }));
        let given = star_of(
            meta.clone(),
            &[
                (
                    "policy",
                    v(json!({ "$type": MEMBER, "publicRead": true, "junk": 1 })),
                ),
                (
                    &format!("{MEMBER_RECORD}/did:web:localhost:2583"),
                    member(true),
                ),
                (
                    "writer/did:plc:w",
                    V::Map(vec![
                        ("repoRev".into(), V::Str(TID.into())),
                        ("data".into(), V::Bytes(vec![1, 2])),
                        ("spaceRev".into(), V::Str(TID2.into())),
                        ("more".into(), V::Int(1)),
                    ]),
                ),
            ],
        );
        import(&s, &ta, &given).await.unwrap();
        assert_eq!(
            policy_of(&s, &ta, &fresh).await,
            json!({ "$type": MEMBER, "publicRead": true, "publicWrite": false, "publishPolicy": false, "publishMembers": false })
        );
        assert_eq!(
            members(&s, &ta, &fresh).await,
            json!([{ "did": web, "read": true, "write": false }])
        );
        assert_eq!(writers(&s, &alice, &fresh), ["did:plc:w"]);
        // The faults, each refused, and each leaving nothing.
        let target = sp(&alice, "refused");
        let meta = json!({ "$type": "atproto-space-meta", "ver": 1, "space": target });
        let ms = v(json!({ "$type": MEMBER }));
        let w = writer_v(TID, &[0; 32], TID2);
        let mk = |did: &str| format!("{MEMBER_RECORD}/{did}");
        let ok_member = v(json!({ "$type": MEMBER_RECORD, "read": true, "write": true }));
        let with_meta = |m: Json| star_of(m, &[("policy", ms.clone())]);
        let mut bad_star = star_of(meta.clone(), &[("policy", ms.clone())]);
        bad_star.push(0);
        let mut long_key = String::from("writer/did:plc:");
        long_key.push_str(&"a".repeat(4096));
        for (why, archive, want) in [
            ("no bytes", Vec::new(), "InvalidSpaceMeta"),
            ("no magic", b"not an archive".to_vec(), "InvalidSpaceMeta"),
            ("bytes after its end", bad_star, "InvalidSpaceMeta"),
            (
                "a key past the limit",
                star_of(
                    meta.clone(),
                    &[("policy", ms.clone()), (&long_key, w.clone())],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "another $type",
                with_meta(json!({ "$type": "atproto-space-repo", "ver": 1, "space": target })),
                "InvalidSpaceMeta",
            ),
            (
                "ver 2",
                with_meta(json!({ "$type": "atproto-space-meta", "ver": 2, "space": target })),
                "InvalidSpaceMeta",
            ),
            (
                "ver \"1\"",
                with_meta(json!({ "$type": "atproto-space-meta", "ver": "1", "space": target })),
                "InvalidSpaceMeta",
            ),
            (
                "no space",
                with_meta(json!({ "$type": "atproto-space-meta", "ver": 1 })),
                "InvalidSpaceMeta",
            ),
            (
                "a space that is no space-ref",
                with_meta(
                    json!({ "$type": "atproto-space-meta", "ver": 1, "space": format!("at://{alice}/space/{T}") }),
                ),
                "InvalidSpaceMeta",
            ),
            ("no pairs", star_of(meta.clone(), &[]), "InvalidSpaceMeta"),
            (
                "no policy",
                star_of(meta.clone(), &[(&mk(&bob), ok_member.clone())]),
                "InvalidSpaceMeta",
            ),
            (
                "the policy not first",
                star_of(
                    meta.clone(),
                    &[(&mk(&bob), ok_member.clone()), ("policy", ms.clone())],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "the policy twice",
                star_of(
                    meta.clone(),
                    &[("policy", ms.clone()), ("policy", ms.clone())],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "state after the writers",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        ("writer/did:plc:a", w.clone()),
                        (&mk(&bob), ok_member.clone()),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "members out of order",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        (&mk("did:plc:b"), ok_member.clone()),
                        (&mk("did:plc:a"), ok_member.clone()),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a member repeated",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        (&mk("did:plc:a"), ok_member.clone()),
                        (&mk("did:plc:a"), ok_member.clone()),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "writers out of order",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        ("writer/did:plc:b", w.clone()),
                        ("writer/did:plc:a", w.clone()),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a writer that is no DID",
                star_of(
                    meta.clone(),
                    &[("policy", ms.clone()), ("writer/bob", w.clone())],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a writer with no data",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        (
                            "writer/did:plc:a",
                            v(json!({ "repoRev": TID, "spaceRev": TID2 })),
                        ),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a writer whose rev is no TID",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        ("writer/did:plc:a", writer_v("x", &[0; 32], TID2)),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a writer that is not DRISL",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        ("writer/did:plc:a", V::Str("x".into())),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a writer whose repoRev is named rev",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        (
                            "writer/did:plc:a",
                            V::Map(vec![
                                ("rev".into(), V::Str(TID.into())),
                                ("data".into(), V::Bytes(vec![0; 32])),
                                ("spaceRev".into(), V::Str(TID2.into())),
                            ]),
                        ),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a policy that is no map",
                star_of(meta.clone(), &[("policy", v(json!([1])))]),
                "InvalidSpaceMeta",
            ),
            (
                "a policy that is no DRISL",
                {
                    let m = v(meta.clone());
                    star::write(
                        &m,
                        [(b"policy".as_slice(), [0xffu8].as_slice())].into_iter(),
                    )
                },
                "InvalidSpaceMeta",
            ),
            (
                "policy state that is no map, under an unsupported policy",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", v(json!({ "$type": "com.example.policy" }))),
                        (&mk("bob"), V::Int(1)),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "another's space, and no policy",
                star_of(
                    json!({ "$type": "atproto-space-meta", "ver": 1, "space": sp(&bob, "x") }),
                    &[("writer/did:plc:a", w.clone())],
                ),
                "InvalidSpaceMeta",
            ),
            // Ownership, after the archive's shape, before its policy.
            (
                "another's space",
                star_of(
                    json!({ "$type": "atproto-space-meta", "ver": 1, "space": sp(&bob, "x") }),
                    &[("policy", v(json!({ "$type": "com.example.policy" })))],
                ),
                "NotAuthorized",
            ),
            (
                "an unsupported policy",
                star_of(
                    meta.clone(),
                    &[("policy", v(json!({ "$type": "com.example.policy" })))],
                ),
                "UnsupportedPolicy",
            ),
            (
                "a policy its lexicon refuses",
                star_of(
                    meta.clone(),
                    &[("policy", v(json!({ "$type": MEMBER, "publicRead": 1 })))],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a policy with no $type",
                star_of(
                    meta.clone(),
                    &[("policy", v(json!({ "publicRead": true })))],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a bad managingApp",
                star_of(
                    meta.clone(),
                    &[(
                        "policy",
                        v(json!({ "$type": MANAGED, "managingApp": "app" })),
                    )],
                ),
                "InvalidRequest",
            ),
            (
                "a managingApp with no fragment, and a member",
                star_of(
                    meta.clone(),
                    &[
                        (
                            "policy",
                            v(
                                json!({ "$type": MANAGED, "managingApp": "did:plc:appappappappappappappapp" }),
                            ),
                        ),
                        (&mk(&bob), ok_member.clone()),
                    ],
                ),
                "InvalidRequest",
            ),
            (
                "an unsupported policy, and a member keyed by no DID",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", v(json!({ "$type": "com.example.policy" }))),
                        (&mk("bob"), v(json!({ "a": 1 }))),
                    ],
                ),
                "UnsupportedPolicy",
            ),
            // The policy state, last.
            (
                "members of a personalspace",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", v(json!({ "$type": PERSONAL }))),
                        (&mk(&bob), ok_member.clone()),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "state it does not know",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        ("com.example.state/x", ok_member.clone()),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a member keyed by no DID",
                star_of(
                    meta.clone(),
                    &[("policy", ms.clone()), (&mk("bob"), ok_member.clone())],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a member keyed with a %3A",
                star_of(
                    meta.clone(),
                    &[("policy", ms.clone()), (&mk(web), ok_member.clone())],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a member with no $type",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        (&mk(&bob), v(json!({ "read": true, "write": true }))),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
            (
                "a member with no write",
                star_of(
                    meta.clone(),
                    &[
                        ("policy", ms.clone()),
                        (
                            &mk(&bob),
                            v(json!({ "$type": MEMBER_RECORD, "read": true })),
                        ),
                    ],
                ),
                "InvalidSpaceMeta",
            ),
        ] {
            assert_eq!(import(&s, &ta, &archive).await, err_of(want), "{why}");
        }
        assert_eq!(
            query(&s, GET, Some(&ta), &[("space", &target)]).await,
            err_of("SpaceNotFound")
        );
        // The token first, then the body's type; an app-password session may not import.
        let good = star_of(meta.clone(), &[("policy", ms.clone())]);
        assert_eq!(
            import_as(&s, None, Some("application/json"), b"{}").await,
            err_of("AuthMissing")
        );
        assert_eq!(
            import_as(&s, Some("abc.def.ghi"), Some(crate::STAR_TYPE), &good).await,
            err_of("InvalidToken")
        );
        let app = app_password_session(&s, "alice", &ta).await;
        assert_eq!(import(&s, &app, &good).await, err_of("InvalidToken"));
        // Another Content-Type, or none, before the body is read: even an empty one.
        for ctype in [
            Some("application/json"),
            Some("application/octet-stream"),
            None,
        ] {
            assert_eq!(
                import_as(&s, Some(&ta), ctype, &good).await,
                err_of("InvalidRequest"),
                "{ctype:?}"
            );
            assert_eq!(
                import_as(&s, Some(&ta), ctype, b"").await,
                err_of("InvalidRequest"),
                "{ctype:?}"
            );
        }
        assert_eq!(import(&s, &tb, &good).await, err_of("NotAuthorized"));
        assert_eq!(
            query(&s, GET, Some(&ta), &[("space", &target)]).await,
            err_of("SpaceNotFound")
        );
        import_as(&s, Some(&ta), Some("Application/VND.dasl.STAR; x=y"), &good)
            .await
            .unwrap();
        assert_eq!(
            export(&s, &ta, &target).await.unwrap(),
            star_of(meta, &[("policy", v(policy_of(&s, &ta, &target).await))])
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// What takedown and deactivation answer, by method and session.
    #[test]
    fn takedown_and_deactivation() {
        use Kind::*;
        use Session::*;
        use Status::*;
        let name = |r: Result<(), XrpcError>| r.err().map(|e| (e.status, e.name));
        for kind in [Manage, Import, Read, Export] {
            assert_eq!(name(admit(kind, Password, Active)), None, "{kind:?}");
        }
        let deactivated = Some((401, "AccountDeactivated"));
        let taken_down = Some((401, "AccountTakedown"));
        let invalid = Some((400, "InvalidToken"));
        for (kind, session, status, want) in [
            (Manage, Password, Deactivated, deactivated),
            (Manage, AppPassword, Deactivated, deactivated),
            (Manage, Password, TakenDown, taken_down),
            (Import, Password, Deactivated, None),
            (Import, Password, TakenDown, taken_down),
            (Import, AppPassword, Active, invalid),
            (Read, Password, Deactivated, None),
            (Read, AppPassword, TakenDown, None),
            (Export, Password, Deactivated, None),
            (Export, Password, TakenDown, None),
            // A taken-down account's own session: exportSpaceMeta alone takes it.
            (Export, Takendown, TakenDown, None),
            (Read, Takendown, TakenDown, invalid),
            (Manage, Takendown, TakenDown, invalid),
            (Import, Takendown, TakenDown, invalid),
        ] {
            assert_eq!(
                name(admit(kind, session, status)),
                want,
                "{kind:?} {session:?} {status:?}"
            );
        }
    }

    #[test]
    fn member_record_keys() {
        for (did, rkey) in [
            ("did:web:localhost%3A2583", "did:web:localhost:2583"),
            ("did:plc:abc", "did:plc:abc"),
            ("did:web:example.com", "did:web:example.com"),
        ] {
            assert_eq!(member_rkey(did), rkey);
            assert_eq!(member_did(rkey).as_deref(), Some(did));
        }
        for bad in ["bob", "did:web:a%3A1", "did:web:", "did:plc:a b"] {
            assert_eq!(member_did(bad), None, "{bad}");
        }
    }

    /// A `managingApp`'s forms (`spec/SPACES-POLICIES.md`, "Policies").
    #[test]
    fn managing_app_forms() {
        let plc = "did:plc:abcdefghijklmnopqrstuvwx";
        for ok in [
            format!("{plc}#forum"),
            "did:web:app.example#svc".into(),
            "did:web:localhost%3A2583#x".into(),
            format!("{plc}#a/b?c:@!$&'()*+,;=-._~%41"),
        ] {
            assert!(service_ref(&ok), "{ok}");
        }
        for bad in [
            plc.to_string(),
            "did:".into(),
            "did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme#x".into(),
            "did:example:abc#x".into(),
            "did:plc:abc#x".into(),
            "did:plc:abcdefghijklmnopqrstuvw1#x".into(),
            "did:plc:ABCDEFGHIJKLMNOPQRSTUVWX#x".into(),
            format!("{plc}#"),
            format!("{plc}#a#b"),
            format!("{plc}#a b"),
            format!("{plc}#%4"),
            format!("{plc}#%4g"),
            "did:web:app.example:path#x".into(),
            "did:web:app.example%3A443#x".into(),
            "web:app.example#x".into(),
        ] {
            assert!(!service_ref(&bad), "{bad}");
        }
    }

    /// A checkpoint's spaces: each live one's archive, as `exportSpaceMeta`
    /// writes it with its writers taken out marked removed, restores the
    /// same. One a checkpoint could not have kept, a writer's repo rev as
    /// `rev` or a `managingApp` no `createSpace` takes, is refused.
    #[test]
    fn a_checkpoints_spaces_round_trip() {
        let did = "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa";
        let writer = "did:plc:bbbbbbbbbbbbbbbbbbbbbbbb";
        let (m, g) = (sp(did, "m"), sp(did, "g"));
        let meta = |uri: &str| json!({ "$type": META_TYPE, "ver": 1, "space": uri });
        let policy = v(
            json!({ "$type": MEMBER, "publicRead": false, "publicWrite": false, "publishPolicy": false, "publishMembers": false }),
        );
        let (member_key, writer_key) = (
            format!("{MEMBER_RECORD}/{writer}"),
            format!("{WRITER}{writer}"),
        );
        let member = v(json!({ "$type": MEMBER_RECORD, "read": true, "write": false }));
        let archive = |w: V| {
            star_of(
                meta(&m),
                &[
                    ("policy", policy.clone()),
                    (&member_key, member.clone()),
                    (&writer_key, w),
                ],
            )
        };
        let V::Map(mut removed) = writer_v(TID, &[3; 32], TID2) else {
            panic!("a writer that is no map")
        };
        removed.push(("removed".into(), V::Bool(true)));
        let kept = archive(V::Map(removed));
        let managed = |app: &str| {
            star_of(
                meta(&g),
                &[(
                    "policy",
                    v(json!({ "$type": MANAGED, "managingApp": app, "publishPolicy": false })),
                )],
            )
        };
        let mut a = Authority::default();
        a.restore(did, &m, Some(&kept)).unwrap();
        a.restore(did, &g, Some(&managed("did:web:app.example#x")))
            .unwrap();
        assert_eq!(
            a.live(&m).unwrap().writers.get(writer),
            Some(&Writer {
                rev: TID.into(),
                data: vec![3; 32],
                space_rev: TID2.into(),
                removed: true,
            })
        );
        let saved: BTreeMap<String, Option<Vec<u8>>> = a
            .saved()
            .map(|(uri, bytes)| (uri.to_string(), bytes))
            .collect();
        assert_eq!(saved[&m].as_deref(), Some(kept.as_slice()));
        let mut b = Authority::default();
        for (uri, bytes) in &saved {
            b.restore(did, uri, bytes.as_deref()).unwrap();
        }
        assert_eq!(b, a);
        let rev_named = archive(V::Map(vec![
            ("rev".into(), V::Str(TID.into())),
            ("data".into(), V::Bytes(vec![3; 32])),
            ("spaceRev".into(), V::Str(TID2.into())),
        ]));
        for (uri, bad) in [(&m, rev_named), (&g, managed("did:"))] {
            assert!(Authority::default().restore(did, uri, Some(&bad)).is_err());
        }
    }
}