screentinker/server/test/status-debug-toggle.test.js
ScreenTinker 9418582de5 feat(#146): always-on devices_connected + admin-toggleable /api/status debug block
1. devices_connected (always on, never gated): a top-level /api/status field next to
   loop_lag = LIVE WS socket count from the heartbeat connection map (getConnectedCount),
   NOT devices.status='online' (which lags by the offline-timeout). The single
   most-glanced operational number, so it can't disappear when debug is off. Also dropped
   4 dead per-poll COUNT(*) queries the route computed but never returned.

2. debug block behind an admin flag: new minimal app_settings KV table (none existed;
   ai_settings is per-workspace, white_labels is branding) + lib/app-settings.js (cached,
   refresh-on-write so status polls read a cached boolean, not a DB row).
   routes/status.js includes `debug` ONLY when status_debug_enabled is on (persisted value
   overrides the STATUS_DEBUG_ENABLED env default); when off the key is omitted entirely.

3. Admin toggle: GET/PUT /api/admin/status-debug (requirePlatformAdmin, mirrors the
   branding endpoints) + a checkbox in the Admin tab "Status endpoint" section
   (mirrors the branding checkbox). Takes effect on the next poll, no restart.

Tests: devices_connected always present+numeric and rises with a live socket (booted +
socket.io-client); debug present by default, admin flips OFF -> key omitted (loop_lag +
devices_connected remain) -> ON again, no restart; non-admin 403, anon 401; unit coverage
for getConnectedCount + app-settings default/override. Suite 289/289.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 18:45:40 -05:00

88 lines
4.6 KiB
JavaScript

'use strict';
// #146 — /api/status: always-on live-fleet gauge (devices_connected, from the WS
// connection map) + admin-toggleable debug block. Booted server + JWT + DB access.
const { test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const { spawn } = require('node:child_process');
const path = require('node:path');
const os = require('node:os');
const fs = require('node:fs');
const crypto = require('node:crypto');
const Database = require('better-sqlite3');
const ioClient = require('socket.io-client');
const PORT = 3998;
const BASE = `http://127.0.0.1:${PORT}`;
const DATA_DIR = path.join(os.tmpdir(), 'st-statusdbg-' + crypto.randomBytes(4).toString('hex'));
let proc, db;
before(async () => {
const logFd = fs.openSync(path.join(os.tmpdir(), 'st-statusdbg.log'), 'w');
proc = spawn('node', ['server.js'], {
cwd: path.join(__dirname, '..'),
env: { ...process.env, DATA_DIR, SELF_HOSTED: 'true', PORT: String(PORT), NODE_ENV: 'test' }, // no STATUS_DEBUG_ENABLED -> default ON
stdio: ['ignore', logFd, logFd],
});
let up = false;
for (let i = 0; i < 80; i++) { try { const r = await fetch(BASE + '/api/status'); if (r.ok) { up = true; break; } } catch { /* */ } await new Promise(r => setTimeout(r, 250)); }
if (!up) throw new Error('server did not boot');
db = new Database(path.join(DATA_DIR, 'db', 'remote_display.db'));
});
after(() => { try { db && db.close(); } catch { /* */ } try { proc.kill('SIGKILL'); } catch { /* */ } });
const status = async () => (await fetch(BASE + '/api/status')).json();
const reg = (o) => ({ method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(o) });
const put = (tok, o) => ({ method: 'PUT', headers: tok ? { Authorization: 'Bearer ' + tok, 'Content-Type': 'application/json' } : { 'Content-Type': 'application/json' }, body: JSON.stringify(o) });
test('devices_connected is always present, numeric, and reflects the LIVE socket map', async () => {
const b = await status();
assert.equal(typeof b.devices_connected, 'number', 'devices_connected always present + numeric');
const before = b.devices_connected;
// open a real device socket -> the connection map (and the count) must move
const s = ioClient(`${BASE}/device`, { transports: ['websocket'], reconnection: false, forceNew: true });
await new Promise((resolve) => {
s.on('connect', () => s.emit('device:register', { pairing_code: String(crypto.randomInt(100000, 1000000)) }));
s.on('device:registered', resolve);
setTimeout(resolve, 3000);
});
await new Promise(r => setTimeout(r, 150));
const during = (await status()).devices_connected;
assert.ok(during >= before + 1, `devices_connected rose with a live socket (${before} -> ${during})`);
try { s.close(); } catch { /* */ }
});
test('debug block: present by default (env), and gated by the admin flag', async () => {
// default (no env override) -> ON
let b = await status();
assert.ok(b.debug, 'debug present by default');
assert.equal(typeof b.debug.flap.buckets, 'number');
// register an admin + a normal user; promote the admin in the DB (role read from DB).
const adminEmail = 'ad' + crypto.randomBytes(4).toString('hex') + '@x.local';
const userEmail = 'u' + crypto.randomBytes(4).toString('hex') + '@x.local';
const adminTok = (await (await fetch(BASE + '/api/auth/register', reg({ email: adminEmail, password: 'Passw0rd123' }))).json()).token;
const userTok = (await (await fetch(BASE + '/api/auth/register', reg({ email: userEmail, password: 'Passw0rd123' }))).json()).token;
db.prepare("UPDATE users SET role = 'platform_admin' WHERE email = ?").run(adminEmail);
// non-admin cannot flip it
assert.equal((await fetch(BASE + '/api/admin/status-debug', put(userTok, { enabled: false }))).status, 403, 'non-admin denied');
// unauthenticated cannot flip it
assert.equal((await fetch(BASE + '/api/admin/status-debug', put(null, { enabled: false }))).status, 401, 'anon denied');
// admin flips OFF -> debug omitted; loop_lag + devices_connected remain
const off = await fetch(BASE + '/api/admin/status-debug', put(adminTok, { enabled: false }));
assert.equal(off.status, 200);
b = await status();
assert.equal('debug' in b, false, 'debug key omitted entirely when off');
assert.ok(b.loop_lag, 'loop_lag still present when debug off');
assert.equal(typeof b.devices_connected, 'number', 'devices_connected still present when debug off');
// admin flips ON -> debug back, no restart
assert.equal((await fetch(BASE + '/api/admin/status-debug', put(adminTok, { enabled: true }))).status, 200);
b = await status();
assert.ok(b.debug, 'debug back on after re-enable, no restart');
});