Merge commit '12fce6c' into HEAD
# Conflicts: # index.js # src/configs/index.js # src/configs/machineGroupControl.json # src/helper/assetUtils.js # src/helper/childRegistrationUtils.js # src/helper/configUtils.js # src/helper/logger.js # src/helper/menuUtils.js # src/helper/menuUtils_DEPRECATED.js # src/helper/outputUtils.js # src/helper/validationUtils.js # src/measurements/Measurement.js # src/measurements/MeasurementContainer.js # src/measurements/examples.js # src/outliers/outlierDetection.js
This commit is contained in:
360
test/childRegistration.test.js
Normal file
360
test/childRegistration.test.js
Normal file
@@ -0,0 +1,360 @@
|
||||
const ChildRegistrationUtils = require('../src/helper/childRegistrationUtils');
|
||||
const { POSITIONS } = require('../src/constants/positions');
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Create a minimal mock parent (mainClass) that ChildRegistrationUtils expects. */
|
||||
function createMockParent(opts = {}) {
|
||||
return {
|
||||
child: {},
|
||||
logger: {
|
||||
debug: jest.fn(),
|
||||
info: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
error: jest.fn(),
|
||||
},
|
||||
// optionally provide a registerChild callback so the utils can delegate
|
||||
registerChild: opts.registerChild || undefined,
|
||||
...opts,
|
||||
};
|
||||
}
|
||||
|
||||
/** Create a minimal mock child node with the given overrides. */
|
||||
function createMockChild(overrides = {}) {
|
||||
const defaults = {
|
||||
config: {
|
||||
general: {
|
||||
id: overrides.id || 'child-1',
|
||||
name: overrides.name || 'TestChild',
|
||||
},
|
||||
functionality: {
|
||||
softwareType: overrides.softwareType !== undefined ? overrides.softwareType : 'measurement',
|
||||
positionVsParent: overrides.position || POSITIONS.UPSTREAM,
|
||||
},
|
||||
asset: {
|
||||
category: overrides.category || 'sensor',
|
||||
type: overrides.assetType || 'pressure',
|
||||
},
|
||||
},
|
||||
measurements: overrides.measurements || null,
|
||||
};
|
||||
// allow caller to add extra top-level props
|
||||
return { ...defaults, ...(overrides.extra || {}) };
|
||||
}
|
||||
|
||||
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('ChildRegistrationUtils', () => {
|
||||
let parent;
|
||||
let utils;
|
||||
|
||||
beforeEach(() => {
|
||||
parent = createMockParent();
|
||||
utils = new ChildRegistrationUtils(parent);
|
||||
});
|
||||
|
||||
// ── Construction ─────────────────────────────────────────────────────────
|
||||
describe('constructor', () => {
|
||||
it('should store a reference to the mainClass', () => {
|
||||
expect(utils.mainClass).toBe(parent);
|
||||
});
|
||||
|
||||
it('should initialise with an empty registeredChildren map', () => {
|
||||
expect(utils.registeredChildren.size).toBe(0);
|
||||
});
|
||||
|
||||
it('should use the parent logger', () => {
|
||||
expect(utils.logger).toBe(parent.logger);
|
||||
});
|
||||
});
|
||||
|
||||
// ── registerChild ────────────────────────────────────────────────────────
|
||||
describe('registerChild()', () => {
|
||||
it('should register a child and store it in the internal map', async () => {
|
||||
const child = createMockChild();
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(utils.registeredChildren.size).toBe(1);
|
||||
expect(utils.registeredChildren.has('child-1')).toBe(true);
|
||||
});
|
||||
|
||||
it('should store softwareType, position and timestamp in the registry entry', async () => {
|
||||
const child = createMockChild({ softwareType: 'machine' });
|
||||
const before = Date.now();
|
||||
await utils.registerChild(child, POSITIONS.DOWNSTREAM);
|
||||
const after = Date.now();
|
||||
|
||||
const entry = utils.registeredChildren.get('child-1');
|
||||
expect(entry.softwareType).toBe('machine');
|
||||
expect(entry.position).toBe(POSITIONS.DOWNSTREAM);
|
||||
expect(entry.registeredAt).toBeGreaterThanOrEqual(before);
|
||||
expect(entry.registeredAt).toBeLessThanOrEqual(after);
|
||||
});
|
||||
|
||||
it('should store the child in mainClass.child[softwareType][category]', async () => {
|
||||
const child = createMockChild({ softwareType: 'measurement', category: 'sensor' });
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(parent.child.measurement).toBeDefined();
|
||||
expect(parent.child.measurement.sensor).toBeInstanceOf(Array);
|
||||
expect(parent.child.measurement.sensor).toContain(child);
|
||||
});
|
||||
|
||||
it('should set the parent reference on the child', async () => {
|
||||
const child = createMockChild();
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(child.parent).toEqual([parent]);
|
||||
});
|
||||
|
||||
it('should set positionVsParent on the child', async () => {
|
||||
const child = createMockChild();
|
||||
await utils.registerChild(child, POSITIONS.DOWNSTREAM);
|
||||
|
||||
expect(child.positionVsParent).toBe(POSITIONS.DOWNSTREAM);
|
||||
});
|
||||
|
||||
it('should lowercase the softwareType before storing', async () => {
|
||||
const child = createMockChild({ softwareType: 'Measurement' });
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
const entry = utils.registeredChildren.get('child-1');
|
||||
expect(entry.softwareType).toBe('measurement');
|
||||
expect(parent.child.measurement).toBeDefined();
|
||||
});
|
||||
|
||||
it('should delegate to mainClass.registerChild when it is a function', async () => {
|
||||
const registerSpy = jest.fn();
|
||||
parent.registerChild = registerSpy;
|
||||
const child = createMockChild({ softwareType: 'measurement' });
|
||||
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(registerSpy).toHaveBeenCalledWith(child, 'measurement');
|
||||
});
|
||||
|
||||
it('should NOT throw when mainClass has no registerChild method', async () => {
|
||||
delete parent.registerChild;
|
||||
const child = createMockChild();
|
||||
|
||||
await expect(utils.registerChild(child, POSITIONS.UPSTREAM)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('should log a debug message on registration', async () => {
|
||||
const child = createMockChild({ name: 'Pump1', id: 'p1' });
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(parent.logger.debug).toHaveBeenCalledWith(
|
||||
expect.stringContaining('Registering child: Pump1')
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle empty softwareType gracefully', async () => {
|
||||
const child = createMockChild({ softwareType: '' });
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
const entry = utils.registeredChildren.get('child-1');
|
||||
expect(entry.softwareType).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
// ── Multiple children ────────────────────────────────────────────────────
|
||||
describe('multiple children registration', () => {
|
||||
it('should register multiple children of the same softwareType', async () => {
|
||||
const c1 = createMockChild({ id: 'c1', name: 'Sensor1', softwareType: 'measurement' });
|
||||
const c2 = createMockChild({ id: 'c2', name: 'Sensor2', softwareType: 'measurement' });
|
||||
|
||||
await utils.registerChild(c1, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(c2, POSITIONS.DOWNSTREAM);
|
||||
|
||||
expect(utils.registeredChildren.size).toBe(2);
|
||||
expect(parent.child.measurement.sensor).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should register children of different softwareTypes', async () => {
|
||||
const sensor = createMockChild({ id: 's1', softwareType: 'measurement' });
|
||||
const machine = createMockChild({ id: 'm1', softwareType: 'machine', category: 'pump' });
|
||||
|
||||
await utils.registerChild(sensor, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(machine, POSITIONS.AT_EQUIPMENT);
|
||||
|
||||
expect(parent.child.measurement).toBeDefined();
|
||||
expect(parent.child.machine).toBeDefined();
|
||||
expect(parent.child.machine.pump).toContain(machine);
|
||||
});
|
||||
|
||||
it('should register children of different categories under the same softwareType', async () => {
|
||||
const sensor = createMockChild({ id: 's1', softwareType: 'measurement', category: 'sensor' });
|
||||
const analyser = createMockChild({ id: 'a1', softwareType: 'measurement', category: 'analyser' });
|
||||
|
||||
await utils.registerChild(sensor, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(analyser, POSITIONS.DOWNSTREAM);
|
||||
|
||||
expect(parent.child.measurement.sensor).toHaveLength(1);
|
||||
expect(parent.child.measurement.analyser).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should support multiple parents on a child (array append)', async () => {
|
||||
const parent2 = createMockParent();
|
||||
const utils2 = new ChildRegistrationUtils(parent2);
|
||||
const child = createMockChild();
|
||||
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
await utils2.registerChild(child, POSITIONS.DOWNSTREAM);
|
||||
|
||||
expect(child.parent).toEqual([parent, parent2]);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Duplicate registration ───────────────────────────────────────────────
|
||||
describe('duplicate registration', () => {
|
||||
it('should overwrite the registry entry when the same child id is registered twice', async () => {
|
||||
const child = createMockChild({ id: 'dup-1' });
|
||||
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(child, POSITIONS.DOWNSTREAM);
|
||||
|
||||
// Map.set overwrites, so still size 1
|
||||
expect(utils.registeredChildren.size).toBe(1);
|
||||
const entry = utils.registeredChildren.get('dup-1');
|
||||
expect(entry.position).toBe(POSITIONS.DOWNSTREAM);
|
||||
});
|
||||
|
||||
it('should push the child into the category array again on duplicate registration', async () => {
|
||||
const child = createMockChild({ id: 'dup-1' });
|
||||
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
// _storeChild does a push each time
|
||||
expect(parent.child.measurement.sensor).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Measurement context setup ────────────────────────────────────────────
|
||||
describe('measurement context on child', () => {
|
||||
it('should call setChildId, setChildName, setParentRef when child has measurements', async () => {
|
||||
const measurements = {
|
||||
setChildId: jest.fn(),
|
||||
setChildName: jest.fn(),
|
||||
setParentRef: jest.fn(),
|
||||
};
|
||||
const child = createMockChild({ id: 'mc-1', name: 'Sensor1', measurements });
|
||||
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(measurements.setChildId).toHaveBeenCalledWith('mc-1');
|
||||
expect(measurements.setChildName).toHaveBeenCalledWith('Sensor1');
|
||||
expect(measurements.setParentRef).toHaveBeenCalledWith(parent);
|
||||
});
|
||||
|
||||
it('should skip measurement setup when child has no measurements object', async () => {
|
||||
const child = createMockChild({ measurements: null });
|
||||
|
||||
// Should not throw
|
||||
await expect(utils.registerChild(child, POSITIONS.UPSTREAM)).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// ── getChildrenOfType ────────────────────────────────────────────────────
|
||||
describe('getChildrenOfType()', () => {
|
||||
beforeEach(async () => {
|
||||
const s1 = createMockChild({ id: 's1', softwareType: 'measurement', category: 'sensor' });
|
||||
const s2 = createMockChild({ id: 's2', softwareType: 'measurement', category: 'sensor' });
|
||||
const a1 = createMockChild({ id: 'a1', softwareType: 'measurement', category: 'analyser' });
|
||||
const m1 = createMockChild({ id: 'm1', softwareType: 'machine', category: 'pump' });
|
||||
|
||||
await utils.registerChild(s1, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(s2, POSITIONS.DOWNSTREAM);
|
||||
await utils.registerChild(a1, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(m1, POSITIONS.AT_EQUIPMENT);
|
||||
});
|
||||
|
||||
it('should return all children of a given softwareType', () => {
|
||||
const measurements = utils.getChildrenOfType('measurement');
|
||||
expect(measurements).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('should return children filtered by category', () => {
|
||||
const sensors = utils.getChildrenOfType('measurement', 'sensor');
|
||||
expect(sensors).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should return empty array for unknown softwareType', () => {
|
||||
expect(utils.getChildrenOfType('nonexistent')).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array for unknown category', () => {
|
||||
expect(utils.getChildrenOfType('measurement', 'nonexistent')).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
// ── getChildById ─────────────────────────────────────────────────────────
|
||||
describe('getChildById()', () => {
|
||||
it('should return the child by its id', async () => {
|
||||
const child = createMockChild({ id: 'find-me' });
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(utils.getChildById('find-me')).toBe(child);
|
||||
});
|
||||
|
||||
it('should return null for unknown id', () => {
|
||||
expect(utils.getChildById('does-not-exist')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── getAllChildren ───────────────────────────────────────────────────────
|
||||
describe('getAllChildren()', () => {
|
||||
it('should return an empty array when no children registered', () => {
|
||||
expect(utils.getAllChildren()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return all registered child objects', async () => {
|
||||
const c1 = createMockChild({ id: 'c1' });
|
||||
const c2 = createMockChild({ id: 'c2' });
|
||||
await utils.registerChild(c1, POSITIONS.UPSTREAM);
|
||||
await utils.registerChild(c2, POSITIONS.DOWNSTREAM);
|
||||
|
||||
const all = utils.getAllChildren();
|
||||
expect(all).toHaveLength(2);
|
||||
expect(all).toContain(c1);
|
||||
expect(all).toContain(c2);
|
||||
});
|
||||
});
|
||||
|
||||
// ── logChildStructure ───────────────────────────────────────────────────
|
||||
describe('logChildStructure()', () => {
|
||||
it('should log the child structure via debug', async () => {
|
||||
const child = createMockChild({ id: 'log-1', name: 'LogChild' });
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
utils.logChildStructure();
|
||||
|
||||
expect(parent.logger.debug).toHaveBeenCalledWith(
|
||||
'Current child structure:',
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ── _storeChild (internal) ──────────────────────────────────────────────
|
||||
describe('_storeChild() internal behaviour', () => {
|
||||
it('should create the child object on parent if it does not exist', async () => {
|
||||
delete parent.child;
|
||||
const child = createMockChild();
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(parent.child).toBeDefined();
|
||||
expect(parent.child.measurement.sensor).toContain(child);
|
||||
});
|
||||
|
||||
it('should use "sensor" as default category when asset.category is absent', async () => {
|
||||
const child = createMockChild();
|
||||
// remove asset.category to trigger default
|
||||
delete child.config.asset.category;
|
||||
await utils.registerChild(child, POSITIONS.UPSTREAM);
|
||||
|
||||
expect(parent.child.measurement.sensor).toContain(child);
|
||||
});
|
||||
});
|
||||
});
|
||||
217
test/configManager.test.js
Normal file
217
test/configManager.test.js
Normal file
@@ -0,0 +1,217 @@
|
||||
const path = require('path');
|
||||
const ConfigManager = require('../src/configs/index');
|
||||
|
||||
describe('ConfigManager', () => {
|
||||
const configDir = path.resolve(__dirname, '../src/configs');
|
||||
let cm;
|
||||
|
||||
beforeEach(() => {
|
||||
cm = new ConfigManager(configDir);
|
||||
});
|
||||
|
||||
// ── getConfig() ──────────────────────────────────────────────────────
|
||||
describe('getConfig()', () => {
|
||||
it('should load and parse a known JSON config file', () => {
|
||||
const config = cm.getConfig('baseConfig');
|
||||
expect(config).toBeDefined();
|
||||
expect(typeof config).toBe('object');
|
||||
});
|
||||
|
||||
it('should return the same content on successive calls', () => {
|
||||
const a = cm.getConfig('baseConfig');
|
||||
const b = cm.getConfig('baseConfig');
|
||||
expect(a).toEqual(b);
|
||||
});
|
||||
|
||||
it('should throw when the config file does not exist', () => {
|
||||
expect(() => cm.getConfig('nonExistentConfig_xyz'))
|
||||
.toThrow(/Failed to load config/);
|
||||
});
|
||||
|
||||
it('should throw a descriptive message including the config name', () => {
|
||||
expect(() => cm.getConfig('missing'))
|
||||
.toThrow("Failed to load config 'missing'");
|
||||
});
|
||||
});
|
||||
|
||||
// ── hasConfig() ──────────────────────────────────────────────────────
|
||||
describe('hasConfig()', () => {
|
||||
it('should return true for a config that exists', () => {
|
||||
expect(cm.hasConfig('baseConfig')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for a config that does not exist', () => {
|
||||
expect(cm.hasConfig('doesNotExist_abc')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ── getAvailableConfigs() ────────────────────────────────────────────
|
||||
describe('getAvailableConfigs()', () => {
|
||||
it('should return an array of strings', () => {
|
||||
const configs = cm.getAvailableConfigs();
|
||||
expect(Array.isArray(configs)).toBe(true);
|
||||
configs.forEach(name => expect(typeof name).toBe('string'));
|
||||
});
|
||||
|
||||
it('should include known config names without .json extension', () => {
|
||||
const configs = cm.getAvailableConfigs();
|
||||
expect(configs).toContain('baseConfig');
|
||||
expect(configs).toContain('diffuser');
|
||||
expect(configs).toContain('measurement');
|
||||
});
|
||||
|
||||
it('should not include .json extension in returned names', () => {
|
||||
const configs = cm.getAvailableConfigs();
|
||||
configs.forEach(name => {
|
||||
expect(name).not.toMatch(/\.json$/);
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw when pointed at a non-existent directory', () => {
|
||||
const bad = new ConfigManager('/tmp/nonexistent_dir_xyz_123');
|
||||
expect(() => bad.getAvailableConfigs()).toThrow(/Failed to read config directory/);
|
||||
});
|
||||
});
|
||||
|
||||
// ── buildConfig() ────────────────────────────────────────────────────
|
||||
describe('buildConfig()', () => {
|
||||
it('should return an object with general and functionality sections', () => {
|
||||
const uiConfig = { name: 'TestNode', unit: 'bar', enableLog: true, logLevel: 'debug' };
|
||||
const result = cm.buildConfig('measurement', uiConfig, 'node-id-1');
|
||||
expect(result).toHaveProperty('general');
|
||||
expect(result).toHaveProperty('functionality');
|
||||
expect(result).toHaveProperty('output');
|
||||
});
|
||||
|
||||
it('should populate general.name from uiConfig.name', () => {
|
||||
const uiConfig = { name: 'MySensor' };
|
||||
const result = cm.buildConfig('measurement', uiConfig, 'id-1');
|
||||
expect(result.general.name).toBe('MySensor');
|
||||
});
|
||||
|
||||
it('should default general.name to nodeName when uiConfig.name is empty', () => {
|
||||
const result = cm.buildConfig('measurement', {}, 'id-1');
|
||||
expect(result.general.name).toBe('measurement');
|
||||
});
|
||||
|
||||
it('should set general.id from the nodeId argument', () => {
|
||||
const result = cm.buildConfig('valve', {}, 'node-42');
|
||||
expect(result.general.id).toBe('node-42');
|
||||
});
|
||||
|
||||
it('should default unit to unitless', () => {
|
||||
const result = cm.buildConfig('valve', {}, 'id-1');
|
||||
expect(result.general.unit).toBe('unitless');
|
||||
});
|
||||
|
||||
it('should default logging.enabled to true when enableLog is undefined', () => {
|
||||
const result = cm.buildConfig('valve', {}, 'id-1');
|
||||
expect(result.general.logging.enabled).toBe(true);
|
||||
});
|
||||
|
||||
it('should respect enableLog = false', () => {
|
||||
const result = cm.buildConfig('valve', { enableLog: false }, 'id-1');
|
||||
expect(result.general.logging.enabled).toBe(false);
|
||||
});
|
||||
|
||||
it('should default logLevel to info', () => {
|
||||
const result = cm.buildConfig('valve', {}, 'id-1');
|
||||
expect(result.general.logging.logLevel).toBe('info');
|
||||
});
|
||||
|
||||
it('should set functionality.softwareType to lowercase nodeName', () => {
|
||||
const result = cm.buildConfig('Valve', {}, 'id-1');
|
||||
expect(result.functionality.softwareType).toBe('valve');
|
||||
});
|
||||
|
||||
it('should default positionVsParent to atEquipment', () => {
|
||||
const result = cm.buildConfig('valve', {}, 'id-1');
|
||||
expect(result.functionality.positionVsParent).toBe('atEquipment');
|
||||
});
|
||||
|
||||
it('should set distance when hasDistance is true', () => {
|
||||
const result = cm.buildConfig('valve', { hasDistance: true, distance: 5.5 }, 'id-1');
|
||||
expect(result.functionality.distance).toBe(5.5);
|
||||
});
|
||||
|
||||
it('should set distance to undefined when hasDistance is false', () => {
|
||||
const result = cm.buildConfig('valve', { hasDistance: false, distance: 5.5 }, 'id-1');
|
||||
expect(result.functionality.distance).toBeUndefined();
|
||||
});
|
||||
|
||||
// ── asset section ──────────────────────────────────────────────────
|
||||
it('should not include asset section when no asset fields provided', () => {
|
||||
const result = cm.buildConfig('valve', {}, 'id-1');
|
||||
expect(result.asset).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should include asset section when supplier is provided', () => {
|
||||
const result = cm.buildConfig('valve', { supplier: 'Siemens' }, 'id-1');
|
||||
expect(result.asset).toBeDefined();
|
||||
expect(result.asset.supplier).toBe('Siemens');
|
||||
});
|
||||
|
||||
it('should populate asset defaults for missing optional fields', () => {
|
||||
const result = cm.buildConfig('valve', { supplier: 'ABB' }, 'id-1');
|
||||
expect(result.asset.category).toBe('sensor');
|
||||
expect(result.asset.type).toBe('Unknown');
|
||||
expect(result.asset.model).toBe('Unknown');
|
||||
});
|
||||
|
||||
// ── domainConfig merge ─────────────────────────────────────────────
|
||||
it('should merge domainConfig sections into the result', () => {
|
||||
const domain = { scaling: { enabled: true, factor: 2 } };
|
||||
const result = cm.buildConfig('measurement', {}, 'id-1', domain);
|
||||
expect(result.scaling).toEqual({ enabled: true, factor: 2 });
|
||||
});
|
||||
|
||||
it('should handle empty domainConfig gracefully', () => {
|
||||
const result = cm.buildConfig('measurement', {}, 'id-1', {});
|
||||
expect(result).toHaveProperty('general');
|
||||
expect(result).toHaveProperty('functionality');
|
||||
});
|
||||
|
||||
it('should default output formats to process and influxdb', () => {
|
||||
const result = cm.buildConfig('measurement', {}, 'id-1');
|
||||
expect(result.output).toEqual({
|
||||
process: 'process',
|
||||
dbase: 'influxdb',
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow output format overrides from ui config', () => {
|
||||
const result = cm.buildConfig('measurement', {
|
||||
processOutputFormat: 'json',
|
||||
dbaseOutputFormat: 'csv',
|
||||
}, 'id-1');
|
||||
expect(result.output).toEqual({
|
||||
process: 'json',
|
||||
dbase: 'csv',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ── createEndpoint() ─────────────────────────────────────────────────
|
||||
describe('createEndpoint()', () => {
|
||||
it('should return a JavaScript string containing the node name', () => {
|
||||
const script = cm.createEndpoint('baseConfig');
|
||||
expect(typeof script).toBe('string');
|
||||
expect(script).toContain('baseConfig');
|
||||
expect(script).toContain('window.EVOLV');
|
||||
});
|
||||
|
||||
it('should throw for a non-existent config', () => {
|
||||
expect(() => cm.createEndpoint('doesNotExist_xyz'))
|
||||
.toThrow(/Failed to create endpoint/);
|
||||
});
|
||||
});
|
||||
|
||||
// ── getBaseConfig() ──────────────────────────────────────────────────
|
||||
describe('getBaseConfig()', () => {
|
||||
it('should load the baseConfig.json file', () => {
|
||||
const base = cm.getBaseConfig();
|
||||
expect(base).toBeDefined();
|
||||
expect(typeof base).toBe('object');
|
||||
});
|
||||
});
|
||||
});
|
||||
336
test/measurementContainer.test.js
Normal file
336
test/measurementContainer.test.js
Normal file
@@ -0,0 +1,336 @@
|
||||
const MeasurementContainer = require('../src/measurements/MeasurementContainer');
|
||||
|
||||
describe('MeasurementContainer', () => {
|
||||
let mc;
|
||||
|
||||
beforeEach(() => {
|
||||
mc = new MeasurementContainer({ windowSize: 5, autoConvert: false });
|
||||
});
|
||||
|
||||
// ── Construction ─────────────────────────────────────────────────────
|
||||
describe('constructor', () => {
|
||||
it('should initialise with default windowSize when none provided', () => {
|
||||
const m = new MeasurementContainer();
|
||||
expect(m.windowSize).toBe(10);
|
||||
});
|
||||
|
||||
it('should accept a custom windowSize', () => {
|
||||
expect(mc.windowSize).toBe(5);
|
||||
});
|
||||
|
||||
it('should start with an empty measurements map', () => {
|
||||
expect(mc.measurements).toEqual({});
|
||||
});
|
||||
|
||||
it('should populate default units', () => {
|
||||
expect(mc.defaultUnits.pressure).toBe('mbar');
|
||||
expect(mc.defaultUnits.flow).toBe('m3/h');
|
||||
});
|
||||
|
||||
it('should allow overriding default units', () => {
|
||||
const m = new MeasurementContainer({ defaultUnits: { pressure: 'Pa' } });
|
||||
expect(m.defaultUnits.pressure).toBe('Pa');
|
||||
});
|
||||
});
|
||||
|
||||
// ── Chainable setters ───────────────────────────────────────────────
|
||||
describe('chaining API — type / variant / position', () => {
|
||||
it('should set type and return this for chaining', () => {
|
||||
const ret = mc.type('pressure');
|
||||
expect(ret).toBe(mc);
|
||||
expect(mc._currentType).toBe('pressure');
|
||||
});
|
||||
|
||||
it('should reset variant and position when type is called', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
mc.type('flow');
|
||||
expect(mc._currentVariant).toBeNull();
|
||||
expect(mc._currentPosition).toBeNull();
|
||||
});
|
||||
|
||||
it('should set variant and return this', () => {
|
||||
mc.type('pressure');
|
||||
const ret = mc.variant('measured');
|
||||
expect(ret).toBe(mc);
|
||||
expect(mc._currentVariant).toBe('measured');
|
||||
});
|
||||
|
||||
it('should throw if variant is called without type', () => {
|
||||
expect(() => mc.variant('measured')).toThrow(/Type must be specified/);
|
||||
});
|
||||
|
||||
it('should set position (lowercased) and return this', () => {
|
||||
mc.type('pressure').variant('measured');
|
||||
const ret = mc.position('Upstream');
|
||||
expect(ret).toBe(mc);
|
||||
expect(mc._currentPosition).toBe('upstream');
|
||||
});
|
||||
|
||||
it('should throw if position is called without variant', () => {
|
||||
mc.type('pressure');
|
||||
expect(() => mc.position('upstream')).toThrow(/Variant must be specified/);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Storing and retrieving values ───────────────────────────────────
|
||||
describe('value() and retrieval methods', () => {
|
||||
beforeEach(() => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
});
|
||||
|
||||
it('should store a value and retrieve it with getCurrentValue()', () => {
|
||||
mc.value(42, 1000);
|
||||
expect(mc.getCurrentValue()).toBe(42);
|
||||
});
|
||||
|
||||
it('should return this for chaining from value()', () => {
|
||||
const ret = mc.value(1, 1000);
|
||||
expect(ret).toBe(mc);
|
||||
});
|
||||
|
||||
it('should store multiple values and keep the latest', () => {
|
||||
mc.value(10, 1).value(20, 2).value(30, 3);
|
||||
expect(mc.getCurrentValue()).toBe(30);
|
||||
});
|
||||
|
||||
it('should respect the windowSize (rolling window)', () => {
|
||||
for (let i = 1; i <= 8; i++) {
|
||||
mc.value(i, i);
|
||||
}
|
||||
const all = mc.getAllValues();
|
||||
// windowSize is 5, so only the last 5 values should remain
|
||||
expect(all.values.length).toBe(5);
|
||||
expect(all.values).toEqual([4, 5, 6, 7, 8]);
|
||||
});
|
||||
|
||||
it('should compute getAverage() correctly', () => {
|
||||
mc.value(10, 1).value(20, 2).value(30, 3);
|
||||
expect(mc.getAverage()).toBe(20);
|
||||
});
|
||||
|
||||
it('should compute getMin()', () => {
|
||||
mc.value(10, 1).value(5, 2).value(20, 3);
|
||||
expect(mc.getMin()).toBe(5);
|
||||
});
|
||||
|
||||
it('should compute getMax()', () => {
|
||||
mc.value(10, 1).value(5, 2).value(20, 3);
|
||||
expect(mc.getMax()).toBe(20);
|
||||
});
|
||||
|
||||
it('should return null for getCurrentValue() when no values exist', () => {
|
||||
expect(mc.getCurrentValue()).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for getAverage() when no values exist', () => {
|
||||
expect(mc.getAverage()).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for getMin() when no values exist', () => {
|
||||
expect(mc.getMin()).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for getMax() when no values exist', () => {
|
||||
expect(mc.getMax()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── getAllValues() ──────────────────────────────────────────────────
|
||||
describe('getAllValues()', () => {
|
||||
it('should return values, timestamps, and unit', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
mc.unit('bar');
|
||||
mc.value(10, 100).value(20, 200);
|
||||
const all = mc.getAllValues();
|
||||
expect(all.values).toEqual([10, 20]);
|
||||
expect(all.timestamps).toEqual([100, 200]);
|
||||
expect(all.unit).toBe('bar');
|
||||
});
|
||||
|
||||
it('should return null when chain is incomplete', () => {
|
||||
mc.type('pressure');
|
||||
expect(mc.getAllValues()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── unit() ──────────────────────────────────────────────────────────
|
||||
describe('unit()', () => {
|
||||
it('should set unit on the underlying measurement', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
mc.unit('bar');
|
||||
const measurement = mc.get();
|
||||
expect(measurement.unit).toBe('bar');
|
||||
});
|
||||
});
|
||||
|
||||
// ── get() ───────────────────────────────────────────────────────────
|
||||
describe('get()', () => {
|
||||
it('should return the Measurement instance for a complete chain', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
mc.value(1, 1);
|
||||
const m = mc.get();
|
||||
expect(m).toBeDefined();
|
||||
expect(m.type).toBe('pressure');
|
||||
expect(m.variant).toBe('measured');
|
||||
expect(m.position).toBe('upstream');
|
||||
});
|
||||
|
||||
it('should return null when chain is incomplete', () => {
|
||||
mc.type('pressure');
|
||||
expect(mc.get()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── exists() ────────────────────────────────────────────────────────
|
||||
describe('exists()', () => {
|
||||
it('should return false for a non-existent measurement', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
expect(mc.exists()).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true after a value has been stored', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream').value(1, 1);
|
||||
expect(mc.exists()).toBe(true);
|
||||
});
|
||||
|
||||
it('should support requireValues option', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
// Force creation of measurement without values
|
||||
mc.get();
|
||||
expect(mc.exists({ requireValues: false })).toBe(true);
|
||||
expect(mc.exists({ requireValues: true })).toBe(false);
|
||||
});
|
||||
|
||||
it('should support explicit type/variant/position overrides', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream').value(1, 1);
|
||||
// Reset chain, then query by explicit keys
|
||||
mc.type('flow');
|
||||
expect(mc.exists({ type: 'pressure', variant: 'measured', position: 'upstream' })).toBe(true);
|
||||
expect(mc.exists({ type: 'flow', variant: 'measured', position: 'upstream' })).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false when type is not set and not provided', () => {
|
||||
const fresh = new MeasurementContainer({ autoConvert: false });
|
||||
expect(fresh.exists()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ── getLaggedValue() / getLaggedSample() ─────────────────────────────
|
||||
describe('getLaggedValue() and getLaggedSample()', () => {
|
||||
beforeEach(() => {
|
||||
mc.type('pressure').variant('measured').position('upstream');
|
||||
mc.value(10, 100).value(20, 200).value(30, 300);
|
||||
});
|
||||
|
||||
it('should return the value at lag=1 (previous value)', () => {
|
||||
expect(mc.getLaggedValue(1)).toBe(20);
|
||||
});
|
||||
|
||||
it('should return null when lag exceeds stored values', () => {
|
||||
expect(mc.getLaggedValue(10)).toBeNull();
|
||||
});
|
||||
|
||||
it('should return a sample object from getLaggedSample()', () => {
|
||||
const sample = mc.getLaggedSample(0);
|
||||
expect(sample).toHaveProperty('value', 30);
|
||||
expect(sample).toHaveProperty('timestamp', 300);
|
||||
});
|
||||
|
||||
it('should return null from getLaggedSample when not enough values', () => {
|
||||
expect(mc.getLaggedSample(10)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── Listing helpers ─────────────────────────────────────────────────
|
||||
describe('getTypes() / getVariants() / getPositions()', () => {
|
||||
beforeEach(() => {
|
||||
mc.type('pressure').variant('measured').position('upstream').value(1, 1);
|
||||
mc.type('flow').variant('predicted').position('downstream').value(2, 2);
|
||||
});
|
||||
|
||||
it('should list all stored types', () => {
|
||||
const types = mc.getTypes();
|
||||
expect(types).toContain('pressure');
|
||||
expect(types).toContain('flow');
|
||||
});
|
||||
|
||||
it('should list variants for a given type', () => {
|
||||
mc.type('pressure');
|
||||
expect(mc.getVariants()).toContain('measured');
|
||||
});
|
||||
|
||||
it('should return empty array for type with no variants', () => {
|
||||
mc.type('temperature');
|
||||
expect(mc.getVariants()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should throw if getVariants() called without type', () => {
|
||||
const fresh = new MeasurementContainer({ autoConvert: false });
|
||||
expect(() => fresh.getVariants()).toThrow(/Type must be specified/);
|
||||
});
|
||||
|
||||
it('should list positions for type+variant', () => {
|
||||
mc.type('pressure').variant('measured');
|
||||
expect(mc.getPositions()).toContain('upstream');
|
||||
});
|
||||
|
||||
it('should throw if getPositions() called without type and variant', () => {
|
||||
const fresh = new MeasurementContainer({ autoConvert: false });
|
||||
expect(() => fresh.getPositions()).toThrow(/Type and variant must be specified/);
|
||||
});
|
||||
});
|
||||
|
||||
// ── clear() ─────────────────────────────────────────────────────────
|
||||
describe('clear()', () => {
|
||||
it('should reset all measurements and chain state', () => {
|
||||
mc.type('pressure').variant('measured').position('upstream').value(1, 1);
|
||||
mc.clear();
|
||||
expect(mc.measurements).toEqual({});
|
||||
expect(mc._currentType).toBeNull();
|
||||
expect(mc._currentVariant).toBeNull();
|
||||
expect(mc._currentPosition).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── Child context setters ───────────────────────────────────────────
|
||||
describe('child context', () => {
|
||||
it('should set childId and return this', () => {
|
||||
expect(mc.setChildId('c1')).toBe(mc);
|
||||
expect(mc.childId).toBe('c1');
|
||||
});
|
||||
|
||||
it('should set childName and return this', () => {
|
||||
expect(mc.setChildName('pump1')).toBe(mc);
|
||||
expect(mc.childName).toBe('pump1');
|
||||
});
|
||||
|
||||
it('should set parentRef and return this', () => {
|
||||
const parent = { id: 'p1' };
|
||||
expect(mc.setParentRef(parent)).toBe(mc);
|
||||
expect(mc.parentRef).toBe(parent);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Event emission ──────────────────────────────────────────────────
|
||||
describe('event emission', () => {
|
||||
it('should emit an event when a value is set', (done) => {
|
||||
mc.emitter.on('pressure.measured.upstream', (data) => {
|
||||
expect(data.value).toBe(42);
|
||||
expect(data.type).toBe('pressure');
|
||||
expect(data.variant).toBe('measured');
|
||||
expect(data.position).toBe('upstream');
|
||||
done();
|
||||
});
|
||||
mc.type('pressure').variant('measured').position('upstream').value(42, 1);
|
||||
});
|
||||
});
|
||||
|
||||
// ── setPreferredUnit ────────────────────────────────────────────────
|
||||
describe('setPreferredUnit()', () => {
|
||||
it('should store preferred unit and return this', () => {
|
||||
const ret = mc.setPreferredUnit('pressure', 'Pa');
|
||||
expect(ret).toBe(mc);
|
||||
expect(mc.preferredUnits.pressure).toBe('Pa');
|
||||
});
|
||||
});
|
||||
});
|
||||
69
test/outputUtils.test.js
Normal file
69
test/outputUtils.test.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const OutputUtils = require('../src/helper/outputUtils');
|
||||
|
||||
describe('OutputUtils', () => {
|
||||
let outputUtils;
|
||||
let config;
|
||||
|
||||
beforeEach(() => {
|
||||
outputUtils = new OutputUtils();
|
||||
config = {
|
||||
general: {
|
||||
name: 'Pump-1',
|
||||
id: 'node-1',
|
||||
unit: 'm3/h',
|
||||
},
|
||||
functionality: {
|
||||
softwareType: 'pump',
|
||||
role: 'test-role',
|
||||
},
|
||||
asset: {
|
||||
supplier: 'EVOLV',
|
||||
type: 'sensor',
|
||||
},
|
||||
output: {
|
||||
process: 'process',
|
||||
dbase: 'influxdb',
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
it('keeps legacy process output by default', () => {
|
||||
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'process');
|
||||
expect(msg).toEqual({
|
||||
topic: 'Pump-1',
|
||||
payload: { flow: 12.5 },
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps legacy influxdb output by default', () => {
|
||||
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'influxdb');
|
||||
expect(msg.topic).toBe('Pump-1');
|
||||
expect(msg.payload).toEqual(expect.objectContaining({
|
||||
measurement: 'Pump-1',
|
||||
fields: { flow: 12.5 },
|
||||
tags: expect.objectContaining({
|
||||
id: 'node-1',
|
||||
name: 'Pump-1',
|
||||
softwareType: 'pump',
|
||||
}),
|
||||
}));
|
||||
});
|
||||
|
||||
it('supports config-driven json formatting on the process channel', () => {
|
||||
config.output.process = 'json';
|
||||
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'process');
|
||||
expect(msg.topic).toBe('Pump-1');
|
||||
expect(typeof msg.payload).toBe('string');
|
||||
expect(msg.payload).toContain('"measurement":"Pump-1"');
|
||||
expect(msg.payload).toContain('"flow":12.5');
|
||||
});
|
||||
|
||||
it('supports config-driven csv formatting on the database channel', () => {
|
||||
config.output.dbase = 'csv';
|
||||
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'influxdb');
|
||||
expect(msg.topic).toBe('Pump-1');
|
||||
expect(typeof msg.payload).toBe('string');
|
||||
expect(msg.payload).toContain('Pump-1');
|
||||
expect(msg.payload).toContain('flow=12.5');
|
||||
});
|
||||
});
|
||||
554
test/validationUtils.test.js
Normal file
554
test/validationUtils.test.js
Normal file
@@ -0,0 +1,554 @@
|
||||
const ValidationUtils = require('../src/helper/validationUtils');
|
||||
const { validateNumber, validateInteger, validateBoolean, validateString, validateEnum } = require('../src/helper/validators/typeValidators');
|
||||
const { validateArray, validateSet, validateObject } = require('../src/helper/validators/collectionValidators');
|
||||
const { validateCurve, validateMachineCurve, isSorted, isUnique, areNumbers } = require('../src/helper/validators/curveValidator');
|
||||
|
||||
// Shared mock logger used across tests
|
||||
function mockLogger() {
|
||||
return { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() };
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Type validators
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
describe('typeValidators', () => {
|
||||
let logger;
|
||||
beforeEach(() => { logger = mockLogger(); });
|
||||
|
||||
// ── validateNumber ──────────────────────────────────────────────────
|
||||
describe('validateNumber()', () => {
|
||||
it('should accept a valid number', () => {
|
||||
expect(validateNumber(42, {}, { default: 0 }, 'n', 'k', logger)).toBe(42);
|
||||
});
|
||||
|
||||
it('should parse a string to a number', () => {
|
||||
expect(validateNumber('3.14', {}, { default: 0 }, 'n', 'k', logger)).toBe(3.14);
|
||||
expect(logger.warn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return default when below min', () => {
|
||||
expect(validateNumber(1, { min: 5 }, { default: 5 }, 'n', 'k', logger)).toBe(5);
|
||||
});
|
||||
|
||||
it('should return default when above max', () => {
|
||||
expect(validateNumber(100, { max: 50 }, { default: 50 }, 'n', 'k', logger)).toBe(50);
|
||||
});
|
||||
|
||||
it('should accept boundary value equal to min', () => {
|
||||
expect(validateNumber(5, { min: 5 }, { default: 0 }, 'n', 'k', logger)).toBe(5);
|
||||
});
|
||||
|
||||
it('should accept boundary value equal to max', () => {
|
||||
expect(validateNumber(50, { max: 50 }, { default: 0 }, 'n', 'k', logger)).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateInteger ─────────────────────────────────────────────────
|
||||
describe('validateInteger()', () => {
|
||||
it('should accept a valid integer', () => {
|
||||
expect(validateInteger(7, {}, { default: 0 }, 'n', 'k', logger)).toBe(7);
|
||||
});
|
||||
|
||||
it('should parse a string to an integer', () => {
|
||||
expect(validateInteger('10', {}, { default: 0 }, 'n', 'k', logger)).toBe(10);
|
||||
});
|
||||
|
||||
it('should return default for a non-parseable value', () => {
|
||||
expect(validateInteger('abc', {}, { default: -1 }, 'n', 'k', logger)).toBe(-1);
|
||||
});
|
||||
|
||||
it('should return default when below min', () => {
|
||||
expect(validateInteger(2, { min: 5 }, { default: 5 }, 'n', 'k', logger)).toBe(5);
|
||||
});
|
||||
|
||||
it('should return default when above max', () => {
|
||||
expect(validateInteger(100, { max: 50 }, { default: 50 }, 'n', 'k', logger)).toBe(50);
|
||||
});
|
||||
|
||||
it('should parse a float string and truncate to integer', () => {
|
||||
expect(validateInteger('7.9', {}, { default: 0 }, 'n', 'k', logger)).toBe(7);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateBoolean ─────────────────────────────────────────────────
|
||||
describe('validateBoolean()', () => {
|
||||
it('should pass through a true boolean', () => {
|
||||
expect(validateBoolean(true, 'n', 'k', logger)).toBe(true);
|
||||
});
|
||||
|
||||
it('should pass through a false boolean', () => {
|
||||
expect(validateBoolean(false, 'n', 'k', logger)).toBe(false);
|
||||
});
|
||||
|
||||
it('should parse string "true" to boolean true', () => {
|
||||
expect(validateBoolean('true', 'n', 'k', logger)).toBe(true);
|
||||
});
|
||||
|
||||
it('should parse string "false" to boolean false', () => {
|
||||
expect(validateBoolean('false', 'n', 'k', logger)).toBe(false);
|
||||
});
|
||||
|
||||
it('should pass through non-boolean non-string values unchanged', () => {
|
||||
expect(validateBoolean(42, 'n', 'k', logger)).toBe(42);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateString ──────────────────────────────────────────────────
|
||||
describe('validateString()', () => {
|
||||
it('should accept a lowercase string', () => {
|
||||
expect(validateString('hello', {}, { default: '' }, 'n', 'k', logger)).toBe('hello');
|
||||
});
|
||||
|
||||
it('should convert uppercase to lowercase', () => {
|
||||
expect(validateString('Hello', {}, { default: '' }, 'n', 'k', logger)).toBe('hello');
|
||||
});
|
||||
|
||||
it('should convert a number to a string', () => {
|
||||
expect(validateString(42, {}, { default: '' }, 'n', 'k', logger)).toBe('42');
|
||||
});
|
||||
|
||||
it('should return null when nullable and value is null', () => {
|
||||
expect(validateString(null, { nullable: true }, { default: '' }, 'n', 'k', logger)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateEnum ────────────────────────────────────────────────────
|
||||
describe('validateEnum()', () => {
|
||||
const rules = { values: [{ value: 'open' }, { value: 'closed' }, { value: 'partial' }] };
|
||||
|
||||
it('should accept a valid enum value', () => {
|
||||
expect(validateEnum('open', rules, { default: 'closed' }, 'n', 'k', logger)).toBe('open');
|
||||
});
|
||||
|
||||
it('should be case-insensitive', () => {
|
||||
expect(validateEnum('OPEN', rules, { default: 'closed' }, 'n', 'k', logger)).toBe('open');
|
||||
});
|
||||
|
||||
it('should return default for an invalid value', () => {
|
||||
expect(validateEnum('invalid', rules, { default: 'closed' }, 'n', 'k', logger)).toBe('closed');
|
||||
});
|
||||
|
||||
it('should return default when value is null', () => {
|
||||
expect(validateEnum(null, rules, { default: 'closed' }, 'n', 'k', logger)).toBe('closed');
|
||||
});
|
||||
|
||||
it('should return default when rules.values is not an array', () => {
|
||||
expect(validateEnum('open', {}, { default: 'closed' }, 'n', 'k', logger)).toBe('closed');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Collection validators
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
describe('collectionValidators', () => {
|
||||
let logger;
|
||||
beforeEach(() => { logger = mockLogger(); });
|
||||
|
||||
// ── validateArray ───────────────────────────────────────────────────
|
||||
describe('validateArray()', () => {
|
||||
it('should return default when value is not an array', () => {
|
||||
expect(validateArray('not-array', { itemType: 'number' }, { default: [1] }, 'n', 'k', logger))
|
||||
.toEqual([1]);
|
||||
});
|
||||
|
||||
it('should filter items by itemType', () => {
|
||||
const result = validateArray([1, 'a', 2], { itemType: 'number', minLength: 1 }, { default: [] }, 'n', 'k', logger);
|
||||
expect(result).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it('should respect maxLength', () => {
|
||||
const result = validateArray([1, 2, 3, 4, 5], { itemType: 'number', maxLength: 3, minLength: 1 }, { default: [] }, 'n', 'k', logger);
|
||||
expect(result).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('should return default when fewer items than minLength after filtering', () => {
|
||||
const result = validateArray(['a'], { itemType: 'number', minLength: 1 }, { default: [0] }, 'n', 'k', logger);
|
||||
expect(result).toEqual([0]);
|
||||
});
|
||||
|
||||
it('should pass all items through when itemType is null', () => {
|
||||
const result = validateArray([1, 'a', true], { itemType: 'null', minLength: 1 }, { default: [] }, 'n', 'k', logger);
|
||||
expect(result).toEqual([1, 'a', true]);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateSet ─────────────────────────────────────────────────────
|
||||
describe('validateSet()', () => {
|
||||
it('should convert default to Set when value is not a Set', () => {
|
||||
const result = validateSet('not-a-set', { itemType: 'number' }, { default: [1, 2] }, 'n', 'k', logger);
|
||||
expect(result).toBeInstanceOf(Set);
|
||||
expect([...result]).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it('should filter Set items by type', () => {
|
||||
const input = new Set([1, 'a', 2]);
|
||||
const result = validateSet(input, { itemType: 'number', minLength: 1 }, { default: [] }, 'n', 'k', logger);
|
||||
expect([...result]).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it('should return default Set when too few items remain', () => {
|
||||
const input = new Set(['a']);
|
||||
const result = validateSet(input, { itemType: 'number', minLength: 1 }, { default: [0] }, 'n', 'k', logger);
|
||||
expect([...result]).toEqual([0]);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateObject ──────────────────────────────────────────────────
|
||||
describe('validateObject()', () => {
|
||||
it('should return default when value is not an object', () => {
|
||||
expect(validateObject('str', {}, { default: { a: 1 } }, 'n', 'k', jest.fn(), logger))
|
||||
.toEqual({ a: 1 });
|
||||
});
|
||||
|
||||
it('should return default when value is an array', () => {
|
||||
expect(validateObject([1, 2], {}, { default: {} }, 'n', 'k', jest.fn(), logger))
|
||||
.toEqual({});
|
||||
});
|
||||
|
||||
it('should return default when no schema is provided', () => {
|
||||
expect(validateObject({ a: 1 }, {}, { default: { b: 2 } }, 'n', 'k', jest.fn(), logger))
|
||||
.toEqual({ b: 2 });
|
||||
});
|
||||
|
||||
it('should call validateSchemaFn when schema is provided', () => {
|
||||
const mockFn = jest.fn().mockReturnValue({ validated: true });
|
||||
const rules = { schema: { x: { default: 1 } } };
|
||||
const result = validateObject({ x: 2 }, rules, {}, 'n', 'k', mockFn, logger);
|
||||
expect(mockFn).toHaveBeenCalledWith({ x: 2 }, rules.schema, 'n.k');
|
||||
expect(result).toEqual({ validated: true });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Curve validators
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
describe('curveValidator', () => {
|
||||
let logger;
|
||||
beforeEach(() => { logger = mockLogger(); });
|
||||
|
||||
// ── Helper utilities ────────────────────────────────────────────────
|
||||
describe('isSorted()', () => {
|
||||
it('should return true for a sorted array', () => {
|
||||
expect(isSorted([1, 2, 3, 4])).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for an unsorted array', () => {
|
||||
expect(isSorted([3, 1, 2])).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true for an empty array', () => {
|
||||
expect(isSorted([])).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for equal adjacent values', () => {
|
||||
expect(isSorted([1, 1, 2])).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isUnique()', () => {
|
||||
it('should return true when all values are unique', () => {
|
||||
expect(isUnique([1, 2, 3])).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false when duplicates exist', () => {
|
||||
expect(isUnique([1, 2, 2])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('areNumbers()', () => {
|
||||
it('should return true for all numbers', () => {
|
||||
expect(areNumbers([1, 2.5, -3])).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false when a non-number is present', () => {
|
||||
expect(areNumbers([1, 'a', 3])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateCurve ───────────────────────────────────────────────────
|
||||
describe('validateCurve()', () => {
|
||||
const defaultCurve = { line1: { x: [0, 1], y: [0, 1] } };
|
||||
|
||||
it('should return default when input is null', () => {
|
||||
expect(validateCurve(null, defaultCurve, logger)).toEqual(defaultCurve);
|
||||
});
|
||||
|
||||
it('should return default for an empty object', () => {
|
||||
expect(validateCurve({}, defaultCurve, logger)).toEqual(defaultCurve);
|
||||
});
|
||||
|
||||
it('should validate a correct curve', () => {
|
||||
const curve = { line1: { x: [1, 2, 3], y: [10, 20, 30] } };
|
||||
const result = validateCurve(curve, defaultCurve, logger);
|
||||
expect(result.line1.x).toEqual([1, 2, 3]);
|
||||
expect(result.line1.y).toEqual([10, 20, 30]);
|
||||
});
|
||||
|
||||
it('should sort unsorted x values and reorder y accordingly', () => {
|
||||
const curve = { line1: { x: [3, 1, 2], y: [30, 10, 20] } };
|
||||
const result = validateCurve(curve, defaultCurve, logger);
|
||||
expect(result.line1.x).toEqual([1, 2, 3]);
|
||||
expect(result.line1.y).toEqual([10, 20, 30]);
|
||||
});
|
||||
|
||||
it('should remove duplicate x values', () => {
|
||||
const curve = { line1: { x: [1, 1, 2], y: [10, 11, 20] } };
|
||||
const result = validateCurve(curve, defaultCurve, logger);
|
||||
expect(result.line1.x).toEqual([1, 2]);
|
||||
expect(result.line1.y.length).toBe(2);
|
||||
});
|
||||
|
||||
it('should return default when y contains non-numbers', () => {
|
||||
const curve = { line1: { x: [1, 2], y: ['a', 'b'] } };
|
||||
expect(validateCurve(curve, defaultCurve, logger)).toEqual(defaultCurve);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateMachineCurve ────────────────────────────────────────────
|
||||
describe('validateMachineCurve()', () => {
|
||||
const defaultMC = {
|
||||
nq: { line1: { x: [0, 1], y: [0, 1] } },
|
||||
np: { line1: { x: [0, 1], y: [0, 1] } },
|
||||
};
|
||||
|
||||
it('should return default when input is null', () => {
|
||||
expect(validateMachineCurve(null, defaultMC, logger)).toEqual(defaultMC);
|
||||
});
|
||||
|
||||
it('should return default when nq or np is missing', () => {
|
||||
expect(validateMachineCurve({ nq: {} }, defaultMC, logger)).toEqual(defaultMC);
|
||||
});
|
||||
|
||||
it('should validate a correct machine curve', () => {
|
||||
const input = {
|
||||
nq: { line1: { x: [1, 2], y: [10, 20] } },
|
||||
np: { line1: { x: [1, 2], y: [5, 10] } },
|
||||
};
|
||||
const result = validateMachineCurve(input, defaultMC, logger);
|
||||
expect(result.nq.line1.x).toEqual([1, 2]);
|
||||
expect(result.np.line1.y).toEqual([5, 10]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// ValidationUtils class
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
describe('ValidationUtils', () => {
|
||||
let vu;
|
||||
|
||||
beforeEach(() => {
|
||||
vu = new ValidationUtils(true, 'error'); // suppress most logging noise
|
||||
});
|
||||
|
||||
// ── constrain() ─────────────────────────────────────────────────────
|
||||
describe('constrain()', () => {
|
||||
it('should return value when within range', () => {
|
||||
expect(vu.constrain(5, 0, 10)).toBe(5);
|
||||
});
|
||||
|
||||
it('should clamp to min when value is below range', () => {
|
||||
expect(vu.constrain(-5, 0, 10)).toBe(0);
|
||||
});
|
||||
|
||||
it('should clamp to max when value is above range', () => {
|
||||
expect(vu.constrain(15, 0, 10)).toBe(10);
|
||||
});
|
||||
|
||||
it('should return min for boundary value equal to min', () => {
|
||||
expect(vu.constrain(0, 0, 10)).toBe(0);
|
||||
});
|
||||
|
||||
it('should return max for boundary value equal to max', () => {
|
||||
expect(vu.constrain(10, 0, 10)).toBe(10);
|
||||
});
|
||||
|
||||
it('should return min when value is not a number', () => {
|
||||
expect(vu.constrain('abc', 0, 10)).toBe(0);
|
||||
});
|
||||
|
||||
it('should return min when value is null', () => {
|
||||
expect(vu.constrain(null, 0, 10)).toBe(0);
|
||||
});
|
||||
|
||||
it('should return min when value is undefined', () => {
|
||||
expect(vu.constrain(undefined, 0, 10)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateSchema() ────────────────────────────────────────────────
|
||||
describe('validateSchema()', () => {
|
||||
it('should use default value when config key is missing', () => {
|
||||
const schema = {
|
||||
speed: { default: 100, rules: { type: 'number' } },
|
||||
};
|
||||
const result = vu.validateSchema({}, schema, 'test');
|
||||
expect(result.speed).toBe(100);
|
||||
});
|
||||
|
||||
it('should use provided value over default', () => {
|
||||
const schema = {
|
||||
speed: { default: 100, rules: { type: 'number' } },
|
||||
};
|
||||
const result = vu.validateSchema({ speed: 200 }, schema, 'test');
|
||||
expect(result.speed).toBe(200);
|
||||
});
|
||||
|
||||
it('should strip unknown keys from config', () => {
|
||||
const schema = {
|
||||
speed: { default: 100, rules: { type: 'number' } },
|
||||
};
|
||||
const config = { speed: 50, unknownKey: 'bad' };
|
||||
const result = vu.validateSchema(config, schema, 'test');
|
||||
expect(result.unknownKey).toBeUndefined();
|
||||
expect(result.speed).toBe(50);
|
||||
});
|
||||
|
||||
it('should validate number type with min/max', () => {
|
||||
const schema = {
|
||||
speed: { default: 10, rules: { type: 'number', min: 0, max: 100 } },
|
||||
};
|
||||
// within range
|
||||
expect(vu.validateSchema({ speed: 50 }, schema, 'test').speed).toBe(50);
|
||||
// below min -> default
|
||||
expect(vu.validateSchema({ speed: -1 }, schema, 'test').speed).toBe(10);
|
||||
// above max -> default
|
||||
expect(vu.validateSchema({ speed: 101 }, schema, 'test').speed).toBe(10);
|
||||
});
|
||||
|
||||
it('should validate boolean type', () => {
|
||||
const schema = {
|
||||
enabled: { default: true, rules: { type: 'boolean' } },
|
||||
};
|
||||
expect(vu.validateSchema({ enabled: false }, schema, 'test').enabled).toBe(false);
|
||||
expect(vu.validateSchema({ enabled: 'true' }, schema, 'test').enabled).toBe(true);
|
||||
});
|
||||
|
||||
it('should validate string type (lowercased)', () => {
|
||||
const schema = {
|
||||
mode: { default: 'auto', rules: { type: 'string' } },
|
||||
};
|
||||
expect(vu.validateSchema({ mode: 'Manual' }, schema, 'test').mode).toBe('manual');
|
||||
});
|
||||
|
||||
it('should validate enum type', () => {
|
||||
const schema = {
|
||||
state: {
|
||||
default: 'open',
|
||||
rules: { type: 'enum', values: [{ value: 'open' }, { value: 'closed' }] },
|
||||
},
|
||||
};
|
||||
expect(vu.validateSchema({ state: 'closed' }, schema, 'test').state).toBe('closed');
|
||||
expect(vu.validateSchema({ state: 'invalid' }, schema, 'test').state).toBe('open');
|
||||
});
|
||||
|
||||
it('should validate integer type', () => {
|
||||
const schema = {
|
||||
count: { default: 5, rules: { type: 'integer', min: 1, max: 100 } },
|
||||
};
|
||||
expect(vu.validateSchema({ count: 10 }, schema, 'test').count).toBe(10);
|
||||
expect(vu.validateSchema({ count: '42' }, schema, 'test').count).toBe(42);
|
||||
});
|
||||
|
||||
it('should validate array type', () => {
|
||||
const schema = {
|
||||
items: { default: [1, 2], rules: { type: 'array', itemType: 'number', minLength: 1 } },
|
||||
};
|
||||
expect(vu.validateSchema({ items: [3, 4, 5] }, schema, 'test').items).toEqual([3, 4, 5]);
|
||||
expect(vu.validateSchema({ items: 'not-array' }, schema, 'test').items).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it('should handle nested object with schema recursively', () => {
|
||||
const schema = {
|
||||
logging: {
|
||||
rules: { type: 'object', schema: {
|
||||
enabled: { default: true, rules: { type: 'boolean' } },
|
||||
level: { default: 'info', rules: { type: 'string' } },
|
||||
}},
|
||||
},
|
||||
};
|
||||
const result = vu.validateSchema(
|
||||
{ logging: { enabled: false, level: 'Debug' } },
|
||||
schema,
|
||||
'test'
|
||||
);
|
||||
expect(result.logging.enabled).toBe(false);
|
||||
expect(result.logging.level).toBe('debug');
|
||||
});
|
||||
|
||||
it('should skip reserved keys (rules, description, schema)', () => {
|
||||
const schema = {
|
||||
rules: 'should be skipped',
|
||||
description: 'should be skipped',
|
||||
schema: 'should be skipped',
|
||||
speed: { default: 10, rules: { type: 'number' } },
|
||||
};
|
||||
const result = vu.validateSchema({}, schema, 'test');
|
||||
expect(result).not.toHaveProperty('rules');
|
||||
expect(result).not.toHaveProperty('description');
|
||||
expect(result).not.toHaveProperty('schema');
|
||||
expect(result.speed).toBe(10);
|
||||
});
|
||||
|
||||
it('should use default for unknown validation type', () => {
|
||||
const schema = {
|
||||
weird: { default: 'fallback', rules: { type: 'unknownType' } },
|
||||
};
|
||||
const result = vu.validateSchema({ weird: 'value' }, schema, 'test');
|
||||
expect(result.weird).toBe('fallback');
|
||||
});
|
||||
|
||||
it('should handle curve type', () => {
|
||||
const schema = {
|
||||
curve: {
|
||||
default: { line1: { x: [0, 1], y: [0, 1] } },
|
||||
rules: { type: 'curve' },
|
||||
},
|
||||
};
|
||||
const validCurve = { line1: { x: [1, 2], y: [10, 20] } };
|
||||
const result = vu.validateSchema({ curve: validCurve }, schema, 'test');
|
||||
expect(result.curve.line1.x).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
// ── removeUnwantedKeys() ────────────────────────────────────────────
|
||||
describe('removeUnwantedKeys()', () => {
|
||||
it('should remove rules and description keys', () => {
|
||||
const input = {
|
||||
speed: { default: 10, rules: { type: 'number' }, description: 'Speed setting' },
|
||||
};
|
||||
const result = vu.removeUnwantedKeys(input);
|
||||
expect(result.speed).toBe(10);
|
||||
});
|
||||
|
||||
it('should recurse into nested objects', () => {
|
||||
const input = {
|
||||
logging: {
|
||||
enabled: { default: true, rules: {} },
|
||||
level: { default: 'info', description: 'Log level' },
|
||||
},
|
||||
};
|
||||
const result = vu.removeUnwantedKeys(input);
|
||||
expect(result.logging.enabled).toBe(true);
|
||||
expect(result.logging.level).toBe('info');
|
||||
});
|
||||
|
||||
it('should handle arrays', () => {
|
||||
const input = [
|
||||
{ a: { default: 1, rules: {} } },
|
||||
{ b: { default: 2, description: 'x' } },
|
||||
];
|
||||
const result = vu.removeUnwantedKeys(input);
|
||||
expect(result[0].a).toBe(1);
|
||||
expect(result[1].b).toBe(2);
|
||||
});
|
||||
|
||||
it('should return primitives as-is', () => {
|
||||
expect(vu.removeUnwantedKeys(42)).toBe(42);
|
||||
expect(vu.removeUnwantedKeys('hello')).toBe('hello');
|
||||
expect(vu.removeUnwantedKeys(null)).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user