Files
corescope-rx/test/uplinksched.test.mjs
efitenandClaude Opus 5.5 70a59c6197 feat: uplink ping, test whether the mesh hears the companion
CoreDrive only measured what the companion hears. With uplinkPing on (off by
default), it sends a short message on a dedicated hashtag channel
(#coredrive) carrying only a random tag, listens 10 s for a repeater
re-broadcasting it, and publishes the result on the new topic
meshcore/client/{pk}/uplink (UPLINK_PING): tag, packet hash of the echo,
every echo with its first and last hop, and the GPS fix. The position never
goes over the mesh.

- src/grptxt.js: hashtag channel key, channel hash, GRP_TXT MAC check and
  AES-128-ECB decrypt, and the packet hash, per the firmware
  (Mesh.cpp:540-557, Utils.cpp:127-145, Packet.cpp:41-50). Checked against a
  real #wardriving packet from CoreScope live: same text and same hash
  (12c82f82df4cc29c). AES-ECB comes from @noble/ciphers 2.0.1, as WebCrypto
  has no ECB.
- src/uplinkping.js: channel message, get/set channel and flood-scope frames
  (companion_radio/MyMesh.cpp), slot choice that never overwrites another
  channel, echo matching on the tag.
- src/uplinksched.js: a ping only with a fix of 100 m or better, after
  uplinkIntervalSec (60) and uplinkMinMoveM (250), under uplinkMaxPerHour (50),
  and not in a ~180 m cell pinged in the last uplinkSkipDays (7).
- uplinkScope (e.g. "be") sends the ping as a region-scoped flood via
  CMD_SET_FLOOD_SCOPE (fw v8+) and resets the override right after.
- app.js holds the radio (acquireRadio) for the whole exchange, as OK/ERR
  replies carry no correlator; it skips while parked, signing or asking.
- DEVICE_INFO now yields the channel slot count; log header gets a ping row.

Tests: 417 pass (+20): crypto against the real packet, frames, slot choice,
echo matching, scheduler, config defaults, payload shape, header row. Not
tested on air: no ping has been sent from a real companion yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 15:52:02 +02:00

46 lines
2.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { pingDecision, pruneCells, skipCell, MAX_FIX_ACC_M } from '../src/uplinksched.js';
const cfg = { uplinkIntervalSec: 60, uplinkMinMoveM: 250, uplinkMaxPerHour: 50, uplinkSkipDays: 7 };
const fix = { lat: 50.99, lon: 5.38, acc_m: 10 };
const base = { now: 10_000_000, fix, last: null, sentTimes: [], recentCells: {}, cfg };
const north = (m) => ({ ...fix, lat: fix.lat + m / 110540 });
test('first ping with a good fix is sent, and names its cell', () => {
const d = pingDecision(base);
assert.equal(d.send, true);
assert.equal(d.cell, skipCell(fix));
});
test('no fix or a poor fix holds the ping', () => {
assert.equal(pingDecision({ ...base, fix: null }).reason, 'no GPS fix');
assert.equal(pingDecision({ ...base, fix: { ...fix, acc_m: MAX_FIX_ACC_M + 1 } }).send, false);
});
test('interval and distance both have to be met', () => {
const last = { at: base.now - 30_000, lat: fix.lat, lon: fix.lon };
assert.equal(pingDecision({ ...base, fix: north(1000), last }).reason, 'interval');
const older = { ...last, at: base.now - 61_000 };
assert.equal(pingDecision({ ...base, fix: north(200), last: older }).send, false);
assert.equal(pingDecision({ ...base, fix: north(300), last: older }).send, true);
});
test('the hourly cap counts only the last hour', () => {
const sentTimes = Array.from({ length: 50 }, (_, i) => base.now - i * 1000);
assert.equal(pingDecision({ ...base, sentTimes }).reason, 'hourly cap 50');
const old = sentTimes.map((t) => t - 3_600_000);
assert.equal(pingDecision({ ...base, sentTimes: old }).send, true);
});
test('a cell pinged within the skip window is skipped; older ones are not', () => {
const cell = skipCell(fix);
assert.equal(pingDecision({ ...base, recentCells: { [cell]: base.now - 86_400_000 } }).reason, 'cell pinged recently');
assert.equal(pingDecision({ ...base, recentCells: { [cell]: base.now - 8 * 86_400_000 } }).send, true);
});
test('pruneCells keeps only cells inside the window', () => {
const now = 100 * 86_400_000;
assert.deepEqual(pruneCells({ a: now - 86_400_000, b: now - 8 * 86_400_000 }, now, 7), { a: now - 86_400_000 });
});