Files
pulse/tests/condition.test.js
T
jaredandClaude Opus 5.5 235d9240c0
CI / UI screenshots (before/after) (push) Skipped
CI / Lint (eslint) (push) Successful in 23s
CI / Test (jest) (push) Successful in 22s
CI / Security (npm audit) (push) Successful in 14s
CI / Scan (secrets, workflows, shell) (push) Failing after 18s
CI / Integration (api) (push) Successful in 2m43s
CI / Lint (eslint) (pull_request) Successful in 17s
CI / Test (jest) (pull_request) Successful in 18s
CI / Security (npm audit) (pull_request) Successful in 13s
CI / UI (playwright) (push) Successful in 3m0s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Scan (secrets, workflows, shell) (pull_request) Failing after 14s
CI / Integration (api) (pull_request) Successful in 2m24s
CI / UI (playwright) (pull_request) Successful in 2m16s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m18s
CI: eslint-plugin-security and zizmor; harden the workflow
- eslint-plugin-security 4.2.1 (recommended-legacy) with
  detect-object-injection off (it flags every obj[key]) and file/regex
  rules off for CI scripts and tests. The remaining hits were false
  positives with a one-line reason each (fixed page names, the worker's
  own directory, a null check), plus one real tidy-up: the condition
  tokenizer's number regex is now two flat patterns with no backtracking,
  and malformed numbers like 1.2.3 are refused.
- zizmor 1.32.0 in the Scan job. Its findings are fixed:
  persist-credentials: false on every checkout (no job pushes), container
  images pinned by digest (tag kept in the reference), and the deploy tag
  step takes github.* values through env instead of template expansion.
  Left as is: secrets-outside-env (auditor persona; Gitea has no
  deployment environments).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 23:45:35 -05:00

138 lines
6.3 KiB
JavaScript

'use strict';
const vm = require('vm');
const { evaluateCondition, checkCondition } = require('../lib/condition');
const { evalCondition, conditionErrors } = 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);
expect(ev('1.5e2 === 150 && .5 === 0.5 && 2E-1 === 0.2 && 3. === 3')).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',
'1.2.3 === 1',
"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([]);
});
});