refactor: adopt POSITIONS constants, fix ESLint warnings, break menuUtils into modules

- Replace hardcoded position strings with POSITIONS.* constants
- Prefix unused variables with _ to resolve no-unused-vars warnings
- Fix no-prototype-builtins with Object.prototype.hasOwnProperty.call()
- Extract menuUtils.js (543 lines) into 6 focused modules under menu/
- menuUtils.js now 35 lines, delegates via prototype mixin pattern
- Add 158 unit tests for ConfigManager, MeasurementContainer, ValidationUtils

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Rene De Ren
2026-03-11 15:35:28 +01:00
parent fe2631f29b
commit dec5f63b21
25 changed files with 1753 additions and 587 deletions

View File

@@ -1,5 +1,4 @@
var metric var metric;
, imperial;
metric = { metric = {
ea: { ea: {

View File

@@ -1,5 +1,4 @@
var metric var metric;
, imperial;
metric = { metric = {
ppm: { ppm: {

View File

@@ -249,7 +249,7 @@ Converter.prototype.list = function (measure) {
Converter.prototype.throwUnsupportedUnitError = function (what) { Converter.prototype.throwUnsupportedUnitError = function (what) {
var validUnits = []; var validUnits = [];
each(measures, function (systems, measure) { each(measures, function (systems, _measure) {
each(systems, function (units, system) { each(systems, function (units, system) {
if(system == '_anchors') if(system == '_anchors')
return false; return false;

View File

@@ -7,7 +7,6 @@
* Available under MIT license <http://lodash.com/license> * Available under MIT license <http://lodash.com/license>
*/ */
var isObject = require('./../lodash.isobject'), var isObject = require('./../lodash.isobject'),
noop = require('./../lodash.noop'),
reNative = require('./../lodash._renative'); reNative = require('./../lodash._renative');
/* Native method shortcuts for methods with the same name as other `lodash` methods */ /* Native method shortcuts for methods with the same name as other `lodash` methods */
@@ -21,7 +20,7 @@ var nativeCreate = reNative.test(nativeCreate = Object.create) && nativeCreate;
* @param {Object} prototype The object to inherit from. * @param {Object} prototype The object to inherit from.
* @returns {Object} Returns the new object. * @returns {Object} Returns the new object.
*/ */
function baseCreate(prototype, properties) { // eslint-disable-line no-func-assign function baseCreate(prototype, _properties) { // eslint-disable-line no-func-assign
return isObject(prototype) ? nativeCreate(prototype) : {}; return isObject(prototype) ? nativeCreate(prototype) : {};
} }
// fallback for browsers without `Object.create` // fallback for browsers without `Object.create`

View File

@@ -47,7 +47,7 @@ function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, ar
var isBind = bitmask & 1, var isBind = bitmask & 1,
isBindKey = bitmask & 2, isBindKey = bitmask & 2,
isCurry = bitmask & 4, isCurry = bitmask & 4,
isCurryBound = bitmask & 8, /* isCurryBound = bitmask & 8, */
isPartial = bitmask & 16, isPartial = bitmask & 16,
isPartialRight = bitmask & 32; isPartialRight = bitmask & 32;

View File

@@ -7,7 +7,6 @@
* Available under MIT license <http://lodash.com/license> * Available under MIT license <http://lodash.com/license>
*/ */
var createWrapper = require('./../lodash._createwrapper'), var createWrapper = require('./../lodash._createwrapper'),
reNative = require('./../lodash._renative'),
slice = require('./../lodash._slice'); slice = require('./../lodash._slice');
/** /**

View File

@@ -5,7 +5,7 @@ class ChildRegistrationUtils {
this.registeredChildren = new Map(); this.registeredChildren = new Map();
} }
async registerChild(child, positionVsParent, distance) { async registerChild(child, positionVsParent, _distance) {
const softwareType = (child.config.functionality.softwareType || '').toLowerCase(); const softwareType = (child.config.functionality.softwareType || '').toLowerCase();
const { name, id } = child.config.general; const { name, id } = child.config.general;

View File

@@ -76,7 +76,7 @@ class ConfigUtils {
// loop through objects and merge them obj1 will be updated with obj2 values // loop through objects and merge them obj1 will be updated with obj2 values
mergeObjects(obj1, obj2) { mergeObjects(obj1, obj2) {
for (let key in obj2) { for (let key in obj2) {
if (obj2.hasOwnProperty(key)) { if (Object.prototype.hasOwnProperty.call(obj2, key)) {
if (typeof obj2[key] === 'object') { if (typeof obj2[key] === 'object') {
if (!obj1[key]) { if (!obj1[key]) {
obj1[key] = {}; obj1[key] = {};

View File

@@ -0,0 +1,123 @@
/**
* Data fetching methods for MenuUtils.
* Handles primary/fallback URL fetching and API calls.
*/
const dataFetching = {
async fetchData(url, fallbackUrl) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const responsData = await response.json();
//responsData
const data = responsData.data;
/* .map(item => {
const { vendor_name, ...rest } = item;
return {
name: vendor_name,
...rest
};
}); */
console.log(url);
console.log("Response Data: ", data);
return data;
} catch (err) {
console.warn(
`Primary URL failed: ${url}. Trying fallback URL: ${fallbackUrl}`,
err
);
try {
const response = await fetch(fallbackUrl);
if (!response.ok)
throw new Error(`HTTP error! status: ${response.status}`);
return await response.json();
} catch (fallbackErr) {
console.error("Both primary and fallback URLs failed:", fallbackErr);
return [];
}
}
},
async fetchProjectData(url) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const responsData = await response.json();
console.log("Response Data: ", responsData);
return responsData;
} catch (err) {
/* intentionally empty */
}
},
// Save changes to API
async apiCall(node) {
try{
// OLFIANT when a browser refreshes the tag code is lost!!! fix this later!!!!!
// FIX UUID ALSO LATER
if(node.assetTagCode !== "" || node.assetTagCode !== null){ /* intentionally empty */ }
// API call to register or check asset in central database
let assetregisterAPI = node.configUrls.cloud.taggcodeAPI + "/asset/create_asset.php";
const assetModelId = node.modelMetadata.id; //asset_product_model_id
const uuid = node.uuid; //asset_product_model_uuid
const assetName = node.assetType; //asset_name / type?
const description = node.name; // asset_description
const assetStatus = "actief"; //asset_status -> koppel aan enable / disable node ? or make dropdown ?
const assetProfileId = 1; //asset_profile_id these are the rules to check if the childs are valid under this node (parent / child id?)
const child_assets = ["63247"]; //child_assets tagnummer of id?
const assetProcessId = node.processId; //asset_process_id
const assetLocationId = node.locationId; //asset_location_id
const tagCode = node.assetTagCode; // if already exists in the node information use it to tell the api it exists and it will update else we will get it from the api call
//console.log(`this is my tagCode: ${tagCode}`);
// Build base URL with required parameters
let apiUrl = `?asset_product_model_id=${assetModelId}&asset_product_model_uuid=${uuid}&asset_name=${assetName}&asset_description=${description}&asset_status=${assetStatus}&asset_profile_id=${assetProfileId}&asset_location_id=${assetLocationId}&asset_process_id=${assetProcessId}&child_assets=${child_assets}`;
// Only add tagCode to URL if it exists
if (tagCode) {
apiUrl += `&asset_tag_number=${tagCode}`;
console.log('hello there');
}
assetregisterAPI += apiUrl;
console.log("API call to register asset in central database", assetregisterAPI);
const response = await fetch(assetregisterAPI, {
method: "POST"
});
// Get the response text first
const responseText = await response.text();
console.log("Raw API response:", responseText);
// Try to parse the JSON, handling potential parsing errors
let jsonResponse;
try {
jsonResponse = JSON.parse(responseText);
} catch (parseError) {
console.error("JSON Parsing Error:", parseError);
console.error("Response that could not be parsed:", responseText);
throw new Error("Failed to parse API response");
}
console.log(jsonResponse);
if(jsonResponse.success){
console.log(`${jsonResponse.message}, tag number: ${jsonResponse.asset_tag_number}, asset id: ${jsonResponse.asset_id}`);
// Save the asset tag number and id to the node
} else {
console.log("Asset not registered in central database");
}
return jsonResponse;
} catch (error) {
console.log("Error saving changes to asset register API", error);
}
},
};
module.exports = dataFetching;

View File

@@ -0,0 +1,283 @@
/**
* Dropdown population methods for MenuUtils.
* Handles populating and cascading dropdown menus for assets, suppliers, models, units, etc.
*/
const dropdownPopulation = {
populateSmoothingMethods(configUrls, elements, node) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const smoothingMethods =
configData.smoothing?.smoothMethod?.rules?.values?.map(
(o) => o.value
) || [];
this.populateDropdown(
elements.smoothMethod,
smoothingMethods,
node,
"smooth_method"
);
})
.catch((err) => {
console.error("Error loading smoothing methods", err);
});
},
populateInterpolationMethods(configUrls, elements, node) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const interpolationMethods =
configData?.interpolation?.type?.rules?.values.map((m) => m.value) ||
[];
this.populateDropdown(
elements.interpolationMethodInput,
interpolationMethods,
node,
"interpolationMethod"
);
// Find the selected method and use it to spawn 1 more field to fill in tension
//const selectedMethod = interpolationMethods.find(m => m === node.interpolationMethod);
this.initTensionToggles(elements, node);
})
.catch((err) => {
console.error("Error loading interpolation methods", err);
});
},
populateLogLevelOptions(logLevelSelect, configData, node) {
// debug log level
//console.log("Displaying configData => ", configData) ;
const logLevels =
configData?.general?.logging?.logLevel?.rules?.values?.map(
(l) => l.value
) || [];
//console.log("Displaying logLevels => ", logLevels);
// Reuse your existing generic populateDropdown helper
this.populateDropdown(logLevelSelect, logLevels, node.logLevel);
},
//cascade dropdowns for asset type, supplier, subType, model, unit
fetchAndPopulateDropdowns(configUrls, elements, node) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const assetType = configData.asset?.type?.default;
const localSuppliersUrl = this.constructUrl(configUrls.local.taggcodeAPI,`${assetType}s`,"suppliers.json");
const cloudSuppliersUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/vendor/get_vendors.php");
return this.fetchData(cloudSuppliersUrl, localSuppliersUrl)
.then((supplierData) => {
const suppliers = supplierData.map((supplier) => supplier.name);
// Populate suppliers dropdown and set up its change handler
return this.populateDropdown(
elements.supplier,
suppliers,
node,
"supplier",
function (selectedSupplier) {
if (selectedSupplier) {
this.populateSubTypes(configUrls, elements, node, selectedSupplier);
}
}
);
})
.then(() => {
// If we have a saved supplier, trigger subTypes population
if (node.supplier) {
this.populateSubTypes(configUrls, elements, node, node.supplier);
}
});
})
.catch((error) => {
console.error("Error in initial dropdown population:", error);
});
},
populateSubTypes(configUrls, elements, node, selectedSupplier) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const assetType = configData.asset?.type?.default;
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI, `${assetType}s`, selectedSupplier );
const localSubTypesUrl = this.constructUrl(supplierFolder, "subtypes.json");
const cloudSubTypesUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_subtypesFromVendor.php?vendor_name=" + selectedSupplier);
return this.fetchData(cloudSubTypesUrl, localSubTypesUrl)
.then((subTypeData) => {
const subTypes = subTypeData.map((subType) => subType.name);
return this.populateDropdown(
elements.subType,
subTypes,
node,
"subType",
function (selectedSubType) {
if (selectedSubType) {
// When subType changes, update both models and units
this.populateModels(
configUrls,
elements,
node,
selectedSupplier,
selectedSubType
);
this.populateUnitsForSubType(
configUrls,
elements,
node,
selectedSubType
);
}
}
);
})
.then(() => {
// If we have a saved subType, trigger both models and units population
if (node.subType) {
this.populateModels(
configUrls,
elements,
node,
selectedSupplier,
node.subType
);
this.populateUnitsForSubType(configUrls, elements, node, node.subType);
}
//console.log("In fetch part of subtypes ");
// Store all data from selected model
/* node["modelMetadata"] = modelData.find(
(model) => model.name === node.model
);
console.log("Model Metadata: ", node["modelMetadata"]); */
});
})
.catch((error) => {
console.error("Error populating subtypes:", error);
});
},
populateUnitsForSubType(configUrls, elements, node, selectedSubType) {
// Fetch the units data
this.fetchData(configUrls.cloud.units, configUrls.local.units)
.then((unitsData) => {
// Find the category that matches the subType name
const categoryData = unitsData.units.find(
(category) =>
category.category.toLowerCase() === selectedSubType.toLowerCase()
);
if (categoryData) {
// Extract just the unit values and descriptions
const units = categoryData.values.map((unit) => ({
value: unit.value,
description: unit.description,
}));
// Create the options array with descriptions as labels
const options = units.map((unit) => ({
value: unit.value,
label: `${unit.value} - ${unit.description}`,
}));
// Populate the units dropdown
this.populateDropdown(
elements.unit,
options.map((opt) => opt.value),
node,
"unit"
);
// If there's no currently selected unit but we have options, select the first one
if (!node.unit && options.length > 0) {
node.unit = options[0].value;
elements.unit.value = options[0].value;
}
} else {
// If no matching category is found, provide a default % option
const defaultUnits = [{ value: "%", description: "Percentage" }];
this.populateDropdown(
elements.unit,
defaultUnits.map((unit) => unit.value),
node,
"unit"
);
console.warn(
`No matching unit category found for subType: ${selectedSubType}`
);
}
})
.catch((error) => {
console.error("Error fetching units:", error);
});
},
populateModels(
configUrls,
elements,
node,
selectedSupplier,
selectedSubType
) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const assetType = configData.asset?.type?.default;
// save assetType to fetch later
node.assetType = assetType;
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI,`${assetType}s`,selectedSupplier);
const subTypeFolder = this.constructUrl(supplierFolder, selectedSubType);
const localModelsUrl = this.constructUrl(subTypeFolder, "models.json");
const cloudModelsUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_product_models.php?vendor_name=" + selectedSupplier + "&product_subtype_name=" + selectedSubType);
return this.fetchData(cloudModelsUrl, localModelsUrl).then((modelData) => {
const models = modelData.map((model) => model.name); // use this to populate the dropdown
// If a model is already selected, store its metadata immediately
if (node.model) {
node["modelMetadata"] = modelData.find((model) => model.name === node.model);
}
this.populateDropdown(elements.model, models, node, "model", (selectedModel) => {
// Store only the metadata for the selected model
node["modelMetadata"] = modelData.find((model) => model.name === selectedModel);
});
/*
console.log('hello here I am:');
console.log(node["modelMetadata"]);
*/
});
})
.catch((error) => {
console.error("Error populating models:", error);
});
},
async populateDropdown(
htmlElement,
options,
node,
property,
callback
) {
this.generateHtml(htmlElement, options, node[property]);
htmlElement.addEventListener("change", async (e) => {
const newValue = e.target.value;
console.log(`Dropdown changed: ${property} = ${newValue}`);
node[property] = newValue;
RED.nodes.dirty(true);
if (callback) await callback(newValue); // Ensure async callback completion
});
},
};
module.exports = dropdownPopulation;

