Files
measurement/test/basic/calibration-and-stability.basic.test.js
znetsixe 495b4cf400 feat: digital (MQTT) mode + fix silent dispatcher bug for camelCase methods
Runtime:
- Fix silent no-op when user selected any camelCase smoothing or outlier
  method from the editor. validateEnum in generalFunctions lowercases enum
  values (zScore -> zscore, lowPass -> lowpass, ...) but the dispatcher
  compared against camelCase keys. Effect: 5 of 11 smoothing methods
  (lowPass, highPass, weightedMovingAverage, bandPass, savitzkyGolay) and
  2 of 3 outlier methods (zScore, modifiedZScore) silently fell through.
  Users got the raw last value or no outlier filtering with no error log.
  Review any pre-2026-04-13 flows that relied on these methods.
  Fix: normalize method names to lowercase on both sides of the lookup.

- New Channel class (src/channel.js) — self-contained per-channel pipeline:
  outlier -> offset -> scaling -> smoothing -> min/max -> constrain -> emit.
  Pure domain logic, no Node-RED deps, reusable by future nodes that need
  the same signal-conditioning chain.

Digital mode:
- config.mode.current = 'digital' opts in. config.channels declares one
  entry per expected JSON key; each channel has its own type, position,
  unit, distance, and optional scaling/smoothing/outlierDetection blocks
  that override the top-level analog-mode fields. One MQTT-shaped payload
  ({t:22.5, h:45, p:1013}) dispatches N independent pipelines and emits N
  MeasurementContainer slots from a single input message.
- Backward compatible: absent mode config = analog = pre-digital behaviour.
  Every existing measurement flow keeps working unchanged.

UI:
- HTML editor: new Mode dropdown and Channels JSON textarea. The Node-RED
  help panel is rewritten end-to-end with topic reference, port contracts,
  per-mode configuration, smoothing/outlier method tables, and a note
  about the pre-fix behaviour.
- README.md rewritten (was a one-line stub).

Tests (12 -> 71, all green):
- test/basic/smoothing-methods.basic.test.js (+16): every smoothing method
  including the formerly-broken camelCase ones.
- test/basic/outlier-detection.basic.test.js (+10): every outlier method,
  fall-through, toggle.
- test/basic/scaling-and-interpolation.basic.test.js (+10): offset,
  interpolateLinear, constrain, handleScaling edge cases, min/max
  tracking, updateOutputPercent fallback, updateOutputAbs emit dedup.
- test/basic/calibration-and-stability.basic.test.js (+11): calibrate
  (stable and unstable), isStable, evaluateRepeatability refusals,
  toggleSimulation, tick simulation on/off.
- test/integration/digital-mode.integration.test.js (+12): channel build
  (including malformed entries), payload dispatch, multi-channel emit,
  unknown keys, per-channel scaling/smoothing/outlier, empty channels,
  non-numeric value rejection, getDigitalOutput shape, analog-default
  back-compat.

E2E verified on Dockerized Node-RED: analog regression unchanged; digital
mode deploys with three channels, dispatches MQTT-style payload, emits
per-channel events, accumulates per-channel smoothing, ignores unknown
keys.

Depends on generalFunctions commit e50be2e (permissive unit check +
mode/channels schema).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 13:43:03 +02:00

