~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
84 lines · 3.8 KB Raw History
// The JSON Schemas HUD publishes for what an operator writes
// (schemas/json/, made by `hud schema`, written by `just json-schemas`):
// every view HUD bundles (views/) and Grain's app file and views as HUD's
// tests load them (crates/hud-core/src/testdata/grain/apps) pass them, so
// an editor never marks what HUD takes; and what HUD refuses for its shape
// (a key or a prop it doesn't know, a word it doesn't) fails them, but for
// a view's optional key (`?key`).
//
//   node harness/schemas.mjs [<apps>...]    more directories of apps, as
//                                           HUD_APPS names one, to hold to them
//
// Prints each problem as `file: /json/pointer what`, failing if there is
// any. `cd harness && npm ci` once.

import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { dirname, join, relative, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

// Required, so CI's NODE_PATH finds it where the harness's packages aren't.
const Ajv2020 = createRequire(import.meta.url)('ajv/dist/2020')

const root = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const json = (file) => JSON.parse(readFileSync(file, 'utf8'))
const ajv = new Ajv2020({ strict: false, allErrors: true })
const app = ajv.compile(json(join(root, 'schemas/json/app.json')))
const view = ajv.compile(json(join(root, 'schemas/json/view.json')))

const problems = []
const held = (check, doc, file) => {
  if (check(doc)) return
  for (const e of check.errors) problems.push(`${file}: ${e.instancePath || '/'} ${e.message}`)
}

/// The `.json` files directly in `dir`, none where it is no directory.
const jsons = (dir) =>
  existsSync(dir)
    ? readdirSync(dir)
        .filter((f) => f.endsWith('.json'))
        .sort()
        .map((f) => join(dir, f))
    : []

/// A directory of apps as HUD loads one: an app file at its top for each
/// file whose name holds a dot, and its views.
const apps = (dir) => {
  for (const f of jsons(dir).filter((f) => f.slice(dir.length + 1, -5).includes('.')))
    held(app, json(f), relative(root, f))
  for (const f of jsons(join(dir, 'views'))) held(view, json(f), relative(root, f))
}

for (const f of jsons(join(root, 'views'))) held(view, json(f), relative(root, f))
apps(join(root, 'crates/hud-core/src/testdata/grain/apps'))
for (const dir of process.argv.slice(2)) apps(resolve(dir))

// What HUD refuses for its shape fails them; a view's optional key passes.
const listing = (layout, more = {}) => ({
  collection: 'a.b.c',
  description: 'Things',
  list: { layout },
  ...more,
})
const badge = (props) => ({ component: 'Badge', props: { value: 'x', ...props } })
const minimal = { domain: 'example.test', name: 'Example', tabs: [{ label: 'Home', call: { name: 'query' } }] }
for (const [what, check, doc, pass] of [
  ['a view', view, listing(badge()), true],
  ['an optional key', view, listing(badge({ '?shiny': true }), { '?newer': 1 }), true],
  ['an unknown key', view, listing(badge(), { newer: 1 }), false],
  ['an unknown prop', view, listing(badge({ shiny: true })), false],
  ['an unknown component', view, listing({ component: 'Shiny' }), false],
  ['a word it does not know', view, listing({ component: 'Stack', props: { gap: 'huge' } }), false],
  ['children of what takes none', view, listing({ ...badge(), children: [] }), false],
  ['an app file', app, minimal, true],
  ['an app file with no tabs', app, { ...minimal, tabs: [] }, false],
  ['a frame key it does not know', app, { ...minimal, frame: { shiny: true } }, false],
]) {
  if (check(doc) !== pass) problems.push(`${what}: ${pass ? 'refused' : 'passed'} ${JSON.stringify(doc)}`)
}

if (problems.length) {
  for (const p of problems) console.log(p)
  process.exit(1)
}
console.log('the views and app files pass their JSON Schemas')