Is the box up, and speech, and the brain; how many conversations, turns, IR presses, and looks, today and all time. Counts and times only: no transcripts. - status/status.js: what the events and checks add up to, and the page - status/status-server.js: the page, the box's events and heartbeat, and a TCP check of each part every 30 s; events kept in a file - status/pdtv26-status.service and Makefile: sudo make install on the home server, answering only the private network - checks: status (its tests) Generated-by: Claude Opus 5.5
109 lines
5.1 KiB
JavaScript
109 lines
5.1 KiB
JavaScript
// 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('a ready during the outage counts: services restart together, and the check sees the box up last', () => {
|
|
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.record({ at: 2.5 * MIN, kind: 'ready', source: 'pdtv26-button' });
|
|
s.check('box', true, 3 * MIN);
|
|
assert.deepEqual(s.summary(4 * MIN).state, { word: 'ready for duty', since: 2.5 * MIN });
|
|
});
|
|
|
|
test('a ready from before the server started counts, if the server saw no outage', () => {
|
|
const s = new Status(dayOf);
|
|
s.record({ at: 1 * MIN, kind: 'ready', source: 'pdtv26-button' });
|
|
s.check('box', true, 10 * MIN);
|
|
s.beat(10.5 * MIN);
|
|
assert.deepEqual(s.summary(11 * MIN).state, { word: 'ready for duty', since: 1 * MIN });
|
|
});
|
|
|
|
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: '<script>', times: 99 }),
|
|
{ at: 5, kind: 'ir', source: 'x' });
|
|
});
|
|
|
|
test('the page says what is down, and why, and escapes what it shows', () => {
|
|
const s = new Status(dayOf);
|
|
s.check('box', true, 0);
|
|
s.check('speech', true, 0);
|
|
s.check('brain', false, 0);
|
|
s.record({ at: 1, kind: 'end', source: 'pdtv26-button', reason: '<b>quiet</b>' });
|
|
const html = render(s.summary(MIN), (t) => `t${t}`, 'box');
|
|
assert.match(html, /<tr><td>IR presses<\/td><td>\d+<\/td><td>\d+<\/td><\/tr>/);
|
|
assert.match(html, /<b>booting<\/b>/);
|
|
assert.match(html, /✗ the brain \(its channel\)/);
|
|
assert.match(html, /<title>box status<\/title>/);
|
|
assert.match(html, /waiting at its prompt/);
|
|
assert.match(html, /end <b>quiet<\/b>/);
|
|
assert.match(html, /name="viewport"/);
|
|
});
|