~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
1757 lines · 103.4 KB Raw History
// Limits (spec/LIMITS.md): request caps, applyWrites' 200 writes, invite
// codes, what an app password's session may call, and createSession's time,
// each held to the reference in every run; codes required, in a run of
// their own (--invites); and the rate limits, held to the reference, then
// what the server holds, held to the spec, in another (--limits).

import { readFileSync, readdirSync, statSync } from 'node:fs'
import { request as httpRequest } from 'node:http'
import { connect } from 'node:net'
import { join } from 'node:path'
import { isDeepStrictEqual } from 'node:util'
import { ONLY } from '../lib/args.mjs'
import { ADMIN_PASSWORD, LIMITS_UPLOAD_LIMIT, REFERENCE_TRUSTED_PROXIES } from '../lib/env.mjs'
import { Browser, Dpop, form, fresh, newKey, pkce, signInCall } from '../lib/oauth.mjs'
import { assertion, b64u, newPasskey, registration } from '../lib/webauthn.mjs'
import { checkDidDoc } from '../lib/plc.mjs'
import { reach } from '../lib/front.mjs'
import { below, chance, goodRecord, pick, rand, subset } from '../lib/random.mjs'
import { cand, dataDir, metricsUrl, plcProxy, ref, server, wait } from '../lib/processes.mjs'
import { begin, end, now } from '../lib/observe.mjs'
import { count, differ } from '../lib/report.mjs'
import { takeMail } from '../lib/mail.mjs'
import { accounts, cids, didOf, nextBatch, noteCommit, noteKeys, tokenOf } from '../lib/state.mjs'
import { answerWith, ownHost, silentHost, takeRequests } from '../lib/services.mjs'
import { normalize, shown, xrpc } from '../lib/xrpc.mjs'
import { RESERVABLE, createAccount, freshFront, mixCase, newAccountInput, sendCreate } from './accounts.mjs'
import { describeServer } from './session.mjs'
import { OAUTH_LIMIT_CHECKS, OAUTH_SERVER_CHECKS, serverSession } from './oauth-limits.mjs'
import { addLink, addPasskey, checkList, checkPage, newDevice, optionsOn, origin, parOn, passkeySignIn, rpId, serverWho, signInOn } from './passkeys.mjs'

const CREATE_ACCOUNT = 'com.atproto.server.createAccount'
const SESSION = 'com.atproto.server.createSession'
const REFRESH = 'com.atproto.server.refreshSession'
const DELETE_SESSION = 'com.atproto.server.deleteSession'
const CREATE_AP = 'com.atproto.server.createAppPassword'
const REVOKE_AP = 'com.atproto.server.revokeAppPassword'
const CODE = 'com.atproto.server.createInviteCode'
const CODES = 'com.atproto.server.createInviteCodes'
const ACCOUNT_CODES = 'com.atproto.server.getAccountInviteCodes'
const UPDATE_HANDLE = 'com.atproto.identity.updateHandle'
const CREATE_RECORD = 'com.atproto.repo.createRecord'
const PUT_RECORD = 'com.atproto.repo.putRecord'
const DELETE_RECORD = 'com.atproto.repo.deleteRecord'
const APPLY = 'com.atproto.repo.applyWrites'
const UPLOAD = 'com.atproto.repo.uploadBlob'
const DESCRIBE_SERVER = 'com.atproto.server.describeServer'
/// A DID no server here holds.
const NOBODY = 'did:plc:aaaaaaaaaaaaaaaaaaaaaaaa'
const DATETIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/
/// An invite code of a server whose PDS_HOSTNAME is `localhost`.
const CODE_SHAPE = /^localhost-[a-z2-7]{5}-[a-z2-7]{5}$/
const sides = () => [ref, cand]
let serial = 0

const basic = (user, password) => `Basic ${Buffer.from(`${user}:${password}`).toString('base64')}`
const ADMIN = basic('admin', ADMIN_PASSWORD)

/// A client address of its own, as a proxy before the server would report
/// it in `X-Forwarded-For`, so that each check's limits start afresh.
let ipSerial = 0
const freshIp = () => {
  ipSerial += 1
  return `203.0.${Math.floor(ipSerial / 250)}.${(ipSerial % 250) + 1}`
}

/// One request to one side over HTTP, with what the other modules do not
/// send: any `Authorization`, an `X-Forwarded-For` (`ip`), a body given as
/// bytes or a string, chunked or not. Answers `{status, body, headers}`,
/// `headers` lowercased, and `ms`, how long it took.
export function send(side, nsid, { params, body, raw, token, auth, ip, headers = {}, method, chunked = false, contentType } = {}) {
  const url = new URL(`${side.url}/xrpc/${nsid}`)
  for (const [k, v] of Object.entries(params ?? {})) if (v !== undefined) url.searchParams.append(k, String(v))
  const h = { ...headers }
  const t = token && typeof token === 'object' ? tokenOf(side, token) : token
  if (t) h.authorization = `Bearer ${t}`
  if (auth) h.authorization = auth
  if (ip) h['x-forwarded-for'] = ip
  const bytes = raw !== undefined ? Buffer.from(raw) : body !== undefined ? Buffer.from(typeof body === 'string' ? body : JSON.stringify(body)) : null
  if (bytes) h['content-type'] = contentType ?? 'application/json'
  if (bytes && !chunked) h['content-length'] = String(bytes.length)
  const started = Date.now()
  const m = method ?? (bytes ? 'POST' : 'GET')
  // Each request to the server is noted, with its trace (spec/OPERATIONS.md).
  const rec = side === cand ? begin(m, url, h, bytes && raw === undefined ? bytes.toString('utf8') : undefined) : null
  return new Promise((ok) => {
    const req = httpRequest(url, { method: m, headers: h }, (res) => {
      const chunks = []
      res.on('data', (c) => chunks.push(c))
      res.on('end', () => {
        const text = Buffer.concat(chunks).toString('utf8')
        if (rec) end(rec, res.statusCode, text)
        let parsed = null
        try {
          parsed = text ? JSON.parse(text) : null
        } catch {
          parsed = { unparsed: text.slice(0, 200) }
        }
        ok({ status: res.statusCode, body: parsed, headers: res.headers, ms: Date.now() - started })
      })
      res.on('error', (err) => {
        if (rec) rec.ended = now()
        ok({ status: 0, body: { error: `request failed: ${err.message}` }, headers: {}, ms: Date.now() - started })
      })
    })
    req.on('error', (err) => {
      if (rec) rec.ended = now()
      ok({ status: 0, body: { error: `request failed: ${err.message}` }, headers: {}, ms: Date.now() - started })
    })
    if (bytes && chunked) for (let i = 0; i < bytes.length; i += 65536) req.write(bytes.subarray(i, i + 65536))
    else if (bytes) req.write(bytes)
    req.end()
  })
}

/// The same request to both sides at once; `request` may be a function of
/// the side.
const pair = (nsid, request) => Promise.all(sides().map((side) => send(side, nsid, typeof request === 'function' ? request(side) : request)))

/// Both answers compared as `normalize` takes them, with invite codes
/// named; answers both.
async function compared(kind, nsid, request, answers) {
  const [a, b] = answers
  const [na, nb] = [named(ref, normalize(ref, a, nsid)), named(cand, normalize(cand, b, nsid))]
  if (!isDeepStrictEqual(na, nb)) await differ(nsid, shown(request), na, nb, kind)
  count(kind, a.status < 400)
  return answers
}

const bothSent = async (kind, nsid, request) => compared(kind, nsid, request, await pair(nsid, request))

// ---------------------------------------------------------------- caps

/// applyWrites of 201 to 250 writes, alone or with a fault the count is
/// checked after (another account's repository, one no account has) or
/// before (a wrong swap, a wrong `$type`); or of exactly 200 creates, which
/// both make (spec/LIMITS.md, "Commits").
export async function bigBatch(acct) {
  const n = chance(0.15) ? 200 : 201 + below(50)
  const fault = n > 200 && chance(0.5) ? pick(['another account', 'no account', 'a wrong swap', 'a wrong $type']) : null
  const collection = 'com.example.bulk'
  const batch = ++serial
  const writes = Array.from({ length: n }, (_, i) => ({ $type: 'com.atproto.repo.applyWrites#create', collection, rkey: `bulk${batch}-${i}`, value: { ...goodRecord(collection), batch: nextBatch() } }))
  if (fault === 'a wrong $type') writes[below(n)].value.$type = 'com.example.note'
  const other = accounts.find((x) => x !== acct)
  const repo = (side) => (fault === 'another account' && other ? didOf(side, other) : fault === 'no account' ? NOBODY : acct.handle)
  const swap = fault === 'a wrong swap' && cids.length ? pick(cids) : undefined
  const request = (side) => ({ body: { repo: repo(side), writes, ...(swap ? { swapCommit: swap } : {}) }, token: acct })
  const [a, b] = await Promise.all(sides().map((side) => xrpc(side, APPLY, request(side))))
  const [na, nb] = [normalize(ref, a, APPLY), normalize(cand, b, APPLY)]
  const shownRequest = { writes: `${n} creates`, fault }
  if (!isDeepStrictEqual(na, nb)) await differ(APPLY, shownRequest, na, nb, 'applyWrites takes 200 writes at most')
  count(`applyWrites of ${n > 200 ? 'more than 200' : '200'} writes${fault ? `, ${fault}` : ''}`, a.status < 400)
  if (a.status === 200) {
    writes.forEach((w, i) => {
      const cid = a.body.results?.[i]?.cid
      if (cid) {
        acct.records.set(`${w.collection}/${w.rkey}`, cid)
        cids.push(cid)
      }
    })
    noteCommit(ref, acct, a)
    noteCommit(cand, acct, b)
  }
}

/// A body of exactly `size` bytes of JSON, from `make`, given a padding
/// string.
function sized(size, make) {
  const bare = Buffer.byteLength(JSON.stringify(make('')))
  const body = JSON.stringify(make('x'.repeat(size - bare)))
  if (Buffer.byteLength(body) !== size) throw new Error(`a body of ${size} bytes came out ${Buffer.byteLength(body)}`)
  return body
}

/// A method's JSON body at its cap or one byte over, sent with its length
/// or chunked (spec/LIMITS.md, "Request size"): `createRecord`, `putRecord`
/// and `applyWrites` take 1000000 bytes, the others 153600. A record made
/// at the cap is deleted again on both sides, and an app password revoked,
/// so the run carries none.
export async function bodyCaps(acct) {
  const [nsid, cap] = pick([[CREATE_RECORD, 1000000], [PUT_RECORD, 1000000], [APPLY, 1000000], [DELETE_RECORD, 153600], [CREATE_AP, 153600]])
  const over = chance(0.5)
  const size = cap + (over ? 1 : 0)
  const chunked = chance(0.5)
  const collection = 'com.example.big'
  const rkey = `big${++serial}`
  const record = (pad) => ({ $type: collection, pad })
  const make = {
    [CREATE_RECORD]: (pad) => ({ repo: acct.handle, collection, rkey, record: record(pad) }),
    [PUT_RECORD]: (pad) => ({ repo: acct.handle, collection, rkey, record: record(pad) }),
    [APPLY]: (pad) => ({ repo: acct.handle, writes: [{ $type: 'com.atproto.repo.applyWrites#create', collection, rkey, value: record(pad) }] }),
    [DELETE_RECORD]: (pad) => ({ repo: acct.handle, collection, rkey, pad }),
    [CREATE_AP]: (pad) => ({ name: `big-${rkey}-${pad}` }),
  }[nsid]
  const body = sized(size, make)
  const request = { body, token: acct, chunked }
  const answers = await pair(nsid, request)
  for (const [side, res] of sides().map((s, i) => [s, answers[i]])) noteKeys(side, nsid, JSON.parse(body), res)
  await compared(`${nsid.split('.').pop()} of ${over ? 'one byte over' : 'exactly'} its cap${chunked ? ', chunked' : ''}`, nsid, { body: `${size} bytes`, chunked }, answers.map(masked))
  if (answers[0].status !== 200) return
  if (nsid === CREATE_AP) return void (await bothSent('revokeAppPassword of a big name', REVOKE_AP, { body: { name: JSON.parse(body).name }, token: acct }))
  if (nsid === DELETE_RECORD) return
  answers.forEach((res, i) => noteCommit(sides()[i], acct, res))
  const del = { body: { repo: acct.handle, collection, rkey }, token: acct }
  const gone = await Promise.all(sides().map((side) => xrpc(side, DELETE_RECORD, del)))
  await compared('deleteRecord of a record at the cap', DELETE_RECORD, del, gone)
  gone.forEach((res, i) => noteCommit(sides()[i], acct, res))
}

// ---------------------------------------------------------- invite codes

/// Each side's invite codes, named by the order the harness first saw them.
const codeNames = { reference: new Map(), server: new Map() }
const codeName = (side, code) => {
  const names = codeNames[side.name]
  if (!names.has(code)) names.set(code, `<code:${names.size + 1}>`)
  return names.get(code)
}
/// Each code made, by name: when it was made, `[from, to]` in seconds, and
/// the account it is for, if one here.
const madeCodes = new Map()
/// The accounts some code was made for, which getAccountInviteCodes lists.
const holders = new Set()
const codeOf = (side, name) => [...codeNames[side.name]].find(([, n]) => n === name)?.[0]

/// `v` with each code the side made named, as the other modules name DIDs.
function named(side, v) {
  const names = codeNames[side.name]
  if (typeof v === 'string') return names.get(v) ?? v
  if (Array.isArray(v)) return v.map((x) => named(side, x))
  if (v && typeof v === 'object') return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, named(side, x)]))
  return v
}

/// Note the codes a side answered, in order, each of the form the spec
/// gives (spec/LIMITS.md, "The admin").
async function noteCodes(side, codes, window, forAccount, request) {
  for (const code of codes) {
    if (typeof code !== 'string' || !CODE_SHAPE.test(code)) await differ(`the ${side.name}'s invite code`, request, 'localhost-xxxxx-xxxxx, each x a lower case letter or 2 to 7', code, side === ref ? 'the reference fails the check it is held to' : undefined)
    const name = codeName(side, code)
    if (side === ref) madeCodes.set(name, { window, forAccount })
  }
}

