B3.1 + B3.2 + B3.3: ChildRouter fan-out + commandRegistry 'none' + UnitPolicy dual-shape

B3.1 ChildRouter per-listener fan-out (drop emit monkey-patch):
  Partial-filter subscriptions enumerate every concrete
  <type>.measured.<position> event name (cartesian product over
  the canonical POSITIONS list + 19 KNOWN_TYPES) and register a
  plain emitter.on() per combo. Multi-parent semantics are trivial:
  each ChildRouter's listeners are independent. Drop the wrap/unwrap
  bookkeeping in tearDown. ChildRouter.js 184→164 lines.

B3.2 commandRegistry 'none' + description:
  Add 'none' to payloadSchema.type — handler still fires; logs warn
  if msg.payload is non-empty (catches accidental passes). Add
  optional `description` field per descriptor; surfaced via .list()
  so wikiGen can render per-topic effect text.
  commandRegistry.js 157→164 lines. 23/23 tests pass.

B3.3 UnitPolicy dual-shape:
  policy.canonical/output/curve are now BOTH callable methods AND
  frozen property bags. policy.canonical('flow') === 'm3/s' and
  policy.canonical.flow === 'm3/s' both work. Property bags are
  frozen (assign/delete/redefine throw in strict). Drops the
  _unitView workaround in MGC + rotatingMachine specificClass.
  UnitPolicy.js 149→163 lines, 15/15 tests pass.

CONTRACTS.md §4 + §6 updated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
znetsixe
2026-05-11 17:13:15 +02:00
parent ff9aec8702
commit f11754635b
6 changed files with 255 additions and 88 deletions

View File

@@ -195,3 +195,74 @@ test('chainable API returns the router instance', () => {
.onPrediction('machine', { type: 'flow', position: 'downstream' }, () => {});
assert.equal(r, router);
});
test('multi-parent: two routers on the same child both receive every event and tear down independently', () => {
// Regression for the pre-2026-05-11 emit-patching stack: two parents
// subscribing partial-filter wildcards on the same child must compose
// without stacking wrappers, and either teardown order must work.
const routerA = new ChildRouter(makeDomain());
const routerB = new ChildRouter(makeDomain());
const a = []; const b = [];
routerA.onMeasurement('measurement', { type: 'pressure' },
(data) => a.push(data.value));
routerB.onMeasurement('measurement', { type: 'pressure' },
(data) => b.push(data.value));
const ch = makeChild();
routerA.dispatchRegister(ch, 'measurement');
routerB.dispatchRegister(ch, 'measurement');
emitMeasured(ch, 'pressure', 'upstream', 11);
emitMeasured(ch, 'pressure', 'downstream', 22);
assert.deepEqual(a.sort(), [11, 22]);
assert.deepEqual(b.sort(), [11, 22]);
// Tear down B first — A must continue to fire on subsequent events.
routerB.tearDown();
emitMeasured(ch, 'pressure', 'upstream', 33);
assert.deepEqual(a.sort(), [11, 22, 33]);
assert.deepEqual(b.sort(), [11, 22], 'B receives nothing after its teardown');
// Now tear down A in the reverse order; neither should fire.
routerA.tearDown();
emitMeasured(ch, 'pressure', 'upstream', 44);
assert.deepEqual(a.sort(), [11, 22, 33], 'A receives nothing after its teardown');
assert.deepEqual(b.sort(), [11, 22]);
});
test('position-only filter fans out across every known type for that position', () => {
const router = new ChildRouter(makeDomain());
const hits = [];
router.onMeasurement('measurement', { position: 'upstream' },
(data) => hits.push(data.value));
const ch = makeChild();
router.dispatchRegister(ch, 'measurement');
emitMeasured(ch, 'pressure', 'upstream', 1);
emitMeasured(ch, 'flow', 'upstream', 2);
emitMeasured(ch, 'temperature', 'upstream', 3);
emitMeasured(ch, 'pressure', 'downstream', 99); // wrong position
emitPredicted(ch, 'pressure', 'upstream', 99); // wrong variant
assert.deepEqual(hits.sort(), [1, 2, 3]);
});
test('empty filter ({}) fires for every type/position combination', () => {
const router = new ChildRouter(makeDomain());
const hits = [];
router.onMeasurement('measurement', {}, (data) => hits.push(data.value));
const ch = makeChild();
router.dispatchRegister(ch, 'measurement');
emitMeasured(ch, 'pressure', 'upstream', 1);
emitMeasured(ch, 'flow', 'downstream', 2);
emitMeasured(ch, 'level', 'atequipment', 3);
emitPredicted(ch, 'flow', 'upstream', 99); // wrong variant
assert.deepEqual(hits.sort(), [1, 2, 3]);
});

View File

