const { test } = require('node:test'); const assert = require('node:assert'); const WebSocket = require('ws'); const RemoteServer = require('../lib/remote-server'); const TOKEN = 'a'.repeat(64); function startAgent(onDiagnosticRequest, extra) { const srv = new RemoteServer(); return srv.start({ port: 0, host: '127.0.0.1', token: TOKEN, diagnosticMode: true, onDiagnosticRequest, ...(extra || {}) }) .then(() => srv); } function connect(port) { return new WebSocket(`ws://127.0.0.1:${port}`); } function once(ws, type) { return new Promise((resolve, reject) => { ws.on('message', (raw) => { const m = JSON.parse(raw); if (m.type === type) resolve(m); }); ws.on('close', (code) => reject(new Error('closed ' + code))); ws.on('error', reject); }); } test('diagnostic client: auth -> diag-request -> reqId-correlated diag-response', async () => { const agent = await startAgent((msg, _client, reply) => { assert.equal(msg.op, 'server_health'); reply({ ok: true, data: { hello: 'world', echo: msg.args } }); }); const port = agent.getPort(); const ws = connect(port); await new Promise((r) => ws.on('open', r)); ws.send(JSON.stringify({ type: 'auth', token: TOKEN, role: 'diagnostic' })); const ok = await once(ws, 'auth-ok'); assert.ok(ok.clientId); ws.send(JSON.stringify({ type: 'diag-request', reqId: 'r1', op: 'server_health', args: { errorLimit: 3 } })); const resp = await once(ws, 'diag-response'); assert.equal(resp.reqId, 'r1'); assert.equal(resp.ok, true); assert.equal(resp.data.hello, 'world'); assert.equal(resp.data.echo.errorLimit, 3); assert.equal(agent.getLastAccess() !== null, true, 'access timestamp recorded'); ws.close(); agent.stop(); }); test('a diagnostic client NEVER triggers the screen-capture window', async () => { let captureCreated = false; const agent = await startAgent(() => {}, { onCreateCaptureWindow: () => { captureCreated = true; } }); const ws = connect(agent.getPort()); await new Promise((r) => ws.on('open', r)); ws.send(JSON.stringify({ type: 'auth', token: TOKEN, role: 'diagnostic' })); await once(ws, 'auth-ok'); await new Promise((r) => setTimeout(r, 50)); assert.equal(captureCreated, false, 'diagnosticMode must not spawn the capture window'); ws.close(); agent.stop(); }); test('wrong token is rejected and the ip is locked out after 5 attempts', async () => { const agent = await startAgent(() => {}); const port = agent.getPort(); for (let i = 0; i < 5; i++) { const ws = connect(port); await new Promise((r) => ws.on('open', r)); ws.send(JSON.stringify({ type: 'auth', token: 'wrong', role: 'diagnostic' })); await new Promise((r) => ws.on('close', r)); } const ws = connect(port); const closeCode = await new Promise((resolve) => ws.on('close', (c) => resolve(c))); assert.equal(closeCode, 4003, 'locked out after 5 failed attempts'); agent.stop(); });