mirror of
https://github.com/screentinker/screentinker.git
synced 2026-08-14 14:23:14 -06:00
Best-effort "last gasp" so Offline is annotated with WHY it went away — completing the liveness story.
Categories: crashed (client uncaught-exception), clean_exit (client confident lifecycle-end, best-effort),
silent (SERVER-inferred by absence — the honest catch-all for violent/external death incl. force-stop/MDM).
SERVER:
- device:exit socket handler + token-authed beacon POST /api/device/exit (reliable-on-unload). Both gated
by liveness.sanitizeExitReason (honesty: only crashed/clean_exit accepted; 'silent'/unknown rejected).
- offline_reason/offline_reason_at/offline_detail columns (additive migration). Clear-on-online (a reason
is always THIS session's); offline transition COALESCEs to 'silent'. Pure annotation — offline detection
and #148/liveness are untouched. Offline dashboard emits carry offline_reason + client_type.
CLIENTS (canonical {reason,detail} shape):
- /player: window error/unhandledrejection + pagehide(persisted=false) -> sendBeacon.
- .wgt: same + BACK-key exit -> socket.emit + sendBeacon.
- APK: global UncaughtExceptionHandler -> crashed (blocking beacon, chains to default); Service.onDestroy
-> clean_exit (socket + bounded beacon). New ExitSignal.kt. onStop/onPause NOT wired (background != exit).
Proven (Phase 3): per-category classification, nothing misclassified, external kill -> silent (never
clean_exit), backgrounding emits no false exit, #148/reconnect-vs-exit intact. 382/382 suite green.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
130 lines
9.6 KiB
JavaScript
130 lines
9.6 KiB
JavaScript
// Exit-signal PHASE 3 — proof. (A) socket/#148/liveness safety; (B) per-category classification with
|
|
// NOTHING misclassified. The JS-client handlers are proven by EXECUTING THE REAL SOURCE blocks (sliced
|
|
// out of index.html / app.js) against shimmed window/navigator/socket and firing synthetic death events.
|
|
const path = require('node:path'); const os = require('node:os'); const fs = require('node:fs'); const crypto = require('node:crypto');
|
|
const { test, before, after } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { spawn } = require('node:child_process');
|
|
const ioClient = require('../node_modules/socket.io-client');
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
|
|
// ---- harness: run a real client exit-block against shims, capturing what it sends ----
|
|
function harness(file, startLine, endLine) {
|
|
const src = fs.readFileSync(file, 'utf8').split('\n').slice(startLine - 1, endLine).join('\n');
|
|
const beacons = [], socketSends = [], handlers = {};
|
|
const windowShim = { addEventListener: (ev, fn) => { (handlers[ev] = handlers[ev] || []).push(fn); }, location: { origin: 'http://srv' } };
|
|
const navShim = { sendBeacon: (url, blob) => { beacons.push({ url, ...JSON.parse(blob.__body) }); return true; } };
|
|
class BlobShim { constructor(parts, o) { this.__body = parts[0]; this.type = o && o.type; } }
|
|
const socketShim = { connected: true, emit: (ev, payload) => { socketSends.push({ ev, ...payload }); } };
|
|
const config = { deviceId: 'D1', deviceToken: 'T1', serverUrl: 'http://srv' }; // /player reads config.*
|
|
const fn = new Function('window', 'navigator', 'Blob', 'fetch', 'config', 'deviceId', 'deviceToken', 'serverUrl', 'socket', 'JSON', src);
|
|
fn(windowShim, navShim, BlobShim, () => Promise.resolve(), config, 'D1', 'T1', 'http://srv', socketShim, JSON);
|
|
return { beacons, socketSends, fire: (ev, e) => (handlers[ev] || []).forEach(f => f(e)), handlers };
|
|
}
|
|
const WINDOW = 'window-target'; // sentinel for ev.target === window (real error event on window)
|
|
|
|
// ============ PART B — /player classification (real source, lines 349-385) ============
|
|
const PLAYER = path.join(__dirname, '../player/index.html');
|
|
test('B/player CRASH: uncaught error + unhandledrejection -> crashed (via sendBeacon, NOT the socket)', () => {
|
|
let h = harness(PLAYER, 349, 385); h.fire('error', { error: { message: 'boom' }, target: undefined });
|
|
assert.equal(h.beacons.length, 1); assert.equal(h.beacons[0].reason, 'crashed');
|
|
assert.equal(h.socketSends.length, 0, '/player uses the beacon channel only — no send over the dying socket');
|
|
h = harness(PLAYER, 349, 385); h.fire('unhandledrejection', { reason: { message: 'rej' } });
|
|
assert.equal(h.beacons[0].reason, 'crashed');
|
|
});
|
|
test('B/player NO-MISCLASSIFY: a RESOURCE load error (img/script) is NOT a crash', () => {
|
|
const h = harness(PLAYER, 349, 385); h.fire('error', { target: { src: 'https://x/img.png' } });
|
|
assert.equal(h.beacons.length, 0, 'resource error must not emit crashed');
|
|
});
|
|
test('B/player CLEAN-CLOSE: pagehide(persisted=false) -> clean_exit', () => {
|
|
const h = harness(PLAYER, 349, 385); h.fire('pagehide', { persisted: false });
|
|
assert.equal(h.beacons[0].reason, 'clean_exit');
|
|
});
|
|
test('B/player BACKGROUNDING: pagehide(persisted=true) bfcache suspend -> NO exit (not a death)', () => {
|
|
const h = harness(PLAYER, 349, 385); h.fire('pagehide', { persisted: true });
|
|
assert.equal(h.beacons.length, 0, 'a suspend must NOT emit clean_exit');
|
|
assert.equal((h.handlers['visibilitychange'] || []).length, 0, 'exit block wires NO visibilitychange -> hidden never emits exit');
|
|
});
|
|
test('B/player IDEMPOTENT: crash then pagehide -> only crashed (crash not relabelled clean_exit)', () => {
|
|
const h = harness(PLAYER, 349, 385); h.fire('error', { error: { message: 'boom' } }); h.fire('pagehide', { persisted: false });
|
|
assert.equal(h.beacons.length, 1); assert.equal(h.beacons[0].reason, 'crashed');
|
|
});
|
|
|
|
// ============ PART B — .wgt classification (real source, lines 663-697) ============
|
|
const TIZEN = path.join(__dirname, '../../tizen/js/app.js');
|
|
test('B/wgt CRASH: error/rejection -> crashed (socket AND beacon; server dedups)', () => {
|
|
const h = harness(TIZEN, 663, 697); h.fire('error', { error: { message: 'boom' } });
|
|
assert.equal(h.beacons[0].reason, 'crashed');
|
|
assert.equal(h.socketSends[0].reason, 'crashed'); assert.equal(h.socketSends[0].ev, 'device:exit');
|
|
});
|
|
test('B/wgt NO-MISCLASSIFY: resource error is not a crash', () => {
|
|
const h = harness(TIZEN, 663, 697); h.fire('error', { target: { src: 'x.png' } });
|
|
assert.equal(h.beacons.length, 0); assert.equal(h.socketSends.length, 0);
|
|
});
|
|
test('B/wgt CLEAN-CLOSE: pagehide(false) -> clean_exit; BACKGROUNDING pagehide(true) -> NO exit', () => {
|
|
let h = harness(TIZEN, 663, 697); h.fire('pagehide', { persisted: false });
|
|
assert.equal(h.beacons[0].reason, 'clean_exit');
|
|
h = harness(TIZEN, 663, 697); h.fire('pagehide', { persisted: true });
|
|
assert.equal(h.beacons.length, 0, 'suspend must NOT emit clean_exit');
|
|
assert.equal((h.handlers['visibilitychange'] || []).length, 0, 'no visibilitychange in the exit block');
|
|
});
|
|
|
|
// ============ PART A — server-side socket / #148 / reconnect-vs-exit safety ============
|
|
const PORT = 3975; const BASE = `http://127.0.0.1:${PORT}`;
|
|
const DATA_DIR = path.join(os.tmpdir(), 'st-exit3-' + crypto.randomBytes(4).toString('hex'));
|
|
let proc, JWT;
|
|
before(async () => {
|
|
const logFd = fs.openSync(path.join(os.tmpdir(), 'st-exit3.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' }, stdio: ['ignore', logFd, logFd] });
|
|
let up = false; for (let i = 0; i < 80; i++) { try { if ((await fetch(BASE + '/api/status')).ok) { up = true; break; } } catch {} await sleep(250); }
|
|
if (!up) throw new Error('boot fail');
|
|
JWT = (await (await fetch(BASE + '/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: 'op@t.local', password: 'test12345', name: 'Op' }) })).json()).token;
|
|
});
|
|
after(() => { try { proc.kill('SIGKILL'); } catch {} });
|
|
const connect = () => ioClient(`${BASE}/device`, { transports: ['websocket'], reconnection: false, forceNew: true });
|
|
const reg = (s, m) => new Promise((res, rej) => { s.once('device:registered', d => res(d)); s.emit('device:register', m); setTimeout(() => rej(new Error('to')), 5000); });
|
|
const pair = (c) => fetch(BASE + '/api/provision/pair', { method: 'POST', headers: { Authorization: 'Bearer ' + JWT, 'Content-Type': 'application/json' }, body: JSON.stringify({ pairing_code: c, name: 't' }) });
|
|
const row = async (id) => (await (await fetch(`${BASE}/api/devices/${id}`, { headers: { Authorization: 'Bearer ' + JWT } })).json());
|
|
const ackWithin = (s, hb, ms = 1500) => new Promise(r => { let d = false; const f = v => { if (!d) { d = true; r(v); } }; s.once('device:heartbeat-ack', () => f(true)); s.emit('device:heartbeat', hb); setTimeout(() => f(false), ms); });
|
|
|
|
test('A: crash-emit then teardown -> device goes Offline normally (no orphan/half-open) + reason kept', async () => {
|
|
const s = connect(); await new Promise(r => s.on('connect', r));
|
|
const d = await reg(s, { pairing_code: '820001', fingerprint: 'f1', device_info: {}, client_type: 'apk', contract_version: 'v4' });
|
|
await pair('820001'); await sleep(150);
|
|
s.emit('device:exit', { reason: 'crashed', detail: 'boom' }); // emit AS the socket is about to die
|
|
s.close(); // teardown immediately after
|
|
await sleep(5800);
|
|
const r = await row(d.device_id);
|
|
assert.equal(r.status, 'offline', 'normal offline transition still fired (teardown not disturbed)');
|
|
assert.equal(r.offline_reason, 'crashed');
|
|
});
|
|
|
|
test('A: RECONNECT is NOT an exit, and an exit does not block reconnect — #148 one socket, reason cleared', async () => {
|
|
const s1 = connect(); await new Promise(r => s1.on('connect', r));
|
|
const d = await reg(s1, { pairing_code: '820002', fingerprint: 'f2', device_info: {}, client_type: 'apk', contract_version: 'v4' });
|
|
await pair('820002'); await sleep(150);
|
|
s1.emit('device:exit', { reason: 'crashed' }); await sleep(150);
|
|
assert.equal((await row(d.device_id)).offline_reason, 'crashed');
|
|
s1.close(); await sleep(300);
|
|
// genuine reconnect (new socket) — must NOT emit an exit, and must clear the stale reason
|
|
const s2 = connect(); await new Promise(r => s2.on('connect', r));
|
|
await reg(s2, { device_id: d.device_id, device_token: d.device_token, fingerprint: 'f2', device_info: {}, client_type: 'apk', contract_version: 'v4' });
|
|
await sleep(200);
|
|
assert.equal((await row(d.device_id)).offline_reason, null, 'reconnect cleared the reason (reconnect != exit)');
|
|
assert.equal(await ackWithin(s2, { device_id: d.device_id, telemetry: {} }), true, 'the single reconnected socket is healthy (#148 intact)');
|
|
assert.equal((await row(d.device_id)).status, 'online');
|
|
s2.close();
|
|
});
|
|
|
|
test('A: VIOLENT kill (abrupt drop, NO device:exit) -> silent, never crashed/clean_exit (Bold-critical)', async () => {
|
|
const s = connect(); await new Promise(r => s.on('connect', r));
|
|
const d = await reg(s, { pairing_code: '820003', fingerprint: 'f3', device_info: {}, client_type: 'apk', contract_version: 'v4' });
|
|
await pair('820003'); await sleep(150);
|
|
s.io.engine.close(); // hard transport drop — no clean disconnect, no exit signal (force-stop/power/MDM)
|
|
await sleep(5800);
|
|
const r = await row(d.device_id);
|
|
assert.equal(r.status, 'offline');
|
|
assert.equal(r.offline_reason, 'silent', 'external/violent death reads as silent');
|
|
assert.notEqual(r.offline_reason, 'clean_exit'); assert.notEqual(r.offline_reason, 'crashed');
|
|
});
|