Add proximity-based pairing for Even devices

Stream BLE advertisements into a dedicated pairing flow with arm, ring, serial, and hardware variant detection.

Add proximity ranking, device artwork, pairing safeguards, and unit coverage for discovery and candidate grouping.
This commit is contained in:
Kalani Helekunihi committed 2026-08-22 21:50:17 -05:00
1 parent 35e6977c6c
commit aaaa7288ee
35 files changed
+3309 -140

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
# Tests
Faceclaw's pairing logic (`app/g2/glasses-hardware-identity.ts`,
`ble-proximity.ts`, `even-advertisement.ts`, `pairing-candidates.ts`,
`glasses-artwork.ts`) is written without NativeScript imports so it can run
under plain node. The tests here are ports of SybilSight's
`GlassesHardwareIdentityTests` / `BLEProximityTests` plus coverage for the
advertisement parsers and left↔right pair grouping.
Run with:
npm test
That compiles the pure modules with the project's TypeScript into
`.test-build/` (git-ignored) and runs `node --test tests/`. No extra
dependencies are needed — the tests are plain `.test.cjs` files that use
`node:test` and `node:assert`.
+112
View File
@@ -0,0 +1,112 @@
// 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, null, GLASSES_CALIBRATION);
const mid = estimateProximity(-70, null, GLASSES_CALIBRATION);
const farAway = estimateProximity(-95, null, 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, null, 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, null, GLASSES_CALIBRATION).meters, MINIMUM_METERS, 0.0001));
assert.ok(near(estimateProximity(-127, null, GLASSES_CALIBRATION).meters, MAXIMUM_METERS, 0.0001));
});
test("an advertised TX power overrides the calibration", () => {
const assumed = estimateProximity(-60, null, GLASSES_CALIBRATION);
const measured = estimateProximity(-60, -50, GLASSES_CALIBRATION);
// A device that says it transmits at -50 dBm at 1 m, heard at -60, is further
// away than one assumed to transmit at -62 — and it is trusted more.
assert.ok(measured.meters > assumed.meters);
assert.ok(measured.confidence > assumed.confidence);
});
test("no signal yields no estimate (127 is the unavailable sentinel)", () => {
assert.equal(estimateProximity(null, null, GLASSES_CALIBRATION), null);
assert.equal(estimateProximity(undefined, null, GLASSES_CALIBRATION), null);
assert.equal(estimateProximity(127, null, 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, -60, GLASSES_CALIBRATION);
const weak = estimateProximity(-95, null, 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, -60, 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, null, 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, null, RING_CALIBRATION);
const glasses = estimateProximity(-70, null, GLASSES_CALIBRATION);
assert.ok(ring.meters < glasses.meters);
});
+157
View File
@@ -0,0 +1,157 @@
// Advertisement parsers, checked against the layouts SybilSight confirmed by
// HCI capture (G2: "ER"+SN(14)+MAC(6,LE)+flag) and on-disk forensics (R1:
// "ER"+MAC(6,wire)+ASCII serial suffix on ring firmware 2.2.7.x).
const test = require("node:test");
const assert = require("node:assert/strict");
const {
hexToBytes,
bytesToHex,
hasEvenManufacturerSignature,
extractG2Serial,
extractG2Mac,
extractG2AdvertisementFlag,
g2SideFromName,
g2NameAddressTail,
wireBytesFromHumanReadableAddress,
humanReadableAddressFromWireBytes,
ringNameAddressOctets,
ringMacWireBytes,
extractRingSerialSuffix,
classifyAdvertisement,
normalizeAddress,
EVEN_COMPANY_IDENTIFIER,
} = require("../.test-build/app/g2/even-advertisement.js");
const ascii = (s) => bytesToHex(Array.from(s).map((c) => c.charCodeAt(0)));
// Real pair serial S211GBBC180304 at E0:12:14:B6:EC:E0 (HCI capture 2026-04-12).
const G2_MFG_HEX = "4552" + ascii("S211GBBC180304") + "E0ECB61412E0" + "01";
// Ring identity proven on disk: wire E2 6E B5 D2 7F DA ⇄ human DA:7F:D2:B5:6E:E2,
// name suffix B56EE2 = human last three octets, followed by ASCII serial "140137".
const RING_MFG_HEX = "4552" + "E26EB5D27FDA" + ascii("140137");
const raw = (overrides) => ({
address: "E0:12:14:B6:EC:E0",
name: "Even G2_32_L_B6ECE0",
manufacturerData: G2_MFG_HEX,
rssi: -55,
txPower: null,
connectable: true,
bonded: false,
source: "scan",
seenAtMs: 1000,
...overrides,
});
test("company identifier is the ASCII bytes ER read little-endian", () => {
assert.equal(EVEN_COMPANY_IDENTIFIER, 0x5245);
assert.ok(hasEvenManufacturerSignature(hexToBytes("4552")));
assert.ok(!hasEvenManufacturerSignature(hexToBytes("5245")));
});
test("G2 manufacturer data yields serial, MAC, and flag", () => {
const mfg = hexToBytes(G2_MFG_HEX);
assert.equal(mfg.length, 23);
assert.equal(extractG2Serial(mfg), "S211GBBC180304");
assert.equal(extractG2Mac(mfg), "E0:12:14:B6:EC:E0");
assert.equal(extractG2AdvertisementFlag(mfg), 1);
});
test("short or binary manufacturer data yields no serial", () => {
assert.equal(extractG2Serial(hexToBytes("4552")), null);
assert.equal(extractG2Serial(hexToBytes("4552" + "FF".repeat(14))), null);
assert.equal(extractG2Mac(hexToBytes("4552" + ascii("S211GBBC180304"))), null);
assert.equal(extractG2AdvertisementFlag(hexToBytes(G2_MFG_HEX.slice(0, -2))), null);
});
test("side comes from the local name; the hex tail is per-arm", () => {
assert.equal(g2SideFromName("Even G2_32_L_ACD458"), "left");
assert.equal(g2SideFromName("Even G2_32_R_8D6E3C"), "right");
assert.equal(g2SideFromName("Even G2_32"), null);
assert.equal(g2SideFromName("X_L_R_"), null);
assert.equal(g2NameAddressTail("Even G2_32_L_ACD458"), "ACD458");
assert.equal(g2NameAddressTail("Even G2"), null);
});
test("address codec crosses wire/human order exactly once", () => {
const wire = wireBytesFromHumanReadableAddress("DA:7F:D2:B5:6E:E2");
assert.equal(bytesToHex(wire), "E26EB5D27FDA");
assert.equal(humanReadableAddressFromWireBytes(wire), "DA:7F:D2:B5:6E:E2");
assert.equal(wireBytesFromHumanReadableAddress("nope"), null);
assert.equal(normalizeAddress("e0ecb61412e0"), "E0:EC:B6:14:12:E0");
});
test("ring name suffix octets parse from the full name and the bare id", () => {
assert.deepEqual(ringNameAddressOctets("EVEN R1_B56EE2"), [0xb5, 0x6e, 0xe2]);
assert.deepEqual(ringNameAddressOctets("B56EE2"), [0xb5, 0x6e, 0xe2]);
assert.equal(ringNameAddressOctets("EVEN R1_B56E"), null);
assert.equal(ringNameAddressOctets(null), null);
});
test("ring MAC extraction anchors on the marker and rejects the serial suffix", () => {
const mfg = hexToBytes(RING_MFG_HEX);
// The historical suffix(6) slice would have returned "140137" as ASCII bytes.
assert.equal(bytesToHex(mfg.subarray(mfg.length - 6)), "313430313337");
const wire = ringMacWireBytes(mfg, "EVEN R1_B56EE2");
assert.equal(bytesToHex(wire), "E26EB5D27FDA");
assert.equal(humanReadableAddressFromWireBytes(wire), "DA:7F:D2:B5:6E:E2");
assert.equal(extractRingSerialSuffix(mfg), "140137");
});
test("ring MAC extraction fails closed when the name contradicts every window", () => {
assert.equal(ringMacWireBytes(hexToBytes(RING_MFG_HEX), "EVEN R1_000000"), null);
// Without a verifiable name, the documented marker layout wins.
assert.equal(bytesToHex(ringMacWireBytes(hexToBytes(RING_MFG_HEX), null)), "E26EB5D27FDA");
});
test("classifies a G2 left arm with its serial and embedded MAC", () => {
const ad = classifyAdvertisement(raw({}));
assert.equal(ad.role, "left");
assert.equal(ad.serial, "S211GBBC180304");
assert.equal(ad.embeddedMac, "E0:12:14:B6:EC:E0");
assert.equal(ad.embeddedMacMismatch, false);
assert.equal(ad.flag, 1);
assert.equal(ad.note, null);
});
test("flags an embedded MAC that disagrees with the radio address", () => {
const ad = classifyAdvertisement(raw({ address: "AA:BB:CC:DD:EE:FF", name: "Even G2_32_R_DDEEFF" }));
assert.equal(ad.role, "right");
assert.equal(ad.embeddedMacMismatch, true);
});
test("renamed firmware is admitted on the ER signature alone", () => {
const ad = classifyAdvertisement(raw({ name: "Faceclaw_L_1" }));
assert.ok(ad);
assert.equal(ad.role, "left");
assert.equal(ad.serial, "S211GBBC180304");
});
test("a G2 name without manufacturer data is admitted but carries a note", () => {
const ad = classifyAdvertisement(raw({ manufacturerData: "" }));
assert.equal(ad.role, "left");
assert.equal(ad.serial, null);
assert.match(ad.note, /no manufacturer data/);
const short = classifyAdvertisement(raw({ manufacturerData: "4552AABB" }));
assert.match(short.note, /too short/);
});
test("a G2 advertisement with no side marker cannot be placed in a pair", () => {
assert.equal(classifyAdvertisement(raw({ name: "Even G2_32" })), null);
});
test("classifies the ring from its name and validated MAC", () => {
const ad = classifyAdvertisement(raw({ address: "DA:7F:D2:B5:6E:E2", name: "EVEN R1_B56EE2", manufacturerData: RING_MFG_HEX }));
assert.equal(ad.role, "ring");
assert.equal(ad.serial, null);
assert.equal(ad.embeddedMac, "DA:7F:D2:B5:6E:E2");
assert.equal(ad.embeddedMacMismatch, false);
assert.equal(ad.ringSerialSuffix, "140137");
});
test("ignores unrelated advertisers", () => {
assert.equal(classifyAdvertisement(raw({ name: "Pixel Buds", manufacturerData: "E000010203" })), null);
assert.equal(classifyAdvertisement(raw({ name: "", manufacturerData: "" })), null);
});
+132
View File
@@ -0,0 +1,132 @@
// Ported from SybilSight's GlassesHardwareIdentityTests.
//
// The reference mapping is the Even Realities app 2.2.0 (`_parseGlassesSku`,
// `evenSNName`, `matchModel`). `S211GBBC180304` and `S211GCBC300403` are
// serials observed on real hardware, so they are the anchors here rather than
// synthetic strings.
const test = require("node:test");
const assert = require("node:assert/strict");
const {
GlassesHardwareIdentity,
evaluatePairSerials,
pairSerialWarning,
BUNDLED_VARIANT_ASSET_NAMES,
} = require("../.test-build/app/g2/glasses-hardware-identity.js");
test("decodes an observed G2 serial", () => {
const identity = GlassesHardwareIdentity.decode("S211GBBC180304");
assert.ok(identity);
assert.equal(identity.family, "g2");
assert.equal(identity.frame, "b");
assert.equal(identity.colorway, "brown");
assert.equal(identity.modelCode, "S211");
assert.equal(identity.shortSerial, "0304");
assert.equal(identity.productName, "Even G2 B");
assert.equal(identity.variantSummary, "Frame B · Brown");
assert.equal(identity.displayName, "Even G2 B · Brown");
});
test("colour byte at index five selects the finish", () => {
assert.equal(GlassesHardwareIdentity.decode("S211GAAA000001").colorway, "grey");
assert.equal(GlassesHardwareIdentity.decode("S211GBBC180304").colorway, "brown");
assert.equal(GlassesHardwareIdentity.decode("S211GCBC300403").colorway, "green");
});
test("frame prefix table", () => {
assert.equal(GlassesHardwareIdentity.decode("S201GAAA000001").frame, "a");
assert.equal(GlassesHardwareIdentity.decode("S281GAAA000001").frame, "a");
assert.equal(GlassesHardwareIdentity.decode("S211GAAA000001").frame, "b");
assert.equal(GlassesHardwareIdentity.decode("S291GAAA000001").frame, "b");
assert.equal(GlassesHardwareIdentity.decode("S221GAAA000001").frame, "c");
});
test("unknown G2 prefix keeps the family but claims no shape", () => {
// The Even app defaults an unknown prefix to frame A. Identification cannot
// afford that: a future SKU must read as "Even G2" with no shape claim.
const identity = GlassesHardwareIdentity.decode("S231GBAA000001");
assert.equal(identity.family, "g2");
assert.equal(identity.frame, null);
assert.equal(identity.colorway, "brown");
assert.equal(identity.productName, "Even G2");
assert.equal(identity.variantSummary, "Brown");
});
test("unknown colour byte claims no finish", () => {
const identity = GlassesHardwareIdentity.decode("S211GZAA000001");
assert.equal(identity.frame, "b");
assert.equal(identity.colorway, null);
assert.equal(identity.displayName, "Even G2 B");
assert.equal(identity.variantSummary, "Frame B");
});
test("bench-unit colour byte D (seen in HCI capture) decodes to no finish", () => {
const identity = GlassesHardwareIdentity.decode("S200LDBE210001");
assert.equal(identity.frame, "a");
assert.equal(identity.colorway, null);
});
test("G1 and ring families", () => {
const g1 = GlassesHardwareIdentity.decode("S110GAAA000001");
assert.equal(g1.family, "g1");
assert.equal(g1.frame, "b");
assert.equal(g1.productName, "Even G1 B");
assert.equal(GlassesHardwareIdentity.decode("S100GAAA000001").frame, "a");
// The ring has neither a frame shape nor a colourway in the Even app's model.
const ring = GlassesHardwareIdentity.decode("B210GAAA000001");
assert.equal(ring.family, "r1");
assert.equal(ring.frame, null);
assert.equal(ring.colorway, null);
assert.equal(ring.variantSummary, null);
assert.equal(ring.displayName, "Even R1");
});
test("arm suffix and case are normalized away", () => {
const suffixed = GlassesHardwareIdentity.decode("S211GBBC180304_L_1");
const lowercased = GlassesHardwareIdentity.decode(" s211gbbc180304 ");
assert.equal(suffixed.serial, "S211GBBC180304");
assert.ok(suffixed.equals(lowercased));
});
test("rejects non-Even and truncated serials", () => {
assert.equal(GlassesHardwareIdentity.decode("EVEN_G2_ALPHA"), null);
assert.equal(GlassesHardwareIdentity.decode("Even G2_32_L_ACD458"), null);
assert.equal(GlassesHardwareIdentity.decode("S211G"), null);
assert.equal(GlassesHardwareIdentity.decode(""), null);
assert.equal(GlassesHardwareIdentity.decode(null), null);
assert.equal(GlassesHardwareIdentity.decode(undefined), null);
});
test("known SKUs resolve to their own artwork; frame C and the ring fall back", () => {
assert.equal(GlassesHardwareIdentity.decode("S211GBBC180304").imageAssetName, "glasses_g2_b_brown");
assert.equal(GlassesHardwareIdentity.decode("S201GAAA000001").imageAssetName, "glasses_g2_a_grey");
assert.equal(GlassesHardwareIdentity.decode("S221GAAA000001").imageAssetName, null);
assert.equal(GlassesHardwareIdentity.decode("B210GAAA000001").imageAssetName, null);
assert.equal(BUNDLED_VARIANT_ASSET_NAMES.size, 6);
});
test("pair serial check verdicts", () => {
assert.deepEqual(evaluatePairSerials("S211GBBC180304", "S211GBBC180304"), { kind: "matched", serial: "S211GBBC180304" });
// The arm suffix and casing differ but the hardware does not.
assert.deepEqual(evaluatePairSerials("S211GBBC180304_L_1", "s211gbbc180304"), { kind: "matched", serial: "S211GBBC180304" });
assert.deepEqual(evaluatePairSerials("S211GBBC180304", "S211GCBC300403"), {
kind: "mismatched",
left: "S211GBBC180304",
right: "S211GCBC300403",
});
assert.deepEqual(evaluatePairSerials("S211GBBC180304", null), { kind: "unknown" });
assert.deepEqual(evaluatePairSerials(null, null), { kind: "unknown" });
assert.deepEqual(evaluatePairSerials(" ", "S211GBBC180304"), { kind: "unknown" });
});
test("pair serial warning names both serials and only fires on mismatch", () => {
assert.equal(pairSerialWarning({ kind: "unknown" }), null);
assert.equal(pairSerialWarning({ kind: "matched", serial: "S211GBBC180304" }), null);
const warning = pairSerialWarning({ kind: "mismatched", left: "S211GBBC180304", right: "S201GCBC300403" });
assert.ok(warning.includes("S211GBBC180304"));
assert.ok(warning.includes("S201GCBC300403"));
// Different frame AND finish — say so, because the wearer can see that difference.
assert.ok(warning.includes("Even G2 B · Brown"));
assert.ok(warning.includes("Even G2 A · Green"));
});
+250
View File
@@ -0,0 +1,250 @@
// Left↔right pair grouping on the advertised serial, closest-first ordering,
// the "Closest"/"Yours" disambiguators (ported from SybilSight's
// PairingScanPresentation tests), and the row presentation.
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const { bytesToHex } = require("../.test-build/app/g2/even-advertisement.js");
const {
DiscoveryAggregator,
nearestCandidateId,
isPreviouslyPaired,
glassesRow,
ringRow,
DEFAULT_STALE_AFTER_MS,
} = require("../.test-build/app/g2/pairing-candidates.js");
const { requiredArtworkFileNames, glassesImagePath, GENERIC_G2_IMAGE_PATH } = require("../.test-build/app/g2/glasses-artwork.js");
const { GlassesHardwareIdentity } = require("../.test-build/app/g2/glasses-hardware-identity.js");
const ascii = (s) => bytesToHex(Array.from(s).map((c) => c.charCodeAt(0)));
const wireOf = (human) => bytesToHex(human.split(":").map((h) => parseInt(h, 16)).reverse());
function arm(side, serial, address, rssi, extra = {}) {
const tail = address.replace(/:/g, "").slice(-6);
return {
address,
name: `Even G2_32_${side === "left" ? "L" : "R"}_${tail}`,
manufacturerData: "4552" + ascii(serial) + wireOf(address) + "01",
rssi,
txPower: null,
connectable: true,
bonded: false,
source: "scan",
seenAtMs: 1000,
...extra,
};
}
function ring(address, rssi, extra = {}) {
const tail = address.replace(/:/g, "").slice(-6);
return {
address,
name: `EVEN R1_${tail}`,
manufacturerData: "4552" + wireOf(address) + ascii("140137"),
rssi,
txPower: null,
connectable: true,
bonded: false,
source: "scan",
seenAtMs: 1000,
...extra,
};
}
// One real pair: the left and right arms carry DIFFERENT name tails (per-arm
// address) but the SAME serial. That serial is the pair identity.
const LEFT = arm("left", "S211GBBC180304", "E0:12:14:AC:D4:58", -50);
const RIGHT = arm("right", "S211GBBC180304", "E0:12:14:8D:6E:3C", -54);
const OTHER_RIGHT = arm("right", "S211GCBC300403", "E0:12:14:14:12:E0", -80);
test("left and right arms advertising the same serial group into one complete pair", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(LEFT);
aggregator.ingest(RIGHT);
const pairs = aggregator.pairs();
assert.equal(pairs.length, 1);
const pair = pairs[0];
assert.equal(pair.id, "S211GBBC180304");
assert.equal(pair.completeness, "complete");
assert.equal(pair.left.address, "E0:12:14:AC:D4:58");
assert.equal(pair.right.address, "E0:12:14:8D:6E:3C");
assert.equal(pair.identity.displayName, "Even G2 B · Brown");
// Strongest arm's signal represents the pair.
assert.equal(pair.rssi, -50);
assert.ok(pair.proximity);
});
test("arms with different serials are different pairs, each incomplete", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(LEFT);
aggregator.ingest(OTHER_RIGHT);
const pairs = aggregator.pairs();
assert.equal(pairs.length, 2);
assert.deepEqual(
pairs.map((p) => [p.id, p.completeness]),
[
["S211GBBC180304", "left-only"],
["S211GCBC300403", "right-only"],
],
);
});
test("pairs sort closest first; a pair without a signal sorts last", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(arm("left", "S211GAAA000001", "00:00:00:00:00:01", -80));
aggregator.ingest(arm("left", "S211GAAA000002", "00:00:00:00:00:02", -40));
aggregator.ingest(arm("left", "S211GAAA000003", "00:00:00:00:00:03", null));
assert.deepEqual(
aggregator.pairs().map((p) => p.id),
["S211GAAA000002", "S211GAAA000001", "S211GAAA000003"],
);
});
test("an arm without a serial stands alone keyed by its address", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest({ ...LEFT, manufacturerData: "" });
const pairs = aggregator.pairs();
assert.equal(pairs.length, 1);
assert.equal(pairs[0].id, "arm:E0:12:14:AC:D4:58");
assert.equal(pairs[0].serial, null);
const row = glassesRow(pairs[0]);
assert.equal(row.canSelect, false);
assert.match(row.armsSummary, /serial unknown/);
});
test("a bonded listing never erases a scanned serial and gains one when heard", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest({ ...LEFT, manufacturerData: "", rssi: null, bonded: true, source: "paired" });
assert.equal(aggregator.pairs()[0].serial, null);
aggregator.ingest({ ...LEFT, seenAtMs: 2000 });
assert.equal(aggregator.pairs()[0].serial, "S211GBBC180304");
assert.equal(aggregator.pairs()[0].left.bonded, true);
aggregator.ingest({ ...LEFT, manufacturerData: "", rssi: null, bonded: true, source: "paired", seenAtMs: 3000 });
assert.equal(aggregator.pairs()[0].serial, "S211GBBC180304");
});
test("stale advertisements are pruned; bonded entries survive", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(LEFT);
aggregator.ingest({ ...RIGHT, bonded: true });
aggregator.prune(1000 + DEFAULT_STALE_AFTER_MS + 1);
const pairs = aggregator.pairs();
assert.equal(pairs.length, 1);
assert.equal(pairs[0].completeness, "right-only");
});
test("RSSI is smoothed across repeated advertisements", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest({ ...LEFT, rssi: -40 });
aggregator.ingest({ ...LEFT, rssi: -80, seenAtMs: 1100 });
const rssi = aggregator.pairs()[0].rssi;
assert.ok(rssi < -40 && rssi > -80, `expected a blend, got ${rssi}`);
});
test("rings are grouped separately and sorted closest first", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(LEFT);
aggregator.ingest(ring("DA:7F:D2:B5:6E:E2", -70));
aggregator.ingest(ring("DA:7F:D2:00:00:01", -45));
assert.equal(aggregator.pairs().length, 1);
assert.deepEqual(
aggregator.rings().map((r) => r.id),
["DA:7F:D2:00:00:01", "DA:7F:D2:B5:6E:E2"],
);
const row = ringRow(aggregator.rings()[1]);
assert.equal(row.modelTitle, "Even R1");
assert.match(row.deviceName, /140137/);
assert.equal(row.ringAddress, "DA:7F:D2:B5:6E:E2");
});
test("nearest requires a clear signal lead", () => {
const d = (id, rssi) => ({ id, rssi });
assert.equal(nearestCandidateId([d("1", -40), d("2", -70)]), "1");
assert.equal(nearestCandidateId([d("1", -60), d("2", -57)]), null);
// A lone advertiser is trivially the nearest.
assert.equal(nearestCandidateId([d("1", -80)]), "1");
// Nothing to rank without RSSI.
assert.equal(nearestCandidateId([d("1", null)]), null);
assert.equal(nearestCandidateId([]), null);
});
test("previously paired matches across the arm suffix", () => {
assert.ok(isPreviouslyPaired("S211GBBC180304", "S211GBBC180304_L_1"));
assert.ok(!isPreviouslyPaired("S211GBBC180304", "S211GCBC300403"));
assert.ok(!isPreviouslyPaired("S211GBBC180304", ""));
assert.ok(!isPreviouslyPaired("S211GBBC180304", null));
assert.ok(!isPreviouslyPaired(null, "S211GBBC180304"));
});
test("scan row carries the decoded variant, artwork, and proximity", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest({ ...LEFT, rssi: -42 });
aggregator.ingest({ ...RIGHT, rssi: -42 });
const row = glassesRow(aggregator.pairs()[0]);
assert.equal(row.modelTitle, "Even G2 B");
assert.equal(row.deviceName, "S211GBBC180304");
assert.equal(row.variantSummary, "Frame B · Brown");
assert.equal(row.colorway, "brown");
assert.equal(row.imagePath, "~/images/glasses/glasses_g2_b_brown.png");
assert.equal(row.hasVariantImage, true);
assert.match(row.proximitySummary, /In your hand|Nearby/);
assert.match(row.armsSummary, /serials match/);
assert.equal(row.canSelect, true);
assert.equal(row.isNearest, false);
assert.equal(row.badge, "");
});
test("the Yours badge wins over Closest; Closest needs the nearest id", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(LEFT);
aggregator.ingest(RIGHT);
const pair = aggregator.pairs()[0];
assert.equal(glassesRow(pair, { nearestId: pair.id }).badge, "Closest");
assert.equal(glassesRow(pair, { nearestId: pair.id, pairedSerial: "S211GBBC180304_L_1" }).badge, "Yours");
});
test("a pair whose serial does not decode falls back to the generic shot and swatch-less row", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(arm("left", "XX11GBBC180304", "00:00:00:00:00:01", -50));
aggregator.ingest(arm("right", "XX11GBBC180304", "00:00:00:00:00:02", -50));
const row = glassesRow(aggregator.pairs()[0]);
assert.equal(row.modelTitle, "Even Realities G2");
assert.equal(row.variantSummary, null);
assert.equal(row.swatchHex, null);
assert.equal(row.imagePath, GENERIC_G2_IMAGE_PATH);
assert.equal(row.hasVariantImage, false);
assert.equal(row.canSelect, true);
});
test("frame C resolves to the generic shot with the swatch still available", () => {
const identity = GlassesHardwareIdentity.decode("S221GCAA000001");
assert.equal(glassesImagePath(identity), GENERIC_G2_IMAGE_PATH);
assert.equal(identity.colorway, "green");
});
test("an incomplete pair cannot be selected and says which arm is missing", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(RIGHT);
const row = glassesRow(aggregator.pairs()[0]);
assert.equal(row.canSelect, false);
assert.match(row.armsSummary, /Only the right arm/);
assert.equal(row.rightAddress, "E0:12:14:8D:6E:3C");
assert.equal(row.leftAddress, "");
});
test("an embedded-MAC mismatch is surfaced as a warning", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest({ ...LEFT, address: "AA:BB:CC:DD:EE:FF" });
aggregator.ingest(RIGHT);
const row = glassesRow(aggregator.pairs()[0]);
assert.match(row.warning, /Left arm's advertised address/);
});
test("every artwork file the resolver claims is bundled exists on disk", () => {
const dir = path.join(__dirname, "..", "app", "images", "glasses");
for (const name of requiredArtworkFileNames()) {
assert.ok(fs.existsSync(path.join(dir, name)), `missing app/images/glasses/${name}`);
}
});
+20
View File
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"lib": ["ES2020"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"types": [],
"rootDir": "..",
"outDir": "../.test-build"
},
"include": [
"../app/g2/glasses-hardware-identity.ts",
"../app/g2/ble-proximity.ts",
"../app/g2/even-advertisement.ts",
"../app/g2/glasses-artwork.ts",
"../app/g2/pairing-candidates.ts"
]
}