View File

@@ -0,0 +1,73 @@
/**
* HTML generation and endpoint methods for MenuUtils.
* Handles generating dropdown HTML and serving MenuUtils code to the browser.
*/
const htmlGeneration = {
generateHtml(htmlElement, options, savedValue) {
htmlElement.innerHTML = options.length
? `<option value="">Select...</option>${options
.map((opt) => `<option value="${opt}">${opt}</option>`)
.join("")}`
: "<option value=''>No options available</option>";
if (savedValue && options.includes(savedValue)) {
htmlElement.value = savedValue;
}
},
createMenuUtilsEndpoint(RED, nodeName, customHelpers = {}) {
RED.httpAdmin.get(`/${nodeName}/resources/menuUtils.js`, function(req, res) {
console.log(`Serving menuUtils.js for ${nodeName} node`);
res.set('Content-Type', 'application/javascript');
const browserCode = this.generateMenuUtilsCode(nodeName, customHelpers);
res.send(browserCode);
}.bind(this));
},
generateMenuUtilsCode(nodeName, customHelpers = {}) {
const defaultHelpers = {
validateRequired: `function(value) {
return value && value.toString().trim() !== '';
}`,
formatDisplayValue: `function(value, unit) {
return \`\${value} \${unit || ''}\`.trim();
}`
};
const allHelpers = { ...defaultHelpers, ...customHelpers };
const helpersCode = Object.entries(allHelpers)
.map(([name, func]) => ` ${name}: ${func}`)
.join(',\n');
const classCode = this.constructor.toString(); // <-- this gives full class MenuUtils {...}
return `
// Create EVOLV namespace structure
window.EVOLV = window.EVOLV || {};
window.EVOLV.nodes = window.EVOLV.nodes || {};
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
// Inject MenuUtils class
${classCode}
// Expose MenuUtils instance to namespace
window.EVOLV.nodes.${nodeName}.utils = {
menuUtils: new MenuUtils(),
helpers: {
${helpersCode}
}
};
// Optionally expose globally
window.MenuUtils = MenuUtils;
console.log('${nodeName} utilities loaded in namespace');
`;
},
};
module.exports = htmlGeneration;