@@ -34,6 +34,53 @@ test('declare returns a policy whose canonical/output match the input', () => {
assert.equal(policy.curve('control'), '%');
});
test('canonical/output/curve are also frozen property bags (dot access)', () => {
const policy = UnitPolicy.declare(baseSpec);
// Property-access form — equivalent to the method-call form above.
assert.equal(policy.canonical.flow, 'm3/s');
assert.equal(policy.canonical.pressure, 'Pa');
assert.equal(policy.output.flow, 'm3/h');
assert.equal(policy.output.temperature, 'C');
assert.equal(policy.curve.flow, 'm3/h');
assert.equal(policy.curve.control, '%');
// Method-call form keeps working alongside it.
assert.equal(policy.canonical('flow'), 'm3/s');
assert.equal(policy.output('power'), 'kW');
});
test('canonical/output/curve property bags are frozen — no assignment / delete / redefine', () => {
'use strict';
const policy = UnitPolicy.declare(baseSpec);
// Existing own-properties are non-writable.
assert.throws(() => { policy.canonical.flow = 'tampered'; }, TypeError);
// Existing own-properties are non-configurable: delete throws.
assert.throws(() => { delete policy.canonical.pressure; }, TypeError);
// Redefining an existing prop throws.
assert.throws(
() => Object.defineProperty(policy.canonical, 'flow', { value: 'tampered' }),
TypeError
);
// Object.isFrozen reports the accessor as frozen.
assert.equal(Object.isFrozen(policy.canonical), true);
assert.equal(Object.isFrozen(policy.output), true);
assert.equal(Object.isFrozen(policy.curve), true);
// Original values survive the failed attempts.
assert.equal(policy.canonical.flow, 'm3/s');
assert.equal(policy.canonical.pressure, 'Pa');
});
test('curve property bag is present (empty) even when no curve was declared', () => {
const policy = UnitPolicy.declare({
canonical: baseSpec.canonical,
output: baseSpec.output,
});
// Method form returns null for unknown types.
assert.equal(policy.curve('flow'), null);
// Property form is an empty frozen function — accessing missing keys is undefined.
assert.equal(policy.curve.flow, undefined);
assert.equal(Object.isFrozen(policy.curve), true);
});
test('declare throws when canonical or output is missing', () => {
assert.throws(() => UnitPolicy.declare({ output: {} }), /canonical/);
assert.throws(() => UnitPolicy.declare({ canonical: {} }), /output/);

View File

@@ -151,15 +151,62 @@ test('list() returns descriptors without handler functions', () => {
topic: 'set.mode',
aliases: ['setMode'],
payloadSchema: { type: 'string' },
description: null,
});
assert.deepEqual(list[1], {
topic: 'cmd.startup',
aliases: [],
payloadSchema: null,
description: null,
});
for (const d of list) assert.ok(!('handler' in d), 'handler must not be in descriptor');
});
test("payloadSchema type 'none' invokes handler with no payload and no warning", async () => {
const logger = makeLogger();
let invoked = 0;
const reg = createRegistry([{
topic: 'cmd.calibrate',
payloadSchema: { type: 'none' },
handler: () => { invoked += 1; },
}], { logger });
await reg.dispatch({ topic: 'cmd.calibrate' }, {}, {});
await reg.dispatch({ topic: 'cmd.calibrate', payload: undefined }, {}, {});
await reg.dispatch({ topic: 'cmd.calibrate', payload: null }, {}, {});
assert.equal(invoked, 3);
assert.equal(logger._calls.warn.length, 0);
});
test("payloadSchema type 'none' invokes handler with non-empty payload but logs warn", async () => {
const logger = makeLogger();
let invoked = 0;
const reg = createRegistry([{
topic: 'cmd.calibrate',
payloadSchema: { type: 'none' },
handler: () => { invoked += 1; },
}], { logger });
await reg.dispatch({ topic: 'cmd.calibrate', payload: 'ignored' }, {}, {});
await reg.dispatch({ topic: 'cmd.calibrate', payload: { a: 1 } }, {}, {});
await reg.dispatch({ topic: 'cmd.calibrate', payload: 0 }, {}, {});
assert.equal(invoked, 3);
const warns = logger._calls.warn.filter((m) => m.includes('payload ignored'));
assert.equal(warns.length, 3);
assert.ok(warns[0].includes('cmd.calibrate'));
assert.ok(warns[0].includes('trigger-only'));
});
test('list() includes description field when present', () => {
const reg = createRegistry([
{ topic: 'cmd.calibrate', payloadSchema: { type: 'none' }, description: 'Trigger calibration.', handler: () => {} },
{ topic: 'set.mode', payloadSchema: { type: 'string' }, handler: () => {} },
]);
const list = reg.list();
assert.equal(list[0].description, 'Trigger calibration.');
assert.equal(list[1].description, null);
});
test('deprecationStats reflects alias hit counts', async () => {
const logger = makeLogger();
const reg = createRegistry([{