forked from Blendman974/faceclaw
Correctness: - Pair-glasses flows no longer strand the app in the manual-disconnected state: the pre-scan disconnect is followed by resumeAutoReconnect(), and the config page's scan hand-off drops the live link too. - The mixed-arms serial warning is reachable now: pairs() groups by the canonical serial key, and a lone left + lone right with different serials get the mismatch warning on both rows (it previously required two different serials inside one serial-keyed group, which cannot happen). - A selected pair that degrades to one arm (prune while the page is open) is deselected, and Save never pads a missing arm from stored addresses. - DiscoveryAggregator.ingest no longer lets a bonded re-emit or split advertisement overwrite the note/name/flag learned from a full advertisement, and reclassifies split reports using remembered fields so a nameless half doesn't drop the arm. - estimateProximity ignores the advertised TX Power Level (radiated power, not measured-power-at-1m); the calibration table is the only reference. - Saving a ring without a glasses pair updates the stored identity's ring name/address instead of leaving the previous ring on record. - Ring detection requires the stock "EVEN R1" name prefix in both the TS classifier and the Java pre-filter; a bare "R1" substring matched unrelated devices, and ring rows are always selectable. - One-shot scans keep every sample (buildAddressSet merges them through DiscoveryAggregator), and flash-flow address resolution takes left/right from one serial-joined complete pair instead of latest-per-role. Lifecycle: - The pairing scan and its refresh timer pause on app suspend and resume with the app, and the model's app-event hooks are detached when the page is left for good. Cleanup: - One MAC normalizer (even-advertisement.normalizeMacAddress, re-exported by device-addresses), used for the ring "Yours" comparison too. - One serial-equality definition (serialKey/serialsMatch); the containment fallback in isPreviouslyPaired is gone. - Shared formatErrorMessage replaces five per-view-model copies; shared hex-util replaces the duplicate TS hex codecs; one Java bytesToHex in FaceclawFirmwareUtil replaces three hand-rolled encoders. - glassesRow/ringRow share a presentation-base helper; dead row address fields, the constant glassesSectionVisibility, the identical-branch status ternary, and zoneRank/ZONE_ORDER are removed. - .gitignore now actually covers .test-build/ as tests/README claims. UI: - Dark-mode styles for the pairing page: readable instructions, dark-gray cards with consistent light text; light mode keeps its current look. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016ZqzwmRQ3HQxasBz5rYyZV
113 lines
4.7 KiB
JavaScript
113 lines
4.7 KiB
JavaScript
// Ported from SybilSight's BLEProximityTests.
|
|
//
|
|
// These pin the parts the pairing screen depends on: that a stronger signal
|
|
// always sorts first, that the absolute numbers stay inside a plausible band,
|
|
// and that a missing signal degrades to "unknown" rather than to a fabricated
|
|
// distance.
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const {
|
|
estimateProximity,
|
|
GLASSES_CALIBRATION,
|
|
RING_CALIBRATION,
|
|
MINIMUM_METERS,
|
|
MAXIMUM_METERS,
|
|
zoneFromMeters,
|
|
zoneLabel,
|
|
formatDistance,
|
|
proximitySummary,
|
|
sortedByProximity,
|
|
} = require("../.test-build/app/g2/ble-proximity.js");
|
|
|
|
const near = (a, b, eps) => Math.abs(a - b) <= eps;
|
|
|
|
test("distance falls as signal weakens", () => {
|
|
const close = estimateProximity(-40, GLASSES_CALIBRATION);
|
|
const mid = estimateProximity(-70, GLASSES_CALIBRATION);
|
|
const farAway = estimateProximity(-95, GLASSES_CALIBRATION);
|
|
assert.ok(close.meters < mid.meters);
|
|
assert.ok(mid.meters < farAway.meters);
|
|
});
|
|
|
|
test("the reference power reads as one metre", () => {
|
|
const estimate = estimateProximity(GLASSES_CALIBRATION.txPowerAtOneMeter, GLASSES_CALIBRATION);
|
|
assert.ok(near(estimate.meters, 1.0, 0.001));
|
|
assert.equal(estimate.zone, "near");
|
|
});
|
|
|
|
test("estimates stay inside a plausible band", () => {
|
|
assert.ok(near(estimateProximity(20, GLASSES_CALIBRATION).meters, MINIMUM_METERS, 0.0001));
|
|
assert.ok(near(estimateProximity(-127, GLASSES_CALIBRATION).meters, MAXIMUM_METERS, 0.0001));
|
|
});
|
|
|
|
test("the calibration table is the only reference power", () => {
|
|
// An advertised BLE "TX Power Level" is radiated power at the antenna, not a
|
|
// measured power at 1 m; estimateProximity deliberately takes no TX power
|
|
// argument, so a 0 dBm advertisement cannot inflate the estimate. The API
|
|
// itself enforces it — this pins the arity so a txPower parameter cannot
|
|
// quietly return.
|
|
assert.equal(estimateProximity.length, 2);
|
|
});
|
|
|
|
test("no signal yields no estimate (127 is the unavailable sentinel)", () => {
|
|
assert.equal(estimateProximity(null, GLASSES_CALIBRATION), null);
|
|
assert.equal(estimateProximity(undefined, GLASSES_CALIBRATION), null);
|
|
assert.equal(estimateProximity(127, GLASSES_CALIBRATION), null);
|
|
});
|
|
|
|
test("zones and their boundaries", () => {
|
|
assert.equal(zoneFromMeters(0.2), "immediate");
|
|
assert.equal(zoneFromMeters(0.5), "near");
|
|
assert.equal(zoneFromMeters(2.9), "near");
|
|
assert.equal(zoneFromMeters(3), "far");
|
|
assert.equal(zoneFromMeters(9.9), "far");
|
|
assert.equal(zoneFromMeters(10), "distant");
|
|
assert.equal(zoneLabel("immediate"), "In your hand");
|
|
});
|
|
|
|
test("weak signals are marked as approximate", () => {
|
|
const strong = estimateProximity(-45, GLASSES_CALIBRATION);
|
|
const weak = estimateProximity(-95, GLASSES_CALIBRATION);
|
|
assert.ok(!formatDistance(strong).startsWith("~"));
|
|
assert.ok(formatDistance(weak).startsWith("~"));
|
|
assert.ok(weak.confidence < strong.confidence);
|
|
});
|
|
|
|
test("distance formatting is rounded hard", () => {
|
|
const centimetres = estimateProximity(-30, GLASSES_CALIBRATION);
|
|
assert.ok(formatDistance(centimetres).includes("cm"), formatDistance(centimetres));
|
|
assert.equal(formatDistance({ meters: 2.34, confidence: 1, zone: "near" }), "2.3 m");
|
|
assert.equal(formatDistance({ meters: 42.6, confidence: 1, zone: "distant" }), "43 m");
|
|
assert.equal(proximitySummary({ meters: 0.3, confidence: 1, zone: "immediate" }), "In your hand · 30 cm");
|
|
});
|
|
|
|
const device = (id, rssi) => ({ id, rssi });
|
|
const estimate = (d) => estimateProximity(d.rssi, GLASSES_CALIBRATION);
|
|
const byId = (d) => d.id;
|
|
|
|
test("glasses sort closest first", () => {
|
|
const sorted = sortedByProximity([device("1", -80), device("2", -40), device("3", -60)], estimate, byId);
|
|
assert.deepEqual(sorted.map(byId), ["2", "3", "1"]);
|
|
});
|
|
|
|
test("devices without a signal sort last and are not dropped", () => {
|
|
const sorted = sortedByProximity([device("silent", null), device("heard", -70)], estimate, byId);
|
|
assert.deepEqual(sorted.map(byId), ["heard", "silent"]);
|
|
});
|
|
|
|
test("equal signals keep a stable order", () => {
|
|
const forward = sortedByProximity([device("b", -60), device("a", -60)], estimate, byId);
|
|
const reversed = sortedByProximity([device("a", -60), device("b", -60)], estimate, byId);
|
|
assert.deepEqual(forward.map(byId), ["a", "b"]);
|
|
assert.deepEqual(reversed.map(byId), forward.map(byId));
|
|
});
|
|
|
|
test("ring and glasses are calibrated separately", () => {
|
|
// The ring's antenna reads weaker than the glasses at the same distance, so
|
|
// the same RSSI must not be reported as the same distance for both.
|
|
const ring = estimateProximity(-70, RING_CALIBRATION);
|
|
const glasses = estimateProximity(-70, GLASSES_CALIBRATION);
|
|
assert.ok(ring.meters < glasses.meters);
|
|
});
|