~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
96 lines · 5.9 KB Raw History
// Odd requests: those at the edges of what the spec settles, across the
// methods, each held to the reference.

import { isDeepStrictEqual } from 'node:util'
import { COLLECTIONS, RKEYS, chance, goodRecord, pick, rand, record } from '../lib/random.mjs'
import { cand, ref } from '../lib/processes.mjs'
import { count, differ } from '../lib/report.mjs'
import { cids, exists, nextBatch, noteCommit } from '../lib/state.mjs'
import { both, normalize, shown, xrpcHttp } from '../lib/xrpc.mjs'

/// A DID no server here holds.
const NOBODY = 'did:plc:aaaaaaaaaaaaaaaaaaaaaaaa'

/// Requests at the edges of what spec/PDS.md and its pages settle, each held to the
/// reference: writes with no token or one never given out, whatever their
/// input; a write to a repository no account has; the sync methods and
/// describeRepo of a DID no one holds; arguments their lexicon refuses; a
/// method called with the other HTTP method; and writes with `validate` set
/// (spec/PDS.md, "What it serves", "What the reference does that the specs
/// leave open"). Each names the account's repository, with its token.
export async function oddRequests(acct) {
  const collection = pick(COLLECTIONS)
  const bad = chance(0.5)
  const handle = acct.handle
  const writeBody = () => ({ repo: handle, collection: bad ? 'not an nsid' : collection, rkey: pick(RKEYS), record: record(collection, cids) })
  const r = rand()
  if (r < 0.2) {
    const nsid = pick(['com.atproto.repo.createRecord', 'com.atproto.repo.putRecord', 'com.atproto.repo.deleteRecord'])
    return void (await both(`write with no token${bad ? ', bad input' : ''}`, nsid, { body: writeBody() }))
  }
  if (r < 0.3) return void (await both('write with a token never given out, bad input', 'com.atproto.repo.createRecord', { body: { ...writeBody(), collection: 'not an nsid' }, token: 'never-given-out' }))
  if (r < 0.45) return void (await both('write to a repository no account has', 'com.atproto.repo.createRecord', { body: { ...writeBody(), collection, repo: NOBODY }, token: acct }))
  if (r < 0.6 && chance(0.5)) {
    // A repository the server does not hold is `400 RepoNotFound`
    // (spec/REPO.md, "What the reference does that the specs leave open";
    // spec/SYNC.md, "What the reference does that the specs leave open").
    const [nsid, params] = pick([
      ['com.atproto.repo.describeRepo', { repo: NOBODY }],
      ['com.atproto.sync.getLatestCommit', { did: NOBODY }],
      ['com.atproto.sync.getRepo', { did: NOBODY }],
    ])
    return void (await both('a repository no one holds', nsid, { params }))
  }
  if (r < 0.6) {
    // Arguments of the wrong syntax or type, which the method's lexicon
    // refuses. Over HTTP only: a command parses its flags first, as
    // COMMANDS.md says, and a value such as `reverse: maybe` is no flag.
    const k = pick(RKEYS)
    const [nsid, request] = pick([
      ['com.atproto.repo.createRecord', { body: { repo: handle, collection, record: goodRecord(collection), validate: null }, token: acct }],
      ['com.atproto.repo.listRecords', { params: { repo: handle, collection, reverse: 'maybe' } }],
      ['com.atproto.repo.createRecord', { body: { repo: 'not a did', collection, record: goodRecord(collection) }, token: acct }],
      ['com.atproto.repo.describeRepo', { params: { repo: 'not a did' } }],
      ['com.atproto.sync.getLatestCommit', { params: { did: 'not a did' } }],
      ['com.atproto.repo.getRecord', { params: { repo: handle, collection, rkey: k, cid: 'not a cid' } }],
      ['com.atproto.repo.createRecord', { body: { repo: handle, collection, record: goodRecord(collection), swapCommit: 'not a cid' }, token: acct }],
      ['com.atproto.repo.putRecord', { body: { repo: handle, collection, rkey: k, record: goodRecord(collection), swapRecord: 'not a cid' }, token: acct }],
      ['com.atproto.sync.getRepo', { params: { did: NOBODY, since: 'notarev' } }],
      ['com.atproto.server.createSession', { body: { identifier: handle, password: acct.password ?? 'x', authFactorToken: 5 } }],
    ])
    const [a, b] = await Promise.all([ref, cand].map((side) => xrpcHttp(side, nsid, request)))
    const [na, nb] = [normalize(ref, a), normalize(cand, b)]
    if (!isDeepStrictEqual(na, nb)) await differ(nsid, shown(request), na, nb, 'arguments the lexicon refuses')
    return count('arguments the lexicon refuses', a.status < 400)
  }
  if (r < 0.75) {
    // The other HTTP method: a procedure by GET, a query by POST.
    const [nsid, request] = chance(0.5)
      ? ['com.atproto.repo.createRecord', { method: 'GET' }]
      : ['com.atproto.repo.getRecord', { method: 'POST', body: {}, params: { repo: handle, collection, rkey: pick(RKEYS) } }]
    const [a, b] = await Promise.all([ref, cand].map((side) => xrpcHttp(side, nsid, { ...request, token: acct })))
    const [na, nb] = [normalize(ref, a), normalize(cand, b)]
    if (!isDeepStrictEqual(na, nb)) await differ(nsid, { by: request.method }, na, nb, 'called with the other HTTP method')
    return count('the other HTTP method', false)
  }
  // validate: true is refused for want of a lexicon; false writes, with no
  // validationStatus (spec/VALIDATION.md, "validate").
  const validate = chance(0.5)
  const key = pick(RKEYS)
  const value = { ...goodRecord(collection), batch: nextBatch() }
  const [nsid, body] = pick([
    ['com.atproto.repo.createRecord', { repo: handle, collection, rkey: key, record: value, validate }],
    ['com.atproto.repo.putRecord', { repo: handle, collection, rkey: key, record: value, validate }],
    ['com.atproto.repo.applyWrites', { repo: handle, validate, writes: [{ $type: `com.atproto.repo.applyWrites#${exists(acct, collection, key) ? 'update' : 'create'}`, collection, rkey: key, value }] }],
  ])
  const [a, b] = await both(`validate: ${validate}`, nsid, { body, token: acct })
  if (a.status < 400) {
    const cid = a.body.cid ?? a.body.results?.[0]?.cid
    if (cid) {
      acct.records.set(`${collection}/${key}`, cid)
      cids.push(cid)
    }
  }
  noteCommit(ref, acct, a)
  noteCommit(cand, acct, b)
}