// The four event recipes (MODULE_API 1.13.0): what each one builds, and what it // refuses in words an author can read. // // This suite runs against a FAKE core, so it can say a spec has the right shape // but not that core accepts it. That half is `scripts/templateBuild.js`, which // builds every recipe through core's own registry and spec validator in CI. const test = require('node:test') const assert = require('node:assert') const register = require('../index') const { fakeCtx, fakeApi } = require('./_fakes') const templates = require('../eventTemplates') const actions = require('../config/uoEventActions') const byId = (id) => templates.TEMPLATES.find((t) => t.id === id) const build = (id, inputs) => byId(id).build({ inputs }) const ID_GRAMMAR = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/ const PHASE_KEY = /^[a-z][a-z0-9]*(?:[-_][a-z0-9]+)*$/ // What core's `checkActionParam` lets through (registries.js). const INPUT_KEYS = ['name', 'type', 'required', 'example', 'description', 'source', 'duration', 'max'] const TYPES = ['string', 'int', 'float', 'boolean', 'datetime', 'url'] const refused = (result, pattern) => { assert.strictEqual(result.ok, false, 'expected a refusal') assert.strictEqual(typeof result.error, 'string') assert.ok(result.error.length > 10, 'a refusal is a sentence') if (pattern) assert.match(result.error, pattern) } const stepIds = (phase) => phase.steps.map((s) => s.actionId) // ── Registration and declarations ─────────────────────────────────────────── test('index.js registers the four recipes, once, through registerEventTemplates', () => { const api = fakeApi() register(fakeCtx(), api) assert.deepStrictEqual( api.record.eventTemplates.map((t) => t.id).sort(), ['uo.template.announce', 'uo.template.boss_raid', 'uo.template.gathering_giveaway', 'uo.template.seasonal'], ) }) test('every recipe is shaped the way core\'s registry demands', () => { assert.strictEqual(templates.TEMPLATES.length, 4) const actionIds = new Set(actions.ACTIONS.map((a) => a.id).concat('core.lease')) const leaseIds = new Set(actions.LEASES.map((l) => l.id)) const sourceIds = new Set(actions.OPTION_SOURCES.map((s) => s.id)) for (const t of templates.TEMPLATES) { assert.match(t.id, ID_GRAMMAR) assert.ok(t.id.startsWith('uo.'), `${t.id} must carry this module's prefix`) assert.ok(t.label, `${t.id} has no label`) assert.ok(t.description.length > 40, `${t.id} needs a description an author can read`) assert.strictEqual(typeof t.build, 'function') assert.ok(t.inputs.length >= 1 && t.inputs.length <= 8, `${t.id} asks for ${t.inputs.length} inputs`) const seen = new Set() for (const input of t.inputs) { const where = `${t.id}.${input.name}` assert.match(input.name, /^[A-Za-z][A-Za-z0-9_]*$/, where) assert.ok(!seen.has(input.name), `${where} declared twice`) seen.add(input.name) assert.ok(TYPES.includes(input.type), `${where} has type ${input.type}`) for (const key of Object.keys(input)) assert.ok(INPUT_KEYS.includes(key), `${where} has unknown key ${key}`) // Core refuses an input with no example, optional or not, and silently drops `default`. assert.ok(input.example !== undefined && input.example !== null && input.example !== '', `${where} needs an example`) assert.ok(input.description && input.description.length > 10, `${where} needs a plain-English description`) assert.strictEqual(input.default, undefined, `${where}: core drops \`default\`; apply it in build()`) assert.strictEqual(input.multiple, undefined, `${where}: a template input cannot be multiple`) assert.strictEqual(typeof input.required, 'boolean', `${where} should say whether it is required`) if (input.duration !== undefined) { assert.strictEqual(input.duration, 'minutes') assert.ok(input.type === 'int' || input.type === 'float', `${where}: a duration is a number`) } if (input.source) assert.ok(sourceIds.has(input.source), `${where} names a source no module declares`) } } // The actions and lease the recipes build steps out of all exist. const used = new Set() const sample = { 'uo.template.announce': { text: 'Hello', newsTitle: 'News' }, 'uo.template.boss_raid': { place: 'Felucca/Britain', creature: 'OrcCaptain', name: 'Gruk', minutes: 10 }, 'uo.template.seasonal': { event: 'Fellowship', minutes: 60, message: 'On' }, 'uo.template.gathering_giveaway': { place: 'Felucca/Britain', minutes: 10, item: 'gold' }, } for (const [id, inputs] of Object.entries(sample)) { const built = build(id, inputs) for (const phase of built.spec.phases) { for (const step of phase.steps) { used.add(step.actionId) assert.ok(actionIds.has(step.actionId), `${id} uses ${step.actionId}, which nobody declares`) if (step.actionId === 'core.lease') assert.ok(leaseIds.has(step.params.lease), `${id} leases ${step.params.lease}`) } } } assert.ok(used.size >= 7) }) test('every built plan is a plain spec: three top-level keys at most, legal phase keys, one gate form', () => { const fixtures = [ ['uo.template.announce', { text: 'Hello', newsTitle: 'News', newsMinutes: 90, hue: 5 }], ['uo.template.boss_raid', { place: 'Felucca/Britain', creature: 'OrcCaptain', name: 'Gruk', minutes: 30, count: 2, strength: 2.5 }], ['uo.template.seasonal', { event: 'RisingTide', minutes: 720 }], ['uo.template.gathering_giveaway', { place: 'Felucca/Cities/Britain', minutes: 30, item: 'cloak' }], ] for (const [id, inputs] of fixtures) { const built = build(id, inputs) assert.ok(!('ok' in built), `${id}: ${built.error}`) assert.ok(built.title && built.summary) assert.deepStrictEqual(built.spec.schedule, { kind: 'manual' }) for (const key of Object.keys(built.spec)) assert.ok(['schedule', 'phases', 'starts'].includes(key), `${id}: ${key}`) const keys = new Set() for (const phase of built.spec.phases) { assert.match(phase.key, PHASE_KEY) assert.ok(!keys.has(phase.key)) keys.add(phase.key) assert.ok(phase.label) assert.ok(phase.steps.length > 0) if (phase.advance) assert.match(phase.advance.after, /^\d+m$/) for (const step of phase.steps) { assert.ok(step.label) assert.strictEqual(typeof step.params, 'object') } } } }) // ── Announcement ──────────────────────────────────────────────────────────── test('announce: a broadcast alone has no hold', () => { const built = build('uo.template.announce', { text: ' The gates open at dusk. ' }) assert.strictEqual(built.spec.phases.length, 1) const [phase] = built.spec.phases assert.strictEqual(phase.key, 'announce') assert.deepStrictEqual(stepIds(phase), ['uo.broadcast']) assert.deepStrictEqual(phase.steps[0].params, { text: 'The gates open at dusk.' }) assert.strictEqual(phase.advance, undefined) assert.match(built.title, /^Announcement: The gates open at dusk\.$/) }) test('announce: a hue is passed through, and an empty one is left out', () => { const [phase] = build('uo.template.announce', { text: 'Hi', hue: 1153 }).spec.phases assert.deepStrictEqual(phase.steps[0].params, { text: 'Hi', hue: 1153 }) const [bare] = build('uo.template.announce', { text: 'Hi', hue: '' }).spec.phases assert.deepStrictEqual(bare.steps[0].params, { text: 'Hi' }) // Zero is a colour, not an empty box. const [zero] = build('uo.template.announce', { text: 'Hi', hue: 0 }).spec.phases assert.strictEqual(zero.steps[0].params.hue, 0) }) test('announce: a news title adds the article, holds the phase for a day by default, and does not proclaim twice', () => { const built = build('uo.template.announce', { text: 'Fair opens Friday.', newsTitle: ' The Fair ' }) const [phase] = built.spec.phases assert.deepStrictEqual(stepIds(phase), ['uo.broadcast', 'uo.news.post']) assert.deepStrictEqual(phase.steps[1].params, { title: 'The Fair', body: 'Fair opens Friday.', announce: false }) assert.deepStrictEqual(phase.advance, { after: '1440m' }) assert.match(built.summary, /Town Cryer news for 24 hours/) }) test('announce: newsMinutes sets the hold', () => { const [phase] = build('uo.template.announce', { text: 'x y', newsTitle: 'T', newsMinutes: 90 }).spec.phases assert.deepStrictEqual(phase.advance, { after: '90m' }) }) test('announce: refusals are sentences', () => { refused(build('uo.template.announce', { text: ' ' }), /no text/) refused(build('uo.template.announce', { text: 'x'.repeat(actions.MAX_BROADCAST_LEN + 1) }), /capped at 300/) // At the cap is fine. assert.ok(!('ok' in build('uo.template.announce', { text: 'x'.repeat(actions.MAX_BROADCAST_LEN) }))) refused(build('uo.template.announce', { text: 'Hi', hue: -1 }), /colour/) refused(build('uo.template.announce', { text: 'Hi', hue: 1.5 }), /colour/) refused(build('uo.template.announce', { text: 'Hi', newsMinutes: 60 }), /needs a news title/) refused(build('uo.template.announce', { text: 'Hi', newsTitle: 'T'.repeat(actions.MAX_NEWS_TITLE + 1) }), /news title is capped/) refused(build('uo.template.announce', { text: 'Hi', newsTitle: 'T', newsMinutes: 0 }), /1 to \d+ minutes/) refused(build('uo.template.announce', { text: 'Hi', newsTitle: 'T', newsMinutes: 1.5 }), /1 to \d+ minutes/) refused(build('uo.template.announce', { text: 'Hi', newsTitle: 'T', newsMinutes: templates.MAX_HOLD_MINUTES + 1 }), /1 to \d+ minutes/) }) // ── Boss raid ─────────────────────────────────────────────────────────────── const RAID = { place: 'Felucca/Britain', creature: 'OrcCaptain', name: 'Gruk the Unbroken', minutes: 30 } test('boss raid: defaults — one boss, no strength keys, a plain message, held for the raid', () => { const built = build('uo.template.boss_raid', RAID) assert.strictEqual(built.title, 'Orc Captain raid at Felucca/Britain for 30 minutes') assert.match(built.summary, /^Gruk the Unbroken, built from Orc Captain, appears near Felucca\/Britain and is removed after 30 minutes\.$/) const [phase] = built.spec.phases assert.strictEqual(built.spec.phases.length, 1) assert.strictEqual(phase.key, 'raid') assert.deepStrictEqual(phase.advance, { after: '30m' }) assert.deepStrictEqual(stepIds(phase), ['uo.boss.spawn', 'uo.broadcast']) assert.deepStrictEqual(phase.steps[0].params, { place: 'Felucca/Britain', creature: 'OrcCaptain', name: 'Gruk the Unbroken', count: 1, }) assert.deepStrictEqual(phase.steps[1].params, { text: 'Gruk the Unbroken has appeared near Britain!' }) }) test('boss raid: strength 1 is the same as leaving it out', () => { const [phase] = build('uo.template.boss_raid', { ...RAID, strength: 1 }).spec.phases assert.strictEqual(phase.steps[0].params.hitsMultiplier, undefined) assert.strictEqual(phase.steps[0].params.damageMultiplier, undefined) }) test('boss raid: strength sets both multipliers, and a count and message are kept', () => { const built = build('uo.template.boss_raid', { ...RAID, strength: 2.5, count: 3, message: ' Run! ', minutes: 120 }) const [phase] = built.spec.phases assert.strictEqual(phase.steps[0].params.hitsMultiplier, 2.5) assert.strictEqual(phase.steps[0].params.damageMultiplier, 2.5) assert.strictEqual(phase.steps[0].params.count, 3) assert.deepStrictEqual(phase.steps[1].params, { text: 'Run!' }) assert.deepStrictEqual(phase.advance, { after: '120m' }) assert.match(built.title, /for 2 hours$/) assert.match(built.summary, /^3 bosses called Gruk the Unbroken, built from Orc Captain and 2\.5 times as strong, appear near/) }) test('boss raid: the bounds are the action\'s own', () => { const top = build('uo.template.boss_raid', { ...RAID, count: actions.MAX_BOSSES, strength: actions.MAX_BOSS_MULTIPLIER }) assert.ok(!('ok' in top)) refused(build('uo.template.boss_raid', { ...RAID, count: actions.MAX_BOSSES + 1 }), /1 to 4 bosses/) refused(build('uo.template.boss_raid', { ...RAID, count: 0 }), /1 to 4 bosses/) refused(build('uo.template.boss_raid', { ...RAID, strength: actions.MAX_BOSS_MULTIPLIER + 0.5 }), /1 to 10 times/) refused(build('uo.template.boss_raid', { ...RAID, strength: 0.5 }), /1 to 10 times/) }) test('boss raid: refusals are sentences', () => { refused(build('uo.template.boss_raid', { ...RAID, place: 'Britain' }), /not a place/) refused(build('uo.template.boss_raid', { ...RAID, place: 'Felucca/' }), /not a place/) refused(build('uo.template.boss_raid', { ...RAID, creature: ' ' }), /pick a creature/) refused(build('uo.template.boss_raid', { ...RAID, name: '' }), /needs a name/) refused(build('uo.template.boss_raid', { ...RAID, minutes: 0 }), /1 to \d+ minutes/) refused(build('uo.template.boss_raid', { ...RAID, minutes: 'soon' }), /1 to \d+ minutes/) refused(build('uo.template.boss_raid', { ...RAID, message: 'x'.repeat(301) }), /capped at 300/) // The default message is what overflows when the name is huge, and the refusal says so. refused(build('uo.template.boss_raid', { ...RAID, name: 'N'.repeat(300) }), /default message/) }) // ── Seasonal ──────────────────────────────────────────────────────────────── test('seasonal: one lease step on the chosen event, held for the same length, no broadcast without a message', () => { const built = build('uo.template.seasonal', { event: 'KrampusEncounter', minutes: 180 }) const [phase] = built.spec.phases assert.strictEqual(phase.key, 'season') assert.deepStrictEqual(phase.advance, { after: '180m' }) assert.deepStrictEqual(stepIds(phase), ['core.lease']) assert.deepStrictEqual(phase.steps[0].params, { lease: 'uo.seasonal.status', target: 'KrampusEncounter', value: 'Active', minutes: 180, }) assert.strictEqual(built.title, 'Krampus Encounter for 3 hours') }) test('seasonal: a message adds a broadcast after the lease', () => { const [phase] = build('uo.template.seasonal', { event: 'Fellowship', minutes: 45, message: 'It is on!' }).spec.phases assert.deepStrictEqual(stepIds(phase), ['core.lease', 'uo.broadcast']) assert.deepStrictEqual(phase.steps[1].params, { text: 'It is on!' }) }) test('seasonal: every event the lease allows builds, and the lease ceiling is the limit', () => { for (const event of actions.SEASONAL_EVENTS) { assert.ok(!('ok' in build('uo.template.seasonal', { event, minutes: 60 })), event) } assert.strictEqual(templates.MAX_SEASONAL_MINUTES, actions.MAX_PROP_LEASE_MS / 60_000) assert.strictEqual(templates.MAX_SEASONAL_MINUTES, 720) assert.ok(!('ok' in build('uo.template.seasonal', { event: 'Fellowship', minutes: 720 }))) refused(build('uo.template.seasonal', { event: 'Fellowship', minutes: 721 }), /1 to 720 minutes/) }) test('seasonal: refusals are sentences', () => { refused(build('uo.template.seasonal', { event: 'TreasuresOfTokuno', minutes: 60 }), /not a seasonal event/) refused(build('uo.template.seasonal', { event: 'Christmas', minutes: 60 }), /not a seasonal event/) refused(build('uo.template.seasonal', { event: 'Fellowship', minutes: 0 }), /1 to 720 minutes/) refused(build('uo.template.seasonal', { event: 'Fellowship', minutes: 2.5 }), /1 to 720 minutes/) refused(build('uo.template.seasonal', { event: 'Fellowship', minutes: 60, message: 'x'.repeat(301) }), /capped at 300/) }) test('seasonal: the description carries the warnings the decision asks for', () => { const { description } = byId('uo.template.seasonal') assert.match(description, /back off by itself|puts the old one back/) assert.match(description, /six of the eight/i) assert.match(description, /12 hours/) }) // ── Gathering giveaway ────────────────────────────────────────────────────── const GIVEAWAY = { place: 'Felucca/Britain', minutes: 30, item: 'gold' } test('giveaway: two phases — gather (held), then reward — with defaults applied', () => { const built = build('uo.template.gathering_giveaway', GIVEAWAY) assert.strictEqual(built.spec.phases.length, 2) const [gather, reward] = built.spec.phases assert.strictEqual(gather.key, 'gather') assert.deepStrictEqual(gather.advance, { after: '30m' }) assert.deepStrictEqual(stepIds(gather), ['uo.broadcast', 'uo.participation.open']) assert.deepStrictEqual(gather.steps[0].params, { text: 'Gather near Britain in the next 30 minutes for a gift!' }) assert.deepStrictEqual(gather.steps[1].params, { place: 'Felucca/Britain', radius: 40 }) assert.strictEqual(reward.key, 'reward') assert.strictEqual(reward.advance, undefined) assert.deepStrictEqual(stepIds(reward), ['uo.participation.collect', 'uo.item.grant', 'uo.broadcast']) assert.deepStrictEqual(reward.steps[0].params, {}) assert.deepStrictEqual(reward.steps[1].params, { item: 'gold', amount: 1 }) assert.deepStrictEqual(reward.steps[2].params, { text: 'Thank you for coming — gifts are in your backpacks.' }) assert.strictEqual(built.title, 'Gathering giveaway at Felucca/Britain for 30 minutes') assert.match(built.summary, /Gifts cannot be taken back\.$/) }) test('giveaway: amount, radius and message are carried', () => { const built = build('uo.template.gathering_giveaway', { ...GIVEAWAY, amount: 500, radius: 100, message: 'Come on down' }) const [gather, reward] = built.spec.phases assert.deepStrictEqual(gather.steps[0].params, { text: 'Come on down' }) assert.deepStrictEqual(gather.steps[1].params, { place: 'Felucca/Britain', radius: 100 }) assert.deepStrictEqual(reward.steps[1].params, { item: 'gold', amount: 500 }) assert.match(built.summary, /500 gold each/) }) test('giveaway: a long window reads in hours in the default message', () => { const [gather] = build('uo.template.gathering_giveaway', { ...GIVEAWAY, minutes: 120 }).spec.phases assert.match(gather.steps[0].params.text, /in the next 2 hours for a gift!$/) }) test('giveaway: a non-stackable item is one each, and says why when asked for more', () => { const ok = build('uo.template.gathering_giveaway', { ...GIVEAWAY, item: 'cloak', amount: 1 }) assert.deepStrictEqual(ok.spec.phases[1].steps[1].params, { item: 'cloak', amount: 1 }) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, item: 'cloak', amount: 2 }), /does not stack/) // Every non-stackable on the allowlist behaves the same; gold alone stacks. for (const g of actions.GRANTABLE) { const result = build('uo.template.gathering_giveaway', { ...GIVEAWAY, item: g.key, amount: 2 }) if (g.stackable) assert.ok(!('ok' in result), g.key) else refused(result, /does not stack/) } }) test('giveaway: refusals are sentences', () => { refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, place: 'nowhere' }), /not a place/) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, minutes: -5 }), /1 to \d+ minutes/) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, item: 'dragon' }), /does not give out "dragon"/) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, amount: 0 }), /1 to 1000 each/) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, amount: actions.MAX_GRANT_STACK + 1 }), /1 to 1000 each/) assert.ok(!('ok' in build('uo.template.gathering_giveaway', { ...GIVEAWAY, amount: actions.MAX_GRANT_STACK }))) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, radius: 0 }), /1 to 300 tiles/) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, radius: actions.MAX_AREA_RADIUS + 1 }), /1 to 300 tiles/) assert.ok(!('ok' in build('uo.template.gathering_giveaway', { ...GIVEAWAY, radius: actions.MAX_AREA_RADIUS }))) refused(build('uo.template.gathering_giveaway', { ...GIVEAWAY, message: 'x'.repeat(301) }), /capped at 300/) }) test('giveaway: the description says who gets it and that it cannot be undone', () => { const { description } = byId('uo.template.gathering_giveaway') assert.match(description, /everyone the shard counted/) assert.match(description, /cannot be taken back/) }) test('boss raid: the description says boss spawning must be on and the boss is cleared at the end', () => { const { description } = byId('uo.template.boss_raid') assert.match(description, /switched on/) assert.match(description, /cleared when the run ends/) }) // ── No shard calls ────────────────────────────────────────────────────────── test('build() makes no sidecar call and reads no database', async () => { const link = require('../utils/uoLinkClient') const atlas = require('../model/shardAtlas/shardAtlas.model') const real = {} const touched = [] // Every function either module exposes is replaced by one that records the call. for (const [name, mod] of [['uoLinkClient', link], ['shardAtlas', atlas]]) { for (const key of Object.keys(mod)) { if (typeof mod[key] !== 'function') continue real[`${name}.${key}`] = mod[key] mod[key] = (...args) => { touched.push(`${name}.${key}`); return Promise.resolve({ ok: false }) } } } try { await build('uo.template.boss_raid', RAID) await build('uo.template.seasonal', { event: 'Fellowship', minutes: 60 }) await build('uo.template.gathering_giveaway', GIVEAWAY) await build('uo.template.announce', { text: 'Hi', newsTitle: 'T' }) } finally { for (const [full, fn] of Object.entries(real)) { const [name, key] = full.split('.') ;(name === 'uoLinkClient' ? link : atlas)[key] = fn } } assert.deepStrictEqual(touched, []) })