feat(dashboardapi): diff-skip regen via flows:started predicate (#36)
Subscribes to Node-RED's flows:started runtime event, caches the {diff}
payload on the dashboardAPI source, and short-circuits the child.register
handler when none of {dashboardAPI id, child id, grandchild ids} appears in
diff.added/changed/removed/rewired. Predicate verified by S1 spike (#32).
- src/nodeClass.js: _attachLifecycleHook subscribes, _attachCloseHandler
cleans up. No-op when RED.events isn't available (unit-test friendly).
- src/specificClass.js: subtreeChanged() predicate + subtreeIdsFor() helper.
- src/commands/handlers.js: registerChild consults predicate before composing;
logs {event:'regen-skipped', outcome:'no-diff'} when skipping.
- test/basic/slice36-diff-predicate.basic.test.js: 8 cases — null/empty diff,
affected/unaffected ids, tab-id over-triggering avoidance, grandchild
inclusion, no-grandchild case.
Cold start (no cached diff yet) always regenerates — safe default. Edge case
documented in #32: when a brand-new child is wired to a registered parent,
the new child's id is in diff.added but not yet in registeredChildren when
flows:started fires. Mitigation (b) per spike findings: one-deploy race
accepted for R&D — next deploy picks up the new registration.
Closes #36
This commit is contained in:
73
test/basic/slice36-diff-predicate.basic.test.js
Normal file
73
test/basic/slice36-diff-predicate.basic.test.js
Normal file
@@ -0,0 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const DashboardApi = require('../../src/specificClass.js');
|
||||
|
||||
test('subtreeChanged: null diff → always regen (safe default for cold start)', () => {
|
||||
const api = new DashboardApi({});
|
||||
assert.equal(api.subtreeChanged(null, new Set(['a', 'b'])), true);
|
||||
assert.equal(api.subtreeChanged(undefined, new Set(['a', 'b'])), true);
|
||||
});
|
||||
|
||||
test('subtreeChanged: empty diff arrays → no regen needed', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: [], changed: [], removed: [], rewired: [], linked: [], flowChanged: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), false);
|
||||
});
|
||||
|
||||
test('subtreeChanged: id in added → regen', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: ['x', 'b'], changed: [], removed: [], rewired: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), true);
|
||||
});
|
||||
|
||||
test('subtreeChanged: id in changed → regen', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: [], changed: ['a'], removed: [], rewired: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), true);
|
||||
});
|
||||
|
||||
test('subtreeChanged: only unrelated ids → no regen', () => {
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: ['z'], changed: ['y'], removed: ['x'], rewired: ['w'] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['a', 'b'])), false);
|
||||
});
|
||||
|
||||
test('subtreeChanged: tab id in diff but not in subtree → no regen', () => {
|
||||
// Tab id over-triggering avoidance: when an unrelated tab changes, its
|
||||
// tab id lands in changed/added but should not affect this dashboardAPI.
|
||||
const api = new DashboardApi({});
|
||||
const diff = { added: [], changed: ['unrelated_tab'], removed: [], rewired: [] };
|
||||
assert.equal(api.subtreeChanged(diff, new Set(['dashboardApiId', 'childA'])), false);
|
||||
});
|
||||
|
||||
test('subtreeIdsFor: includes dashboardAPI id + child id + grandchild ids', () => {
|
||||
const api = new DashboardApi({});
|
||||
const grandchild = {
|
||||
config: { general: { id: 'gc-1' }, functionality: { softwareType: 'measurement' } },
|
||||
};
|
||||
const grandchildEntry = { child: grandchild, position: 'downstream', softwareType: 'measurement' };
|
||||
const child = {
|
||||
config: { general: { id: 'child-1' }, functionality: { softwareType: 'pumpingStation' } },
|
||||
childRegistrationUtils: {
|
||||
registeredChildren: new Map([['gc-1', grandchildEntry]]),
|
||||
},
|
||||
};
|
||||
const ids = api.subtreeIdsFor('dApi-1', child);
|
||||
assert.equal(ids.has('dApi-1'), true);
|
||||
assert.equal(ids.has('child-1'), true);
|
||||
assert.equal(ids.has('gc-1'), true);
|
||||
assert.equal(ids.size, 3);
|
||||
});
|
||||
|
||||
test('subtreeIdsFor: handles child with no grandchildren', () => {
|
||||
const api = new DashboardApi({});
|
||||
const child = {
|
||||
config: { general: { id: 'child-1' }, functionality: { softwareType: 'measurement' } },
|
||||
};
|
||||
const ids = api.subtreeIdsFor('dApi-1', child);
|
||||
assert.equal(ids.size, 2);
|
||||
assert.ok(ids.has('dApi-1') && ids.has('child-1'));
|
||||
});
|
||||
Reference in New Issue
Block a user