const seconds = () => Date.now() / 1000

/// createInviteCode with the admin's auth, of 0 to 3 uses, for the
/// account, a DID no account has, or none (spec/LIMITS.md, "The admin").
async function createCode(acct) {
  const useCount = pick([0, 1, 1, 2, 3])
  const r = rand()
  const forAccount = (side) => (r < 0.4 ? didOf(side, acct) : r < 0.55 ? NOBODY : undefined)
  const request = (side) => ({ body: { useCount, ...(forAccount(side) ? { forAccount: forAccount(side) } : {}) }, auth: ADMIN })
  const t0 = seconds()
  const answers = await pair(CODE, request)
  const window = [t0, seconds()]
  for (const [side, res] of sides().map((s, i) => [s, answers[i]])) if (res.status === 200) await noteCodes(side, [res.body.code], window, r < 0.4 ? acct : null, shown(request))
  if (r < 0.4 && answers[0].status === 200) holders.add(acct)
  await compared('createInviteCode', CODE, request, answers)
}

/// createInviteCodes with the admin's auth, for some accounts and a DID no
/// account has, none, or an empty list (spec/LIMITS.md, "The admin").
async function createCodes() {
  const codeCount = below(4)
  const useCount = pick([0, 1, 2])
  const r = rand()
  const chosen = r < 0.6 ? subset([...accounts, null], 0.4) : null
  const forAccounts = (side) => (r < 0.6 ? chosen.map((a) => (a ? didOf(side, a) : NOBODY)) : undefined)
  const request = (side) => ({ body: { codeCount, useCount, ...(forAccounts(side) ? { forAccounts: forAccounts(side) } : {}) }, auth: ADMIN })
  const t0 = seconds()
  const answers = await pair(CODES, request)
  const window = [t0, seconds()]
  for (const [side, res] of sides().map((s, i) => [s, answers[i]])) {
    if (res.status !== 200) continue
    for (const [i, entry] of (res.body?.codes ?? []).entries()) await noteCodes(side, entry.codes ?? [], window, chosen?.[i] ?? null, shown(request))
  }
  if (answers[0].status === 200) for (const a of chosen ?? []) if (a && codeCount > 0) holders.add(a)
  await compared('createInviteCodes', CODES, request, answers)
}

/// The admin's methods refused: no auth, a wrong password or user, a Basic
/// value that does not decode, a Bearer token, input the lexicon refuses
/// with the right auth or the wrong, and the other HTTP method
/// (spec/LIMITS.md, "The admin").
async function adminRefused(acct) {
  const nsid = pick([CODE, CODES])
  const good = nsid === CODE ? { useCount: 1 } : { codeCount: 1, useCount: 1 }
  const bad = pick([{}, { useCount: 'one' }, { codeCount: 1 }, { useCount: 1.5 }])
  const [kind, request] = pick([
    ['no auth', { body: good }],
    ['a wrong password', { body: good, auth: basic('admin', `${ADMIN_PASSWORD}x`) }],
    ['another user', { body: good, auth: basic(pick(['root', 'Admin', '']), ADMIN_PASSWORD) }],
    ['a Basic value that does not decode', { body: good, auth: 'Basic !!!' }],
    ['an account\'s token', { body: good, token: acct }],
    ['input the lexicon refuses', { body: bad, auth: ADMIN }],
    ['wrong auth and input', { body: bad, auth: basic('admin', 'nope') }],
    ['GET', { auth: ADMIN, method: 'GET' }],
  ])
  await bothSent(`${nsid.split('.').pop()} refused: ${kind}`, nsid, request)
}

/// createAccount with a code: one made, in any state, in upper case, one
/// never made, or an empty one (spec/LIMITS.md, "createAccount with a
/// code").
async function createWithCode() {
  const names = [...madeCodes.keys()]
  const name = names.length ? pick(names) : null
  const never = `localhost-${pick(['aaaaa', 'bcdef'])}-${pick(['22222', 'zzzzz'])}`
  const r = rand()
  const [kind, codeOn] =
    r < 0.6 && name ? ['a code made', (side) => codeOf(side, name)]
    : r < 0.7 && name ? ['a code made, in upper case', (side) => codeOf(side, name).toUpperCase()]
    : r < 0.85 ? ['a code never made', () => never]
    : ['an empty code', () => '']
  const input = newAccountInput()
  await sendCreate(`createAccount with ${kind}`, (side) => ({ ...input, inviteCode: codeOn(side) }))
}

/// getAccountInviteCodes, its codes named and sorted, their times held to
/// when each was made and then taken out: with the account's token, an app
/// password's session's, a refresh token, the admin's auth, or none
/// (spec/LIMITS.md, "getAccountInviteCodes").
async function accountCodes(acct, kind = 'getAccountInviteCodes') {
  const r = rand()
  const params = { includeUsed: pick([undefined, true, false]), createAvailable: pick([undefined, true, false]) }
  const [how, request] =
    r < 0.7 ? ['', { params, token: acct }]
    : r < 0.8 ? [', no token', { params }]
    : r < 0.9 ? [', a refresh token', (side) => ({ params, token: acct.sides[side.name].refresh })]
    : [', the admin\'s auth', { params, auth: ADMIN }]
  const answers = await pair(ACCOUNT_CODES, request)
  for (const [side, res] of sides().map((s, i) => [s, answers[i]])) {
    if (res.status !== 200) continue
    for (const c of res.body?.codes ?? []) {
      const made = madeCodes.get(codeNames[side.name].get(c.code))
      const at = Date.parse(c.createdAt) / 1000
      if (!DATETIME.test(c.createdAt ?? '') || (made && !(at >= made.window[0] - 1 && at <= made.window[1] + 1)))
        await differ(`the ${side.name}'s ${ACCOUNT_CODES}`, shown(request), `createdAt, a datetime within a second of when the code was made`, c, side === ref ? 'the reference fails the check it is held to' : undefined)
      for (const u of c.uses ?? []) if (!DATETIME.test(u.usedAt ?? '') || Date.parse(u.usedAt) < Date.parse(c.createdAt) - 1000) await differ(`the ${side.name}'s ${ACCOUNT_CODES}`, shown(request), 'usedAt, a datetime after the code was made', u)
      c.createdAt = '<time>'
      for (const u of c.uses ?? []) u.usedAt = '<time>'
    }
    res.body?.codes?.sort((x, y) => String(codeName(side, x.code)).localeCompare(String(codeName(side, y.code)), 'en', { numeric: true }))
  }
  await compared(`${kind}${how}`, ACCOUNT_CODES, request, answers)
}

/// A createAppPassword answer with its password and when it was made taken
/// out, which SESSIONS.md's checks hold to their form.
const masked = (res) => (res.status === 200 && res.body?.password ? { ...res, body: { ...res.body, password: '<password>', createdAt: '<time>' } } : res)

/// Invite codes, with codes not required: made by the admin, refused to
/// anyone else, used, and listed (spec/LIMITS.md, "Invite codes").
export async function inviteCodes(acct) {
  const r = rand()
  if (r < 0.25) return createCode(acct)
  if (r < 0.4) return createCodes()
  if (r < 0.55) return adminRefused(acct)
  if (r < 0.8) return createWithCode()
  return accountCodes(chance(0.6) && holders.size ? pick([...holders]) : acct)
}

/// After a crash: the codes of the accounts that have some, and a code made
/// before it, used after (spec/LIMITS.md, "createAccount with a code").
export async function invitesSurvive() {
  for (const acct of holders) await accountCodes(acct, 'getAccountInviteCodes after a crash')
  if (madeCodes.size) await createWithCode()
}

// ------------------------------------------------ app passwords, login

/// A session made with an app password, privileged or not, on
/// updateHandle (to a reserved handle, which it may try, and is refused for
/// the handle), createAppPassword and getAccountInviteCodes, which refuse
/// it. The app password is revoked after (spec/LIMITS.md, "App passwords'
/// sessions").
export async function appPasswordScopes(acct) {
  const name = `scope-${++serial}`
  const privileged = chance(0.5)
  const request = { body: { name, privileged }, token: acct }
  const [a, b] = await pair(CREATE_AP, request)
  const passwords = { reference: a.body?.password, server: b.body?.password }
  // The password and when it was made are SESSIONS.md's to check.
  await compared('createAppPassword for a session', CREATE_AP, request, [masked(a), masked(b)])
  if (a.status !== 200 || b.status !== 200) return
  const logins = await pair(SESSION, (side) => ({ body: { identifier: acct.handle, password: passwords[side.name] } }))
  await compared('createSession with an app password', SESSION, { body: { identifier: acct.handle, password: '<app password>' } }, logins)
  if (logins[0].status === 200 && logins[1].status === 200) {
    const token = (side) => logins[side === ref ? 0 : 1].body.accessJwt
    const calls = [
      [UPDATE_HANDLE, { body: { handle: `${pick(RESERVABLE)}.test` } }],
      [UPDATE_HANDLE, { body: { handle: pick(['not a handle', 'a..test']) } }],
      [CREATE_AP, { body: { name: `${name}-again` } }],
      [ACCOUNT_CODES, {}],
    ]
    for (const [nsid, rest] of subset(calls, 0.7).length ? subset(calls, 0.7) : calls)
      await bothSent(`${nsid.split('.').pop()} with a${privileged ? ' privileged' : 'n'} app password's session`, nsid, (side) => ({ ...rest, token: token(side) }))
  }
  await bothSent('revokeAppPassword after', REVOKE_AP, { body: { name }, token: acct })
}

/// createSession for an identifier no account has, with a wrong password,
/// and with the right one: on each side, every answer no sooner than 350 ms
/// after it was sent. The reference waits with a Node timer, which may fire
/// a millisecond or two early; it is held to 345 ms. The server is held to
/// 350 (spec/LIMITS.md, "Passwords").
export async function loginTiming(acct) {
  const tries = [
    ['no such account', { identifier: pick([`${freshFront()}.test`, `${freshFront()}@example.com`]), password: 'x' }],
    ['a wrong password', { identifier: acct.handle, password: `${acct.password ?? ''}wrong` }],
    ...(acct.password ? [['the right password', { identifier: acct.handle, password: acct.password }]] : []),
  ]
  const answers = await Promise.all(tries.map(([, body]) => pair(SESSION, { body })))
  for (const [i, [kind, body]] of tries.entries()) {
    const [a, b] = answers[i]
    if (a.status !== b.status) await differ(SESSION, body, a.status, b.status, kind)
    for (const [side, res] of [[ref, a], [cand, b]]) if (res.ms < (side === ref ? 345 : 350)) await differ(`the ${side.name}'s ${SESSION}`, body, 'an answer no sooner than 350 ms after it was sent', `${res.ms} ms`, side === ref ? 'the reference fails the check it is held to' : kind)
    count(`createSession timed, ${kind}`, a.status === 200)
  }
}

/// OAuth's sign-in for an identifier no account has, with a wrong password,
/// and with the right one: on each side, every answer no sooner than 400 ms
/// after it was sent (the reference, timed by a Node timer, held to 395);
/// and a body one byte past 100 KiB, `413` on both (spec/OAUTH.md, "The
/// API"; spec/LIMITS.md, "Passwords"). Statuses and errors compared.
export async function oauthSignInTiming(acct) {
  const handle = acct.handle
  if (!handle) return
  const tries = [
    ['no such account', { locale: 'en', identifier: pick([`${freshFront()}.test`, `${freshFront()}@example.com`]), password: 'x' }],
    ['a wrong password', { locale: 'en', identifier: handle, password: `${acct.password ?? ''}wrong` }],
    ...(acct.password ? [['the right password', { locale: 'en', identifier: handle, password: acct.password }]] : []),
  ]
  const timed = async (side, input, raw) => {
    const started = Date.now()
    const r = await signInCall(new URL(side.publicUrl).origin, input, { raw })
    return { status: r.status, error: r.json?.error ?? null, ms: Date.now() - started }
  }
  const answers = await Promise.all(tries.map(([, input]) => Promise.all(sides().map((side) => timed(side, input)))))
  for (const [i, [kind, input]] of tries.entries()) {
    const [a, b] = answers[i]
    const shownInput = { ...input, password: '<password>' }
    if (a.status !== b.status || a.error !== b.error) await differ('OAuth sign-in', shownInput, { status: a.status, error: a.error }, { status: b.status, error: b.error }, kind)
    for (const [side, res] of [[ref, a], [cand, b]]) if (res.ms < (side === ref ? 395 : 400)) await differ(`the ${side.name}'s OAuth sign-in`, shownInput, 'an answer no sooner than 400 ms after it was sent', `${res.ms} ms`, side === ref ? 'the reference fails the check it is held to' : kind)
    count(`OAuth sign-in timed, ${kind}`, a.status === 200)
  }
  const body = JSON.stringify({ locale: 'en', identifier: handle, password: 'x' })
  const raw = `${body.slice(0, -1)},"pad":"${'x'.repeat(102401 - body.length - 9)}"}`
  const [a, b] = await Promise.all(sides().map((side) => timed(side, null, raw)))
  if (a.status !== b.status || a.error !== b.error) await differ('OAuth sign-in', { body: `${raw.length} bytes` }, { status: a.status, error: a.error }, { status: b.status, error: b.error }, 'a body one byte past 100 KiB')
  count('OAuth sign-in, a body past 100 KiB', false)
}

// ------------------------------------------- the connection after a refusal

