'use strict'; const vm = require('vm'); const { evaluateCondition, checkCondition } = require('../lib/condition'); const { evalCondition, conditionErrors, gotoErrors } = require('../lib/utils'); // Every condition used by the production workflows (2026-11-22). const PRODUCTION = [ "params.switch_ip && params.switch_ip.trim() !== ''", "(state.carrier||'x') === 'error'", "(state.carrier||'1') === '0' && (state.has_dom||'no') === 'yes'", "(state.carrier||'1') === '0'", "parseInt(state.carrier_changes||'0') > 100 && (state.has_dom||'no') === 'yes'", "parseInt(state.carrier_changes||'0') > 100", "parseInt(state.carrier_changes||'0') > 20 && (state.has_dom||'no') === 'yes'", "parseInt(state.carrier_changes||'0') > 20", "(state.duplex||'full') === 'half'", "parseInt(state.crc_errors||'0') > 10 || parseInt(state.frame_errors||'0') > 10", "(state.has_dom||'no') === 'no'", "state.sfp_rx_dbm === 'no_dom' || parseFloat(state.sfp_rx_dbm||'0') < -26", "parseFloat(state.sfp_rx_dbm||'0') < -18", "parseFloat(state.sfp_tx_dbm||'0') < -10", "(state.carrier||'0') === '1' && (state.operstate||'down') === 'up'", "state.sfp_rx_dbm && state.sfp_rx_dbm !== 'no_dom' && parseFloat(state.sfp_rx_dbm||'0') < -25", "state.sfp_rx_dbm && state.sfp_rx_dbm !== 'no_dom' && parseFloat(state.sfp_rx_dbm||'0') < -18", "(state.carrier||'0') === '0'", "(state.duplex||'half') === 'full' && parseInt(state.crc_errors||'0') < 10", "(state.duplex||'half') === 'full'", "(state.has_dom||'no') === 'yes' && state.sfp_rx_dbm !== 'no_dom' && parseFloat(state.sfp_rx_dbm||'0') < -18", "(state.has_dom||'no') === 'yes'", "state.ring_rx_cur && state.ring_rx_max && state.ring_rx_cur !== 'unknown' && state.ring_rx_max !== 'unknown' && parseInt(state.ring_rx_cur||'0') < parseInt(state.ring_rx_max||'0') && parseInt(state.rx_dropped||'0') + parseInt(state.rx_missed||'0') > 0", "parseInt(state.rx_dropped||'0') + parseInt(state.rx_missed||'0') > 100", "(state.flowctl_rx||'on') === 'on' || (state.flowctl_tx||'on') === 'on'", "state.speed && state.speed !== '-1' && parseInt(state.speed||'0') > 0 && (state.duplex||'full') === 'full'", ]; const VALUES = [undefined, '', '0', '1', '-1', '5', '11', '25', '150', '1000', '-12', '-20', '-27', 'error', 'yes', 'no', 'no_dom', 'full', 'half', 'on', 'off', 'up', 'down', 'unknown', ' 10.0.0.1 ']; // Small deterministic PRNG so failures are reproducible. function rng(seed) { return () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648; } describe('production conditions behave exactly as plain JavaScript did', () => { for (const cond of PRODUCTION) { test(cond, () => { expect(() => checkCondition(cond)).not.toThrow(); const keys = [...new Set([...cond.matchAll(/(?:state|params)\.([a-z_]+)/g)].map(m => m[1]))]; const rand = rng(cond.length); for (let i = 0; i < 150; i++) { const state = {}; const params = {}; for (const k of keys) { const v = VALUES[Math.floor(rand() * VALUES.length)]; if (v !== undefined) { state[k] = v; params[k] = v; } } // Reference: the old behaviour (vm), on trusted test input only. const expected = !!vm.runInNewContext(cond, { state, params, parseInt, parseFloat }); expect(evaluateCondition(cond, state, params)).toBe(expected); } }); } }); describe('the language', () => { const ev = (c, state = {}, params = {}) => evaluateCondition(c, state, params); test('operators, precedence and literals', () => { expect(ev('1 + 2 * 3 === 7')).toBe(true); expect(ev('(1 + 2) * 3 === 9')).toBe(true); expect(ev('!false && (null || "x") === "x"')).toBe(true); expect(ev('-5 < -4 && 10 % 3 === 1 && 7 / 2 === 3.5')).toBe(true); expect(ev("'a' + 1 === 'a1'")).toBe(true); expect(ev('1 == "1" && 1 != 2')).toBe(true); expect(ev("state.x === undefined", {})).toBe(true); expect(ev("'it\\'s' === \"it's\"")).toBe(true); }); test('functions and string methods', () => { expect(ev("parseInt('42px') === 42 && parseFloat('-3.5') === -3.5")).toBe(true); expect(ev("Number('7') === 7 && String(7) === '7' && Boolean('')=== false")).toBe(true); expect(ev("state.h.toLowerCase().startsWith('web') && state.h.includes('01')", { h: 'WEB-01' })).toBe(true); expect(ev("state.h.length === 6", { h: 'WEB-01' })).toBe(true); expect(ev('promptResponse === "Yes"', { promptResponse: 'Yes' })).toBe(true); }); }); describe('a condition can only read the workflow data', () => { const rejected = [ 'this', 'state.constructor', 'state.__proto__', 'params.prototype', "state['x']", 'globalThis', 'process', 'require', 'Function', 'eval("1")', 'state.x = 1', '(() => 1)()', '`template`', '1; 2', "state.x.toString()", "'a'.constructor", "parseInt.call(null, '1')", ]; test.each(rejected)('%s is refused', cond => { expect(() => checkCondition(cond)).toThrow(); expect(evalCondition(cond, {}, {})).toBe(false); // and the engine treats it as false }); test('inherited properties are invisible', () => { expect(evaluateCondition('state.toString === undefined && state.hasOwnProperty === undefined', {}, {})).toBe(true); }); test('string methods only work on strings', () => { expect(evalCondition('state.n.trim() === "1"', { n: 1 }, {})).toBe(false); }); test('huge or deeply nested input is refused', () => { expect(() => checkCondition('1+'.repeat(2000) + '1')).toThrow(); expect(() => checkCondition('('.repeat(200) + '1' + ')'.repeat(200))).toThrow(); }); }); describe('conditionErrors', () => { test('lists every condition that does not parse, by step', () => { const problems = conditionErrors({ steps: [ { id: 'ok', type: 'execute', condition: "params.env === 'prod'" }, { id: 'bad', type: 'execute', condition: 'state.constructor' }, { id: 'route', type: 'route', conditions: [{ if: 'state.x ===' }, { default: true, goto: 'end' }] }, ] }); expect(problems.map(p => p.step)).toEqual(['bad', 'route']); }); test('accepts a JSON string definition and ignores malformed JSON', () => { expect(conditionErrors(JSON.stringify({ steps: [{ condition: 'process' }] }))).toHaveLength(1); expect(conditionErrors('{not json')).toEqual([]); }); }); describe('gotoErrors (#20)', () => { test('lists jumps to step ids that do not exist; end is always valid', () => { const problems = gotoErrors({ steps: [ { id: 'a', type: 'execute', goto: 'c' }, { id: 'b', type: 'route', conditions: [{ if: 'true', goto: 'nope' }, { default: true, goto: 'end' }] }, { id: 'c', type: 'prompt', routes: { Yes: 'a', No: '__end__', Maybe: 'missing' } }, ] }); expect(problems.map(p => `${p.step}->${p.goto}`)).toEqual(['b->nope', 'c->missing']); }); test('accepts a JSON string and ignores malformed JSON', () => { expect(gotoErrors(JSON.stringify({ steps: [{ id: 'x', goto: 'y' }] }))).toHaveLength(1); expect(gotoErrors('{oops')).toEqual([]); }); });