Files
meshcore-analyzer/tests/unit/test-path-inspector.js
T
Alex B 893773338e chore(tests): move root test-*.js into tests/unit and tests/e2e (#2036)
Moves 290 root test-*.js into tests/unit (177, listed in test-all.sh) and tests/e2e (113, classified in scripts/non-unit-tests.json), per #1981 and PR-D of #1385. Root goes from 348 entries to 48. test-all.sh and test-fixtures/ stay put. The inventory guard now fails if a test reappears in the root or sits in the wrong folder.

Verified independently of the diff: the invoked sets are unchanged (test-all.sh 177 before and after, deploy.yml 96 before and after, both identical as sets), and a full local run of test-all.sh on master and on the branch produced 4702 output lines each whose only differences are absolute paths, stack-trace line numbers shifted by the REPO_ROOT line, the inventory wording and two perf ratios. The guard was mutation-checked: a test back in the root, a unit suite in tests/e2e, and a suite dropped from test-all.sh each make it exit 1. CI run 35246304316 ran 97 suites from tests/e2e and is green.

Follow-up 9335c51d finished the instruction files: no bare root test command is left in AGENTS.md, the squad charters, .github or docs, and every tests/ path they name resolves.

Merged by the interim maintainer without a second human reviewer: CI and the local runs above are the independent checks.

Known and deliberately out of scope: 18 of the 113 files in tests/e2e are invoked by no runner at all, and one of them cannot run anywhere because it requires jsdom, which is not a declared dependency. Tracked separately.
2026-09-17 19:06:07 +02:00

108 lines
4.0 KiB
JavaScript

// test-path-inspector.js — vm.createContext sandbox tests for path-inspector.js
'use strict';
const REPO_ROOT = require('path').resolve(__dirname, '..', '..');
const vm = require('vm');
const fs = require('fs');
const assert = require('assert');
const src = fs.readFileSync(REPO_ROOT + '/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!');