18
src/helper/menu/index.js Normal file
View File

@@ -0,0 +1,18 @@
/**
* menu/index.js
* Barrel file for the menu module components.
*/
const toggles = require('./toggles');
const dataFetching = require('./dataFetching');
const urlUtils = require('./urlUtils');
const dropdownPopulation = require('./dropdownPopulation');
const htmlGeneration = require('./htmlGeneration');
module.exports = {
toggles,
dataFetching,
urlUtils,
dropdownPopulation,
htmlGeneration,
};

View File

@@ -0,0 +1,56 @@
/**
* Toggle initialization methods for MenuUtils.
* Controls visibility of UI elements based on checkbox/dropdown state.
*/
const toggles = {
initBasicToggles(elements) {
// Toggle visibility for log level
elements.logCheckbox.addEventListener("change", function () {
elements.rowLogLevel.style.display = this.checked ? "block" : "none";
});
elements.rowLogLevel.style.display = elements.logCheckbox.checked
? "block"
: "none";
},
// Define the initialize toggles function within scope
initMeasurementToggles(elements) {
// Toggle visibility for scaling inputs
elements.scalingCheckbox.addEventListener("change", function () {
elements.rowInputMin.style.display = this.checked ? "block" : "none";
elements.rowInputMax.style.display = this.checked ? "block" : "none";
});
// Set initial states
elements.rowInputMin.style.display = elements.scalingCheckbox.checked
? "block"
: "none";
elements.rowInputMax.style.display = elements.scalingCheckbox.checked
? "block"
: "none";
},
initTensionToggles(elements, node) {
const currentMethod = node.interpolationMethod;
elements.rowTension.style.display =
currentMethod === "monotone_cubic_spline" ? "block" : "none";
console.log(
"Initial tension row display: ",
elements.rowTension.style.display
);
elements.interpolationMethodInput.addEventListener("change", function () {
const selectedMethod = this.value;
console.log(`Interpolation method changed: ${selectedMethod}`);
node.interpolationMethod = selectedMethod;
// Toggle visibility for tension input
elements.rowTension.style.display =
selectedMethod === "monotone_cubic_spline" ? "block" : "none";
console.log("Tension row display: ", elements.rowTension.style.display);
});
},
};
module.exports = toggles;

