chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
// What the server answers over XRPC, against HUD's own lexicons
// (schemas/): each answer hud-server's tests received, as they write them
// to the directory HUD_ANSWERS names (lines of `{path, status, body}`). A
// success must be valid for its method's output, a failure XRPC's
// `{error, message}` with an error its method declares (or AuthRequired,
// MethodNotImplemented), and every parameter of a success must be one it
// declares: a test asking one it does not, to see it refused, is a
// failure, and its refusal is held to the shape above. Every method must
// have been answered at least once.
//
// HUD_ANSWERS=$PWD/target/answers cargo test -p hud-server
// node harness/answers.mjs target/answers
//
// (`just lex` runs both.) @atproto/lexicon is the harness's: cd harness &&
// npm ci, once.
import { readdirSync, readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const { Lexicons, jsonToLex } = createRequire(import.meta.url)('@atproto/lexicon')
// A space lexicon (the permissioned-data alpha's) is no schema
// @atproto/lexicon takes, and no method answers with one.
const docs = readdirSync(join(root, 'schemas'))
.filter((f) => f.endsWith('.json'))
.map((f) => JSON.parse(readFileSync(join(root, 'schemas', f), 'utf8')))
.filter((d) => d.defs?.main?.type !== 'space')
const lexicons = new Lexicons(docs)
const methods = docs
.filter((d) => ['query', 'procedure'].includes(d.defs.main?.type))
.map((d) => d.id)
const [dir] = process.argv.slice(2)
if (!dir) {
console.log('usage: node harness/answers.mjs <dir of answers>')
process.exit(1)
}
const answers = readdirSync(dir).flatMap((f) =>
readFileSync(join(dir, f), 'utf8')
.split('\n')
.filter(Boolean)
.map((l) => JSON.parse(l)),
)
// A space's at-uri (at://<did>/space/<type>/<key>, and a record in one,
// …/<author>/<collection>/<rkey>) is beyond the grammar @atproto/lexicon
// knows; HUD's own validator (the atproto crate's) holds those, so such a
// URI is held to its public form here.
const SPACED = /^at:\/\/(did:[^/]+)\/space\/[^/]+\/[^/]+(?:\/(did:[^/]+\/[^/]+\/[^/]+))?$/
const publicForm = (v) =>
typeof v === 'string'
? v.replace(SPACED, (_, did, record) => `at://${record ?? did}`)
: Array.isArray(v)
? v.map(publicForm)
: v && typeof v === 'object'
? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, publicForm(x)]))
: v
const problems = []
const said = new Set()
const seen = new Set()
let checked = 0
for (const { path, status, body } of answers) {
const url = new URL(path, 'http://hud')
const nsid = url.pathname.match(/^\/xrpc\/(.+)$/)?.[1]
if (!nsid) continue
const say = (what) => {
const p = `${url.pathname}: ${what}`
if (!said.has(p)) problems.push(`${path} (${status}): ${what}`)
said.add(p)
}
const def = lexicons.getDef(nsid)
if (!def) {
if (status !== 501 || body?.error !== 'MethodNotImplemented')
say('no lexicon names this method')
continue
}
seen.add(nsid)
checked++
const declared = Object.keys(def.parameters?.properties ?? {})
if (status === 200)
for (const name of url.searchParams.keys())
if (!declared.includes(name)) say(`${name} is no parameter of ${nsid}`)
if (status !== 200) {
if (typeof body?.error !== 'string' || typeof body?.message !== 'string') {
say('a failure is {error, message}')
continue
}
// XRPC's own errors, which any method may answer: AuthRequired, and
// MethodNotImplemented (501) where this server serves it not (sign-in
// off, or HUD_WORKSPACE=off).
const named = (def.errors ?? []).map((e) => e.name)
const xrpc = body.error === 'AuthRequired' || (status === 501 && body.error === 'MethodNotImplemented')
if (!named.includes(body.error) && !xrpc)
say(`${body.error} is no error ${nsid} declares (${named.join(', ')})`)
continue
}
if (def.output?.encoding !== 'application/json') continue
try {
// As a client reads it: a blob ref a BlobRef, a $link a CID.
lexicons.assertValidXrpcOutput(nsid, jsonToLex(publicForm(body)))
} catch (e) {
say(e.message)
}
}
for (const m of methods) if (!seen.has(m)) problems.push(`${m}: no test received an answer of it`)
for (const p of problems) console.log(p)
if (problems.length) process.exit(1)
console.log(`answers: ${checked} XRPC answers the tests received are as their lexicons say`)