122 lines
4.4 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { makeMeasurementInstance } = require('../helpers/factories');
/**
* Tests for the calibration / stability / repeatability primitives. These
* methods interact with the stored window from the smoothing pipeline, so
* each test seeds storedValues explicitly.
*/
test("isStable returns false with fewer than 2 samples", () => {
const m = makeMeasurementInstance();
m.storedValues = [];
assert.equal(m.isStable(), false); // current implementation returns false (not object) at <2 samples
});
test("isStable reports stability and stdDev for a flat window", () => {
const m = makeMeasurementInstance();
m.storedValues = [10, 10, 10, 10, 10];
const { isStable, stdDev } = m.isStable();
assert.equal(isStable, true);
assert.equal(stdDev, 0);
});
test("evaluateRepeatability returns stdDev when conditions are met", () => {
const m = makeMeasurementInstance({
smoothing: { smoothWindow: 5, smoothMethod: 'mean' },
});
m.storedValues = [10, 10, 10, 10, 10];
const rep = m.evaluateRepeatability();
assert.equal(rep, 0);
});
test("evaluateRepeatability refuses when smoothing is disabled", () => {
const m = makeMeasurementInstance({
smoothing: { smoothWindow: 5, smoothMethod: 'none' },
});
m.storedValues = [10, 10, 10, 10, 10];
assert.equal(m.evaluateRepeatability(), null);
});
test("evaluateRepeatability refuses with insufficient samples", () => {
const m = makeMeasurementInstance({
smoothing: { smoothWindow: 5, smoothMethod: 'mean' },
});
m.storedValues = [10];
assert.equal(m.evaluateRepeatability(), null);
});
test("calibrate sets offset when input is stable and scaling enabled", () => {
const m = makeMeasurementInstance({
scaling: { enabled: true, inputMin: 4, inputMax: 20, absMin: 0, absMax: 100, offset: 0 },
smoothing: { smoothWindow: 5, smoothMethod: 'mean' },
});
// Stable window fed through calculateInput so outputAbs reflects the
// pipeline (important because calibrate uses outputAbs for its delta).
[3, 3, 3, 3, 3].forEach((v) => m.calculateInput(v));
const outputBefore = m.outputAbs;
m.calibrate();
// Offset should now be inputMin - outputAbs(before).
assert.equal(m.config.scaling.offset, 4 - outputBefore);
});
test("calibrate aborts when input is not stable", () => {
const m = makeMeasurementInstance({
scaling: { enabled: true, inputMin: 0, inputMax: 100, absMin: 0, absMax: 10, offset: 0 },
smoothing: { smoothWindow: 5, smoothMethod: 'mean' },
});
// Cheat: populate storedValues with clearly non-stable data. calibrate
// calls isStable() -> stdDev > threshold -> warn + no offset change.
m.storedValues = [0, 100, 0, 100, 0];
const offsetBefore = m.config.scaling.offset;
m.calibrate();
assert.equal(m.config.scaling.offset, offsetBefore);
});
test("calibrate uses absMin when scaling is disabled", () => {
const m = makeMeasurementInstance({
scaling: { enabled: false, inputMin: 0, inputMax: 1, absMin: 5, absMax: 10, offset: 0 },
smoothing: { smoothWindow: 5, smoothMethod: 'mean' },
});
[5, 5, 5, 5, 5].forEach((v) => m.calculateInput(v));
const out = m.outputAbs;
m.calibrate();
assert.equal(m.config.scaling.offset, 5 - out);
});
test("toggleSimulation flips the simulation flag", () => {
const m = makeMeasurementInstance({ simulation: { enabled: false } });
m.toggleSimulation();
assert.equal(m.config.simulation.enabled, true);
m.toggleSimulation();
assert.equal(m.config.simulation.enabled, false);
});
test("tick runs simulateInput when simulation is enabled", async () => {
const m = makeMeasurementInstance({
scaling: { enabled: false, inputMin: 0, inputMax: 1, absMin: 0, absMax: 100, offset: 0 },
smoothing: { smoothWindow: 1, smoothMethod: 'none' },
simulation: { enabled: true },
});
const before = m.inputValue;
await m.tick();
await m.tick();
await m.tick();
// Simulated input must drift from its initial state.
assert.notEqual(m.inputValue, before);
});
test("tick is a no-op on inputValue when simulation is disabled", async () => {
const m = makeMeasurementInstance({
scaling: { enabled: false, inputMin: 0, inputMax: 1, absMin: 0, absMax: 100, offset: 0 },
smoothing: { smoothWindow: 1, smoothMethod: 'none' },
simulation: { enabled: false },
});
m.inputValue = 42;
await m.tick();
await m.tick();
assert.equal(m.inputValue, 42);
});