feat(rust): notifications and engagement (phase 10, protocol 7)

Registers the engagement set R7 put in v1: thirteen triggers, four push
streams, three audiences, four bodies (two triggers, email and in-app)
and thirteen disabled rules in seven groups (PLAN.md §25, D59-D68).

The raid alert goes to everyone authorised on the tool cupboard, one
emit per linked person with ownerUserId, so the owner ceiling holds per
emit. It covers doors and walls (protocol 7), never names the raider,
alerts nobody when there is no cupboard, and carries ownerOnline so
"offline only" is the seeded rule's condition rather than code.

The fan-out runs off ingest before a frame is applied, since applying a
disband deletes the roster the notice is sent to. A replayed event is
told only while it is news: 15 minutes for broadcasts, 24 hours for
personal and staff events. Dedupe keys come from the event, not the
sidecar's row id. Server online/offline and a new kills leader are
in-memory transitions, never on first sight, and a tie is not a lead.
A login with no approval within a minute becomes a staff notice via a
query, so a restart loses nothing.

Also fixes a phase-4 gap (D68): the refresh now asks /health, so a game
that hung, or whose bridge was unloaded, while the sidecar stayed up no
longer reads as online. It stops naming players as online, and a stale
board no longer moves "last seen".

engagement-triggers.json is the committed freeze of all of it, checked
in CI with line endings normalised. The check was verified by breaking
it both ways.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E14m6SuuY6i1vASFeGDBeY
This commit is contained in:
2026-09-23 06:06:08 -05:00
parent dc3c9689b4
commit 285db0baa7
22 changed files with 3256 additions and 32 deletions

View File

@@ -91,9 +91,12 @@ test('neither unhappy path calls putState', async () => {
const originalPut = db.putState
const originalMark = db.markUnreachable
const originalBoards = sidecar.boards
const originalHealth = sidecar.health
const marked = []
let putCalls = 0
sidecar.health = async () => ({ ok: false, status: 0, data: null })
db.putState = async () => { putCalls += 1 }
db.markUnreachable = async (id, reachable) => { marked.push([id, reachable]) }
@@ -112,5 +115,83 @@ test('neither unhappy path calls putState', async () => {
db.putState = originalPut
db.markUnreachable = originalMark
sidecar.boards = originalBoards
sidecar.health = originalHealth
}
})
test('a board the game left behind is not a game that is up (D68)', async () => {
// The sidecar keeps its last `server.hello` after the plugin disconnects, so
// until phase 10 a hung game — or an unloaded bridge — with the sidecar still
// up read as ONLINE here, with the players it had when it stopped. Only
// `/health` knows whether the plugin is connected now.
withCore(fakeCtx({ db: { query: () => Promise.resolve([]), pool: {} } }))
const db = require('../model/servers/servers.db')
const sidecar = require('../sidecarClient')
const ingest = require('../ingest')
const boot = require('../boot')
const engagement = require('../engagement/emit')
const saved = { put: db.putState, boards: sidecar.boards, health: sidecar.health, apply: ingest.applyBoards }
const puts = []
const applied = []
engagement.reset()
db.putState = async (state) => { puts.push(state) }
ingest.applyBoards = async (id, boards) => { applied.push(boards) }
sidecar.boards = async () => ({
ok: true,
status: 200,
data: { boards: {
'server.hello': { players: 12, maxPlayers: 100, hostname: 'Main' },
'players.online': { players: [{ steamId: '1', name: 'Still here?' }] },
} },
})
const server = { id: 'main', name: 'Main', baseUrl: 'http://127.0.0.1:1', token: 't', protocol: 7 }
try {
sidecar.health = async () => ({ ok: true, status: 200, data: { plugin_connected: false } })
await boot.refreshOne(server)
assert.strictEqual(puts[0].online, false)
assert.strictEqual(puts[0].players, 0, 'the last count is not a count')
assert.strictEqual(puts[0].seen, false, 'a stale board must not move "last seen"')
assert.strictEqual(puts[0].hostname, 'Main', 'the description is still written')
assert.deepStrictEqual(applied[0]['players.online'].players, [], 'nobody is named as online')
sidecar.health = async () => ({ ok: true, status: 200, data: { plugin_connected: true } })
await boot.refreshOne(server)
assert.strictEqual(puts[1].online, true)
assert.strictEqual(puts[1].players, 12)
assert.notStrictEqual(puts[1].seen, false)
assert.strictEqual(applied[1]['players.online'].players.length, 1)
// An unanswered /health is unknown, and unknown is not up.
sidecar.health = async () => ({ ok: false, status: 0, data: null })
await boot.refreshOne(server)
assert.strictEqual(puts[2].online, false)
} finally {
db.putState = saved.put
sidecar.boards = saved.boards
sidecar.health = saved.health
ingest.applyBoards = saved.apply
engagement.reset()
}
})
test('putState moves last_seen_at only for a game that was seen', async () => {
const queries = []
withCore(fakeCtx({
db: { query: (sql, params) => { queries.push({ sql, params }); return Promise.resolve([]) }, pool: {} },
}))
const db = require('../model/servers/servers.db')
await db.putState({ serverId: 'main', reachable: true, online: false, seen: false })
await db.putState({ serverId: 'main', reachable: true, online: true })
// The two trailing parameters feed the two IF(?, CURRENT_TIMESTAMP, …)s.
assert.deepStrictEqual(queries[0].params.slice(-2), [0, 0])
assert.deepStrictEqual(queries[1].params.slice(-2), [1, 1])
assert.strictEqual((queries[0].sql.match(/\?/g) || []).length, queries[0].params.length, 'every placeholder has a value')
})