// ── Dynamic PVP's zones (PLAN_REDESIGNS §14.3.3, protocol 14) ────────────── // // Each test is one way the feature could look right and be wrong: // // a zone's position reaches a viewer below staff through the feed, round the // map's own audience (D111) // an operator's own event name, typed into Dynamic PVP's config, reaches a // public page // the feed shapes a zone line without its grid // a frame of another kind is changed by the zone reader const test = require('node:test') const assert = require('node:assert') const { fakeCtx } = require('./_fakes') /** Installs a core whose `db.query` answers `rows`. */ function withRows(rows = []) { const ctx = fakeCtx({ db: { query: (sql, params = []) => Promise.resolve(typeof rows === 'function' ? rows(sql, params) : rows), pool: {} }, }) require('../core')._reset() require('../core').init(ctx) } withRows() const dynamicPvp = require('../dynamicPvp') const { gridOf } = require('../worldEvents') const NOW = Date.now() const GEOMETRY = { worldSize: 6000, gridCells: 41, gridCellSize: 146.34146341463415 } const created = (over = {}) => ({ kind: 'pvp.zone.created', type: 'event', t: NOW, serverId: 'main', wipeId: 'w1', zoneId: '1412203045', event: 'Helicopter', x: 812, z: -340.5, radius: 100, shape: 'sphere', durationSec: 600, ...over, }) test('the public read the zone, its grid and how long it lasts, and never where it is', () => { const out = dynamicPvp.shapeFor(created(), { staff: false, geometry: GEOMETRY }) assert.strictEqual(out.event, 'Helicopter') assert.strictEqual(out.grid, gridOf(GEOMETRY, 812, -340.5)) assert.strictEqual(out.radius, 100) assert.strictEqual(out.durationSec, 600) for (const hidden of ['x', 'z', 'zoneId']) assert.strictEqual(hidden in out, false, `${hidden} is staff's`) }) test('staff read the position and the zone id too', () => { const out = dynamicPvp.shapeFor(created(), { staff: true, geometry: GEOMETRY }) assert.strictEqual(out.x, 812) assert.strictEqual(out.z, -340.5) assert.strictEqual(out.zoneId, '1412203045') }) test("a monument's or an operator's own event name is staff's alone", () => { for (const event of ['Airfield', 'My secret arena']) { assert.strictEqual('event' in dynamicPvp.shapeFor(created({ event }), { staff: false, geometry: GEOMETRY }), false) assert.strictEqual(dynamicPvp.shapeFor(created({ event }), { staff: true, geometry: GEOMETRY }).event, event) } for (const event of dynamicPvp.GENERAL_EVENTS) assert.ok(dynamicPvp.isGeneral(event)) }) test('without a map geometry a line has no grid, and off the map neither', () => { assert.strictEqual('grid' in dynamicPvp.shapeFor(created(), { geometry: null }), false) assert.strictEqual('grid' in dynamicPvp.shapeFor(created({ x: 9000 }), { geometry: GEOMETRY }), false) }) test('a frame of another kind is passed back untouched', () => { const other = { kind: 'world.event', event: 'heli.downed', x: 1, z: 2 } assert.strictEqual(dynamicPvp.shapeFor(other, { staff: false }), other) }) test('the feed reads a zone line for its viewer, with the map’s grid', async () => { const stored = created({ event: 'Airfield' }) withRows((sql) => { if (sql.includes('FROM rust_events')) return [{ id: 11, kind: 'pvp.zone.created', t: NOW, wipeId: 'w1', steamId: null, raw: JSON.stringify(stored) }] if (sql.includes('FROM rust_servers')) return [{ id: 'main', name: 'Main', enabled: 1 }] if (sql.includes('FROM rust_map_images')) return [{ serverId: 'main', ...GEOMETRY }] return [] }) const events = require('../model/events/events.model') const [signedOut] = await events.recent({ serverId: 'main' }) assert.strictEqual(signedOut.frame.grid, gridOf(GEOMETRY, 812, -340.5)) assert.strictEqual('x' in signedOut.frame, false) assert.strictEqual('event' in signedOut.frame, false) const [staff] = await events.recent({ serverId: 'main', staff: true }) assert.strictEqual(staff.frame.x, 812) assert.strictEqual(staff.frame.event, 'Airfield') })