The four closed recurrence shapes computed in the definition's own IANA zone, a fourteen-day materialisation horizon with projections beyond it, series as a managed thing, and the admin calendar that replaces the plugin this feature exists to replace. An event now happens on its own. No schema change: Phase 1 built every column this needed. - events/recurrence.js is the ONE place an occurrence is computed, so the runner's expansion and the calendar's forecast cannot disagree. No date library added — Node ships the tzdata one would vendor, behind Intl. - The runner's materialise leg is now two halves: expand, then sweep. The window starts at `now - grace`, so an occurrence nobody could have seen is never invented retroactively; the horizon is what makes the missed sweep mean anything for a recurrence. - Publishing is the schedule switch and archiving turns it off, and publishing re-pins every occurrence that has not started. - A projection is never drawn over an instant a run occupies, so a cancelled occurrence does not reappear as a forecast. 54 new tests, incl. the DST fixture set the plan asked for and three new statements proved against a real MariaDB. Suite 1768/1711/56 skipped/1 fail (pre-existing CRLF). Walked end to end on the local review stack. Docs: RunicGateway/docs#PENDING Co-Authored-By: Claude <noreply@anthropic.com>
323 lines
13 KiB
JavaScript
323 lines
13 KiB
JavaScript
// ── The event spec validator (EVENTS.md §C/§D, Phase 1) ────────────────────
|
|
//
|
|
// The boundary that decides whether a version may exist. Its interesting cases
|
|
// are all about time rather than shape:
|
|
//
|
|
// • a step's params are checked against the action's DECLARED params, and the
|
|
// action version it was authored against is captured at save
|
|
// • `on_failure` is defaulted from the risk class, because a `change` action
|
|
// that fell back to `skip` would advance a run over a half-changed world
|
|
// • an unregistered action is refused on a NEW step and KEPT on an existing
|
|
// one — the rule `engagementRules.model` established for a dormant trigger,
|
|
// for the same reason: an uninstall must not be destructive after the fact
|
|
// • a dormant step blocks a PUBLISH and never a SAVE
|
|
// • two phases may not share a key, because `UNIQUE (run_id, phase, seq)`
|
|
// would silently collapse them into one at materialisation
|
|
// • a key a later phase owns (`advance`, `announcements`) is REFUSED rather
|
|
// than preserved, so no corpus of unvalidated specs accumulates
|
|
process.env.DB_HOST = '127.0.0.1'
|
|
process.env.DB_PORT = '59999'
|
|
|
|
const { test, beforeEach, afterEach, after } = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
|
|
const registries = require('../src/modules/registries')
|
|
const spec = require('../src/events/spec')
|
|
const db = require('../src/utils/db')
|
|
|
|
after(() => db.close())
|
|
|
|
beforeEach(() => {
|
|
registries._reset()
|
|
registries.registerCore()
|
|
const api = registries.stage('demo')
|
|
api.registerEventActions([
|
|
{
|
|
id: 'demo.world.change',
|
|
label: 'Change the world',
|
|
risk: 'change',
|
|
reversible: 'none',
|
|
version: 4,
|
|
params: [
|
|
{ name: 'region', type: 'string', required: true, example: 'Yew' },
|
|
{ name: 'count', type: 'int', required: true, example: 12 },
|
|
{ name: 'hue', type: 'int', required: false, example: 1157 },
|
|
],
|
|
perform: async () => ({ ok: true }),
|
|
},
|
|
{
|
|
id: 'demo.world.wreck',
|
|
label: 'Wreck the world',
|
|
risk: 'irreversible',
|
|
reversible: 'none',
|
|
perform: async () => ({ ok: true }),
|
|
},
|
|
])
|
|
registries.apply(api.staged)
|
|
})
|
|
afterEach(() => registries._reset())
|
|
|
|
const oneStep = (step) => ({
|
|
schedule: { kind: 'manual' },
|
|
phases: [{ key: 'main', label: 'Main', steps: [step] }],
|
|
})
|
|
|
|
test('the empty spec is valid, and is what a new draft carries', () => {
|
|
const result = spec.validate(spec.emptySpec())
|
|
assert.equal(result.ok, true)
|
|
assert.equal(result.spec.phases.length, 1)
|
|
assert.deepEqual(result.spec.schedule, { kind: 'manual' })
|
|
})
|
|
|
|
test('params are checked against the declaration and coerced', () => {
|
|
const result = spec.validate(
|
|
oneStep({ actionId: 'demo.world.change', params: { region: 'Yew', count: 12 } }),
|
|
)
|
|
assert.equal(result.ok, true)
|
|
const [step] = result.spec.phases[0].steps
|
|
assert.deepEqual(step.params, { region: 'Yew', count: 12 })
|
|
// Captured from the declaration, not from the request: it is what lets a later
|
|
// bump warn in the editor instead of dispatching a mistyped parameter.
|
|
assert.equal(step.actionVersion, 4)
|
|
assert.equal(step.dormant, false)
|
|
})
|
|
|
|
test('a missing required param, a wrong type and an unknown param are all refused', () => {
|
|
const missing = spec.validate(oneStep({ actionId: 'demo.world.change', params: { region: 'Yew' } }))
|
|
assert.equal(missing.ok, false)
|
|
assert.match(missing.errors.join('\n'), /"count" is required/)
|
|
|
|
const wrong = spec.validate(
|
|
oneStep({ actionId: 'demo.world.change', params: { region: 'Yew', count: 'twelve' } }),
|
|
)
|
|
assert.equal(wrong.ok, false)
|
|
assert.match(wrong.errors.join('\n'), /"count" expected an integer/)
|
|
|
|
// An unknown param is an ERROR, not a silent drop: an author who typed
|
|
// `regions` has written a step that would dispatch with the region missing,
|
|
// and dropping the key makes that look like it saved cleanly.
|
|
const typo = spec.validate(
|
|
oneStep({ actionId: 'demo.world.change', params: { regions: 'Yew', count: 1 } }),
|
|
)
|
|
assert.equal(typo.ok, false)
|
|
assert.match(typo.errors.join('\n'), /"regions" is not a param of demo\.world\.change/)
|
|
})
|
|
|
|
test('on_failure is defaulted from the risk class', () => {
|
|
const notify = spec.validate(
|
|
oneStep({ actionId: 'core.announce', params: { leg: 'discord', body: 'hi' } }),
|
|
)
|
|
assert.equal(notify.spec.phases[0].steps[0].onFailure, 'skip')
|
|
|
|
const change = spec.validate(
|
|
oneStep({ actionId: 'demo.world.change', params: { region: 'Yew', count: 1 } }),
|
|
)
|
|
assert.equal(change.spec.phases[0].steps[0].onFailure, 'pause')
|
|
|
|
const irreversible = spec.validate(oneStep({ actionId: 'demo.world.wreck' }))
|
|
assert.equal(irreversible.spec.phases[0].steps[0].onFailure, 'abort_run')
|
|
|
|
// An author may still choose, within the closed set.
|
|
const chosen = spec.validate(oneStep({ actionId: 'demo.world.wreck', onFailure: 'skip' }))
|
|
assert.equal(chosen.spec.phases[0].steps[0].onFailure, 'skip')
|
|
const invented = spec.validate(oneStep({ actionId: 'demo.world.wreck', onFailure: 'shrug' }))
|
|
assert.equal(invented.ok, false)
|
|
assert.match(invented.errors.join('\n'), /onFailure: must be one of/)
|
|
})
|
|
|
|
test('a NEW step may not name an unregistered action', () => {
|
|
const result = spec.validate(oneStep({ actionId: 'gone.module.verb' }))
|
|
assert.equal(result.ok, false)
|
|
assert.match(result.errors.join('\n'), /no module registers "gone\.module\.verb"/)
|
|
})
|
|
|
|
test('an EXISTING step keeps its action when the module goes away, and is marked dormant', () => {
|
|
const saved = spec.validate(
|
|
oneStep({ actionId: 'demo.world.change', params: { region: 'Yew', count: 3 } }),
|
|
).spec
|
|
|
|
// The module is uninstalled between one save and the next.
|
|
registries._reset()
|
|
registries.registerCore()
|
|
|
|
const again = spec.validate(saved, { knownActionIds: spec.actionIdsIn(saved) })
|
|
assert.equal(again.ok, true, again.errors && again.errors.join('\n'))
|
|
const [step] = again.spec.phases[0].steps
|
|
assert.equal(step.dormant, true)
|
|
// Params pass through untouched: the only thing that could validate them left
|
|
// with the module.
|
|
assert.deepEqual(step.params, { region: 'Yew', count: 3 })
|
|
|
|
// …and that is exactly what publish refuses.
|
|
const publishable = spec.publishable(again.spec)
|
|
assert.equal(publishable.ok, false)
|
|
assert.deepEqual(publishable.dormant, ['demo.world.change'])
|
|
})
|
|
|
|
test('validate accepts its own output — a saved spec is re-validated on every save', () => {
|
|
// The property the dormancy test above found the hard way: `validate` adds
|
|
// `actionVersion` and `dormant`, and a validator that then refused its own
|
|
// fields would make the SECOND save of any definition impossible, and publish
|
|
// — which re-validates before snapshotting — impossible full stop.
|
|
const once = spec.validate(
|
|
oneStep({ actionId: 'demo.world.change', params: { region: 'Yew', count: 3 } }),
|
|
)
|
|
const twice = spec.validate(once.spec)
|
|
assert.equal(twice.ok, true, twice.errors && twice.errors.join('\n'))
|
|
assert.deepEqual(twice.spec, once.spec)
|
|
})
|
|
|
|
test('two phases may not share a key', () => {
|
|
const result = spec.validate({
|
|
schedule: { kind: 'manual' },
|
|
phases: [
|
|
{ key: 'main', label: 'One', steps: [] },
|
|
{ key: 'main', label: 'Two', steps: [] },
|
|
],
|
|
})
|
|
assert.equal(result.ok, false)
|
|
assert.match(result.errors.join('\n'), /used by more than one phase/)
|
|
})
|
|
|
|
test('a key a later phase owns is refused, not silently preserved', () => {
|
|
const top = spec.validate({ schedule: { kind: 'manual' }, phases: [], announcements: [] })
|
|
assert.equal(top.ok, false)
|
|
assert.match(top.errors.join('\n'), /unknown key "announcements"/)
|
|
|
|
const phase = spec.validate({
|
|
schedule: { kind: 'manual' },
|
|
phases: [{ key: 'main', label: 'Main', steps: [], advance: { after: '30m' } }],
|
|
})
|
|
assert.equal(phase.ok, false)
|
|
assert.match(phase.errors.join('\n'), /unknown key\(s\) advance .*Phase 5/)
|
|
})
|
|
|
|
// ── The schedule shapes (Phase 4) ────────────────────────────────────
|
|
//
|
|
// Every check here is on SHAPE. What the shapes MEAN — the zone arithmetic, the
|
|
// DST rules — is `eventRecurrence.test.js`. The split is deliberate: this file
|
|
// answers "may this be saved", that one answers "when does it happen", and the
|
|
// second question is only worth asking of something that passed the first.
|
|
|
|
const withSchedule = (schedule) =>
|
|
spec.validate({ schedule, phases: [{ key: 'main', label: 'Main', steps: [] }] })
|
|
|
|
test('the four closed shapes are accepted and normalised', () => {
|
|
assert.deepEqual(withSchedule({ kind: 'manual' }).spec.schedule, { kind: 'manual' })
|
|
assert.deepEqual(withSchedule({ kind: 'once', at: '2026-10-31T20:00' }).spec.schedule, {
|
|
kind: 'once',
|
|
at: '2026-10-31T20:00',
|
|
})
|
|
assert.deepEqual(withSchedule({ kind: 'weekly', days: ['friday'], time: '20:00' }).spec.schedule, {
|
|
kind: 'weekly',
|
|
days: ['friday'],
|
|
time: '20:00',
|
|
})
|
|
assert.deepEqual(
|
|
withSchedule({ kind: 'monthly', nth: -1, weekday: 'friday', time: '19:30' }).spec.schedule,
|
|
{ kind: 'monthly', nth: -1, weekday: 'friday', time: '19:30' },
|
|
)
|
|
})
|
|
|
|
test('the validator accepts its own output for every shape', () => {
|
|
// Phase 1's rule, and it is a rule about the SECOND save of any definition
|
|
// rather than about a round trip for its own sake: `validate` normalises, and
|
|
// publish re-validates what a save wrote. A normaliser that refuses what it
|
|
// emits makes a published definition uneditable.
|
|
for (const schedule of [
|
|
{ kind: 'manual' },
|
|
{ kind: 'once', at: '2026-10-31T20:00' },
|
|
{ kind: 'weekly', days: ['friday', 'monday'], time: '20:00' },
|
|
{ kind: 'monthly', nth: 4, weekday: 'friday', time: '19:30' },
|
|
]) {
|
|
const first = withSchedule(schedule)
|
|
assert.equal(first.ok, true, JSON.stringify(schedule))
|
|
const second = withSchedule(first.spec.schedule)
|
|
assert.equal(second.ok, true, JSON.stringify(first.spec.schedule))
|
|
assert.deepEqual(second.spec.schedule, first.spec.schedule)
|
|
}
|
|
})
|
|
|
|
test('weekly days are normalised into week order and deduped', () => {
|
|
// Not tidiness. The spec is snapshotted into a version and diffed, so two
|
|
// orderings of the same schedule would show as an edit nobody made.
|
|
const result = withSchedule({ kind: 'weekly', days: ['Friday', 'monday', 'FRIDAY'], time: '20:00' })
|
|
assert.deepEqual(result.spec.schedule.days, ['monday', 'friday'])
|
|
})
|
|
|
|
test('a shape may not carry another shape keys', () => {
|
|
const result = withSchedule({ kind: 'weekly', days: ['friday'], time: '20:00', at: '2026-01-01T00:00' })
|
|
assert.equal(result.ok, false)
|
|
assert.match(result.errors.join('\n'), /unknown key\(s\) at for kind "weekly"/)
|
|
})
|
|
|
|
test('an unknown kind is refused, and the message names the four', () => {
|
|
const result = withSchedule({ kind: 'daily', time: '20:00' })
|
|
assert.equal(result.ok, false)
|
|
assert.match(result.errors.join('\n'), /manual, once, weekly, monthly/)
|
|
})
|
|
|
|
test('a date that is not a real day is refused', () => {
|
|
// The regex admits 2026-02-30 quite happily. A schedule that parses and then
|
|
// resolves to some other day is worse than one that is refused.
|
|
const result = withSchedule({ kind: 'once', at: '2026-02-30T20:00' })
|
|
assert.equal(result.ok, false)
|
|
assert.match(result.errors.join('\n'), /is not a real date/)
|
|
})
|
|
|
|
test('every malformed schedule field is named, not merely rejected', () => {
|
|
assert.match(withSchedule({ kind: 'once', at: 'soon' }).errors.join('\n'), /YYYY-MM-DDTHH:MM/)
|
|
assert.match(withSchedule({ kind: 'weekly', days: [], time: '20:00' }).errors.join('\n'), /non-empty array/)
|
|
assert.match(withSchedule({ kind: 'weekly', days: ['froday'], time: '20:00' }).errors.join('\n'), /unknown weekday/)
|
|
assert.match(withSchedule({ kind: 'weekly', days: ['friday'], time: '25:00' }).errors.join('\n'), /24-hour time/)
|
|
assert.match(
|
|
withSchedule({ kind: 'monthly', nth: 5, weekday: 'friday', time: '19:30' }).errors.join('\n'),
|
|
/1, 2, 3, 4 or -1/,
|
|
)
|
|
assert.match(
|
|
withSchedule({ kind: 'monthly', nth: 1, weekday: 'froday', time: '19:30' }).errors.join('\n'),
|
|
/expected one of sunday/,
|
|
)
|
|
})
|
|
|
|
test('a refused schedule leaves a manual one behind rather than half a recurrence', () => {
|
|
// `validate` collects every error and carries on, so the spec object exists
|
|
// even when the answer is no. A caller reading `schedule.days` of it must not
|
|
// find a partially built weekly.
|
|
const result = spec.validate({
|
|
schedule: { kind: 'weekly', days: ['froday'], time: '20:00' },
|
|
phases: [{ key: 'BAD KEY', label: '', steps: [] }],
|
|
})
|
|
assert.equal(result.ok, false)
|
|
assert.ok(result.errors.length > 1)
|
|
})
|
|
|
|
test('every problem is reported, not just the first', () => {
|
|
const result = spec.validate({
|
|
schedule: { kind: 'manual' },
|
|
phases: [
|
|
{ key: 'BAD KEY', label: '', steps: [{ actionId: 'demo.world.change', params: {} }] },
|
|
],
|
|
})
|
|
assert.equal(result.ok, false)
|
|
const joined = result.errors.join('\n')
|
|
assert.match(joined, /bad phase key/)
|
|
assert.match(joined, /a phase needs a label/)
|
|
assert.match(joined, /"region" is required/)
|
|
assert.match(joined, /"count" is required/)
|
|
})
|
|
|
|
test('the size bounds hold', () => {
|
|
const many = {
|
|
schedule: { kind: 'manual' },
|
|
phases: Array.from({ length: spec.MAX_PHASES + 1 }, (_, i) => ({
|
|
key: `p${i}`,
|
|
label: `P${i}`,
|
|
steps: [],
|
|
})),
|
|
}
|
|
const result = spec.validate(many)
|
|
assert.equal(result.ok, false)
|
|
assert.match(result.errors.join('\n'), new RegExp(`at most ${spec.MAX_PHASES} phases`))
|
|
})
|