/// One answer read off a connection: `{status, headers}`, headers
/// lowercased, once its body is whole; null until then.
function answerIn(buf) {
  const at = buf.indexOf('\r\n\r\n')
  if (at < 0) return null
  const [line, ...rest] = buf.subarray(0, at).toString('latin1').split('\r\n')
  const headers = {}
  for (const h of rest) {
    const i = h.indexOf(':')
    const k = h.slice(0, i).trim().toLowerCase()
    headers[k] = headers[k] === undefined ? h.slice(i + 1).trim() : `${headers[k]}, ${h.slice(i + 1).trim()}`
  }
  const status = Number(line.split(' ')[1])
  let end = at + 4
  if (/chunked/i.test(headers['transfer-encoding'] ?? '')) {
    for (;;) {
      const nl = buf.indexOf('\r\n', end)
      if (nl < 0) return null
      const size = parseInt(buf.subarray(end, nl).toString('latin1'), 16)
      end = nl + 2 + size + 2
      if (buf.length < end) return null
      if (size === 0) break
    }
  } else {
    end += Number(headers['content-length'] ?? 0)
    if (buf.length < end) return null
  }
  return { status, headers, used: end }
}

/// A connection to `url`'s host, kept open between requests: `ask` writes
/// one whole and answers its answer, or `{status: 0, error}` once the
/// connection ends, or 5 s pass, with none.
function connection(url) {
  const u = new URL(reach(url))
  const socket = connect(Number(u.port || 80), u.hostname)
  let buf = Buffer.alloc(0)
  let ended = null
  let wake = () => {}
  socket.on('data', (c) => {
    buf = Buffer.concat([buf, c])
    wake()
  })
  socket.on('error', (err) => {
    ended ??= err.message
    wake()
  })
  socket.on('close', () => {
    ended ??= 'the connection closed'
    wake()
  })
  return {
    host: u.host,
    async ask(head, body) {
      socket.write(head)
      if (body) socket.write(body)
      const deadline = Date.now() + 5000
      for (;;) {
        const res = answerIn(buf)
        if (res) {
          buf = buf.subarray(res.used)
          return { status: res.status, headers: res.headers }
        }
        if (ended) return { status: 0, error: ended }
        const left = deadline - Date.now()
        if (left <= 0) return { status: 0, error: 'no answer within 5 s' }
        await new Promise((ok) => {
          const timer = setTimeout(ok, left)
          wake = () => {
            clearTimeout(timer)
            ok()
          }
        })
      }
    },
    end: () => socket.destroy(),
  }
}

/// A request whose body is refused: OAuth's sign-in and PAR one byte past
/// 100 KiB, which may be refused before the body is read, a sign-in of a
/// few bytes refused for its CSRF token, and createRecord one byte past its
/// cap, sent with a `Content-Length` on a connection of its own. Each
/// leaves at most 4 MiB unread, so on the server the connection takes the
/// next request, sent 100 ms later, as a client that keeps connections open
/// sends it; on the reference, that, or an answer saying `Connection:
/// close` (spec/LIMITS.md, "Request size"). Statuses compared.
export async function refusedBodies(acct) {
  const csrf = 'c'.repeat(24)
  const signIn = JSON.stringify({ locale: 'en', identifier: acct.handle ?? 'nobody.test', password: 'x' })
  const [kind, request] = pick([
    ['OAuth sign-in, a body one byte past 100 KiB', (side) => {
      const origin = new URL(side.publicUrl).origin
      const page = `${origin}/oauth/authorize?client_id=${encodeURIComponent('http://localhost')}&request_uri=${encodeURIComponent(`urn:ietf:params:oauth:request_uri:req-${'0'.repeat(32)}`)}`
      const headers = { origin, referer: page, 'sec-fetch-mode': 'same-origin', 'sec-fetch-site': 'same-origin', 'sec-fetch-dest': 'empty', 'x-csrf-token': csrf, cookie: `csrf-token=${csrf}`, 'content-type': 'application/json' }
      return { url: `${origin}/@atproto/oauth-provider/~api/sign-in`, headers, body: `${signIn.slice(0, -1)},"pad":"${'x'.repeat(102401 - signIn.length - 9)}"}` }
    }],
    ['OAuth sign-in refused for its CSRF token, a body of a few bytes', (side) => {
      const origin = new URL(side.publicUrl).origin
      const page = `${origin}/oauth/authorize?client_id=${encodeURIComponent('http://localhost')}&request_uri=${encodeURIComponent(`urn:ietf:params:oauth:request_uri:req-${'0'.repeat(32)}`)}`
      const headers = { origin, referer: page, 'sec-fetch-mode': 'same-origin', 'sec-fetch-site': 'same-origin', 'sec-fetch-dest': 'empty', 'x-csrf-token': csrf, cookie: `csrf-token=${'d'.repeat(24)}`, 'content-type': 'application/json' }
      return { url: `${origin}/@atproto/oauth-provider/~api/sign-in`, headers, body: signIn }
    }],
    ['PAR, a body one byte past 100 KiB', (side) => {
      const start = `client_id=${encodeURIComponent('http://localhost')}&response_type=code&pad=`
      return { url: `${new URL(side.publicUrl).origin}/oauth/par`, headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: `${start}${'x'.repeat(102401 - start.length)}` }
    }],
    ['createRecord, a body one byte past its cap', (side) => {
      const body = sized(1000001, (pad) => ({ repo: acct.handle, collection: 'com.example.big', record: { $type: 'com.example.big', pad } }))
      return { url: `${side.url}/xrpc/${CREATE_RECORD}`, headers: { authorization: `Bearer ${tokenOf(side, acct)}`, 'content-type': 'application/json' }, body }
    }],
  ])
  const after = await Promise.all(sides().map(async (side) => {
    const { url, headers, body } = request(side)
    const c = connection(url)
    const u = new URL(url)
    const head = [`POST ${u.pathname}${u.search} HTTP/1.1`, `Host: ${c.host}`, ...Object.entries(headers).map(([k, v]) => `${k}: ${v}`), `Content-Length: ${Buffer.byteLength(body)}`, '', ''].join('\r\n')
    const first = await c.ask(head, body)
    let then
    if (first.status === 0) then = `no answer: ${first.error}`
    else if (/(^|,)\s*close\s*(,|$)/i.test(first.headers.connection ?? '')) then = 'Connection: close'
    else {
      await wait(100)
      const next = await c.ask(`GET /xrpc/_health HTTP/1.1\r\nHost: ${c.host}\r\n\r\n`)
      then = next.status === 200 ? 'the next request answered on it' : `the next request on it ${next.status ? `answered ${next.status}` : `not answered: ${next.error}`}`
    }
    c.end()
    return { status: first.status, then }
  }))
  const shownRequest = { kind, chunked: false }
  for (const [side, res] of sides().map((s, i) => [s, after[i]])) {
    const kept = res.then === 'the next request answered on it'
    if (side === cand ? !kept : !kept && res.then !== 'Connection: close')
      await differ(`the ${side.name}'s connection after a body refused`, shownRequest, side === cand ? 'the connection taking the next request' : 'an answer saying Connection: close, or the connection taking the next request', res, side === ref ? 'the reference fails the check it is held to' : kind)
  }
  if (after[0].status !== after[1].status) await differ(kind, shownRequest, after[0].status, after[1].status)
  count(`${kind}, the connection after`, after[0].status < 400)
}

// ------------------------------------------------- invite codes required

/// --invites: the reference with codes required, and the server with
/// PDS_INVITE_REQUIRED=true. createAccount's checks around the code, codes
/// of several uses, used up and of none, a race for a code of one use, and
/// the codes listed after (spec/LIMITS.md, "Environment", "createAccount
/// with a code", "getAccountInviteCodes").
export async function invitesRequired() {
  const step = async (name, fn) => {
    if (ONLY && !ONLY.has(name)) return
    await fn()
  }
  await describeServer()
  const make = async (useCount, forAccount) => {
    const request = (side) => ({ body: { useCount, ...(forAccount ? { forAccount: didOf(side, forAccount) } : {}) }, auth: ADMIN })
    const t0 = seconds()
    const answers = await pair(CODE, request)
    for (const [side, res] of sides().map((s, i) => [s, answers[i]])) if (res.status === 200) await noteCodes(side, [res.body.code], [t0, seconds()], forAccount, shown(request))
    await compared('createInviteCode', CODE, request, answers)
    if (forAccount) holders.add(forAccount)
    return codeName(ref, answers[0].body.code)
  }
  const withCode = (name, input = newAccountInput(true)) => (side) => ({ ...input, inviteCode: codeOf(side, name) })
  const two = await make(2)
  const first = await sendCreate('createAccount with a code of two uses', withCode(two))
  if (!first) await differ(CREATE_ACCOUNT, { inviteCode: two }, 'an account', 'none', 'the reference refuses an account with a code of two uses')

  await step('order', async () => {
    const taken = first
    // Faults, each with no code and with one never made, to place the code's
    // checks among createAccount's.
    const faults = [
      ['nothing else', (b) => b],
      ['a bad email', (b) => ({ ...b, email: 'not-an-email' })],
      ['no email', ({ email: _, ...b }) => b],
      ['a password too long', (b) => ({ ...b, password: 'p'.repeat(300) })],
      ['a handle the lexicon refuses', (b) => ({ ...b, handle: 'not a handle' })],
      ['a disallowed TLD', (b) => ({ ...b, handle: `${freshFront()}.example` })],
      ['an unsupported domain', (b) => ({ ...b, handle: `${freshFront()}.com` })],
      ['a reserved handle', (b) => ({ ...b, handle: `${pick(RESERVABLE)}.test` })],
      ['a handle taken', (b) => ({ ...b, handle: taken.handle })],
      ['an email taken', (b) => ({ ...b, email: taken.email })],
      ['a did', (b) => ({ ...b, did: NOBODY })],
    ]
    for (const [fault, apply] of faults)
      for (const [code, inviteCode] of [['no code', undefined], ['an empty code', ''], ['a code never made', 'localhost-aaaaa-22222']]) {
        if (code === 'an empty code' && fault !== 'nothing else' && chance(0.6)) continue
        const body = apply(newAccountInput(true))
        const acct = await sendCreate(`createAccount, codes required, ${code}, ${fault}`, inviteCode === undefined ? body : { ...body, inviteCode }, true)
        if (acct) await differ(CREATE_ACCOUNT, body, 'refused', `<did:${acct.n}>`, 'both sides make an account without a code')
      }
  })

  await step('uses', async () => {
    const second = await sendCreate('createAccount with a code\'s second use', withCode(two))
    if (!second) await differ(CREATE_ACCOUNT, { inviteCode: two }, 'an account', 'none', 'the reference refuses a code\'s second use')
    await sendCreate('createAccount with a code used up', withCode(two))
    await sendCreate('createAccount with a code used up, and a handle taken', withCode(two, { ...newAccountInput(true), handle: first.handle }))
    const zero = await make(0)
    await sendCreate('createAccount with a code of no uses', withCode(zero))
    const one = await make(1)
    await sendCreate('createAccount with a code in upper case', (side) => ({ ...newAccountInput(true), inviteCode: codeOf(side, one).toUpperCase() }))
    await sendCreate('createAccount with a code of one use', withCode(one))
  })

  await step('race', async () => {
    const one = await make(1)
    const inputs = Array.from({ length: 4 }, () => newAccountInput(true))
    const answers = await Promise.all(sides().map((side) => Promise.all(inputs.map((input) => send(side, CREATE_ACCOUNT, { body: { ...input, inviteCode: codeOf(side, one) } })))))
    const summary = (rs) => rs.map((r) => (r.status === 200 ? 'made' : `${r.status} ${r.body?.error}`)).sort()
    const [sa, sb] = answers.map(summary)
    const request = { createAccount: 4, inviteCode: 'one code of one use', at: 'once' }
    if (sa.filter((x) => x === 'made').length !== 1) await differ(CREATE_ACCOUNT, request, 'one made', sa, 'the reference fails the check it is held to')
    if (!isDeepStrictEqual(sa, sb)) await differ(CREATE_ACCOUNT, request, sa, sb, 'createAccounts at once with a code of one use')
    count('createAccount race for a code', true)
  })

  await step('listed', async () => {
    const forFirst = await make(2, first)
    const user = await sendCreate('createAccount with a code for an account', withCode(forFirst))
    if (!user) await differ(CREATE_ACCOUNT, { inviteCode: forFirst }, 'an account', 'none', 'the reference refuses a code made for an account')
    const request = (side) => ({ body: { codeCount: 2, useCount: 1, forAccounts: [didOf(side, first)] }, auth: ADMIN })
    const t0 = seconds()
    const answers = await pair(CODES, request)
    for (const [side, res] of sides().map((s, i) => [s, answers[i]])) if (res.status === 200) for (const e of res.body.codes) await noteCodes(side, e.codes, [t0, seconds()], first, shown(request))
    await compared('createInviteCodes', CODES, request, answers)
    const listed = [...codeNames.reference.values()].at(-1)
    await sendCreate('createAccount with a code from createInviteCodes', withCode(listed))
    for (let i = 0; i < 4; i++) await accountCodes(first)
  })
}

// ------------------------------------------------------------- --limits

/// The rate-limit headers of an answer, `null` if it has none; `reset` and
/// `retry` apart, as they are compared to within 2 seconds.
function limitView(res) {
  const h = res.headers ?? {}
  const view = {
    status: res.status,
    error: res.status >= 400 ? res.body?.error ?? null : null,
    limit: h['ratelimit-limit'] ?? null,
    remaining: h['ratelimit-remaining'] ?? null,
    policy: h['ratelimit-policy'] ?? null,
    expose: h['access-control-expose-headers'] ?? null,
  }
  return { view, reset: h['ratelimit-reset'] === undefined ? null : Number(h['ratelimit-reset']), retry: h['retry-after'] === undefined ? null : Number(h['retry-after']) }
}

/// Two answers of a limited call compared: status, error and headers, the
/// times to within 2 seconds (spec/LIMITS.md, "Rate limits").
async function sameLimits(kind, request, a, b) {
  const [x, y] = [limitView(a), limitView(b)]
  if (!isDeepStrictEqual(x.view, y.view)) await differ(kind, request, x.view, y.view, 'rate limits: status, error and headers')
  for (const k of ['reset', 'retry']) {
    if ((x[k] === null) !== (y[k] === null) || (x[k] !== null && Math.abs(x[k] - y[k]) > 2)) await differ(kind, request, { [k]: x[k] }, { [k]: y[k] }, `rate limits: ${k === 'reset' ? 'RateLimit-Reset' : 'Retry-After'}, to within 2 s`)
  }
  count(kind.replace(/\d+/g, 'N'), a.status < 400)
}

