Files
simple/frontend/tests/bip.test.js
T
catarrh 18a36a8f27 feat: generic BIP terminal control (Simulator BIP pill) (v3.6.0)
BIP was only reachable through the SCP81 listener; cards that use TCP for
other purposes (an applet's OPEN CHANNEL, a push trigger without HTTP OTA)
now get a generic control surface, independent of SCP81.

Server:
- /api/bip/control starts a plain BIP session in three modes: sink
  (default; a local TcpDumpServer that accepts the card's channels and only
  logs conn/sink-rx/conn-close, never answering; port 0 = ephemeral, the
  bound port is reported), passthru (dial the OPEN CHANNEL destination, TCP
  client only) and redirect (fixed host:port).
- /api/bip/status returns {owner, bip, listener}; /api/bip/log and
  /api/bip/log-clear share the BIP event log with /api/scp81/log.
- The session state is shared with the SCP81 listener and only one session
  runs at a time: _bip_session_stop() stops whichever control started it
  and the control responses report it as `replaced` (both directions,
  SCP81 <-> BIP).  State renamed _SCP81_MODE/_TARGET/_LISTENER/_LINK_EVENTS
  -> _BIP_* plus _BIP_OWNER.
- TcpDumpServer logs conn-close and counts accepted connections; stop()
  joins the accept thread so the port is really free on restart.
- The new control endpoints are blocked during test-script runs.

PWA:
- New Simulator pill BIP (after TR Config): mode select with notes,
  Host/Port rules, Start/Stop with a "replaced" notice, status line (mode,
  bound address, owner, channels), a Channels box and the shared BIP log
  (reuses the SCP81 log renderer, now showing `peer`).
- The SCP81 status line marks a session owned by the BIP pill.

Help EN/RU 8.10, docs/api.md, AGENTS; CAT_TP/UDP recorded as not
implemented.

Tests: tests/test_bip.py (9) and frontend/tests/bip.test.js (6).
600 frontend / 482 Python green; version 3.6.0; sw simple-v273.
2026-09-27 15:28:23 +03:00

146 lines
6.3 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');
const html = fs.readFileSync(path.join(__dirname, '..', 'index.html'), 'utf8');
function extractFunc(src, name) {
const re = new RegExp('function\\s+' + name + '\\s*\\([^)]*\\)\\s*\\{');
const m = re.exec(src);
if (!m) throw new Error('function ' + name + ' not found');
let i = m.index + m[0].length - 1;
let depth = 0;
for (; i < src.length; i++) {
if (src[i] === '{') depth++;
else if (src[i] === '}') {
depth--;
if (depth === 0) break;
}
}
return src.slice(m.index, i + 1);
}
globalThis.esc = s => s;
globalThis.t = s => s;
for (const fn of ['bipReplacedNotice', 'bipChannelLine', 'bipStatusLine', 'bipStartBody', 'bipModeChanged']) {
eval(extractFunc(html, fn));
}
function fakeDom(mode) {
const els = {};
for (const id of ['bip-mode', 'bip-host', 'bip-port', 'bip-sink-note',
'bip-passthru-note', 'bip-redirect-note']) {
const el = {
disabled: false, placeholder: '', toggled: {},
classList: { toggle(c, on) { el.toggled[c] = on; } },
};
els[id] = el;
}
els['bip-mode'].value = mode;
globalThis.document = { getElementById: id => els[id] || null };
return els;
}
test('bipStartBody builds the control body per mode', () => {
// passthru: no host/port at all - the card chooses the destination
assert.deepStrictEqual(bipStartBody('passthru', '127.0.0.1', '9'),
{ body: { action: 'start', mode: 'passthru' } });
// sink: host optional, empty port = server-side ephemeral
assert.deepStrictEqual(bipStartBody('sink', '127.0.0.1', ''),
{ body: { action: 'start', mode: 'sink', host: '127.0.0.1' } });
assert.deepStrictEqual(bipStartBody('sink', '', '9101'),
{ body: { action: 'start', mode: 'sink', port: 9101 } });
// fixed target: host and a real port required
assert.deepStrictEqual(bipStartBody('redirect', '10.0.0.5', '8080'),
{ body: { action: 'start', mode: 'redirect', host: '10.0.0.5', port: 8080 } });
assert.strictEqual(bipStartBody('redirect', '', '8080').error, 'Fixed target requires the host');
assert.strictEqual(bipStartBody('redirect', '10.0.0.5', '').error, 'Port must be 1-65535');
assert.strictEqual(bipStartBody('redirect', '10.0.0.5', '0').error, 'Port must be 1-65535');
assert.strictEqual(bipStartBody('redirect', '10.0.0.5', 'x').error, 'Port must be 1-65535');
assert.strictEqual(bipStartBody('sink', '127.0.0.1', 'x').error, 'Port must be 0-65535');
assert.strictEqual(bipStartBody('sink', '127.0.0.1', '70000').error, 'Port must be 0-65535');
});
test('bipReplacedNotice names what the start replaced', () => {
assert.strictEqual(bipReplacedNotice(null), '');
assert.strictEqual(bipReplacedNotice({}), '');
assert.strictEqual(bipReplacedNotice({ owner: 'scp81', mode: 'tls' }),
'Stopped the SCP81 listener (tls)');
assert.strictEqual(bipReplacedNotice({ owner: 'bip', mode: 'sink' }),
'Stopped the BIP session (sink)');
});
test('bipChannelLine summarizes one channel', () => {
assert.strictEqual(bipChannelLine({ id: 1, requested: '10.0.0.5:80',
target: '10.0.0.5:80', bytes_in: 412, bytes_out: 0, pending: 3,
peer_closed: false }),
'ch1 10.0.0.5:80 -> 10.0.0.5:80 in:412 out:0 buffer:3 open');
assert.strictEqual(bipChannelLine({ id: 2, peer_closed: true }),
'ch2 ? -> ? in:0 out:0 buffer:0 peer closed');
});
test('bipStatusLine shows the mode, bound address, connections, owner and channels', () => {
assert.strictEqual(bipStatusLine({ owner: 'bip', bip: { channels: [] },
listener: { mode: 'sink', host: '127.0.0.1', port: 53121, connections: 1 } }),
'sink 127.0.0.1:53121 | 1 connections \u00b7 owner: bip');
assert.strictEqual(bipStatusLine({ owner: 'bip', bip: { channels: [] },
listener: { mode: 'passthru' } }), 'passthru \u00b7 owner: bip');
assert.strictEqual(bipStatusLine(null), '');
const withChannel = bipStatusLine({
owner: 'bip',
bip: { channels: [
{ id: 1, requested: 'a:1', target: 'b:2', bytes_in: 5, bytes_out: 7, pending: 0 }] },
listener: { mode: 'sink', host: 'h', port: 1 },
});
assert.ok(withChannel.includes('ch1 a:1 -> b:2 in:5 out:7 buffer:0 open'), withChannel);
});
test('bipModeChanged toggles the fields and the mode notes', () => {
let els = fakeDom('sink');
bipModeChanged();
assert.strictEqual(els['bip-host'].disabled, false);
assert.strictEqual(els['bip-port'].disabled, false);
assert.strictEqual(els['bip-port'].placeholder, 'ephemeral');
assert.strictEqual(els['bip-sink-note'].toggled.hidden, false);
assert.strictEqual(els['bip-passthru-note'].toggled.hidden, true);
assert.strictEqual(els['bip-redirect-note'].toggled.hidden, true);
els = fakeDom('passthru');
bipModeChanged();
assert.strictEqual(els['bip-host'].disabled, true);
assert.strictEqual(els['bip-port'].disabled, true);
assert.strictEqual(els['bip-host'].toggled['opacity-40'], true);
assert.strictEqual(els['bip-passthru-note'].toggled.hidden, false);
assert.strictEqual(els['bip-sink-note'].toggled.hidden, true);
els = fakeDom('redirect');
bipModeChanged();
assert.strictEqual(els['bip-host'].disabled, false);
assert.strictEqual(els['bip-port'].placeholder, '8443');
assert.strictEqual(els['bip-redirect-note'].toggled.hidden, false);
});
test('the BIP pill, view and API endpoints are wired', () => {
assert.match(html, /data-phone-sub="bip"[^>]*data-l10n="BIP"/);
assert.match(html, /id="phone-sub-bip"/);
assert.match(html, /\['phone', 'tr', 'bip', 'esim', 'test'\]/);
assert.match(html, /else if \(name === 'bip'\) \{\s*bipEnter\(\);/);
assert.match(html, /bip: 'bip'/);
// the poll stops when the Simulator tab is left
assert.match(html, /if \(_bipTimer && name !== 'phone'\)/);
for (const id of ['bip-mode', 'bip-host', 'bip-port', 'bip-start-btn',
'bip-stop-btn', 'bip-state', 'bip-channels', 'bip-log']) {
assert.ok(html.includes('id="' + id + '"'), id);
}
assert.match(html, /\/api\/bip\/control/);
assert.match(html, /\/api\/bip\/status/);
assert.match(html, /\/api\/bip\/log-clear/);
assert.match(html, /\/api\/bip\/log'/);
// the shared one-session rule and the owner marker are both shown
assert.match(html, /One BIP session at a time: starting here replaces a running SCP81 listener/);
assert.match(html, /if \(st\.owner === 'bip'\) s = t\('BIP session:'\)/);
assert.match(html, /if \(e\.peer\) parts\.push\(e\.peer\);/);
assert.match(html, /if \(isViewVisible\('phone-sub-bip'\)\) \{ bipStatusRefresh\(\); bipLogRefresh\(\); \}/);
});