// ── The model, with no database ─────────────────────────────────────────── // // The `.db.js` / `.model.js` split pays for itself here: the logic worth testing // is in the model, and the model's only dependency is a function that returns a // row. Stub that and there is nothing to stand up. const test = require('node:test') const assert = require('node:assert') const db = require('../model/worldStatus/worldStatus.db') const { getPublicStatus, STALE_AFTER_MS } = require('../model/worldStatus/worldStatus.model') const NOW = Date.parse('2026-08-12T12:00:00Z') /** Replace `getStatus` for one test and put it back afterwards. */ function withRow(row, fn) { const real = db.getStatus db.getStatus = async () => row return Promise.resolve(fn()).finally(() => { db.getStatus = real }) } test('a fresh row reports the world online', () => withRow( { online: 1, players: 12, worldName: 'Example World', updatedAt: new Date(NOW - 1000) }, async () => { const status = await getPublicStatus(NOW) assert.strictEqual(status.online, true) assert.strictEqual(status.players, 12) assert.strictEqual(status.worldName, 'Example World') assert.strictEqual(status.stale, false) }, )) test('a stale row is reported offline, whatever it says', () => withRow( { online: 1, players: 12, worldName: 'Example World', updatedAt: new Date(NOW - STALE_AFTER_MS - 1) }, async () => { const status = await getPublicStatus(NOW) // The row claims the world is up. Nothing has written it in longer than the // freshness window, so the claim is not evidence of anything. assert.strictEqual(status.online, false) assert.strictEqual(status.players, 0) assert.strictEqual(status.stale, true) // The name is still worth showing — it does not go stale the way a player // count does. assert.strictEqual(status.worldName, 'Example World') }, )) test('no row at all is an answer, not an error', () => withRow(null, async () => { // A fresh install whose first boot has not finished replaying the schema. // The site must render; the next boot fixes it. const status = await getPublicStatus(NOW) assert.deepStrictEqual(status, { online: false, players: 0, worldName: null, updatedAt: null, stale: true, }) }))