View File

@@ -0,0 +1,39 @@
/**
* URL construction methods for MenuUtils.
* Helpers for building API and config URLs.
*/
const urlUtils = {
getSpecificConfigUrl(nodeName, cloudAPI) {
const cloudConfigURL = cloudAPI + "/config/" + nodeName + ".json";
const localConfigURL = "http://localhost:1880/"+ nodeName + "/dependencies/"+ nodeName + "/" + nodeName + "Config.json";
return { cloudConfigURL, localConfigURL };
},
// Helper function to construct a URL from a base and path internal
constructUrl(base, ...paths) {
// Remove trailing slash from base and leading slashes from paths
const sanitizedBase = (base || "").replace(/\/+$/, "");
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
// Join sanitized base and paths
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
console.log("Base:", sanitizedBase);
console.log("Paths:", sanitizedPaths);
console.log("Constructed URL:", url);
return url;
},
//Adjust for API Gateway
constructCloudURL(base, ...paths) {
// Remove trailing slash from base and leading slashes from paths
const sanitizedBase = base.replace(/\/+$/, "");
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
// Join sanitized base and paths
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
return url;
},
};
module.exports = urlUtils;

View File

@@ -1,544 +1,34 @@
/**
* MenuUtils — UI menu helper for Node-RED editor.
* Methods are split across focused modules under ./menu/ and mixed onto the prototype.
*/
const toggles = require('./menu/toggles');
const dataFetching = require('./menu/dataFetching');
const urlUtils = require('./menu/urlUtils');
const dropdownPopulation = require('./menu/dropdownPopulation');
const htmlGeneration = require('./menu/htmlGeneration');
class MenuUtils { class MenuUtils {
constructor() {
this.isCloud = false;
initBasicToggles(elements) { this.configData = null;
// Toggle visibility for log level
elements.logCheckbox.addEventListener("change", function () {
elements.rowLogLevel.style.display = this.checked ? "block" : "none";
});
elements.rowLogLevel.style.display = elements.logCheckbox.checked
? "block"
: "none";
}
// Define the initialize toggles function within scope
initMeasurementToggles(elements) {
// Toggle visibility for scaling inputs
elements.scalingCheckbox.addEventListener("change", function () {
elements.rowInputMin.style.display = this.checked ? "block" : "none";
elements.rowInputMax.style.display = this.checked ? "block" : "none";
});
// Set initial states
elements.rowInputMin.style.display = elements.scalingCheckbox.checked
? "block"
: "none";
elements.rowInputMax.style.display = elements.scalingCheckbox.checked
? "block"
: "none";
}
initTensionToggles(elements, node) {
const currentMethod = node.interpolationMethod;
elements.rowTension.style.display =
currentMethod === "monotone_cubic_spline" ? "block" : "none";
console.log(
"Initial tension row display: ",
elements.rowTension.style.display
);
elements.interpolationMethodInput.addEventListener("change", function () {
const selectedMethod = this.value;
console.log(`Interpolation method changed: ${selectedMethod}`);
node.interpolationMethod = selectedMethod;
// Toggle visibility for tension input
elements.rowTension.style.display =
selectedMethod === "monotone_cubic_spline" ? "block" : "none";
console.log("Tension row display: ", elements.rowTension.style.display);
});
}
// Define the smoothing methods population function within scope
populateSmoothingMethods(configUrls, elements, node) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const smoothingMethods =
configData.smoothing?.smoothMethod?.rules?.values?.map(
(o) => o.value
) || [];
this.populateDropdown(
elements.smoothMethod,
smoothingMethods,
node,
"smooth_method"
);
})
.catch((err) => {
console.error("Error loading smoothing methods", err);
});
}
populateInterpolationMethods(configUrls, elements, node) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const interpolationMethods =
configData?.interpolation?.type?.rules?.values.map((m) => m.value) ||
[];
this.populateDropdown(
elements.interpolationMethodInput,
interpolationMethods,
node,
"interpolationMethod"
);
// Find the selected method and use it to spawn 1 more field to fill in tension
//const selectedMethod = interpolationMethods.find(m => m === node.interpolationMethod);
this.initTensionToggles(elements, node);
})
.catch((err) => {
console.error("Error loading interpolation methods", err);
});
}
populateLogLevelOptions(logLevelSelect, configData, node) {
// debug log level
//console.log("Displaying configData => ", configData) ;
const logLevels =
configData?.general?.logging?.logLevel?.rules?.values?.map(
(l) => l.value
) || [];
//console.log("Displaying logLevels => ", logLevels);
// Reuse your existing generic populateDropdown helper
this.populateDropdown(logLevelSelect, logLevels, node.logLevel);
}
//cascade dropdowns for asset type, supplier, subType, model, unit
fetchAndPopulateDropdowns(configUrls, elements, node) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const assetType = configData.asset?.type?.default;
const localSuppliersUrl = this.constructUrl(configUrls.local.taggcodeAPI,`${assetType}s`,"suppliers.json");
const cloudSuppliersUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/vendor/get_vendors.php");
return this.fetchData(cloudSuppliersUrl, localSuppliersUrl)
.then((supplierData) => {
const suppliers = supplierData.map((supplier) => supplier.name);
// Populate suppliers dropdown and set up its change handler
return this.populateDropdown(
elements.supplier,
suppliers,
node,
"supplier",
function (selectedSupplier) {
if (selectedSupplier) {
this.populateSubTypes(configUrls, elements, node, selectedSupplier);
}
}
);
})
.then(() => {
// If we have a saved supplier, trigger subTypes population
if (node.supplier) {
this.populateSubTypes(configUrls, elements, node, node.supplier);
}
});
})
.catch((error) => {
console.error("Error in initial dropdown population:", error);
});
}
getSpecificConfigUrl(nodeName,cloudAPI) {
const cloudConfigURL = cloudAPI + "/config/" + nodeName + ".json";
const localConfigURL = "http://localhost:1880/"+ nodeName + "/dependencies/"+ nodeName + "/" + nodeName + "Config.json";
return { cloudConfigURL, localConfigURL };
}
// Save changes to API
async apiCall(node) {
try{
// OLFIANT when a browser refreshes the tag code is lost!!! fix this later!!!!!
// FIX UUID ALSO LATER
if(node.assetTagCode !== "" || node.assetTagCode !== null){ /* intentionally empty */ }
// API call to register or check asset in central database
let assetregisterAPI = node.configUrls.cloud.taggcodeAPI + "/asset/create_asset.php";
const assetModelId = node.modelMetadata.id; //asset_product_model_id
const uuid = node.uuid; //asset_product_model_uuid
const assetName = node.assetType; //asset_name / type?
const description = node.name; // asset_description
const assetStatus = "actief"; //asset_status -> koppel aan enable / disable node ? or make dropdown ?
const assetProfileId = 1; //asset_profile_id these are the rules to check if the childs are valid under this node (parent / child id?)
const child_assets = ["63247"]; //child_assets tagnummer of id?
const assetProcessId = node.processId; //asset_process_id
const assetLocationId = node.locationId; //asset_location_id
const tagCode = node.assetTagCode; // if already exists in the node information use it to tell the api it exists and it will update else we will get it from the api call
//console.log(`this is my tagCode: ${tagCode}`);
// Build base URL with required parameters
let apiUrl = `?asset_product_model_id=${assetModelId}&asset_product_model_uuid=${uuid}&asset_name=${assetName}&asset_description=${description}&asset_status=${assetStatus}&asset_profile_id=${assetProfileId}&asset_location_id=${assetLocationId}&asset_process_id=${assetProcessId}&child_assets=${child_assets}`;
// Only add tagCode to URL if it exists
if (tagCode) {
apiUrl += `&asset_tag_number=${tagCode}`;
console.log('hello there');
}
assetregisterAPI += apiUrl;
console.log("API call to register asset in central database", assetregisterAPI);
const response = await fetch(assetregisterAPI, {
method: "POST"
});
// Get the response text first
const responseText = await response.text();
console.log("Raw API response:", responseText);
// Try to parse the JSON, handling potential parsing errors
let jsonResponse;
try {
jsonResponse = JSON.parse(responseText);
} catch (parseError) {
console.error("JSON Parsing Error:", parseError);
console.error("Response that could not be parsed:", responseText);
throw new Error("Failed to parse API response");
}
console.log(jsonResponse);
if(jsonResponse.success){
console.log(`${jsonResponse.message}, tag number: ${jsonResponse.asset_tag_number}, asset id: ${jsonResponse.asset_id}`);
// Save the asset tag number and id to the node
} else {
console.log("Asset not registered in central database");
}
return jsonResponse;
} catch (error) {
console.log("Error saving changes to asset register API", error);
}
}
async fetchData(url, fallbackUrl) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const responsData = await response.json();
//responsData
const data = responsData.data;
/* .map(item => {
const { vendor_name, ...rest } = item;
return {
name: vendor_name,
...rest
};
}); */
console.log(url);
console.log("Response Data: ", data);
return data;
} catch (err) {
console.warn(
`Primary URL failed: ${url}. Trying fallback URL: ${fallbackUrl}`,
err
);
try {
const response = await fetch(fallbackUrl);
if (!response.ok)
throw new Error(`HTTP error! status: ${response.status}`);
return await response.json();
} catch (fallbackErr) {
console.error("Both primary and fallback URLs failed:", fallbackErr);
return [];
}
} }
} }
async fetchProjectData(url) { // Mix all method groups onto the prototype
try { const mixins = [toggles, dataFetching, urlUtils, dropdownPopulation, htmlGeneration];
const response = await fetch(url); for (const mixin of mixins) {
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); for (const [name, fn] of Object.entries(mixin)) {
const responsData = await response.json(); if (typeof fn === 'function') {
console.log("Response Data: ", responsData); Object.defineProperty(MenuUtils.prototype, name, {
return responsData; value: fn,
writable: true,
} catch (err) { configurable: true,
/* intentionally empty */ enumerable: false,
}
}
async populateDropdown(
htmlElement,
options,
node,
property,
callback
) {
this.generateHtml(htmlElement, options, node[property]);
htmlElement.addEventListener("change", async (e) => {
const newValue = e.target.value;
console.log(`Dropdown changed: ${property} = ${newValue}`);
node[property] = newValue;
RED.nodes.dirty(true);
if (callback) await callback(newValue); // Ensure async callback completion
});
}
// Helper function to construct a URL from a base and path internal
constructUrl(base, ...paths) {
// Remove trailing slash from base and leading slashes from paths
const sanitizedBase = (base || "").replace(/\/+$/, "");
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
// Join sanitized base and paths
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
console.log("Base:", sanitizedBase);
console.log("Paths:", sanitizedPaths);
console.log("Constructed URL:", url);
return url;
}
//Adjust for API Gateway
constructCloudURL(base, ...paths) {
// Remove trailing slash from base and leading slashes from paths
const sanitizedBase = base.replace(/\/+$/, "");
const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
// Join sanitized base and paths
const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
return url;
}
populateSubTypes(configUrls, elements, node, selectedSupplier) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const assetType = configData.asset?.type?.default;
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI, `${assetType}s`, selectedSupplier );
const localSubTypesUrl = this.constructUrl(supplierFolder, "subtypes.json");
const cloudSubTypesUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_subtypesFromVendor.php?vendor_name=" + selectedSupplier);
return this.fetchData(cloudSubTypesUrl, localSubTypesUrl)
.then((subTypeData) => {
const subTypes = subTypeData.map((subType) => subType.name);
return this.populateDropdown(
elements.subType,
subTypes,
node,
"subType",
function (selectedSubType) {
if (selectedSubType) {
// When subType changes, update both models and units
this.populateModels(
configUrls,
elements,
node,
selectedSupplier,
selectedSubType
);
this.populateUnitsForSubType(
configUrls,
elements,
node,
selectedSubType
);
}
}
);
})
.then(() => {
// If we have a saved subType, trigger both models and units population
if (node.subType) {
this.populateModels(
configUrls,
elements,
node,
selectedSupplier,
node.subType
);
this.populateUnitsForSubType(configUrls, elements, node, node.subType);
}
//console.log("In fetch part of subtypes ");
// Store all data from selected model
/* node["modelMetadata"] = modelData.find(
(model) => model.name === node.model
);
console.log("Model Metadata: ", node["modelMetadata"]); */
});
})
.catch((error) => {
console.error("Error populating subtypes:", error);
});
}
populateUnitsForSubType(configUrls, elements, node, selectedSubType) {
// Fetch the units data
this.fetchData(configUrls.cloud.units, configUrls.local.units)
.then((unitsData) => {
// Find the category that matches the subType name
const categoryData = unitsData.units.find(
(category) =>
category.category.toLowerCase() === selectedSubType.toLowerCase()
);
if (categoryData) {
// Extract just the unit values and descriptions
const units = categoryData.values.map((unit) => ({
value: unit.value,
description: unit.description,
}));
// Create the options array with descriptions as labels
const options = units.map((unit) => ({
value: unit.value,
label: `${unit.value} - ${unit.description}`,
}));
// Populate the units dropdown
this.populateDropdown(
elements.unit,
options.map((opt) => opt.value),
node,
"unit"
);
// If there's no currently selected unit but we have options, select the first one
if (!node.unit && options.length > 0) {
node.unit = options[0].value;
elements.unit.value = options[0].value;
}
} else {
// If no matching category is found, provide a default % option
const defaultUnits = [{ value: "%", description: "Percentage" }];
this.populateDropdown(
elements.unit,
defaultUnits.map((unit) => unit.value),
node,
"unit"
);
console.warn(
`No matching unit category found for subType: ${selectedSubType}`
);
}
})
.catch((error) => {
console.error("Error fetching units:", error);
});
}
populateModels(
configUrls,
elements,
node,
selectedSupplier,
selectedSubType
) {
this.fetchData(configUrls.cloud.config, configUrls.local.config)
.then((configData) => {
const assetType = configData.asset?.type?.default;
// save assetType to fetch later
node.assetType = assetType;
const supplierFolder = this.constructUrl( configUrls.local.taggcodeAPI,`${assetType}s`,selectedSupplier);
const subTypeFolder = this.constructUrl(supplierFolder, selectedSubType);
const localModelsUrl = this.constructUrl(subTypeFolder, "models.json");
const cloudModelsUrl = this.constructCloudURL(configUrls.cloud.taggcodeAPI, "/product/get_product_models.php?vendor_name=" + selectedSupplier + "&product_subtype_name=" + selectedSubType);
return this.fetchData(cloudModelsUrl, localModelsUrl).then((modelData) => {
const models = modelData.map((model) => model.name); // use this to populate the dropdown
// If a model is already selected, store its metadata immediately
if (node.model) {
node["modelMetadata"] = modelData.find((model) => model.name === node.model);
}
this.populateDropdown(elements.model, models, node, "model", (selectedModel) => {
// Store only the metadata for the selected model
node["modelMetadata"] = modelData.find((model) => model.name === selectedModel);
});
/*
console.log('hello here I am:');
console.log(node["modelMetadata"]);
*/
}); });
}
})
.catch((error) => {
console.error("Error populating models:", error);
});
}
generateHtml(htmlElement, options, savedValue) {
htmlElement.innerHTML = options.length
? `<option value="">Select...</option>${options
.map((opt) => `<option value="${opt}">${opt}</option>`)
.join("")}`
: "<option value=''>No options available</option>";
if (savedValue && options.includes(savedValue)) {
htmlElement.value = savedValue;
} }
} }
createMenuUtilsEndpoint(RED, nodeName, customHelpers = {}) {
RED.httpAdmin.get(`/${nodeName}/resources/menuUtils.js`, function(req, res) {
console.log(`Serving menuUtils.js for ${nodeName} node`);
res.set('Content-Type', 'application/javascript');
const browserCode = this.generateMenuUtilsCode(nodeName, customHelpers);
res.send(browserCode);
}.bind(this));
}
generateMenuUtilsCode(nodeName, customHelpers = {}) {
const defaultHelpers = {
validateRequired: `function(value) {
return value && value.toString().trim() !== '';
}`,
formatDisplayValue: `function(value, unit) {
return \`\${value} \${unit || ''}\`.trim();
}`
};
const allHelpers = { ...defaultHelpers, ...customHelpers };
const helpersCode = Object.entries(allHelpers)
.map(([name, func]) => ` ${name}: ${func}`)
.join(',\n');
const classCode = MenuUtils.toString(); // <-- this gives full class MenuUtils {...}
return `
// Create EVOLV namespace structure
window.EVOLV = window.EVOLV || {};
window.EVOLV.nodes = window.EVOLV.nodes || {};
window.EVOLV.nodes.${nodeName} = window.EVOLV.nodes.${nodeName} || {};
// Inject MenuUtils class
${classCode}
// Expose MenuUtils instance to namespace
window.EVOLV.nodes.${nodeName}.utils = {
menuUtils: new MenuUtils(),
helpers: {
${helpersCode}
}
};
// Optionally expose globally
window.MenuUtils = MenuUtils;
console.log('${nodeName} utilities loaded in namespace');
`;
}
}
module.exports = MenuUtils; module.exports = MenuUtils;