/// `n` calls on each side, `at` at once, compared as sets of status, error
/// and RateLimit-Remaining, as the order they land in may differ.
async function limitedBatch(kind, nsid, request, n, at = 10) {
  const got = { reference: [], server: [] }
  for (let i = 0; i < n; i += at) {
    const k = Math.min(at, n - i)
    const answers = await Promise.all(sides().map((side) => Promise.all(Array.from({ length: k }, (_, j) => send(side, nsid, typeof request === 'function' ? request(side, i + j) : request)))))
    sides().forEach((side, s) => got[side.name].push(...answers[s].map((r) => { const v = limitView(r).view; return `${v.status} ${v.error ?? ''} ${v.remaining}` })))
  }
  const sorted = (xs) => [...xs].sort()
  if (!isDeepStrictEqual(sorted(got.reference), sorted(got.server))) await differ(kind, { calls: n, at }, sorted(got.reference).slice(0, 40), sorted(got.server).slice(0, 40), 'rate limits: each call\'s status, error and RateLimit-Remaining')
  count(kind, true)
}

async function limited(kind, nsid, request) {
  const [a, b] = await pair(nsid, request)
  await sameLimits(kind, shown(typeof request === 'function' ? request(ref) : request), a, b)
  return [a, b]
}

/// An account with a password on both sides, new, so that its limits start
/// afresh.
const freshAccount = () => createAccount({ withPassword: true })

/// createAccount: 100 from one address, refused for a handle taken, then
/// one more; another address; input the lexicon refuses, which costs
/// nothing; and addresses in a chain (spec/LIMITS.md, "Rate limits").
async function signupLimit() {
  const taken = accounts[0] ?? (await freshAccount())
  const a = freshIp()
  const body = (i) => ({ handle: taken.handle, email: `${freshFront()}${i}@example.com`, password: 'pw' })
  for (let i = 1; i <= 101; i++) await limited(`createAccount ${i} of 100 from one address`, CREATE_ACCOUNT, { body: body(i), ip: a })
  await limited('createAccount from another address', CREATE_ACCOUNT, { body: body(0), ip: freshIp() })
  const c = freshIp()
  for (let i = 0; i < 3; i++) await limited('createAccount the lexicon refuses', CREATE_ACCOUNT, { body: { handle: 'not a handle', email: 'x@example.com' }, ip: c })
  await limited('createAccount after input the lexicon refused', CREATE_ACCOUNT, { body: body(1), ip: c })
  await limited('createAccount, the limited address before a private one', CREATE_ACCOUNT, { body: body(2), ip: `${a}, 10.0.0.5` })
  await limited('createAccount, the limited address before a public one', CREATE_ACCOUNT, { body: body(3), ip: `${a}, ${freshIp()}` })
}

/// createSession: 30 with a wrong password for one identifier from one
/// address, then the right one; the identifier in another case, and
/// another address, which are other keys (spec/LIMITS.md, "Rate limits").
async function loginLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  await limitedBatch('createSession, 30 wrong passwords', SESSION, { body: { identifier: acct.handle, password: 'wrong' }, ip }, 30)
  await limited('createSession, the 31st', SESSION, { body: { identifier: acct.handle, password: acct.password }, ip })
  await limited('createSession, the identifier in upper case', SESSION, { body: { identifier: acct.handle.toUpperCase(), password: acct.password }, ip })
  await limited('createSession from another address', SESSION, { body: { identifier: acct.handle, password: acct.password }, ip: freshIp() })
}

/// updateHandle: 10 to a reserved handle, then an 11th, and from another
/// address, as the key is the account (spec/LIMITS.md, "Rate limits").
async function handleLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  for (let i = 1; i <= 11; i++) await limited(`updateHandle ${i} of 10`, UPDATE_HANDLE, { body: { handle: `${pick(RESERVABLE)}.test` }, token: acct, ip })
  await limited('updateHandle from another address', UPDATE_HANDLE, { body: { handle: `${pick(RESERVABLE)}.test` }, token: acct, ip: freshIp() })
}

/// uploadBlob: 1000 from one account, then one more (spec/LIMITS.md,
/// "Rate limits").
async function uploadLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  const run = ++serial
  const blob = (side, i) => ({ raw: `spec-pds blob ${run} ${i}`, contentType: 'text/plain', token: acct, ip })
  await limitedBatch('uploadBlob, 1000', UPLOAD, blob, 1000, 25)
  await limited('uploadBlob, the 1001st', UPLOAD, blob(null, 1000))
}

/// The default upload limit, which the run's is past.
const DEFAULT_UPLOAD_LIMIT = 5242880

/// A body of `n` bytes that begins `spec-pds blob `, so that its type is
/// the request's (spec/BLOBS.md, "uploadBlob").
function blobOf(n) {
  const b = Buffer.alloc(n, 0x61)
  b.write(`spec-pds blob limit ${++serial} `)
  return b
}

/// The upload limit set, both sides started with PDS_BLOB_UPLOAD_LIMIT
/// (spec/LIMITS.md, "Environment", "Request size"; spec/BLOBS.md,
/// "uploadBlob"): `describeServer`'s `blobUploadLimit`, then uploadBlob of
/// a body a byte past the default, of one at the limit and of one a byte
/// past it, each with its length or chunked, compared whole.
async function uploadSize() {
  const [da, db] = await pair(DESCRIBE_SERVER, { ip: freshIp() })
  if (da.body?.blobUploadLimit !== LIMITS_UPLOAD_LIMIT) throw new Error(`the reference's blobUploadLimit is ${da.body?.blobUploadLimit}, not ${LIMITS_UPLOAD_LIMIT}: it was not started with it`)
  if (db.body?.blobUploadLimit !== LIMITS_UPLOAD_LIMIT) await differ(DESCRIBE_SERVER, { PDS_BLOB_UPLOAD_LIMIT: LIMITS_UPLOAD_LIMIT }, LIMITS_UPLOAD_LIMIT, db.body?.blobUploadLimit ?? null, 'spec/LIMITS.md: describeServer answers PDS_BLOB_UPLOAD_LIMIT as blobUploadLimit')
  count('describeServer blobUploadLimit, set', true)
  const acct = await freshAccount()
  for (const n of [DEFAULT_UPLOAD_LIMIT + 1, LIMITS_UPLOAD_LIMIT, LIMITS_UPLOAD_LIMIT + 1]) {
    const raw = blobOf(n)
    const chunked = chance(0.5)
    const request = { raw, contentType: 'application/octet-stream', token: acct, ip: freshIp(), chunked }
    await compared(`uploadBlob of ${n} bytes, the limit ${LIMITS_UPLOAD_LIMIT}`, UPLOAD, { body: `${n} bytes`, chunked, token: acct }, await pair(UPLOAD, request))
  }
}

/// A space importRepo under the upload limit set (spec/LIMITS.md,
/// "Request size"; spec/SPACES-EXPORT.md, "importRepo"), which the
/// reference here does not serve: sent as a STAR archive, a body a byte
/// past the default, no archive, `400 InvalidRepoFile`; one a byte past the
/// limit, `413 PayloadTooLarge`.
async function spaceImportSize() {
  const acct = await freshAccount()
  const nsid = 'com.atproto.space.importRepo'
  for (const [n, want] of [[DEFAULT_UPLOAD_LIMIT + 1, { status: 400, error: 'InvalidRepoFile' }], [LIMITS_UPLOAD_LIMIT + 1, { status: 413, error: 'PayloadTooLarge' }]]) {
    const r = await send(cand, nsid, { raw: blobOf(n), contentType: 'application/vnd.dasl.star', token: acct, ip: freshIp() })
    if (r.status !== want.status || r.body?.error !== want.error) await broke(nsid, { body: `${n} bytes`, PDS_BLOB_UPLOAD_LIMIT: LIMITS_UPLOAD_LIMIT }, want, { status: r.status, error: r.body?.error })
  }
  count('space importRepo under the upload limit set', false)
}

/// Writes: applyWrites of 200 creates, 600 points each, past the hour's
/// 5000; a createRecord before, and a deleteRecord after (spec/LIMITS.md,
/// "Rate limits").
async function writeLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  const run = ++serial
  const batch = (j) => ({ body: { repo: acct.handle, writes: Array.from({ length: 200 }, (_, i) => ({ $type: 'com.atproto.repo.applyWrites#create', collection: 'com.example.note', rkey: `w${run}-${j}-${i}`, value: { $type: 'com.example.note', i, j } })) }, token: acct, ip })
  for (let j = 0; j < 8; j++) await limited(`applyWrites of 200 creates, ${j + 1}`, APPLY, batch(j))
  await limited('createRecord, 3 points', CREATE_RECORD, { body: { repo: acct.handle, collection: 'com.example.note', record: { $type: 'com.example.note', n: run } }, token: acct, ip })
  await limited('applyWrites past the hour\'s points', APPLY, batch(8))
  await limited('deleteRecord past the hour\'s points', DELETE_RECORD, { body: { repo: acct.handle, collection: 'com.example.note', rkey: 'none' }, token: acct, ip })
  await limited('applyWrites of none past the hour\'s points', APPLY, { body: { repo: acct.handle, writes: [] }, token: acct, ip })
}

/// global: 3000 describeServers from one address, then one more; then
/// refreshSession, a method not served, and getRepo, which has its own
/// (spec/LIMITS.md, "Rate limits").
async function globalLimit() {
  const acct = await freshAccount()
  const fresh = await pair(SESSION, { body: { identifier: acct.handle, password: acct.password }, ip: freshIp() })
  const ip = freshIp()
  await limitedBatch('describeServer, 3000', DESCRIBE_SERVER, { ip }, 3000, 50)
  await limited('describeServer, the 3001st', DESCRIBE_SERVER, { ip })
  await limited('refreshSession past global', REFRESH, (side) => ({ token: fresh[side === ref ? 0 : 1].body.refreshJwt, method: 'POST', ip }))
  await limited('a method not served past global', 'com.example.nothing.here', { ip })
  await limited('a method forwarded past global', FORWARDED, { params: FORWARDED_PARAMS, token: acct, ip })
  await limited('getPreferences past global', 'app.bsky.actor.getPreferences', { token: acct, ip })
  const [a, b] = await pair('com.atproto.sync.getRepo', (side) => ({ params: { did: didOf(side, acct) }, ip }))
  await sameLimits('getRepo past global', { getRepo: `<did:${acct.n}>` }, { ...a, body: null }, { ...b, body: null })
}

/// The emails both sides sent, dropped: the --limits run compares none.
const dropMail = () => sides().forEach(takeMail)

/// requestEmailConfirmation: 16 from one account, past the hour's 5 and
/// then the day's 15, and one from another address, as the key is the
/// account; then requestEmailUpdate 16 times and once more from another
/// address, its limits its own (spec/EMAIL.md, "Rate limits").
async function emailRequestLimits() {
  const acct = await freshAccount()
  for (const [name, nsid] of [['requestEmailConfirmation', 'com.atproto.server.requestEmailConfirmation'], ['requestEmailUpdate', 'com.atproto.server.requestEmailUpdate']]) {
    const ip = freshIp()
    for (let i = 1; i <= 16; i++) await limited(`${name} ${i} of 5 an hour, 15 a day`, nsid, { token: acct, method: 'POST', ip })
    await limited(`${name} from another address`, nsid, { token: acct, method: 'POST', ip: freshIp() })
    dropMail()
  }
}

/// requestPasswordReset: 51 from one address, past the hour's 15 and then
/// the day's 50, by an account's email in another case and by one no
/// account has, which cost the same; then from another address, as the key
/// is the address (spec/EMAIL.md, "Rate limits").
async function passwordResetLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  const email = (i) => (i % 2 ? mixCase(acct.email) : `${freshFront()}@example.com`)
  for (let i = 1; i <= 51; i++) await limited(`requestPasswordReset ${i} of 15 an hour, 50 a day`, 'com.atproto.server.requestPasswordReset', { body: { email: email(i) }, ip })
  await limited('requestPasswordReset from another address', 'com.atproto.server.requestPasswordReset', { body: { email: acct.email }, ip: freshIp() })
  dropMail()
}

/// resetPassword: 50 with a token never made from one address, then a 51st,
/// and one from another address (spec/EMAIL.md, "Rate limits").
async function resetLimit() {
  const ip = freshIp()
  const body = { token: 'AAAAA-BBBBB', password: 'new-pw' }
  await limitedBatch('resetPassword, 50', 'com.atproto.server.resetPassword', { body, ip }, 50)
  await limited('resetPassword, the 51st', 'com.atproto.server.resetPassword', { body, ip })
  await limited('resetPassword from another address', 'com.atproto.server.resetPassword', { body, ip: freshIp() })
}

/// requestAccountDelete: 16 from one account, past the hour's 5 and then
/// the day's 15, and one from another address, as the key is the account
/// (spec/LIFECYCLE.md, "Rate limits").
async function accountDeleteRequestLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  for (let i = 1; i <= 16; i++) await limited(`requestAccountDelete ${i} of 5 an hour, 15 a day`, 'com.atproto.server.requestAccountDelete', { token: acct, method: 'POST', ip })
  await limited('requestAccountDelete from another address', 'com.atproto.server.requestAccountDelete', { token: acct, method: 'POST', ip: freshIp() })
  dropMail()
}

/// deleteAccount: 50 with a token never mailed from one address, then a
/// 51st, and one from another address (spec/LIFECYCLE.md, "Rate limits").
async function deleteLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  const body = (side) => ({ body: { did: didOf(side, acct), password: acct.password, token: 'AAAAA-BBBBB' }, ip })
  await limitedBatch('deleteAccount, 50', 'com.atproto.server.deleteAccount', body, 50)
  await limited('deleteAccount, the 51st', 'com.atproto.server.deleteAccount', body)
  await limited('deleteAccount from another address', 'com.atproto.server.deleteAccount', (side) => ({ ...body(side), ip: freshIp() }))
}

