Files
meshcore-analyzer/test-path-inspector.js
T
Kpa-clawbot 54f7f9d35b feat: path-prefix candidate inspector with map view (#944) (#945)
## feat: path-prefix candidate inspector with map view (#944)

Implements the locked spec from #944: a beam-search-based path prefix
inspector that enumerates candidate full-pubkey paths from short hex
prefixes and scores them.

### Server (`cmd/server/path_inspect.go`)

- **`POST /api/paths/inspect`** — accepts 1-64 hex prefixes (1-3 bytes,
uniform length per request)
- Beam search (width 20) over cached `prefixMap` + `NeighborGraph`
- Per-hop scoring: edge weight (35%), GPS plausibility (20%), recency
(15%), prefix selectivity (30%)
- Geometric mean aggregation with 0.05 floor per hop
- Speculative threshold: score < 0.7
- Score cache: 30s TTL, keyed by (prefixes, observer, window)
- Cold-start: synchronous NeighborGraph rebuild with 2s hard timeout →
503 `{retry:true}`
- Body limit: 4096 bytes via `http.MaxBytesReader`
- Zero SQL queries in handler hot path
- Request validation: rejects empty, odd-length, >3 bytes, mixed
lengths, >64 hops

### Frontend (`public/path-inspector.js`)

- New page under Tools route with input field (comma/space separated hex
prefixes)
- Client-side validation with error feedback
- Results table: rank, score (color-coded speculative), path names,
per-hop evidence (collapsed)
- "Show on Map" button calls `drawPacketRoute` (one path at a time,
clears prior)
- Deep link: `#/tools/path-inspector?prefixes=2c,a1,f4`

### Nav reorganization

- `Traces` nav item renamed to `Tools`
- Backward-compat: `#/traces/<hash>` redirects to `#/tools/trace/<hash>`
- Tools sub-routing dispatches to traces or path-inspector

### Store changes

- Added `LastSeen time.Time` to `nodeInfo` struct, populated from
`nodes.last_seen`
- Added `inspectMu` + `inspectCache` fields to `PacketStore`

### Tests

- **Go unit tests** (`path_inspect_test.go`): scoreHop components, beam
width cap, speculative flag, all validation error cases, valid request
integration
- **Frontend tests** (`test-path-inspector.js`): parse
comma/space/mixed, validation (empty, odd, >3 bytes, mixed lengths,
invalid hex, valid)
- Anti-tautology gate verified: removing beam pruning fails width test;
removing validation fails reject tests

### CSS

- `--path-inspector-speculative` variable in both themes (amber, WCAG AA
on both dark/light backgrounds)
- All colors via CSS variables (no hardcoded hex in production code)

Closes #944

---------

Co-authored-by: you <you@example.com>
2026-04-30 23:28:16 -07:00

107 lines
3.9 KiB
JavaScript

// test-path-inspector.js — vm.createContext sandbox tests for path-inspector.js
'use strict';
const vm = require('vm');
const fs = require('fs');
const assert = require('assert');
const src = fs.readFileSync(__dirname + '/public/path-inspector.js', 'utf8');
function createSandbox() {
const sandbox = {
window: {},
document: {
getElementById: () => ({ textContent: '', innerHTML: '', addEventListener: () => {}, querySelectorAll: () => [] }),
querySelectorAll: () => []
},
location: { hash: '#/tools/path-inspector' },
history: { replaceState: () => {} },
fetch: () => Promise.resolve({ ok: true, json: () => Promise.resolve({ candidates: [] }) }),
URLSearchParams: URLSearchParams,
registerPage: function () {},
escapeHtml: s => s,
console: console
};
sandbox.self = sandbox;
sandbox.globalThis = sandbox;
const ctx = vm.createContext(sandbox);
vm.runInContext(src, ctx);
return sandbox;
}
// Test: parsePrefixes accepts comma-separated.
(function testParseComma() {
const sb = createSandbox();
const result = sb.window.PathInspector.parsePrefixes('2C,A1,F4');
assert.strictEqual(JSON.stringify(result), JSON.stringify(['2c', 'a1', 'f4']));
console.log('✓ parsePrefixes comma-separated');
})();
// Test: parsePrefixes accepts space-separated.
(function testParseSpace() {
const sb = createSandbox();
const result = sb.window.PathInspector.parsePrefixes('2C A1 F4');
assert.strictEqual(JSON.stringify(result), JSON.stringify(['2c', 'a1', 'f4']));
console.log('✓ parsePrefixes space-separated');
})();
// Test: parsePrefixes accepts mixed.
(function testParseMixed() {
const sb = createSandbox();
const result = sb.window.PathInspector.parsePrefixes(' 2C, A1 F4 ');
assert.strictEqual(JSON.stringify(result), JSON.stringify(['2c', 'a1', 'f4']));
console.log('✓ parsePrefixes mixed separators');
})();
// Test: validatePrefixes rejects empty.
(function testValidateEmpty() {
const sb = createSandbox();
const err = sb.window.PathInspector.validatePrefixes([]);
assert.ok(err !== null, 'should reject empty');
console.log('✓ validatePrefixes rejects empty');
})();
// Test: validatePrefixes rejects odd-length.
(function testValidateOdd() {
const sb = createSandbox();
const err = sb.window.PathInspector.validatePrefixes(['abc']);
assert.ok(err !== null && err.includes('Odd'), 'should reject odd-length');
console.log('✓ validatePrefixes rejects odd-length');
})();
// Test: validatePrefixes rejects >3 bytes.
(function testValidateTooLong() {
const sb = createSandbox();
const err = sb.window.PathInspector.validatePrefixes(['aabbccdd']);
assert.ok(err !== null && err.includes('too long'), 'should reject >3 bytes');
console.log('✓ validatePrefixes rejects >3 bytes');
})();
// Test: validatePrefixes rejects mixed lengths.
(function testValidateMixed() {
const sb = createSandbox();
const err = sb.window.PathInspector.validatePrefixes(['aa', 'bbcc']);
assert.ok(err !== null && err.includes('Mixed'), 'should reject mixed');
console.log('✓ validatePrefixes rejects mixed lengths');
})();
// Test: validatePrefixes accepts valid input.
(function testValidateValid() {
const sb = createSandbox();
const err = sb.window.PathInspector.validatePrefixes(['2c', 'a1', 'f4']);
assert.strictEqual(err, null);
console.log('✓ validatePrefixes accepts valid');
})();
// Test: validatePrefixes rejects invalid hex.
(function testValidateInvalidHex() {
const sb = createSandbox();
const err = sb.window.PathInspector.validatePrefixes(['zz']);
assert.ok(err !== null && err.includes('Invalid hex'), 'should reject invalid hex');
console.log('✓ validatePrefixes rejects invalid hex');
})();
// Anti-tautology: if validation were removed (always return null), the odd-length test would fail.
// Mental revert: validatePrefixes = () => null; → testValidateOdd would fail because err would be null.
console.log('\nAll path-inspector tests passed!');