// Tests for the status page: no network, no clock. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { Status, cleanEvent, render } from './status.js'; const MIN = 60_000; const dayOf = (t) => String(Math.floor(t / (24 * 60 * MIN))); test('unreachable, then booting, then ready, then in a conversation, then ready again', () => { const s = new Status(dayOf); assert.equal(s.summary(0).state.word, 'not checked yet'); s.check('box', false, 1 * MIN); assert.deepEqual(s.summary(2 * MIN).state, { word: 'unreachable', since: 1 * MIN }); s.check('box', true, 3 * MIN); assert.deepEqual(s.summary(3 * MIN).state, { word: 'booting', since: 3 * MIN }); s.record({ at: 3.5 * MIN, kind: 'ready', source: 'pdtv26-button' }); assert.deepEqual(s.summary(4 * MIN).state, { word: 'ready for duty', since: 3.5 * MIN }); s.record({ at: 5 * MIN, kind: 'conversation', source: 'pdtv26-button' }); assert.equal(s.summary(6 * MIN).state.word, 'in a conversation'); s.record({ at: 7 * MIN, kind: 'end', source: 'pdtv26-button', reason: 'goodbye' }); assert.equal(s.summary(8 * MIN).state.word, 'ready for duty'); }); test('heartbeats keep it ready; three missed and it is not responding', () => { const s = new Status(dayOf); s.check('box', true, 0); s.record({ at: 1 * MIN, kind: 'ready', source: 'pdtv26-button' }); s.beat(10 * MIN); assert.equal(s.summary(12 * MIN).state.word, 'ready for duty'); assert.deepEqual(s.summary(14 * MIN).state, { word: 'not responding', since: 10 * MIN, why: 'no heartbeat from its button service' }); assert.match(render(s.summary(14 * MIN), (t) => `t${t}`, 'box'), /last heartbeat: t600000/); }); test('a ready from before the box went away does not count: booting', () => { const s = new Status(dayOf); s.check('box', true, 0); s.record({ at: 1 * MIN, kind: 'ready', source: 'pdtv26-button' }); s.check('box', false, 2 * MIN); s.check('box', true, 3 * MIN); assert.equal(s.summary(4 * MIN).state.word, 'booting'); }); test('checks keep when they last changed', () => { const s = new Status(dayOf); s.check('brain', true, 1 * MIN); s.check('brain', true, 2 * MIN); s.check('brain', false, 3 * MIN); const brain = s.summary(4 * MIN).checks.find((c) => c.name === 'brain'); assert.deepEqual([brain.up, brain.since, brain.at], [false, 3 * MIN, 3 * MIN]); }); test('burgers sold: today and all time, IR presses counted by times', () => { const s = new Status(dayOf); const day = 24 * 60 * MIN; s.record({ at: 1, kind: 'conversation', source: 'pdtv26-button' }); s.record({ at: day + 1, kind: 'conversation', source: 'pdtv26-button' }); s.record({ at: day + 2, kind: 'turn', source: 'pdtv26-button' }); s.record({ at: day + 3, kind: 'huh', source: 'pdtv26-button' }); s.record({ at: day + 4, kind: 'ir', source: 'pdtv26-ir', code: '20 DF F4 0B', times: 3 }); s.record({ at: day + 5, kind: 'ir', source: 'pdtv26-ir', code: '20 DF 10 EF' }); const { today, total } = s.summary(day + 10); assert.deepEqual(today, { conversations: 1, turns: 1, huh: 1, interrupts: 0, irPresses: 4, looks: 0 }); assert.equal(total.conversations, 2); }); test('only our kinds of events, and only their tame parts, are kept', () => { assert.equal(cleanEvent({ kind: 'transcript', text: 'secrets' }), undefined); assert.equal(cleanEvent(null), undefined); assert.deepEqual(cleanEvent({ at: 5, kind: 'ir', source: 'pdtv26-ir', code: '20 DF 10 EF', times: 2, text: 'x' }), { at: 5, kind: 'ir', source: 'pdtv26-ir', code: '20 DF 10 EF', times: 2 }); assert.deepEqual(cleanEvent({ at: 5, kind: 'ir', source: 'x', code: '