/// A method the server forwards, and its parameters.
const FORWARDED = 'app.bsky.feed.getPosts'
const FORWARDED_PARAMS = { uris: 'at://did:plc:aaaaaaaaaaaaaaaaaaaaaaaa/app.bsky.feed.post/3k' }

/// What goes to the AppView, or is served for it (spec/PROXY.md): a method
/// forwarded, with a token and without, getProfile, which the server serves
/// by asking it, and preferences, which it keeps: each `global`'s
/// (spec/LIMITS.md, "Rate limits").
async function forwardedLimit() {
  const acct = await freshAccount()
  const ip = freshIp()
  answerWith()
  await limited('a method forwarded', FORWARDED, { params: FORWARDED_PARAMS, token: acct, ip })
  await limited('a method forwarded, no token', FORWARDED, { params: FORWARDED_PARAMS, ip })
  await limited('getProfile', 'app.bsky.actor.getProfile', { params: { actor: 'other.example.com' }, token: acct, ip })
  await limited('putPreferences', 'app.bsky.actor.putPreferences', { body: { preferences: [] }, token: acct, ip })
  await limited('getPreferences', 'app.bsky.actor.getPreferences', { token: acct, ip })
}

// The rest is held to the spec alone.

/// The server failing a rule of spec/LIMITS.md.
const broke = (what, request, want, got) => differ(`the server's ${what}`, request, want, got, 'spec/LIMITS.md')

/// An account on the server alone, with no password, made from `ip`.
async function serverAccount(ip = freshIp()) {
  const front = freshFront()
  const res = await send(cand, CREATE_ACCOUNT, { body: { handle: `${front}.test`, email: `${front}@example.com` }, ip })
  return { res, did: res.body?.did, handle: `${front}.test`, access: res.body?.accessJwt, refresh: res.body?.refreshJwt }
}

/// Start the server again, with `changes` to its environment (undefined
/// takes a variable out) and at most `ulimit` file descriptors, and wait
/// until it answers; it must not exit.
async function restartServer(changes = {}, ulimit = null, why = 'restarted') {
  await server.kill()
  for (const [k, v] of Object.entries(changes)) {
    if (v === undefined) delete server.env[k]
    else server.env[k] = v
  }
  server.ulimit = ulimit
  server.start()
  await serving(why)
}

/// Wait until the server answers describeServer; it must not exit.
async function serving(why) {
  const started = Date.now()
  for (;;) {
    if (server.exited) await broke('start', { why, ulimit: server.ulimit }, 'serves', { exited: server.exited, log: server.logPath })
    const r = await send(cand, DESCRIBE_SERVER, {})
    if (r.status === 200) return
    if (Date.now() - started > 30000) await broke('start', { why }, 'answers describeServer within 30 s', r)
    await wait(50)
  }
}

/// 101 sessions, made by refreshing, which drops the one that expires
/// soonest: the first, refreshed a second before the others, as expiries
/// are whole seconds and a tie is the server's to break (spec/LIMITS.md,
/// "Sessions and app passwords").
async function sessionCap() {
  const acct = await serverAccount()
  const ip = freshIp()
  const tokens = [acct.refresh]
  for (let i = 0; i < 100; i++) {
    const r = await send(cand, REFRESH, { token: tokens.at(-1), method: 'POST', ip })
    if (r.status !== 200) await broke(REFRESH, { refresh: i + 1 }, 200, r)
    tokens.push(r.body.refreshJwt)
    if (i === 0) await wait(1100)
  }
  const first = await send(cand, REFRESH, { token: tokens[0], method: 'POST', ip })
  if (first.status !== 400 || first.body?.error !== 'ExpiredToken') await broke('101st session', { refresh: 'the first session, after 100 refreshes made 101' }, '400 ExpiredToken: it expired soonest, and was dropped', { status: first.status, error: first.body?.error })
  const second = await send(cand, REFRESH, { token: tokens[1], method: 'POST', ip })
  if (second.status !== 200) await broke('101st session', { refresh: 'the second session, in its grace' }, 200, { status: second.status, error: second.body?.error })
  count('sessions past 100', true)
}

/// 51 app passwords, the last refused (spec/LIMITS.md, "Sessions and app
/// passwords").
async function appPasswordCap() {
  const acct = await serverAccount()
  const ip = freshIp()
  for (let i = 1; i <= 51; i++) {
    const r = await send(cand, CREATE_AP, { body: { name: `cap-${i}` }, token: acct.access, ip })
    if (i <= 50 && r.status !== 200) await broke(CREATE_AP, { appPassword: i }, 200, r)
    if (i === 51 && (r.status !== 400 || r.body?.error !== 'TooManyAppPasswords')) await broke(CREATE_AP, { appPassword: 51 }, '400 TooManyAppPasswords', { status: r.status, error: r.body?.error })
  }
  count('app passwords past 50', false)
}

/// The client address (spec/LIMITS.md, "Rate limits", "Environment"):
/// restarted without PDS_TRUSTED_PROXIES, `describeServer`s with three
/// `X-Forwarded-For`s of their own all count against the connection's
/// address, one bucket; with only 127.0.0.1 trusted, `a, 10.0.0.5` is
/// 10.0.0.5's, not a's. Then the reference's list again.
async function clientAddress() {
  await restartServer({ PDS_TRUSTED_PROXIES: undefined }, null, 'without PDS_TRUSTED_PROXIES')
  const left = []
  for (const ip of [freshIp(), freshIp(), freshIp()]) left.push(Number((await send(cand, DESCRIBE_SERVER, { ip })).headers['ratelimit-remaining']))
  if (!(left[1] === left[0] - 1 && left[2] === left[1] - 1)) await broke('client address', { trusted: 'none', 'x-forwarded-for': 'three of their own, from 127.0.0.1' }, 'one bucket, the connection\'s: RateLimit-Remaining down by one each call', left)
  await restartServer({ PDS_TRUSTED_PROXIES: '127.0.0.1' }, null, 'trusting 127.0.0.1 alone')
  const a = freshIp()
  const chain = Number((await send(cand, DESCRIBE_SERVER, { ip: `${a}, 10.0.0.5` })).headers['ratelimit-remaining'])
  const inner = Number((await send(cand, DESCRIBE_SERVER, { ip: '10.0.0.5' })).headers['ratelimit-remaining'])
  const outer = Number((await send(cand, DESCRIBE_SERVER, { ip: a })).headers['ratelimit-remaining'])
  if (!(chain === 2999 && inner === 2998 && outer === 2999)) await broke('client address', { trusted: '127.0.0.1', 'x-forwarded-for': [`${a}, 10.0.0.5`, '10.0.0.5', a] }, 'the first two 10.0.0.5\'s (2999, 2998), the third a\'s (2999)', [chain, inner, outer])
  await restartServer({ PDS_TRUSTED_PROXIES: REFERENCE_TRUSTED_PROXIES }, null, 'trusting the reference\'s proxies')
  count('client address', true)
}

/// Restarted with PDS_ADMIN_PASSWORD empty: no request is the admin, not
/// `admin:` with an empty password, nor the usual one (spec/LIMITS.md,
/// "Environment", "The admin").
async function adminUnset() {
  await restartServer({ PDS_ADMIN_PASSWORD: '' }, null, 'with PDS_ADMIN_PASSWORD empty')
  for (const auth of [basic('admin', ''), ADMIN]) {
    const r = await send(cand, CODE, { body: { useCount: 1 }, auth, ip: freshIp() })
    if (r.status !== 401 || r.body?.error !== 'AuthenticationRequired') await broke(CODE, { PDS_ADMIN_PASSWORD: '', auth: auth === ADMIN ? 'admin:<the usual password>' : 'admin:' }, '401 AuthenticationRequired', { status: r.status, error: r.body?.error })
  }
  await restartServer({ PDS_ADMIN_PASSWORD: ADMIN_PASSWORD }, null, 'with the admin password')
  count('admin password empty', false)
}

/// Service auth for `lxm` from `iss`, signed by a key no document names:
/// the server must read `iss`'s document before it can refuse it.
async function serviceAuth(iss, lxm) {
  const { Secp256k1Keypair } = await import('@atproto/crypto')
  const key = await Secp256k1Keypair.create()
  const b64 = (x) => Buffer.from(typeof x === 'string' ? x : JSON.stringify(x)).toString('base64url')
  const input = `${b64({ typ: 'JWT', alg: 'ES256K' })}.${b64({ iss, aud: 'did:web:localhost', lxm, iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + 60, jti: String(rand()) })}`
  return `${input}.${Buffer.from(await key.sign(Buffer.from(input))).toString('base64url')}`
}
const REVOKED = 'com.atproto.space.notifyCredentialsRevoked'
const revoke = (iss, ip) => serviceAuth(iss, REVOKED).then((jwt) => send(cand, REVOKED, { body: { space: `at://${NOBODY}/space/com.example.space/x`, credentials: ['x'] }, auth: `Bearer ${jwt}`, ip }))

/// A call whose service auth names a did:web issuer pays before the
/// issuer's document is read (spec/LIMITS.md, "Rate limits", "When"):
/// refused for its signature, it carries the rate-limit headers; once
/// `global` is past for its address, it is `429` and the issuer's host is
/// asked nothing.
async function spacesPayFirst() {
  const host = await ownHost()
  const ip = freshIp()
  const r = await revoke(host.did, ip)
  if (r.status === 429 || r.headers['ratelimit-remaining'] !== '2999') await broke(REVOKED, { iss: '<a did:web of its own>', ip }, 'refused for its token, and paid for: RateLimit-Remaining 2999', { status: r.status, error: r.body?.error, remaining: r.headers['ratelimit-remaining'] ?? null })
  for (let i = 1; i < 3000; i += 100) await Promise.all(Array.from({ length: Math.min(100, 3000 - i) }, () => send(cand, DESCRIBE_SERVER, { ip })))
  host.requests.splice(0)
  const over = await revoke(host.did, ip)
  if (over.status !== 429 || host.requests.length) await broke(REVOKED, { iss: '<a did:web of its own>', ip, global: 'past' }, '429, with nothing fetched of the issuer\'s host', { status: over.status, error: over.body?.error, fetched: host.requests })
  await host.close()
  count('service auth pays before its issuer is read', false)
}

/// did:web documents apart from the directory (spec/LIMITS.md, "The PLC
/// directory"): with 20 notifyCredentialsRevoked whose issuers' hosts take
/// the connection and never answer, a createAccount, which needs the
/// directory, still answers within 5 s.
async function didWebApart() {
  const hosts = await Promise.all(Array.from({ length: 20 }, () => silentHost()))
  const ip = freshIp()
  const waiting = hosts.map((h) => revoke(h.did, ip))
  await wait(1000)
  const started = Date.now()
  const made = await serverAccount()
  const ms = Date.now() - started
  if (made.res.status !== 200 || ms > 5000) await broke(CREATE_ACCOUNT, { while: '20 did:web issuers that never answer are being read' }, 'an account, within 5 s', { status: made.res.status, error: made.res.body?.error, ms, connections: hosts.reduce((n, h) => n + h.connections, 0) })
  await Promise.all(waiting)
  await Promise.all(hosts.map((h) => h.close()))
  count('did:web documents apart from the directory', true)
}

/// What the server reads of what it fetches (spec/PROXY.md, "SSRF
/// protection"): a target whose did:web document redirects to a good one,
/// `500` on both sides (the reference's resolver follows none); held to
/// the spec alone, a target whose document is 70000 bytes, `500`, and a
/// service whose answer is 17 MiB, `502 UpstreamFailure`.
async function outboundCaps() {
  const good = await ownHost()
  const redirecting = await ownHost((req, res) => {
    res.writeHead(302, { location: `${good.url}/.well-known/did.json` })
    res.end()
  })
  const doc = { ...good.document(), id: redirecting.did }
  good.document = () => doc
  const call = (side, target, extra = {}) => send(side, 'com.example.forwarded.thing', { headers: { 'atproto-proxy': `${target}#x` }, ip: freshIp(), ...extra })
  const [a, b] = await Promise.all(sides().map((side) => call(side, redirecting.did)))
  if (a.status !== b.status || a.body?.error !== b.body?.error) await differ('a call forwarded to a did:web whose document redirects', { target: '<a did:web of its own>#x' }, { status: a.status, error: a.body?.error }, { status: b.status, error: b.body?.error }, 'spec/PROXY.md: a redirect is not followed')
  count('forwarded, a did:web document that redirects', false)
  if (good.requests.length) await differ('a did:web document that redirects', { target: '<a did:web of its own>#x' }, 'the redirect not followed', good.requests, 'spec/PROXY.md')
  const acct = await serverAccount()
  const big = await ownHost((req, res, h) => {
    res.writeHead(200, { 'content-type': 'application/json' })
    res.end(JSON.stringify(h.document()).replace(/}$/, `${' '.repeat(70000)}}`))
  })
  const r = await call(cand, big.did, { token: acct.access })
  if (r.status !== 500 || r.body?.error !== 'InternalServerError') await broke('target', { target: '<a did:web whose document is 70000 bytes>#x' }, '500 InternalServerError: a document past 65536 bytes is as one that cannot be fetched', { status: r.status, error: r.body?.error })
  const huge = await ownHost((req, res, h) => {
    res.writeHead(200, { 'content-type': 'application/json' })
    if (req.url === '/.well-known/did.json') return res.end(JSON.stringify(h.document()))
    res.end(`{"x":"${'x'.repeat(17 * 1024 * 1024)}"}`)
  })
  const s = await call(cand, huge.did, { token: acct.access })
  if (s.status !== 502 || s.body?.error !== 'UpstreamFailure') await broke('forwarded answer', { target: '<a service answering 17 MiB>#x' }, '502 UpstreamFailure: an answer past 16 MiB', { status: s.status, error: s.body?.error })
  for (const h of [good, redirecting, big, huge]) await h.close()
  count('what the server reads of what it fetches', false)
}

