screentinker/server/test/observability-units.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

34 lines
1.7 KiB
JavaScript

'use strict';
// #146 — unit coverage for the two new primitives behind the /api/status changes.
const os = require('node:os');
const path = require('node:path');
const crypto = require('node:crypto');
process.env.DATA_DIR = path.join(os.tmpdir(), 'st-obsunit-' + crypto.randomBytes(4).toString('hex'));
const { test } = require('node:test');
const assert = require('node:assert/strict');
const heartbeat = require('../services/heartbeat');
const appSettings = require('../lib/app-settings');
test('heartbeat.getConnectedCount reflects the live connection map (not DB status)', () => {
const start = heartbeat.getConnectedCount();
heartbeat.registerConnection('dev-a', 'sock-a');
heartbeat.registerConnection('dev-b', 'sock-b');
assert.equal(heartbeat.getConnectedCount(), start + 2, 'count rises with registered sockets');
heartbeat.removeConnection('dev-a');
assert.equal(heartbeat.getConnectedCount(), start + 1, 'count falls when a socket leaves');
heartbeat.removeConnection('dev-b');
assert.equal(heartbeat.getConnectedCount(), start);
});
test('app-settings: env default until set, then persisted value overrides (cached)', () => {
assert.equal(appSettings.getBool('status_debug_enabled', true), true, 'falls back to env default when unset');
assert.equal(appSettings.getBool('status_debug_enabled', false), false, 'default honored when unset');
appSettings.setBool('status_debug_enabled', false);
assert.equal(appSettings.getBool('status_debug_enabled', true), false, 'persisted false overrides the (true) default');
appSettings.setBool('status_debug_enabled', true);
assert.equal(appSettings.getBool('status_debug_enabled', false), true, 'persisted true overrides the (false) default');
});