Files
corescope-rx/test/transport.test.mjs
efitenandClaude Opus 5 cc710a3a11 fix: stop transmitting into a BLE link that is down
The 2026-09-13 log ends with two and a half minutes of "discover send failed: GATT
Server is disconnected" — one sweep every 30 seconds, each a write into a characteristic
that no longer exists — an "rf sample incomplete — discarded" for every sampler round,
and region asks failing the same way. None of it could reach a radio.

discoverDecision takes linkUp and reports 'link-down' ahead of every other state; the
monitor tick returns right after rendering it, the RF sampler reschedules without
sampling, and region asks stop with them. The discover clock is not advanced while down,
so the first tick after the link returns sweeps immediately instead of waiting out an
interval.

The link state is read from the browser's own device object through a new
transport.connected(), not mirrored into a flag of ours. A flag would be stale exactly
when it matters, which is the same failure shape as the duplicated default fixed in
v1.17.1 — one source per fact.

Said once in each direction rather than per tick. A log that just goes quiet is
indistinguishable from a crash, an area with nothing to hear, or a feature switched off.

Capture, queueing and uploading are untouched: receptions already in hand are still
queued and published, and the MQTT link keeps its own reconnect.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HSoyBhrLLtSqLgBMEKMiJV
2026-09-13 13:42:56 +02:00

70 lines
2.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { WebBluetoothTransport } from '../src/transport.js';
// A writeChar stub that records overlap: Web Bluetooth rejects a second write
// started before the first settles, so the bug this guards is two sends issued
// in the same tick (the discover sweep and region discovery share one).
function fakeChar() {
const c = { inFlight: 0, maxInFlight: 0, order: [], resolvers: [] };
c.writeValue = (bytes) => {
c.inFlight++;
c.maxInFlight = Math.max(c.maxInFlight, c.inFlight);
c.order.push(bytes[0]);
return new Promise((res) => c.resolvers.push(() => { c.inFlight--; res(); }));
};
return c;
}
test('two sends issued in the same tick never overlap on the GATT characteristic', async () => {
const t = new WebBluetoothTransport();
const c = fakeChar();
t.writeChar = c;
const a = t.send(new Uint8Array([1]));
const b = t.send(new Uint8Array([2]));
// Writes are chained, so the first starts a microtask later rather than
// synchronously; drain the queue before inspecting what actually began.
await Promise.resolve();
await Promise.resolve();
assert.equal(c.maxInFlight, 1, 'the second write must not start before the first settles');
assert.deepEqual(c.order, [1], 'only the first write has been issued');
c.resolvers.shift()();
await a;
await Promise.resolve();
assert.deepEqual(c.order, [1, 2], 'the second write starts once the first has settled');
c.resolvers.shift()();
await b;
assert.equal(c.maxInFlight, 1);
});
test('a failed send rejects for its own caller without poisoning later sends', async () => {
const t = new WebBluetoothTransport();
let calls = 0;
t.writeChar = { writeValue: () => { calls++; return calls === 1 ? Promise.reject(new Error('GATT busy')) : Promise.resolve(); } };
await assert.rejects(t.send(new Uint8Array([1])), /GATT busy/);
await t.send(new Uint8Array([2])); // must still go through
assert.equal(calls, 2);
});
test('send rejects when not connected', async () => {
const t = new WebBluetoothTransport();
await assert.rejects(t.send(new Uint8Array([1])), /not connected/);
});
// connected() is read straight off the browser's device object rather than mirrored
// into a flag: the link drops without warning, and a stale copy is what sends writes
// into a dead characteristic.
test('connected() reports the live GATT state, and false before there is a device', () => {
const t = new WebBluetoothTransport();
assert.equal(t.connected(), false, 'no device yet');
t.device = { gatt: { connected: true } };
assert.equal(t.connected(), true);
t.device.gatt.connected = false;
assert.equal(t.connected(), false);
t.device = { };
assert.equal(t.connected(), false, 'a device with no gatt is not connected');
});