/// The slots of requests out (spec/LIMITS.md, "Requests out"): 256 calls
/// forwarded to the AppView, held there, each answered with its headers at
/// once and then a byte of its body each second, so that none is given up
/// for want of an answer (spec/PROXY.md, "SSRF protection", "What is
/// answered"); a 257th never reaches it, and is `502 UpstreamFailure` once
/// it has waited 10 s; the 256 then ended, each is answered, and a call
/// forwarded after them is.
async function requestsOut() {
  const acct = await serverAccount()
  const ip = freshIp()
  const call = () => send(cand, FORWARDED, { params: FORWARDED_PARAMS, token: acct.access, ip })
  const ends = []
  answerWith(() => ({
    respond: (res) => {
      res.on('error', () => {})
      res.writeHead(200, { 'content-type': 'application/json' })
      res.write('{')
      const tick = setInterval(() => res.destroyed || res.write(' '), 1000)
      ends.push(() => {
        clearInterval(tick)
        if (!res.destroyed) res.end('}')
      })
    },
  }))
  const release = () => {
    answerWith()
    for (const e of ends.splice(0)) e()
  }
  const fail = async (...args) => {
    release()
    await broke(...args)
  }
  let answered = 0
  const held = Array.from({ length: 256 }, () => call().then((r) => ((answered += 1), r)))
  const filling = Date.now()
  while (ends.length < 256 && Date.now() - filling < 8000) await wait(20)
  if (ends.length < 256) await fail('requests out', { forwarded: 256, to: 'an AppView that holds each open' }, '256 in flight at once, within 8 s', { reached: ends.length, answered })
  const late = call()
  const r = await Promise.race([late, wait(20000).then(() => null)])
  const extra = ends.length - 256
  const early = answered
  if (r === null || extra || early || r.status !== 502 || r.body?.error !== 'UpstreamFailure' || r.ms < 9500 || r.ms > 13000) {
    await fail(FORWARDED, { forwarded: 'a 257th', while: '256 requests out are held open' }, '502 UpstreamFailure after waiting 10 s for a slot (9.5 to 13 s), not sent, the 256 still held', { status: r?.status ?? 'no answer within 20 s', error: r?.body?.error, ms: r?.ms, sent: extra > 0, heldAnswered: early })
  }
  release()
  const after = await Promise.all(held)
  const bad = after.filter((a) => a.status !== 200)
  if (bad.length) await broke(FORWARDED, { forwarded: 256, held: 'until the 257th was answered, then ended' }, 'each 200, the answer as it came', { notOk: bad.length, first: { status: bad[0].status, error: bad[0].body?.error, ms: bad[0].ms } })
  takeRequests()
  const again = await call()
  const sent = takeRequests().filter((q) => q.service === 'appview' && q.path.startsWith(`/xrpc/${FORWARDED}`)).length
  if (again.status !== 200 || sent !== 1 || again.ms > 5000) await broke(FORWARDED, { forwarded: 'one more', after: 'the 256 held were answered' }, '200, sent to the AppView once, within 5 s', { status: again.status, error: again.body?.error, sent, ms: again.ms })
  count('requests out, 256 at once', true)
}

/// A subscription to `side`'s firehose from `ip`, live.
function subscribe(side, ip) {
  const ws = new WebSocket(`${side.url.replace(/^http/, 'ws')}/xrpc/com.atproto.sync.subscribeRepos`, { headers: { 'x-forwarded-for': ip } })
  const sub = { ws, events: 0, closed: null }
  ws.binaryType = 'arraybuffer'
  ws.onmessage = () => (sub.events += 1)
  ws.onclose = (e) => (sub.closed = { code: e.code })
  ws.onerror = () => {}
  sub.opened = new Promise((ok) => {
    ws.onopen = () => ok(true)
    setTimeout(() => ok(false), 3000)
  })
  return sub
}

/// The status a WebSocket upgrade to the server's firehose from `ip` is
/// answered with: 101, or the HTTP status that refused it.
function upgradeStatus(ip) {
  return new Promise((ok) => {
    const req = httpRequest(`${cand.url}/xrpc/com.atproto.sync.subscribeRepos`, { headers: { connection: 'Upgrade', upgrade: 'websocket', 'sec-websocket-version': '13', 'sec-websocket-key': Buffer.from('spec-pds-harness').toString('base64'), 'x-forwarded-for': ip } })
    req.on('upgrade', (_res, socket) => {
      socket.destroy()
      ok(101)
    })
    req.on('response', (res) => {
      res.resume()
      ok(res.statusCode)
    })
    req.on('error', (err) => ok(`error ${err.message}`))
    req.end()
  })
}

/// Client messages on a subscription: small ones ignored on both sides,
/// with events still sent; on the server's, one of 4096 bytes ignored, one
/// of 4097 closing it with 1009, and one of 2 MiB closing it, with 1009 or,
/// as the server stops reading it at its header, with no close frame
/// (spec/LIMITS.md, "Firehose").
async function firehoseMessages() {
  const subs = sides().map((side) => subscribe(side, freshIp()))
  await Promise.all(subs.map((s) => s.opened))
  for (const s of subs) {
    s.ws.send('hello')
    s.ws.send(new Uint8Array(1024))
  }
  await wait(300)
  await createAccount()
  const t = Date.now()
  while (subs.some((s) => s.events === 0) && Date.now() - t < 3000) await wait(20)
  const [x, y] = subs.map((s) => ({ open: s.closed === null, events: s.events > 0 }))
  if (!isDeepStrictEqual(x, y)) await differ('subscribeRepos after small client messages', { sent: ['hello', '1 KiB'] }, x, y)
  for (const s of subs) s.ws.close()
  count('firehose, small client messages', true)

  // What one message of `size` bytes does to a new subscription to the
  // server's firehose, within 3 s: `open`, or its close code.
  const sent = async (size) => {
    const sub = subscribe(cand, freshIp())
    if (!(await sub.opened)) await broke('firehose', { subscribe: 'live' }, 'a subscription', 'none')
    sub.ws.send(new Uint8Array(size))
    const t = Date.now()
    while (sub.closed === null && Date.now() - t < 3000) await wait(20)
    if (sub.closed === null) sub.ws.close()
    return sub.closed?.code ?? 'open'
  }
  const at = await sent(4096)
  if (at !== 'open') await broke('firehose', { sent: '4096 bytes' }, 'ignored: the subscription still open after 3 s', at)
  const over = await sent(4097)
  if (over !== 1009) await broke('firehose', { sent: '4097 bytes' }, 'the subscription closed with 1009 within 3 s', over)
  const big = await sent(2 * 1024 * 1024)
  if (big !== 1009 && big !== 1006) await broke('firehose', { sent: '2 MiB' }, 'the subscription closed within 3 s, with 1009 or no close frame', big)
  count('firehose, a big client message', false)
}

/// 16 subscriptions from one address, a 17th refused, and one from another
/// address taken (spec/LIMITS.md, "Firehose").
async function firehoseSubscribers() {
  const ip = freshIp()
  const held = Array.from({ length: 16 }, () => subscribe(cand, ip))
  const opened = await Promise.all(held.map((s) => s.opened))
  if (opened.some((o) => !o)) await broke('firehose', { subscriptions: 16, from: 'one address' }, 'all open', `${opened.filter(Boolean).length} open`)
  const more = await upgradeStatus(ip)
  if (more !== 429) await broke('firehose', { subscriptions: 'a 17th', from: 'the same address' }, 'refused: 429', more)
  const other = await upgradeStatus(freshIp())
  if (other !== 101) await broke('firehose', { subscriptions: 'one', from: 'another address' }, 101, other)
  for (const s of held) s.ws.close()
  count('firehose, subscriptions from one address', false)
}

/// The server's calls to the PLC directory, through the proxy: describeRepo
/// of an account it made asks nothing; after updateHandle, describeRepo
/// answers the new document at once (spec/LIMITS.md, "The PLC directory").
async function plcDocuments() {
  const acct = await serverAccount()
  if (acct.res.status !== 200) await broke(CREATE_ACCOUNT, {}, 200, acct.res)
  const path = `/${acct.did}`
  const before = plcProxy.countOf(path)
  const ip = freshIp()
  await Promise.all(Array.from({ length: 10 }, () => send(cand, 'com.atproto.repo.describeRepo', { params: { repo: acct.did }, ip })))
  for (let i = 0; i < 10; i++) await send(cand, 'com.atproto.repo.describeRepo', { params: { repo: acct.handle }, ip })
  const after = plcProxy.countOf(path)
  if (after !== before) await broke('calls to the PLC directory', { describeRepo: 20 }, `none for the document of an account it made`, `${after - before} requests for ${path}`)
  count('describeRepo, its document kept', true)

  const handle = `${freshFront()}.test`
  const changed = await send(cand, UPDATE_HANDLE, { body: { handle }, token: acct.access, ip })
  if (changed.status !== 200) await broke(UPDATE_HANDLE, { handle }, 200, changed)
  const d = await send(cand, 'com.atproto.repo.describeRepo', { params: { repo: handle }, ip })
  if (d.status !== 200 || d.body?.didDoc?.alsoKnownAs?.[0] !== `at://${handle}`) await broke('describeRepo after updateHandle', { repo: handle }, `the document naming at://${handle}, at once`, d.body)
  await checkDidDoc(cand, acct.did, d.body.didDoc, 'describeRepo after updateHandle, its document kept', { repo: handle })
}

/// While 24 accounts are made at once, with each call to the directory held
/// up, no more than 8 are in flight (spec/LIMITS.md, "The PLC directory").
async function plcAtOnce() {
  const ip = freshIp()
  plcProxy.delayMs = 150
  plcProxy.maxInFlight = 0
  const made = await Promise.all(Array.from({ length: 24 }, (_, i) => serverAccount(i % 2 ? ip : freshIp())))
  plcProxy.delayMs = 0
  const failed = made.filter((m) => m.res.status !== 200)
  if (failed.length) await broke(CREATE_ACCOUNT, { accounts: 24, at: 'once' }, 'each made', failed.map((m) => m.res.body))
  if (plcProxy.maxInFlight > 8) await broke('calls to the PLC directory', { createAccount: 24, at: 'once' }, '8 in flight at most', plcProxy.maxInFlight)
  count('PLC calls at once', true)
}

/// Of 6 accounts made one after another, each post to the directory the
/// first request on a connection of its own, and the reads that follow
/// them over connections earlier reads left open (spec/LIMITS.md, "The PLC
/// directory").
async function plcConnections() {
  // What each connection carried before, so that connections left open by
  // earlier checks count too.
  const before = plcProxy.connections.map((c) => c.length)
  for (let i = 0; i < 6; i++) {
    const made = await serverAccount(freshIp())
    if (made.res.status !== 200) await broke(CREATE_ACCOUNT, { accounts: 6, 'one after another': true }, 200, made.res)
  }
  const request = { createAccount: 6, 'one after another': true }
  const fresh = plcProxy.connections.map((c, i) => ({ earlier: before[i] ?? 0, now: c.slice(before[i] ?? 0) })).filter((c) => c.now.length)
  const isPost = (r) => r.startsWith('POST ')
  const shared = fresh.filter((c) => c.now.some(isPost) && (c.earlier > 0 || !isPost(c.now[0]) || c.now.filter(isPost).length > 1))
  if (shared.length) await broke('connections to the PLC directory', request, 'each post the first request on a connection of its own', shared)
  const reads = fresh.flatMap((c) => c.now).filter((r) => r.startsWith('GET ')).length
  const readOver = fresh.filter((c) => !c.now.some(isPost)).length
  if (!reads || readOver >= reads) await broke('connections to the PLC directory', request, 'the reads over connections earlier reads left open: fewer connections than reads', { reads, connections: readOver })
  count('PLC connections', true)
}

/// An issuer that is an account here: authorizeSpaceSync with a delegation
/// token the server signed for one of its accounts, of a space another of
/// its accounts made, public: taken, with nothing read of the user's
/// document from the directory; and one signed before its account was
/// deleted, with the document read, a deleted account's DID being any
/// other issuer's (spec/SPACES-CREDENTIALS.md, "Issuers' keys").
async function plcLocalIssuer() {
  const ip = freshIp()
  const [authority, user] = [await serverAccount(ip), await serverAccount(ip)]
  if (authority.res.status !== 200 || user.res.status !== 200) await broke(CREATE_ACCOUNT, {}, 200, [authority.res, user.res])
  const space = `at://${authority.did}/space/com.example.group/local`
  const made = await send(cand, 'com.atproto.space.createSpace', { body: { spaceType: 'com.example.group', skey: 'local', policy: { $type: 'com.atproto.memberspace.policy', publicRead: true } }, token: authority.access, ip })
  if (made.status !== 200) await broke('com.atproto.space.createSpace', { publicRead: true }, 200, made)
  const request = { delegationToken: 'by an account here, of a space another account here made', space: 'public' }
  const { P256Keypair } = await import('@atproto/crypto')
  const credentialKey = (await P256Keypair.create()).did()
  const trade = (token) => send(cand, 'com.atproto.space.authorizeSpaceSync', { body: { space, credentialKey, delegationToken: token }, ip })
  const tokenOf = async (access) => {
    const t = await send(cand, 'com.atproto.space.getDelegationToken', { params: { space }, token: access, ip })
    if (t.status !== 200) await broke('com.atproto.space.getDelegationToken', {}, 200, t)
    return t.body?.token
  }
  for (let i = 0; i < 3; i++) {
    const before = plcProxy.countOf(`/${user.did}`)
    const traded = await trade(await tokenOf(user.access))
    const read = plcProxy.countOf(`/${user.did}`) - before
    if (traded.status !== 200 || read) await broke('com.atproto.space.authorizeSpaceSync', request, '200, with nothing read of the user\'s document', { status: traded.status, error: traded.body?.error, reads: read })
  }
  count('an issuer that is an account here, nothing read', true)

  // A deleted account (spec/LIFECYCLE.md, "deleteAccount").
  const front = freshFront()
  const password = `pw-${front}`
  const gone = await send(cand, CREATE_ACCOUNT, { body: { handle: `${front}.test`, email: `${front}@example.com`, password }, ip })
  if (gone.status !== 200) await broke(CREATE_ACCOUNT, { password: true }, 200, gone)
  const signed = await tokenOf(gone.body?.accessJwt)
  takeMail(cand)
  const asked = await send(cand, 'com.atproto.server.requestAccountDelete', { token: gone.body?.accessJwt, method: 'POST', ip })
  const mailed = takeMail(cand).filter((m) => m.kind === 'deleteAccount').at(-1)?.token
  if (asked.status !== 200 || !mailed) await broke('com.atproto.server.requestAccountDelete', {}, '200, a deleteAccount token mailed', { status: asked.status, mailed: Boolean(mailed) })
  const deleted = await send(cand, 'com.atproto.server.deleteAccount', { body: { did: gone.body.did, password, token: mailed }, ip })
  if (deleted.status !== 200) await broke('com.atproto.server.deleteAccount', {}, 200, deleted)
  const before = plcProxy.countOf(`/${gone.body.did}`)
  await trade(signed)
  const read = plcProxy.countOf(`/${gone.body.did}`) - before
  if (!read) await broke('com.atproto.space.authorizeSpaceSync', { delegationToken: 'signed by the server before its account was deleted' }, 'the deleted account\'s document read', 'nothing read')
  count('an issuer whose account here was deleted, its document read', false)
}

