The kit's `template/`: a complete, minimal Runic Gateway module a reader copies,
renames, and runs before reading a chapter. Slice 0 landed the workflow that runs
it; this is the tree that workflow was written against, so the `template` job
arms itself with no edit to the guard.
Installed into a real core it adds one public page at `/examplegame/status`, a nav
row pointing at it, one API route described in an OpenAPI fragment core merges,
one table created by an idempotent schema fragment and dropped by a purge file,
and both lifecycle hooks. That is deliberately less than a real module does; what
it is complete about is the shape — every seam used once, with the reasoning next
to it.
Four decisions, settled with the org lead:
1. **Public tier only, plus the lifecycle hooks.** §2.11.1 d1's "one public route",
plus enough to show the whole vertical seam once. Admin and player tiers become
worked examples quoted from module-uo in chapter 2 rather than two thirds of a
tree the reader deletes on day one.
2. **The release workflow ships as a file, in BOTH flavours** — `.gitea/` and
`.github/`. Neither runs where it sits (a workflow is only read from a
repository root) and each arms itself when the reader's copy is its own repo.
Packaging is the part of a module that cannot be guessed at, and the kit's
audience is outside this org, so assuming Gitea would have been assuming our
own deployment. Core installs from a URL and does not care where the release
lives — only that the host is on the operator's `MODULE_SOURCE_HOSTS`.
3. **A rename checklist that CI verifies**, not a rename script. `template/README.md`
carries the table; `scripts/checkRenameSites.js` holds it against the tree in
both directions — an unlisted file that still carries the placeholder fails, and
so does a listed file that no longer does. The second half is the one usually
left out and the more valuable: a row that has stopped matching reads as
instructions to edit something that is not there. Same rule core's identifier
check follows about its own exemptions. It has its own ten-test suite, run by
CI as `node --test`, because a check that has never been shown to fail is a
check nobody knows the state of.
4. **A neutral invented game.** One deviation from the literal answer, forced by
decision 3: the id is `examplegame`, not `example`. The checklist check is a
text search, and `example` occurs in ordinary English ("for example") all over
prose that is not a rename site — a placeholder that cannot occur by accident is
what makes the check answerable instead of a source of false alarms someone
learns to ignore.
**The pin moves to the 1.4.0 bump** (website `edge` 1b692bf), which is what
`template/module.json` declares as `coreApi`. Slice 0 pinned its parent, before
1.4.0 existed, so `checkCoreApi.js` arms for the first time here — it asserts
EQUALITY, and its failing on the next contract bump is the system working.
Also in CI: the client tests now run AFTER the build (two of them read the built
chunk and skip without one — run first, the job reports green while asking nothing
about the artifact that ships), and `check:swagger` verifies the committed
fragment is current.
## The finding: an UPDATE that changes nothing does not touch ON UPDATE CURRENT_TIMESTAMP
Every suite passed, both guards passed, the chunk built, the module loaded into a
real core and the page rendered correctly. Two hours later the same page said the
world was offline, and it was wrong.
`updated_at` was declared `ON UPDATE CURRENT_TIMESTAMP`, and MariaDB fires that
only when an UPDATE actually CHANGES a value. The boot refresh writes the same
numbers every thirty seconds — which is exactly what a quiet game looks like — so
the timestamp froze at the first write, the row crossed the freshness window, and
the model correctly reported a stale row as offline. Verified against the live
database: two hours of refreshes, `updated_at` still the boot timestamp.
No test in this repo could see it. The model takes its clock as an argument, and
nothing in a suite runs the same UPDATE twice against a real database. It is only
visible as a page that was right when you looked at it and wrong an hour later.
The writer now sets `updated_at = CURRENT_TIMESTAMP` explicitly and the column
drops the clause that was not doing what it looked like it was doing; both carry
the reasoning. Re-verified end to end: the timestamp advances every interval and
the API reports fresh.
Falling out of the fix, the schema fragment gained the rule the reader hits next:
**changing a table is an ALTER, never an edit to its CREATE** — `CREATE TABLE IF
NOT EXISTS` does nothing when the table exists, so an edited column definition
takes effect on a fresh install and on no existing one, which is the worst
possible split because your development database is usually the fresh one.
## Verified
- 29 server tests, 18 client tests, 10 kit-script tests; `check:imports`,
`check:externals`, `check:swagger` and `checkCoreApi` all green, run in CI's own
order from a clean `npm ci`.
- Browser smoke (MODULE_API.md §7.7) against a real core built from the pinned
ref: module `started`, published on `/api/v1/public/modules`, chunk served
`no-cache` with the right MIME from the entry's directory while `module.json`
and the server source 404, script tag injected after core's bundle, the page
rendering inside core's own chrome, the nav row interleaved into the public
header between Wiki and About, SPA navigation into it from another page, the
module's path and schema and tag merged into `/api/docs.json`, and
`[examplegame] registered against core API 1.4.0` in the console with no CSP
report and no React error.
Refs: MODULE_SYSTEM.md §2.11.1 (slice 1), MODULE_API.md §2.x, §3.x, §5.1, §7.7.
Co-Authored-By: Claude <noreply@anthropic.com>
178 lines
8.0 KiB
JavaScript
178 lines
8.0 KiB
JavaScript
// ── What the chunk registers, checked without a browser ───────────────────
|
|
//
|
|
// `build.test.js` says the honest thing about this half: its real failures are
|
|
// timing and resolution, and a DOM-less runner cannot see either. MODULE_API.md
|
|
// §7.7's browser smoke is what proves the client half works, and nothing here
|
|
// replaces it.
|
|
//
|
|
// What a test CAN do is read back what the chunk asked for. Registration is the
|
|
// one thing the chunk does at evaluation time, and it does it through an object
|
|
// core hands it — so: stand up a fake `window.__rg` with a recording registry and
|
|
// the real React behind it, import the BUILT artifact, and inspect the result. No
|
|
// DOM is needed because nothing renders; `<WorldStatus />` is `jsx(WorldStatus)`,
|
|
// an object, and the route table is full of them by design.
|
|
//
|
|
// It catches a page that silently stops being routed, a nav row whose `to` drifts
|
|
// from its route's path, and the whole registration surface disappearing because
|
|
// something threw halfway down entry.jsx.
|
|
//
|
|
// **It runs against `dist/entry.js`, so build before you test.** The skip below
|
|
// is deliberate — `npm test` has to be runnable before `npm run build` — which
|
|
// means a CI job that tests without building is a job asking nothing at all. Ours
|
|
// builds first, on purpose.
|
|
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import * as react from 'react'
|
|
import * as jsxRuntime from 'react/jsx-runtime'
|
|
import * as router from 'react-router-dom'
|
|
|
|
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
const CHUNK = path.resolve(HERE, '..', 'dist', 'entry.js')
|
|
const manifest = JSON.parse(fs.readFileSync(path.resolve(HERE, '..', '..', 'module.json'), 'utf8'))
|
|
|
|
// A component, as far as the registry cares. The kit's real members are core's;
|
|
// nothing renders here, so a named stub is enough to be imported and passed on.
|
|
const stub = (name) => Object.assign(() => null, { displayName: name })
|
|
|
|
function fakeRg() {
|
|
const routes = { public: [], admin: [], player: [] }
|
|
const nav = { public: [], admin: [], player: [] }
|
|
const providers = new Map()
|
|
const extensions = new Map()
|
|
return {
|
|
version: manifest.coreApi.replace(/^\D+/, ''),
|
|
react,
|
|
jsxRuntime,
|
|
router,
|
|
// `react-dom/client` is imported for the identity check in core.js and never
|
|
// called — `createRoot` in a DOM-less process would throw. The shim reads
|
|
// this object, so the check compares against whatever is here.
|
|
reactDom: { createRoot: () => { throw new Error('not in a browser') } },
|
|
ui: Object.fromEntries(
|
|
['PublicLayout', 'PageHeader', 'Loading', 'ErrorState', 'EmptyState', 'useAsync', 'useAuth', 'useSite']
|
|
.map((n) => [n, stub(n)]),
|
|
),
|
|
api: { request: async () => ({}), ApiError: Error, BASE: '/api/v1' },
|
|
registry: {
|
|
registerRoutes(id, byArea) {
|
|
for (const [area, list] of Object.entries(byArea || {})) {
|
|
for (const r of list || []) routes[area].push({ ...r, path: `${id}/${r.path}`, moduleId: id })
|
|
}
|
|
},
|
|
registerNav(id, { area, items }) {
|
|
for (const item of items || []) nav[area].push({ ...item, moduleId: id })
|
|
},
|
|
registerFeatureProvider(id, namespace, hook) { providers.set(namespace, { id, hook }) },
|
|
registerExtension(id, slot, Component) {
|
|
if (extensions.has(slot)) throw new Error(`slot "${slot}" already filled`)
|
|
extensions.set(slot, { id, Component })
|
|
},
|
|
routesFor: (area) => routes[area],
|
|
navFor: (area) => nav[area],
|
|
},
|
|
_read: () => ({ routes, nav, providers, extensions }),
|
|
}
|
|
}
|
|
|
|
// Loaded once: an ES module is evaluated a single time per process however many
|
|
// times it is imported, so every test below reads the same registration pass —
|
|
// which is also how it behaves in a browser.
|
|
let registered = null
|
|
let skip = false
|
|
|
|
if (!fs.existsSync(CHUNK)) {
|
|
skip = true
|
|
} else {
|
|
const rg = fakeRg()
|
|
globalThis.window = { __rg: rg }
|
|
await import(`${new URL(`file://${CHUNK.split(path.sep).join('/')}`)}`)
|
|
registered = rg._read()
|
|
}
|
|
|
|
const it = (name, fn) => test(name, { skip: skip && 'no dist/entry.js — run npm run build' }, fn)
|
|
|
|
it('registers at least one route, namespaced under the module id', () => {
|
|
const all = Object.values(registered.routes).flat()
|
|
assert.ok(all.length > 0, 'the chunk registered no routes at all')
|
|
for (const [area, list] of Object.entries(registered.routes)) {
|
|
for (const r of list) {
|
|
assert.ok(r.path.startsWith(`${manifest.id}/`), `${area} route "${r.path}" is not under the namespace`)
|
|
assert.ok(r.element, `${area} route "${r.path}" has no element`)
|
|
}
|
|
}
|
|
})
|
|
|
|
it('every route path is distinct within its area', () => {
|
|
// Two routes on one path is a page that can never be reached, and React
|
|
// renders the first one without complaint.
|
|
for (const [area, list] of Object.entries(registered.routes)) {
|
|
const paths = list.map((r) => r.path)
|
|
assert.equal(new Set(paths).size, paths.length, `duplicate path in ${area}`)
|
|
}
|
|
})
|
|
|
|
it('every nav row points at a route this module actually registered', () => {
|
|
// The agreement that matters, and the one that rots quietly: a row survives a
|
|
// route rename and becomes a link to core's catch-all redirect. Nav rows carry
|
|
// the FULL rendered path (`/examplegame/status`); routes carry the namespaced
|
|
// one (`examplegame/status`). Reconciling the two is the whole test.
|
|
const rendered = {
|
|
public: (p) => `/${p}`,
|
|
admin: (p) => `/admin/${p}`,
|
|
player: (p) => `/player/${p}`,
|
|
}
|
|
for (const [area, rows] of Object.entries(registered.nav)) {
|
|
const reachable = new Set(registered.routes[area].map((r) => rendered[area](r.path)))
|
|
for (const row of rows) {
|
|
assert.ok(reachable.has(row.to), `${area} nav row "${row.label}" links to ${row.to}, which no route serves`)
|
|
}
|
|
}
|
|
})
|
|
|
|
it('every admin and player nav row carries an icon', () => {
|
|
// Both of those navs draw a glyph on every core row, so a row without one reads
|
|
// as breakage rather than as a design — and core's player portal used to render
|
|
// `<n.icon />` unguarded, which blanked the entire portal with React error #130
|
|
// the first time a module registered a row without one. Core guards it now; a
|
|
// missing icon there is still a visible defect and this is the cheap place to
|
|
// catch it. The PUBLIC header is text buttons and is deliberately excluded.
|
|
for (const area of ['admin', 'player']) {
|
|
for (const row of registered.nav[area]) {
|
|
assert.equal(typeof row.icon, 'function', `${area} nav row "${row.label}" has no icon`)
|
|
}
|
|
}
|
|
})
|
|
|
|
it('a nav row that gates on a feature has a provider to resolve it', () => {
|
|
// Resolution is by the REGISTERING module (§3.3), and every unknown fails OPEN.
|
|
// So a row carrying a `feature` from a module that registered no provider is a
|
|
// row that always shows — which re-advertises a surface an operator hid.
|
|
const gated = Object.values(registered.nav).flat().filter((r) => r.feature)
|
|
if (gated.length === 0) return
|
|
assert.ok(registered.providers.size > 0, 'rows carry feature gates but no provider was registered')
|
|
})
|
|
|
|
it('every slot module.json declares is one the chunk fills', () => {
|
|
// `module.json` declares SERVER slots, and the loader validates those before
|
|
// the chunk is ever served. Client slots cannot be declared there — the server
|
|
// knows nothing about them — so this is the one place the two halves meet.
|
|
for (const slot of manifest.extensions || []) {
|
|
assert.ok(registered.extensions.has(slot), `module.json declares "${slot}" and the chunk does not fill it`)
|
|
}
|
|
})
|
|
|
|
it('registers under exactly one module id, matching the manifest', () => {
|
|
const owners = new Set([
|
|
...Object.values(registered.routes).flat().map((r) => r.moduleId),
|
|
...Object.values(registered.nav).flat().map((r) => r.moduleId),
|
|
...[...registered.extensions.values()].map((e) => e.id),
|
|
...[...registered.providers.values()].map((p) => p.id),
|
|
])
|
|
assert.deepEqual([...owners], [manifest.id])
|
|
})
|