View File

@@ -9,7 +9,7 @@ class OutputUtils {
checkForChanges(output, format) { checkForChanges(output, format) {
const changedFields = {}; const changedFields = {};
for (const key in output) { for (const key in output) {
if (output.hasOwnProperty(key) && output[key] !== this.output[format][key]) { if (Object.prototype.hasOwnProperty.call(output, key) && output[key] !== this.output[format][key]) {
let value = output[key]; let value = output[key];
// For fields: if the value is an object (and not a Date), stringify it. // For fields: if the value is an object (and not a Date), stringify it.
if (value !== null && typeof value === 'object' && !(value instanceof Date)) { if (value !== null && typeof value === 'object' && !(value instanceof Date)) {
@@ -81,13 +81,13 @@ class OutputUtils {
flattenTags(obj) { flattenTags(obj) {
const result = {}; const result = {};
for (const key in obj) { for (const key in obj) {
if (obj.hasOwnProperty(key)) { if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key]; const value = obj[key];
if (value !== null && typeof value === 'object' && !(value instanceof Date)) { if (value !== null && typeof value === 'object' && !(value instanceof Date)) {
// Recursively flatten the nested object. // Recursively flatten the nested object.
const flatChild = this.flattenTags(value); const flatChild = this.flattenTags(value);
for (const childKey in flatChild) { for (const childKey in flatChild) {
if (flatChild.hasOwnProperty(childKey)) { if (Object.prototype.hasOwnProperty.call(flatChild, childKey)) {
result[`${key}_${childKey}`] = String(flatChild[childKey]); result[`${key}_${childKey}`] = String(flatChild[childKey]);
} }
} }

View File

@@ -1,9 +1,10 @@
const MeasurementBuilder = require('./MeasurementBuilder'); const MeasurementBuilder = require('./MeasurementBuilder');
const EventEmitter = require('events'); const EventEmitter = require('events');
const convertModule = require('../convert/index'); const convertModule = require('../convert/index');
const { POSITIONS } = require('../constants/positions');
class MeasurementContainer { class MeasurementContainer {
constructor(options = {},logger) { constructor(options = {},_logger) {
this.emitter = new EventEmitter(); this.emitter = new EventEmitter();
this.measurements = {}; this.measurements = {};
this.windowSize = options.windowSize || 10; // Default window size this.windowSize = options.windowSize || 10; // Default window size
@@ -359,7 +360,7 @@ class MeasurementContainer {
// Difference calculations between positions // Difference calculations between positions
difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) { difference({ from = POSITIONS.DOWNSTREAM, to = POSITIONS.UPSTREAM, unit: requestedUnit } = {}) {
if (!this._currentType || !this._currentVariant) { if (!this._currentType || !this._currentVariant) {
throw new Error("Type and variant must be specified for difference calculation"); throw new Error("Type and variant must be specified for difference calculation");
} }
@@ -526,11 +527,11 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
_convertPositionStr2Num(positionString) { _convertPositionStr2Num(positionString) {
switch(positionString) { switch(positionString) {
case "atEquipment": case POSITIONS.AT_EQUIPMENT:
return 0; return 0;
case "upstream": case POSITIONS.UPSTREAM:
return Number.POSITIVE_INFINITY; return Number.POSITIVE_INFINITY;
case "downstream": case POSITIONS.DOWNSTREAM:
return Number.NEGATIVE_INFINITY; return Number.NEGATIVE_INFINITY;
default: default:
@@ -544,11 +545,11 @@ difference({ from = "downstream", to = "upstream", unit: requestedUnit } = {}) {
_convertPositionNum2Str(positionValue) { _convertPositionNum2Str(positionValue) {
switch (positionValue) { switch (positionValue) {
case 0: case 0:
return "atEquipment"; return POSITIONS.AT_EQUIPMENT;
case (positionValue < 0): case (positionValue < 0):
return "upstream"; return POSITIONS.UPSTREAM;
case (positionValue > 0): case (positionValue > 0):
return "downstream"; return POSITIONS.DOWNSTREAM;
default: default:
console.log(`Invalid position provided: ${positionValue}`); console.log(`Invalid position provided: ${positionValue}`);
} }

View File

@@ -1,4 +1,5 @@
const { MeasurementContainer } = require('./index'); const { MeasurementContainer } = require('./index');
const { POSITIONS } = require('../constants/positions');
const measurements = new MeasurementContainer(); const measurements = new MeasurementContainer();
@@ -29,7 +30,7 @@ console.log('\nSetting pressure values with distances:');
basicContainer basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('upstream') .position(POSITIONS.UPSTREAM)
.distance(1.5) .distance(1.5)
.value(100) .value(100)
.unit('psi'); .unit('psi');
@@ -37,7 +38,7 @@ basicContainer
basicContainer basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('downstream') .position(POSITIONS.DOWNSTREAM)
.distance(5.2) .distance(5.2)
.value(95) .value(95)
.unit('psi'); .unit('psi');
@@ -46,7 +47,7 @@ basicContainer
basicContainer basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('downstream') .position(POSITIONS.DOWNSTREAM)
.value(90); // distance 5.2 is automatically reused .value(90); // distance 5.2 is automatically reused
console.log('✅ Basic setup complete\n'); console.log('✅ Basic setup complete\n');
@@ -55,7 +56,7 @@ console.log('✅ Basic setup complete\n');
const upstreamPressure = basicContainer const upstreamPressure = basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('upstream') .position(POSITIONS.UPSTREAM)
.get(); .get();
console.log(`Retrieved upstream pressure: ${upstreamPressure.getCurrentValue()} ${upstreamPressure.unit}`); console.log(`Retrieved upstream pressure: ${upstreamPressure.getCurrentValue()} ${upstreamPressure.unit}`);
@@ -85,7 +86,7 @@ console.log('Adding pressure with auto-conversion:');
autoContainer autoContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('upstream') .position(POSITIONS.UPSTREAM)
.distance(0.5) .distance(0.5)
.value(1.5, Date.now(), 'bar'); // Input: 1.5 bar → Auto-stored as ~21.76 psi .value(1.5, Date.now(), 'bar'); // Input: 1.5 bar → Auto-stored as ~21.76 psi
@@ -93,7 +94,7 @@ autoContainer
const converted = autoContainer const converted = autoContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('upstream') .position(POSITIONS.UPSTREAM)
.get(); .get();
console.log(`Stored as: ${converted.getCurrentValue()} ${converted.unit} (distance=${converted.distance}m)`); console.log(`Stored as: ${converted.getCurrentValue()} ${converted.unit} (distance=${converted.distance}m)`);
@@ -107,14 +108,14 @@ console.log('--- Example 3: Unit Conversion on Retrieval ---');
autoContainer autoContainer
.type('flow') .type('flow')
.variant('predicted') .variant('predicted')
.position('upstream') .position(POSITIONS.UPSTREAM)
.distance(2.4) .distance(2.4)
.value(100, Date.now(), 'l/min'); .value(100, Date.now(), 'l/min');
const flowMeasurement = autoContainer const flowMeasurement = autoContainer
.type('flow') .type('flow')
.variant('predicted') .variant('predicted')
.position('upstream') .position(POSITIONS.UPSTREAM)
.get(); .get();
console.log(`Flow in l/min: ${flowMeasurement.getCurrentValue('l/min')}`); console.log(`Flow in l/min: ${flowMeasurement.getCurrentValue('l/min')}`);
@@ -155,13 +156,13 @@ console.log('--- Example 5: Basic Value Retrieval ---');
const upstreamVal = basicContainer const upstreamVal = basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('upstream') .position(POSITIONS.UPSTREAM)
.getCurrentValue(); .getCurrentValue();
const upstreamData = basicContainer const upstreamData = basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('upstream') .position(POSITIONS.UPSTREAM)
.get(); .get();
console.log(`Upstream: ${upstreamVal} ${upstreamData.unit} at ${upstreamData.distance}m`); console.log(`Upstream: ${upstreamVal} ${upstreamData.unit} at ${upstreamData.distance}m`);
@@ -169,31 +170,31 @@ console.log(`Upstream: ${upstreamVal} ${upstreamData.unit} at ${upstreamData.dis
const downstreamVal = basicContainer const downstreamVal = basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('downstream') .position(POSITIONS.DOWNSTREAM)
.getCurrentValue(); .getCurrentValue();
const downstreamData = basicContainer const downstreamData = basicContainer
.type('pressure') .type('pressure')
.variant('measured') .variant('measured')
.position('downstream') .position(POSITIONS.DOWNSTREAM)
.get(); .get();
//check wether a serie exists //check wether a serie exists
const hasSeries = measurements const hasSeries = measurements // eslint-disable-line no-unused-vars
.type("flow") .type("flow")
.variant("measured") .variant("measured")
.exists(); // true if any position exists .exists(); // true if any position exists
const hasUpstreamValues = measurements const hasUpstreamValues = measurements // eslint-disable-line no-unused-vars
.type("flow") .type("flow")
.variant("measured") .variant("measured")
.exists({ position: "upstream", requireValues: true }); .exists({ position: POSITIONS.UPSTREAM, requireValues: true });
// Passing everything explicitly // Passing everything explicitly
const hasPercent = measurements.exists({ const hasPercent = measurements.exists({ // eslint-disable-line no-unused-vars
type: "volume", type: "volume",
variant: "percent", variant: "percent",
position: "atEquipment", position: POSITIONS.AT_EQUIPMENT,
}); });
@@ -207,7 +208,7 @@ console.log('--- Example 6: Calculations & Statistics ---');
basicContainer basicContainer
.type('flow') .type('flow')
.variant('predicted') .variant('predicted')
.position('upstream') .position(POSITIONS.UPSTREAM)
.distance(3.0) .distance(3.0)
.value(200) .value(200)
.unit('gpm'); .unit('gpm');
@@ -215,7 +216,7 @@ basicContainer
basicContainer basicContainer
.type('flow') .type('flow')
.variant('predicted') .variant('predicted')
.position('downstream') .position(POSITIONS.DOWNSTREAM)
.distance(8.5) .distance(8.5)
.value(195) .value(195)
.unit('gpm'); .unit('gpm');
@@ -223,7 +224,7 @@ basicContainer
const flowAvg = basicContainer const flowAvg = basicContainer
.type('flow') .type('flow')
.variant('predicted') .variant('predicted')
.position('upstream') .position(POSITIONS.UPSTREAM)
.getAverage(); .getAverage();
console.log(`Average upstream flow: ${flowAvg.toFixed(1)} gpm`); console.log(`Average upstream flow: ${flowAvg.toFixed(1)} gpm`);
@@ -236,8 +237,8 @@ const pressureDiff = basicContainer
console.log(`Pressure difference: ${pressureDiff.value} ${pressureDiff.unit}\n`); console.log(`Pressure difference: ${pressureDiff.value} ${pressureDiff.unit}\n`);
//reversable difference //reversable difference
const deltaP = basicContainer.type("pressure").variant("measured").difference(); // defaults to downstream - upstream const deltaP = basicContainer.type("pressure").variant("measured").difference(); // eslint-disable-line no-unused-vars -- defaults to downstream - upstream
const netFlow = basicContainer.type("flow").variant("measured").difference({ from: "upstream", to: "downstream" }); const netFlow = basicContainer.type("flow").variant("measured").difference({ from: POSITIONS.UPSTREAM, to: POSITIONS.DOWNSTREAM }); // eslint-disable-line no-unused-vars
// ==================================== // ====================================
// ADVANCED STATISTICS & HISTORY // ADVANCED STATISTICS & HISTORY
@@ -247,7 +248,7 @@ console.log('--- Example 7: Advanced Statistics & History ---');
basicContainer basicContainer
.type('flow') .type('flow')
.variant('measured') .variant('measured')
.position('upstream') .position(POSITIONS.UPSTREAM)
.distance(3.0) .distance(3.0)
.value(210) .value(210)
.value(215) .value(215)
@@ -259,7 +260,7 @@ basicContainer
const stats = basicContainer const stats = basicContainer
.type('flow') .type('flow')
.variant('measured') .variant('measured')
.position('upstream'); .position(POSITIONS.UPSTREAM);
const statsData = stats.get(); const statsData = stats.get();

View File

@@ -1,5 +1,5 @@
const AssetMenu = require('./asset.js'); const AssetMenu = require('./asset.js');
const { TagcodeApp, DynamicAssetMenu } = require('./tagcodeApp.js'); // TagcodeApp and DynamicAssetMenu available via ./tagcodeApp.js
const LoggerMenu = require('./logger.js'); const LoggerMenu = require('./logger.js');
const PhysicalPositionMenu = require('./physicalPosition.js'); const PhysicalPositionMenu = require('./physicalPosition.js');

View File

@@ -87,3 +87,5 @@ dataStream.forEach((value, index) => {
console.log("\nFinal detector cluster states:", JSON.stringify(detector.clusters, null, 2)); console.log("\nFinal detector cluster states:", JSON.stringify(detector.clusters, null, 2));
*/ */
module.exports = DynamicClusterDeviation;

View File

@@ -88,7 +88,7 @@ class Interpolation {
array_values(obj) { array_values(obj) {
const new_array = []; const new_array = [];
for (let i in obj) { for (let i in obj) {
if (obj.hasOwnProperty(i)) { if (Object.prototype.hasOwnProperty.call(obj, i)) {
new_array.push(obj[i]); new_array.push(obj[i]);
} }
} }
@@ -231,7 +231,6 @@ class Interpolation {
let xdata = this.input_xdata; let xdata = this.input_xdata;
let ydata = this.input_ydata; let ydata = this.input_ydata;
let interpolationtype = this.interpolationtype;
let tension = this.tension; let tension = this.tension;
let n = ydata.length; let n = ydata.length;

View File

@@ -216,7 +216,6 @@ class movementManager {
return reject(new Error("Movement aborted")); return reject(new Error("Movement aborted"));
} }
const direction = targetPosition > this.currentPosition ? 1 : -1;
const totalDistance = Math.abs(targetPosition - this.currentPosition); const totalDistance = Math.abs(targetPosition - this.currentPosition);
const startPosition = this.currentPosition; const startPosition = this.currentPosition;
this.speed = Math.min(Math.max(this.speed, 0), 1); this.speed = Math.min(Math.max(this.speed, 0), 1);

196
test/configManager.test.js Normal file
View File

@@ -0,0 +1,196 @@
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('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');
});
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');
});
});
// ── 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');
});
});
});

View 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');
});
});
});

View 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();
});
});
});