/// How long until a connection the server is sent `bytes` on, and then
/// nothing, is closed; null if not within `ms`.
function closedAfter(bytes, ms) {
  return new Promise((ok) => {
    const { port } = new URL(cand.url)
    const started = Date.now()
    const socket = connect(Number(port), '127.0.0.1', () => socket.write(bytes))
    const done = () => ok(Date.now() - started)
    socket.on('close', done)
    socket.on('error', () => {})
    socket.on('data', () => {})
    setTimeout(() => {
      socket.off('close', done)
      socket.destroy()
      ok(null)
    }, ms)
  })
}

/// Half a request head, closed within 15 s; a body that stops, within 40
/// (spec/LIMITS.md, "Timeouts").
async function timeouts() {
  const head = closedAfter('GET /xrpc/com.atproto.server.describeServer HTTP/1.1\r\nHost: localhost\r\n', 15000)
  const body = closedAfter('POST /xrpc/com.atproto.server.createSession HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nContent-Length: 100\r\n\r\n{"identifier":', 40000)
  const [h, b] = await Promise.all([head, body])
  const open = [...(h === null ? ['half a request head, then nothing: still open after 15 s'] : []), ...(b === null ? ['a request head and 14 of its 100 bytes of body, then nothing: still open after 40 s'] : [])]
  if (open.length) await broke('timeouts', { sent: ['half a request head', 'a body that stops'] }, 'each connection closed: the head within 15 s, the body within 40 s', open)
  count('timeouts', true)
}

/// Restarted with a floor no disk has free: every write 507, reads
/// answered, the server running; restarted without, writes land
/// (spec/LIMITS.md, "Disk", "Environment").
async function diskFloor() {
  const acct = await serverAccount()
  const ip = freshIp()
  const front = freshFront()
  const sessions = await Promise.all([0, 1].map(() => send(cand, REFRESH, { token: acct.refresh, method: 'POST', ip })))
  const record = { $type: 'com.example.note', n: 1 }
  const made = await send(cand, CREATE_RECORD, { body: { repo: acct.did, collection: 'com.example.note', rkey: 'floor', record }, token: acct.access, ip })
  if (made.status !== 200) await broke(CREATE_RECORD, {}, 200, made)
  const oauthFront = freshFront()
  const oauthAcct = { handle: `${oauthFront}.test`, password: `pw-${oauthFront}` }
  const withPw = await send(cand, CREATE_ACCOUNT, { body: { handle: oauthAcct.handle, email: `${oauthFront}@example.com`, password: oauthAcct.password }, ip })
  if (withPw.status !== 200) await broke(CREATE_ACCOUNT, { password: true }, 200, withPw)
  const os = await serverSession(oauthAcct)
  // The account's OAuth session and the device it signed in on, by the ids
  // its password session lists them by, to end below the floor
  // (spec/OAUTH-ACCOUNT.md, "Requests").
  const pw = withPw.body?.accessJwt
  const listed = async (route, key) => (await send(cand, route, { token: pw, ip })).body?.[key] ?? []
  const [sessionId] = (await listed('_oauthSessions', 'sessions')).map((x) => x.id)
  const [deviceId] = (await listed('_oauthDevices', 'devices')).map((x) => x.id)
  if (!sessionId || !deviceId) await broke('_oauthSessions and _oauthDevices (spec/OAUTH-ACCOUNT.md, "_oauthSessions")', {}, 'the session and device of the OAuth sign-in', { sessionId, deviceId })
  // Two passkeys of the account, one signed in with on a device of its own,
  // remembered, for below the floor (spec/PASSKEYS.md, "passkey-register",
  // "passkey-sign-in").
  const who = serverWho({ did: withPw.body?.did, handle: withPw.body?.handle, token: pw })
  const pa = await addPasskey(who)
  const pb = pa && (await addPasskey(who))
  const pd = newDevice()
  if (!pb || !(await parOn(pd)) || !(await passkeySignIn(who, pa, pd, { remember: true, what: 'before PDS_MIN_FREE_BYTES' }))) return
  await restartServer({ PDS_MIN_FREE_BYTES: '1152921504606846976' }, null, 'with PDS_MIN_FREE_BYTES above any disk\'s free space')
  const writes = [
    [CREATE_ACCOUNT, { body: { handle: `${front}.test`, email: `${front}@example.com` } }],
    [SESSION, { body: { identifier: acct.handle, password: 'x' } }],
    [REFRESH, { token: sessions[0].body?.refreshJwt, method: 'POST' }],
    [DELETE_SESSION, { token: sessions[1].body?.refreshJwt, method: 'POST' }],
    [CREATE_AP, { body: { name: 'floor' }, token: acct.access }],
    [REVOKE_AP, { body: { name: 'floor' }, token: acct.access }],
    [UPDATE_HANDLE, { body: { handle: `${freshFront()}.test` }, token: acct.access }],
    [CREATE_RECORD, { body: { repo: acct.did, collection: 'com.example.note', record }, token: acct.access }],
    [PUT_RECORD, { body: { repo: acct.did, collection: 'com.example.note', rkey: 'floor', record: { ...record, n: 2 } }, token: acct.access }],
    [DELETE_RECORD, { body: { repo: acct.did, collection: 'com.example.note', rkey: 'floor' }, token: acct.access }],
    [APPLY, { body: { repo: acct.did, writes: [{ $type: 'com.atproto.repo.applyWrites#create', collection: 'com.example.note', value: record }] }, token: acct.access }],
    [UPLOAD, { raw: 'spec-pds blob floor', contentType: 'text/plain', token: acct.access }],
    [CODE, { body: { useCount: 1 }, auth: ADMIN }],
    [CODES, { body: { codeCount: 1, useCount: 1 }, auth: ADMIN }],
    ['app.bsky.actor.putPreferences', { body: { preferences: [] }, token: acct.access }],
  ]
  for (const [nsid, request] of writes) {
    const r = await send(cand, nsid, { ...request, ip })
    if (r.status !== 507 || r.body?.error !== 'InsufficientStorage') await broke(nsid, { below: 'PDS_MIN_FREE_BYTES' }, '507 InsufficientStorage', { status: r.status, error: r.body?.error })
  }
  for (const [nsid, params, token] of [[DESCRIBE_SERVER, {}], ['com.atproto.repo.getRecord', { repo: acct.did, collection: 'com.example.note', rkey: 'floor' }], ['com.atproto.sync.getLatestCommit', { did: acct.did }], ['app.bsky.actor.getPreferences', {}, acct.access]]) {
    const r = await send(cand, nsid, { params, token, ip })
    if (r.status !== 200) await broke(nsid, { below: 'PDS_MIN_FREE_BYTES' }, 'a read answered, 200', r)
  }
  // OAuth (spec/OAUTH.md, "Free space"): a PAR and a refresh refused, a
  // read with an OAuth token answered.
  const p = form({ client_id: 'http://localhost', response_type: 'code', redirect_uri: 'http://127.0.0.1/', scope: 'atproto', state: fresh(), code_challenge: pkce().challenge, code_challenge_method: 'S256' })
  const parred = await new Dpop(await newKey()).send(`${cand.url}/oauth/par`, { method: 'POST', headers: { ...p.headers, 'x-forwarded-for': ip }, body: p.body })
  const t = form({ grant_type: 'refresh_token', client_id: os.clientId, refresh_token: os.refresh })
  const refreshed = await os.dpop.send(`${cand.url}/oauth/token`, { method: 'POST', headers: t.headers, body: t.body })
  for (const [what, r] of [['PAR', parred], ['a refresh at the token endpoint', refreshed]]) {
    if (r.status !== 507 || r.body?.error !== 'temporarily_unavailable') await broke(what, { below: 'PDS_MIN_FREE_BYTES' }, '507 temporarily_unavailable', { status: r.status, error: r.body?.error })
  }
  const read = await os.dpop.send(`${cand.url}/xrpc/com.atproto.server.getSession`, { accessToken: os.access })
  if (read.status !== 200) await broke('getSession with an OAuth token', { below: 'PDS_MIN_FREE_BYTES' }, 'a read answered, 200', { status: read.status, error: read.body?.error })
  // The revocations are not refused: a session ended, its token then
  // refused, and a device signed out of (spec/OAUTH-ACCOUNT.md, "Requests").
  for (const [route, id] of [['_oauthRevokeSession', sessionId], ['_oauthRevokeDevice', deviceId]]) {
    const r = await send(cand, route, { token: pw, body: { id }, ip })
    if (r.status !== 200 || !isDeepStrictEqual(r.body, {})) await broke(`${route} (spec/OAUTH-ACCOUNT.md, "Requests")`, { below: 'PDS_MIN_FREE_BYTES' }, '200 {}', r)
  }
  const gone = await os.dpop.send(`${cand.url}/xrpc/com.atproto.server.getSession`, { accessToken: os.access })
  if (gone.status !== 401 || gone.body?.error !== 'invalid_token') await broke('getSession with the token of a session _oauthRevokeSession ended (spec/OAUTH-ACCOUNT.md, "Requests")', { below: 'PDS_MIN_FREE_BYTES' }, '401 invalid_token', { status: gone.status, error: gone.body?.error })
  if ((await listed('_oauthDevices', 'devices')).some((x) => x.id === deviceId)) await broke('_oauthDevices after _oauthRevokeDevice (spec/OAUTH-ACCOUNT.md, "Requests")', { below: 'PDS_MIN_FREE_BYTES' }, 'the device signed out of, not listed', deviceId)
  // Passkeys (spec/PASSKEYS.md, "Requests", "passkey-register",
  // "passkey-sign-in"): `_passkeyAdd` answered and its page loaded, but
  // `passkey-register` `507`, adding nothing, the link holding; a sign-in
  // `507`, signing in nothing; `_passkeyRemove` answered, the passkey gone.
  const unavailable = (r) => r?.status === 507 && r.json?.error === 'temporarily_unavailable' && typeof r.json?.error_description === 'string'
  const link = await addLink(who)
  const lb = new Browser()
  const lp = link && (await checkPage(lb, link, 'below PDS_MIN_FREE_BYTES'))
  if (lp) {
    const r = await lb.api(link.url, '/passkey-register', { credential: registration(newPasskey({ rpId: rpId() }), { challenge: lp.challenge, origin: origin() }).json })
    if (!unavailable(r)) await broke('passkey-register (spec/PASSKEYS.md, "passkey-register")', { below: 'PDS_MIN_FREE_BYTES' }, '507 temporarily_unavailable', { status: r.status, body: r.json })
    await checkPage(lb, link, 'after its passkey-register was refused below PDS_MIN_FREE_BYTES')
  }
  const challenge = await optionsOn(pd)
  const refusedAt = pa.count + 1
  const signed = challenge && (await signInOn(pd, assertion(pa, { challenge, origin: origin() }, { count: refusedAt }).json, { remember: true }))
  if (!unavailable(signed)) await broke('passkey-sign-in (spec/PASSKEYS.md, "passkey-sign-in")', { below: 'PDS_MIN_FREE_BYTES' }, '507 temporarily_unavailable', { status: signed?.status, body: signed?.json })
  const removed = await send(cand, '_passkeyRemove', { token: pw, body: { id: b64u(pb.id) }, ip })
  if (removed.status !== 200 || !isDeepStrictEqual(removed.body, {})) await broke('_passkeyRemove (spec/PASSKEYS.md, "Requests")', { below: 'PDS_MIN_FREE_BYTES' }, '200 {}', removed)
  who.passkeys = [pa]
  await checkList(who, 'below PDS_MIN_FREE_BYTES', [pa], { gone: [pb], cite: 'spec/PASSKEYS.md, "passkey-sign-in"' })
  if (server.exited) await broke('disk floor', {}, 'still running', server.exited)
  await restartServer({ PDS_MIN_FREE_BYTES: undefined }, null, 'without PDS_MIN_FREE_BYTES')
  // The sign-in refused kept nothing: an assertion of its count is taken now;
  // the removal was kept (spec/PASSKEYS.md, "passkey-sign-in", "Durability").
  const pe = newDevice()
  if (!(await parOn(pe)) || !(await passkeySignIn(who, pa, pe, { remember: false, f: { count: refusedAt }, what: 'with the count of one refused below PDS_MIN_FREE_BYTES' }))) return
  await checkList(who, 'once the floor is lifted', [pa], { gone: [pb], cite: 'spec/PASSKEYS.md, "Durability"' })
  const again = await send(cand, CREATE_RECORD, { body: { repo: acct.did, collection: 'com.example.note', record }, token: acct.access, ip })
  if (again.status !== 200) await broke(CREATE_RECORD, { after: 'the floor is lifted' }, 200, again)
  count('writes below the disk floor', false)
}

/// A blob ref to an upload answered `res`.
const refTo = (res) => ({ $type: 'blob', ref: res.body.blob.ref, mimeType: res.body.blob.mimeType, size: res.body.blob.size })

