Stage 6 of the §14.3 integration plan, on protocol 14. Spec: docs rust-link/PROTOCOL.md §20.9. - gc.started and gc.ended public, gc.failed staff's; guardedCrate.js reads each row per viewer (the clearer's name for the presence audience, Steam id, position and key for staff, `won` for everyone). - Triggers rust.gc.started and rust.gc.ended (a crate a player cleared only). - The step rust.gc.place: an event from rust.options.gc_events and a world call's location; refused at verify for Guarded Crate absent, NpcSpawn missing (D364), console unavailable, or an unknown event. Kept (D373); the run log's success line says so (D380). Budget rust.gc.crates. - Warden (D375): guarded_won, title category guardedwon, Crates cleared column. - Map marker, server settings line, swagger, engagement manifest, bundle, README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E14m6SuuY6i1vASFeGDBeY
302 lines
15 KiB
JavaScript
302 lines
15 KiB
JavaScript
// ── The feed's sentences ──────────────────────────────────────────────────
|
||
//
|
||
// `lib/feed.js` is the one part of the client half with real branching in it, and
|
||
// it is pure on purpose so that a DOM-less runner can ask all of it. Everything
|
||
// here is a claim about what a reader sees for a given frame — which is exactly
|
||
// the kind of thing that rots silently, because a wrong killfeed line is still a
|
||
// killfeed line.
|
||
//
|
||
// The fixtures are the frames the bridge plugin actually emits (its
|
||
// `DescribeAttacker`, and PROTOCOL.md §8.4), not invented shapes.
|
||
|
||
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { createRequire } from 'node:module'
|
||
|
||
import { describe, FEED_KINDS, FILTERS, kindsFor } from '../src/lib/feed.js'
|
||
|
||
const row = (kind, frame = {}) => ({ id: 1, kind, t: Date.now(), wipeId: 'w1', steamId: '7656', frame })
|
||
|
||
test('a player kill names the killer and the victim, in that order', () => {
|
||
const line = describe(row('player.death', {
|
||
name: 'Bob',
|
||
attackerType: 'player',
|
||
attackerName: 'Alice',
|
||
weapon: 'rifle.ak',
|
||
distance: 42.4,
|
||
grid: 'H7',
|
||
}))
|
||
|
||
assert.equal(line.tone, 'kill')
|
||
assert.equal(line.actor, 'Alice')
|
||
assert.equal(line.verb, 'killed')
|
||
assert.equal(line.subject, 'Bob')
|
||
assert.match(line.detail, /rifle ak/)
|
||
assert.match(line.detail, /42m/)
|
||
assert.match(line.detail, /H7/)
|
||
})
|
||
|
||
test('the four attacker types are four different sentences', () => {
|
||
// The plugin distinguishes them precisely so a reader does not have to guess
|
||
// from an absent field, and collapsing any two loses something: a fall reported
|
||
// as a kill by nobody is the failure this prevents.
|
||
const victim = { name: 'Bob' }
|
||
|
||
const npc = describe(row('player.death', { ...victim, attackerType: 'npc', attackerName: 'scientistnpc_full_any' }))
|
||
// A family is named, not spelled out as a prefab (PLAN_FIXES F2, D185).
|
||
assert.equal(npc.actor, 'Scientist')
|
||
assert.equal(npc.subject, 'Bob')
|
||
|
||
const self = describe(row('player.death', { ...victim, attackerType: 'self' }))
|
||
assert.equal(self.actor, 'Bob')
|
||
assert.equal(self.subject, null)
|
||
assert.match(self.verb, /own hand/)
|
||
|
||
const environment = describe(row('player.death', { ...victim, attackerType: 'environment' }))
|
||
assert.equal(environment.actor, 'Bob')
|
||
assert.equal(environment.verb, 'died')
|
||
assert.equal(environment.subject, null)
|
||
|
||
// `HitInfo` is legitimately null on the environment path, so a death frame with
|
||
// NO attacker type at all is that case — not a missing field to render around.
|
||
const bare = describe(row('player.death', victim))
|
||
assert.equal(bare.verb, 'died')
|
||
assert.equal(bare.subject, null)
|
||
})
|
||
|
||
test('a sleeping victim is said to have been sleeping', () => {
|
||
const line = describe(row('player.death', { name: 'Bob', attackerType: 'player', attackerName: 'Alice', sleeping: true }))
|
||
assert.match(line.detail, /while sleeping/)
|
||
})
|
||
|
||
test('a disconnect with no session length says nothing about one', () => {
|
||
// The plugin OMITS `sessionSec` for a player who was already on when it loaded:
|
||
// an unknown session is not a session of no length. A line reading "after 0s"
|
||
// would be a lie this module invented.
|
||
const unknown = describe(row('player.disconnected', { name: 'Bob', reason: 'Quit' }))
|
||
assert.equal(unknown.detail, 'Quit')
|
||
|
||
const known = describe(row('player.disconnected', { name: 'Bob', reason: 'Quit', sessionSec: 3720 }))
|
||
assert.equal(known.detail, 'Quit · after 1h 2m')
|
||
})
|
||
|
||
test('a chat line carries the message as text, never as markup', () => {
|
||
// The message is the one field on this wire whose bytes a player chooses. It
|
||
// comes back as a STRING and is rendered as a React child, which escapes it;
|
||
// this test is here so that a later "render the message with formatting" idea
|
||
// has to delete an explicit assertion rather than quietly change behaviour.
|
||
const line = describe(row('player.chat', { name: 'Bob', message: '<img src=x onerror=alert(1)>', channel: 'Global' }))
|
||
assert.equal(line.verb, '<img src=x onerror=alert(1)>')
|
||
assert.equal(typeof line.verb, 'string')
|
||
// Global is the default channel and saying so on every line is noise; Team is
|
||
// information.
|
||
assert.equal(line.detail, '')
|
||
assert.equal(describe(row('player.chat', { name: 'B', message: 'hi', channel: 'Team' })).detail, 'Team')
|
||
|
||
// A chat row is the one line where the actor is a speaker rather than a
|
||
// subject, and "Brannock see you in september" is not a sentence anybody
|
||
// writes. The colon is presentation, so it lives here and not inside the text
|
||
// the player typed.
|
||
assert.equal(line.join, ': ')
|
||
assert.equal(describe(row('player.connected', { name: 'B' })).join, undefined)
|
||
})
|
||
|
||
test('an unknown kind renders as itself rather than vanishing', () => {
|
||
// A later protocol adds kinds, and a module may be older than the game host it
|
||
// is reading. The server's allowlist has already decided the row may be seen;
|
||
// dropping it here would make the page quietly say less than the truth.
|
||
const line = describe(row('player.teleported', { name: 'Bob' }))
|
||
assert.equal(line.verb, 'player.teleported')
|
||
assert.equal(line.tone, 'other')
|
||
})
|
||
|
||
test('the feed never asks for the aggregate kind', () => {
|
||
// `player.tally` is public and is flushed once a minute per active player
|
||
// (§8.6). A feed that included it would be mostly wood counts; it is the
|
||
// leaderboard's input, and that is where it shows up.
|
||
assert.ok(!FEED_KINDS.includes('player.tally'))
|
||
for (const filter of FILTERS) {
|
||
for (const kind of filter.kinds) {
|
||
assert.ok(FEED_KINDS.includes(kind), `filter "${filter.id}" asks for ${kind}, which the feed does not carry`)
|
||
}
|
||
}
|
||
})
|
||
|
||
test('every kind the feed asks for is one the public route will serve', () => {
|
||
// Held against the module's own allowlist rather than against a copy of it: a
|
||
// kind this file asked for and `server/catalogue.js` refuses is a filter that
|
||
// silently returns nothing, which reads as a quiet server.
|
||
//
|
||
// A CommonJS file from the server half, read by an ESM test through
|
||
// `createRequire`. Crossing the two halves is fine HERE and nowhere else:
|
||
// `test/` is not shipped, and `scripts/checkImports.js` governs what is.
|
||
const catalogue = createRequire(import.meta.url)('../../server/catalogue.js')
|
||
for (const kind of FEED_KINDS) {
|
||
assert.ok(catalogue.PUBLIC_KINDS.includes(kind), `the feed asks for ${kind}, which is not public`)
|
||
}
|
||
})
|
||
|
||
test('a boss appearing and falling read as sentences, with its name as typed (runicnpc stage 6)', () => {
|
||
const up = describe(row('npc.boss.spawned', { profile: 'juggernaut', name: 'The Juggernaut' }))
|
||
assert.deepEqual({ actor: up.actor, verb: up.verb, detail: up.detail }, { actor: 'The Juggernaut', verb: 'has appeared', detail: 'a boss' })
|
||
|
||
const killed = describe(row('npc.boss.died', { profile: 'juggernaut', name: 'The Juggernaut', killerName: 'Marisol', damagers: 5 }))
|
||
assert.deepEqual({ tone: killed.tone, actor: killed.actor, verb: killed.verb, subject: killed.subject, detail: killed.detail },
|
||
{ tone: 'kill', actor: 'Marisol', verb: 'killed', subject: 'The Juggernaut', detail: '5 players fought it' })
|
||
|
||
// No player landed the blow (a turret, or another NPC): it still fell.
|
||
const fell = describe(row('npc.boss.died', { profile: 'juggernaut', name: 'The Juggernaut', damagers: 1 }))
|
||
assert.deepEqual({ actor: fell.actor, verb: fell.verb, detail: fell.detail }, { actor: 'The Juggernaut', verb: 'was killed', detail: '' })
|
||
|
||
assert.deepEqual(kindsFor('bosses'), ['npc.boss.spawned', 'npc.boss.died'])
|
||
})
|
||
|
||
test('an unknown filter falls back to everything rather than to nothing', () => {
|
||
assert.deepEqual(kindsFor('nonsense'), FEED_KINDS)
|
||
assert.deepEqual(kindsFor(undefined), FEED_KINDS)
|
||
})
|
||
|
||
// ── Rust's own world events (protocol 14, PLAN_REDESIGNS §14.3.1) ─────────
|
||
//
|
||
// The frames here are what the SERVER sends a viewer (`worldEvents.shapeFor`),
|
||
// not what the plugin emitted: a grid instead of a position, names only for the
|
||
// presence audience, `credited` by the server's rule.
|
||
|
||
const world = (frame) => row('world.event', { kind: 'world.event', ...frame })
|
||
|
||
test('a downed helicopter names whom the rule credits, and only them', () => {
|
||
const one = describe(world({ event: 'heli.downed', grid: 'G7', creditRule: 'blow', creditedCount: 1, credited: [{ name: 'Alice' }] }))
|
||
assert.deepEqual([one.tone, one.verb, one.subject, one.detail], ['kill', 'Patrol helicopter downed by', 'Alice', 'G7'])
|
||
|
||
const many = describe(world({ event: 'heli.downed', grid: 'G7', creditRule: 'all', creditedCount: 4, credited: [{ name: 'Alice' }, { name: 'Bob' }, { name: 'Cleo' }, { name: 'Dov' }] }))
|
||
assert.equal(many.subject, 'Alice, Bob and 2 others')
|
||
})
|
||
|
||
test('below the presence audience the same line names nobody (D376)', () => {
|
||
const line = describe(world({ event: 'heli.downed', grid: 'G8', creditRule: 'blow', creditedCount: 1 }))
|
||
assert.deepEqual([line.verb, line.subject, line.detail], ['Patrol helicopter downed', null, 'G8'])
|
||
|
||
const hack = describe(world({ event: 'crate.hack.started', where: 'oilrig_large', grid: 'C3' }))
|
||
assert.equal(hack.actor, null)
|
||
assert.equal(hack.verb, 'A locked crate is being hacked on Large Oil Rig')
|
||
})
|
||
|
||
test('a named line reads as the plan writes it', () => {
|
||
const hack = describe(world({ event: 'crate.hack.started', where: 'oilrig_large', name: 'Bob' }))
|
||
assert.equal(`${hack.actor} ${hack.verb}`, 'Bob started hacking the locked crate on Large Oil Rig')
|
||
|
||
const road = describe(world({ event: 'bradley.spawned', where: 'road', count: 12 }))
|
||
assert.equal(road.verb, '12 road Bradleys are out')
|
||
|
||
assert.equal(describe(world({ event: 'deepsea.opened' })).verb, 'The Deep Sea is open')
|
||
assert.equal(describe(world({ event: 'cargo.docked', harbourName: 'Harbor', grid: 'Q9' })).verb, 'The cargo ship has docked at Harbor')
|
||
assert.equal(describe(world({ event: 'chinook.dropped', monumentName: 'Water Treatment Plant' })).verb, 'A Chinook dropped a locked crate at Water Treatment Plant')
|
||
})
|
||
|
||
test('every type the server knows has its own words, and an unknown one says its name', () => {
|
||
const { TYPE_IDS } = createRequire(import.meta.url)('../../server/worldEvents.js')
|
||
for (const event of TYPE_IDS) {
|
||
const line = describe(world({ event }))
|
||
assert.notEqual(line.tone, 'other', `${event} has no words of its own`)
|
||
assert.ok(line.verb && line.verb !== event, event)
|
||
}
|
||
assert.equal(describe(world({ event: 'meteor.landed' })).verb, 'meteor.landed')
|
||
})
|
||
|
||
test('the world filter asks for world events, Dynamic PVP zones opening, Dangerous Treasures and Guarded Crate, which the server serves publicly', () => {
|
||
assert.deepEqual(kindsFor('world'), ['world.event', 'pvp.zone.created', 'dt.started', 'dt.won', 'gc.started', 'gc.ended'])
|
||
assert.ok(!FEED_KINDS.includes('dt.ended'), 'a treasure event ending is a staff line')
|
||
assert.ok(!FEED_KINDS.includes('gc.failed'), 'a guarded crate timing out is a staff line')
|
||
assert.ok(FEED_KINDS.includes('pvp.zone.created'))
|
||
assert.ok(!FEED_KINDS.includes('pvp.zone.deleted'), 'a zone closing is a staff line')
|
||
})
|
||
|
||
// ── Dynamic PVP's zones (protocol 14, PLAN_REDESIGNS §14.3.3) ─────────────
|
||
|
||
const pvp = (kind, frame) => row(kind, { kind, ...frame })
|
||
|
||
test('a zone opening says what it is around, where, and for how long, and names nobody', () => {
|
||
const heli = describe(pvp('pvp.zone.created', { event: 'Helicopter', grid: 'G7', durationSec: 600 }))
|
||
assert.equal(heli.verb, 'A PvP zone opened around the downed patrol helicopter')
|
||
assert.equal(heli.detail, 'G7 · for 10 min')
|
||
assert.equal(heli.actor, null)
|
||
assert.equal(heli.subject, null)
|
||
|
||
const cargo = describe(pvp('pvp.zone.created', { event: 'CargoShip', grid: 'Q9', durationSec: -1 }))
|
||
assert.equal(cargo.verb, 'A PvP zone opened around the cargo ship')
|
||
assert.equal(cargo.detail, 'Q9 · while the ship is here')
|
||
|
||
assert.equal(describe(pvp('pvp.zone.created', { event: 'Bradley', durationSec: 90 })).detail, 'for 90 s')
|
||
})
|
||
|
||
test('a zone the public are not told the event of is just a zone; staff read a monument’s name as written', () => {
|
||
assert.equal(describe(pvp('pvp.zone.created', { grid: 'C3' })).verb, 'A PvP zone opened')
|
||
assert.equal(describe(pvp('pvp.zone.created', { event: 'Airfield' })).verb, 'A PvP zone opened (Airfield)')
|
||
})
|
||
|
||
test('a zone closing, for a staff reader, says which one closed', () => {
|
||
assert.equal(describe(pvp('pvp.zone.deleted', { event: 'Helicopter' })).verb, 'The PvP zone around the downed patrol helicopter has closed')
|
||
assert.equal(describe(pvp('pvp.zone.deleted', {})).verb, 'The PvP zone has closed')
|
||
})
|
||
|
||
test('every one of Dynamic PVP’s own events has words, and the server and client agree on which they are', () => {
|
||
const { GENERAL_EVENTS } = createRequire(import.meta.url)('../../server/dynamicPvp.js')
|
||
for (const event of GENERAL_EVENTS) {
|
||
assert.ok(!describe(pvp('pvp.zone.created', { event })).verb.includes(`(${event})`), `${event} has no words of its own`)
|
||
}
|
||
})
|
||
|
||
// ── Dangerous Treasures (protocol 14, PLAN_REDESIGNS §14.3.4) ─────────────
|
||
|
||
const treasure = (kind, frame) => row(kind, { kind, ...frame })
|
||
|
||
test('a treasure event starting says where, and names nobody', () => {
|
||
const line = describe(treasure('dt.started', { grid: 'K12' }))
|
||
assert.equal(line.verb, 'A Dangerous Treasures event has started')
|
||
assert.equal(line.detail, 'K12')
|
||
assert.equal(line.actor, null)
|
||
})
|
||
|
||
test('a win names its winner only when the server sent the name, and says how many fought', () => {
|
||
const named = describe(treasure('dt.won', { grid: 'K12', name: 'Alice', invaderCount: 3 }))
|
||
assert.equal(named.actor, 'Alice')
|
||
assert.equal(named.verb, 'won the treasure')
|
||
assert.equal(named.detail, 'K12 · 3 players fought for it')
|
||
|
||
const unnamed = describe(treasure('dt.won', { grid: 'K12', invaderCount: 1 }))
|
||
assert.equal(unnamed.actor, null)
|
||
assert.equal(unnamed.verb, 'The treasure was won')
|
||
assert.equal(unnamed.detail, 'K12')
|
||
})
|
||
|
||
test('a treasure event ending, for a staff reader, is its own line', () => {
|
||
assert.equal(describe(treasure('dt.ended', { grid: 'K12' })).verb, 'A Dangerous Treasures event has ended')
|
||
})
|
||
|
||
// ── Guarded Crate (protocol 14, PLAN_REDESIGNS §14.3.5) ───────────────────
|
||
|
||
const guarded = (kind, frame) => row(kind, { kind, ...frame })
|
||
|
||
test('a guarded crate landing says where, and names nobody', () => {
|
||
const line = describe(guarded('gc.started', { grid: 'M4' }))
|
||
assert.equal(line.verb, 'A guarded crate landed')
|
||
assert.equal(line.detail, 'M4')
|
||
assert.equal(line.actor, null)
|
||
})
|
||
|
||
test('a clearing names its clearer only when the server sent the name', () => {
|
||
const named = describe(guarded('gc.ended', { grid: 'M4', won: true, name: 'Alice' }))
|
||
assert.equal(named.actor, 'Alice')
|
||
assert.equal(named.verb, 'cleared the guarded crate')
|
||
assert.equal(named.detail, 'M4')
|
||
|
||
const unnamed = describe(guarded('gc.ended', { grid: 'M4', won: true }))
|
||
assert.equal(unnamed.actor, null)
|
||
assert.equal(unnamed.verb, 'The guarded crate was cleared')
|
||
})
|
||
|
||
test('an ending with nobody clearing it, and a timeout for a staff reader, are their own lines', () => {
|
||
assert.equal(describe(guarded('gc.ended', { grid: 'M4', won: false })).verb, 'A guarded crate ended with nobody clearing it')
|
||
assert.equal(describe(guarded('gc.failed', { grid: 'M4' })).verb, 'A guarded crate timed out with its guards standing')
|
||
})
|