/// The server with a blob quota of 12 MiB and unreferenced uploads held
/// 3 s, an account of its own, and the calls to upload and to reference.
async function blobServer() {
  await restartServer({ PDS_BLOB_QUOTA_BYTES: String(12 * 1024 * 1024), PDS_BLOB_TTL_SECS: '3' }, null, 'with a blob quota of 12 MiB, unreferenced uploads held 3 s')
  const acct = await serverAccount()
  const ip = freshIp()
  return {
    acct,
    ip,
    five: (tag) => Buffer.concat([Buffer.from(`spec-pds blob ${tag} ${rand()} `), Buffer.alloc(5 * 1024 * 1024 - 64, tag)]),
    up: (bytes) => send(cand, UPLOAD, { raw: bytes, contentType: 'application/octet-stream', token: acct.access, ip }),
    write: (blob) => send(cand, CREATE_RECORD, { body: { repo: acct.did, collection: 'com.example.blob', record: { $type: 'com.example.blob', blob } }, token: acct.access, ip }),
  }
}

/// With a quota of 12 MiB: a third upload of 5 MiB refused, the same bytes
/// again taken (spec/LIMITS.md, "Blobs", "Environment").
async function blobQuota() {
  const { five, up } = await blobServer()
  const [A, B, C] = ['a', 'b', 'c'].map(five)
  const a = await up(A)
  const b = await up(B)
  if (a.status !== 200 || b.status !== 200) await broke(UPLOAD, { uploads: '2 of 5 MiB, quota 12 MiB' }, 200, [a.status, b.status])
  const c = await up(C)
  if (c.status !== 400 || c.body?.error !== 'BlobQuotaExceeded') await broke(UPLOAD, { uploads: 'a third of 5 MiB, quota 12 MiB' }, '400 BlobQuotaExceeded', { status: c.status, error: c.body?.error })
  const again = await up(A)
  if (again.status !== 200) await broke(UPLOAD, { uploads: 'bytes already held, at the quota' }, 200, { status: again.status, error: again.body?.error })
  await restartServer({ PDS_BLOB_QUOTA_BYTES: undefined, PDS_BLOB_TTL_SECS: undefined }, null, 'with the defaults')
  count('blob quota', false)
}

/// With unreferenced uploads held 3 s, and a quota of 12 MiB: an upload
/// referenced in time kept, one not gone after its time, freeing its
/// quota; one whose time ran out while the server was down gone when it
/// starts (spec/LIMITS.md, "Blobs").
async function blobTtl() {
  const { acct, ip, five, up, write } = await blobServer()
  const [A, B, C] = ['a', 'b', 'c'].map(five)
  const a = await up(A)
  const b = await up(B)
  if (a.status !== 200 || b.status !== 200) await broke(UPLOAD, { uploads: '2 of 5 MiB, quota 12 MiB' }, 200, [a.status, b.status])
  const kept = await write(refTo(a))
  if (kept.status !== 200) await broke(CREATE_RECORD, { ref: 'an upload, in time' }, 200, kept)
  await wait(4500)
  const late = await write(refTo(b))
  if (late.status !== 400 || late.body?.error !== 'BlobNotFound') await broke(CREATE_RECORD, { ref: 'an upload 4.5 s old, held 3 s' }, '400 BlobNotFound', { status: late.status, error: late.body?.error })
  const freed = await up(C)
  if (freed.status !== 200) await broke(UPLOAD, { uploads: '5 MiB, once an unreferenced upload was gone' }, 200, { status: freed.status, error: freed.body?.error })
  const d = await up(Buffer.from(`spec-pds blob down ${rand()}`))
  await server.kill()
  await wait(4000)
  server.start()
  await serving('after an upload outlived its time while the server was down')
  const down = await write(refTo(d))
  if (down.status !== 400 || down.body?.error !== 'BlobNotFound') await broke(CREATE_RECORD, { ref: 'an upload whose time ran out while the server was down' }, '400 BlobNotFound', { status: down.status, error: down.body?.error })
  const served = await send(cand, 'com.atproto.sync.getBlob', { params: { did: acct.did, cid: a.body.blob.ref.$link }, ip })
  if (served.status !== 200) await broke('getBlob', { blob: 'referenced in time' }, 200, served.status)
  await restartServer({ PDS_BLOB_QUOTA_BYTES: undefined, PDS_BLOB_TTL_SECS: undefined }, null, 'with the defaults')
  count('unreferenced uploads', false)
}

/// 300 accounts, each with records enough that its log has been compacted
/// into a table (spec/STORE.md), a file the server opens to read it; then
/// the server started again under `ulimit -n 256`: it serves them all, a
/// record of each read from its table, and an early and a late one take
/// writes; 20 more made under the limit are each 200 or 500, and it never
/// exits; after a restart without the limit, those answered 500 do not
/// exist (spec/LIMITS.md, "Open files and threads", "Disk").
async function openFiles() {
  const made = []
  let ip = freshIp()
  for (let i = 0; i < 300; i += 10) {
    if (i % 50 === 0) ip = freshIp()
    made.push(...(await Promise.all(Array.from({ length: 10 }, () => serverAccount(ip)))))
  }
  const failed = made.filter((m) => m.res.status !== 200)
  if (failed.length) await broke(CREATE_ACCOUNT, { accounts: 300 }, 'each made', failed.slice(0, 3).map((m) => m.res.body))
  // Three records of 140000 bytes are past the mebibyte a log may hold
  // before it is compacted; the second for the last compactions to end.
  const text = 'x'.repeat(140000)
  for (let i = 0; i < made.length; i += 10) {
    if (i % 50 === 0) ip = freshIp()
    const written = await Promise.all(made.slice(i, i + 10).map(async (m) => {
      for (let n = 0; n < 3; n++) {
        const r = await send(cand, CREATE_RECORD, { body: { repo: m.did, collection: 'com.example.note', rkey: `big${n}`, record: { $type: 'com.example.note', n, text } }, token: m.access, ip })
        if (r.status !== 200) return r
      }
      return null
    }))
    const bad = written.find((r) => r)
    if (bad) await broke(CREATE_RECORD, { accounts: 300, bytes: text.length }, 200, bad)
  }
  await wait(1000)
  await restartServer({}, 256, 'with 300 accounts, under ulimit -n 256')
  ip = freshIp()
  for (let i = 0; i < made.length; i += 25) {
    const some = made.slice(i, i + 25)
    const answers = await Promise.all(some.map((m) => send(cand, 'com.atproto.sync.getLatestCommit', { params: { did: m.did }, ip })))
    const bad = answers.find((r) => r.status !== 200)
    if (bad) await broke('getLatestCommit', { accounts: 300, ulimit: 256 }, 200, bad)
    const records = await Promise.all(some.map((m) => send(cand, 'com.atproto.repo.getRecord', { params: { repo: m.did, collection: 'com.example.note', rkey: 'big0' }, ip })))
    const lost = records.find((r) => r.status !== 200 || r.body?.value?.text !== text)
    if (lost) await broke('getRecord', { accounts: 300, ulimit: 256, rkey: 'big0' }, 'the record, as written', { status: lost.status, error: lost.body?.error, bytes: lost.body?.value?.text?.length })
  }
  for (const m of [made[0], made[1], made.at(-2), made.at(-1)]) {
    const r = await send(cand, CREATE_RECORD, { body: { repo: m.did, collection: 'com.example.note', record: { $type: 'com.example.note', n: 1 } }, token: m.access, ip })
    if (r.status !== 200) await broke(CREATE_RECORD, { accounts: 300, ulimit: 256 }, 200, r)
  }
  const more = []
  for (let i = 0; i < 20; i++) more.push(await serverAccount(ip))
  const odd = more.filter((m) => m.res.status !== 200 && m.res.status !== 500)
  if (odd.length) await broke(CREATE_ACCOUNT, { ulimit: 256 }, '200 or 500', odd.map((m) => m.res))
  if (server.exited) await broke('open files', { ulimit: 256, accounts: 320 }, 'still running', server.exited)
  await serving('under ulimit -n 256, after 20 more accounts')
  await restartServer({}, null, 'without the limit')
  for (const m of more) {
    const r = await send(cand, 'com.atproto.repo.describeRepo', { params: { repo: m.handle }, ip })
    const want = m.res.status === 200 ? 200 : 400
    if (r.status !== want) await broke('describeRepo', { account: m.handle, createAccount: m.res.status }, want === 200 ? 'the account' : '400 RepoNotFound: an account answered 500 does not exist', { status: r.status, error: r.body?.error })
  }
  count('300 accounts under ulimit -n 256', true)
}

/// How many syncs to the disk the server has made, as its metrics count
/// them (spec/OPERATIONS.md, `pds_fsync_duration_seconds`).
async function syncsMade() {
  const text = await fetch(metricsUrl).then((r) => r.text())
  const m = text.match(/^pds_fsync_duration_seconds_count (\d+)$/m)
  if (!m) await broke('metrics', { metric: 'pds_fsync_duration_seconds_count' }, 'a count of syncs', 'none')
  return Number(m[1])
}

/// Sixty accounts, each written to once, at once, five times over, on a
/// server started after a shutdown: with `PDS_SYNC` `together` the server
/// makes fewer syncs than half the writes it answers, and with `each` one
/// for each at least; any other value is refused as it starts
/// (spec/DURABILITY.md, "Writes waiting at once").
async function writesTogether() {
  const made = []
  let ip = freshIp()
  for (let i = 0; i < 60; i += 10) {
    if (i % 50 === 0) ip = freshIp()
    made.push(...(await Promise.all(Array.from({ length: 10 }, () => serverAccount(ip)))))
  }
  const failed = made.filter((m) => m.res.status !== 200)
  if (failed.length) await broke(CREATE_ACCOUNT, { accounts: 60 }, 'each made', failed.slice(0, 3).map((m) => m.res.body))
  // Each account's own address, so that no address's limit of writes is met.
  const ips = made.map(() => freshIp())
  const wave = async (n) => {
    const answers = await Promise.all(made.map((m, i) => send(cand, CREATE_RECORD, { body: { repo: m.did, collection: 'com.example.note', record: { $type: 'com.example.note', n } }, token: m.access, ip: ips[i] })))
    const bad = answers.find((r) => r.status !== 200)
    if (bad) await broke(CREATE_RECORD, { accounts: 60, at: 'once', sync: server.env.PDS_SYNC }, 200, bad)
  }
  const waves = async () => {
    // The first wave is each account's first use since the start.
    await wave(0)
    const before = await syncsMade()
    for (let n = 1; n <= 5; n++) await wave(n)
    return (await syncsMade()) - before
  }
  // After a shutdown, so that the syncs counted are these writes' alone:
  // a start that followed a kill makes others (spec/STORE.md, "Scale").
  const restartClean = async (sync) => {
    await server.kill('SIGTERM')
    server.env.PDS_SYNC = sync
    server.ulimit = null
    server.start()
    await serving(`with PDS_SYNC ${sync}, after a shutdown`)
  }
  await restartClean('together')
  const together = await waves()
  console.error(`  300 writes, 60 at once to 60 accounts, PDS_SYNC together: ${together} syncs`)
  if (together >= 150) await broke('syncs', { writes: 300, atOnce: 60, PDS_SYNC: 'together' }, 'fewer than 150: the writes waiting at once share a sync', together)
  await restartClean('each')
  const each = await waves()
  console.error(`  the same, PDS_SYNC each: ${each} syncs`)
  if (each < 300) await broke('syncs', { writes: 300, atOnce: 60, PDS_SYNC: 'each' }, 'at least 300: each account synced by itself', each)
  await server.kill()
  server.env.PDS_SYNC = 'sometimes'
  server.start()
  const started = Date.now()
  while (!server.exited && Date.now() - started < 10000) await wait(20)
  const said = server.exited ? readFileSync(server.errPath, 'utf8') : ''
  if (server.exited?.code !== 1 || !said.includes('pds: PDS_SYNC: ')) await broke('start', { PDS_SYNC: 'sometimes' }, 'exits 1, `pds: PDS_SYNC: ` and why', { exited: server.exited, stderr: said.slice(-300) })
  await restartServer({ PDS_SYNC: undefined }, null, 'with the defaults')
  count('writes waiting at once share syncs', true)
}

/// The data directory 0700, every directory in it 0700 and file 0600,
/// but the harness's own log of the server (spec/LIMITS.md, "Files").
async function fileModes() {
  const wrong = []
  const walk = (dir) => {
    for (const f of readdirSync(dir)) {
      const p = join(dir, f)
      if (p === server.logPath) continue
      const st = statSync(p)
      const mode = st.mode & 0o777
      if (st.isDirectory()) {
        if (mode !== 0o700) wrong.push(`${p}: ${mode.toString(8)}`)
        walk(p)
      } else if (mode !== 0o600) wrong.push(`${p}: ${mode.toString(8)}`)
    }
  }
  const top = statSync(dataDir).mode & 0o777
  if (top !== 0o700) wrong.push(`${dataDir}: ${top.toString(8)}`)
  walk(dataDir)
  if (wrong.length) await broke('files', { dataDir, made: '0755 before it started' }, 'the directory and each directory in it 0700, each file 0600', wrong.slice(0, 20))
  count('file modes', true)
}

/// --limits: the reference's rate limits, then what the server holds.
export async function limitsRun() {
  const checks = [
    signupLimit, loginLimit, handleLimit, uploadLimit, uploadSize, writeLimit, forwardedLimit, globalLimit, emailRequestLimits, passwordResetLimit, resetLimit, accountDeleteRequestLimit, deleteLimit, ...OAUTH_LIMIT_CHECKS,
    sessionCap, appPasswordCap, ...OAUTH_SERVER_CHECKS, clientAddress, adminUnset, spacesPayFirst, didWebApart, outboundCaps, requestsOut, spaceImportSize,
    firehoseMessages, firehoseSubscribers, plcDocuments, plcAtOnce, plcConnections, plcLocalIssuer, timeouts, diskFloor, blobQuota, blobTtl, openFiles, writesTogether, fileModes,
  ]
  for (const check of checks) {
    if (ONLY && !ONLY.has(check.name)) continue
    console.error(`limits: ${check.name}`)
    await check()
  }
}