Download dev/constraints/integration.test.js from Harp404/Space: direct link, hf CLI and curl.
- Browser
- Download file 20.9 kB
-
https://huggingface.co/spaces/Harp404/Space/resolve/main/dev/constraints/integration.test.js
- Command line
-
hf download hf://spaces/Harp404/Space/dev/constraints/integration.test.js
-
curl -L -o integration.test.js https://huggingface.co/spaces/Harp404/Space/resolve/main/dev/constraints/integration.test.js
20.9 kB
| /** | |
| * Adversarial integration tests. | |
| * node dev/constraints/integration.test.js | |
| * | |
| * The engine test suite proves the state machine. This one attacks everything | |
| * around it: malformed rulebooks, hostile contexts, corrupt artefacts, absurd | |
| * numbers, and every path where a module could quietly produce a confident | |
| * answer it has not earned. | |
| * | |
| * The bar throughout: a module may return a correct answer or refuse. It may | |
| * never guess, default, impute, or crash. | |
| * | |
| * Zero dependencies, zero network. | |
| */ | |
| ; | |
| const CE = require('./engine'); | |
| const { maneuverRulebook, classify, rightOfWay, screeningInterval, LIMITS } = require('./rulebooks/orbital'); | |
| const { reentryRulebook } = require('./rulebooks/reentry'); | |
| const { releaseRulebook, SCENARIOS } = require('./rulebooks/release-gate'); | |
| const CV = require('./voting'); | |
| const { PropellantLedger } = require('./ledger'); | |
| const RE = require('./reentry'); | |
| const CF = require('./conformal'); | |
| const A = require('./authoring'); | |
| const FL = require('./flares'); | |
| const SW = require('./spaceweather'); | |
| let pass = 0, fail = 0; | |
| const G = '\x1b[32m', R = '\x1b[31m', B = '\x1b[1m', X = '\x1b[0m'; | |
| const ok = (n, c, x) => { if (c) { pass++; console.log(` ${G}✓${X} ${n}`); } else { fail++; console.log(` ${R}✗${X} ${n}${x ? ' → ' + x : ''}`); } }; | |
| const eq = (n, a, b) => ok(n, a === b, `got ${JSON.stringify(a)}, want ${JSON.stringify(b)}`); | |
| const sec = (t) => console.log(`\n${B}${t}${X}`); | |
| /** Assert a call does not throw, whatever it is fed. */ | |
| const noThrow = (n, fn) => { try { fn(); ok(n, true); } catch (e) { ok(n, false, 'threw: ' + e.message); } }; | |
| const NOW = Date.parse('2026-08-22T12:00:00Z'); | |
| const H = 3600000; | |
| // --------------------------------------------------------------------------- | |
| sec('ENGINE — hostile rulebooks and contexts'); | |
| noThrow('rule with no evaluate() does not crash the engine', () => { | |
| const r = CE.evaluate({ rulebook: { id: 'x', rules: [{ id: 'A', title: 'no evaluator', class: 'HARD' }] }, context: {}, now: NOW }); | |
| if (r.rules[0].state !== CE.STATE.UNEVALUATED) throw new Error('became ' + r.rules[0].state); | |
| }); | |
| noThrow('evaluate() returning garbage becomes UNEVALUATED', () => { | |
| const r = CE.evaluate({ rulebook: { id: 'x', rules: [{ id: 'A', class: 'HARD', evaluate: () => 42 }] }, context: {}, now: NOW }); | |
| if (r.rules[0].state !== CE.STATE.UNEVALUATED) throw new Error('became ' + r.rules[0].state); | |
| }); | |
| noThrow('evaluate() returning an unknown state string is tolerated', () => { | |
| CE.evaluate({ rulebook: { id: 'x', rules: [{ id: 'A', class: 'HARD', evaluate: () => ({ state: 'BANANA' }) }] }, context: {}, now: NOW }); | |
| }); | |
| noThrow('null context does not crash', () => CE.evaluate({ rulebook: maneuverRulebook, context: null, now: NOW })); | |
| noThrow('undefined context does not crash', () => CE.evaluate({ rulebook: maneuverRulebook, context: undefined, now: NOW })); | |
| noThrow('context with circular reference does not crash', () => { | |
| const c = { conj: {} }; c.self = c; | |
| CE.evaluate({ rulebook: maneuverRulebook, context: c, now: NOW }); | |
| }); | |
| noThrow('waivers as a non-array is tolerated', () => CE.evaluate({ rulebook: maneuverRulebook, context: {}, waivers: 'nope', now: NOW })); | |
| noThrow('empty rulebook is tolerated', () => CE.evaluate({ rulebook: { id: 'e', rules: [] }, context: {}, now: NOW })); | |
| { | |
| const r = CE.evaluate({ rulebook: { id: 'e', rules: [] }, context: {}, now: NOW }); | |
| eq('empty rulebook signals COMPLETE', r.signal, CE.SIGNAL.COMPLETE); | |
| eq('...with progress 0 (nothing applicable)', r.progress, 0); | |
| } | |
| { | |
| let threw = false; | |
| try { CE.evaluate({ rulebook: { id: 'x' }, context: {} }); } catch { threw = true; } | |
| ok('rulebook without a rules array throws rather than passing', threw); | |
| } | |
| { | |
| // A rule that mutates the context must not corrupt other rules' view of it. | |
| const ctx = { conj: { min_range_km: 5 } }; | |
| CE.evaluate({ | |
| rulebook: { id: 'm', rules: [ | |
| { id: 'A', class: 'SOFT', evaluate: (c) => { c.conj.min_range_km = 999; return CE.R.pass('ok'); } }, | |
| { id: 'B', class: 'SOFT', evaluate: (c) => CE.R.pass('saw ' + c.conj.min_range_km) }, | |
| ] }, context: ctx, now: NOW, | |
| }); | |
| ok('a mutating rule is visible but does not crash later rules', true); | |
| } | |
| sec('ENGINE — absurd numbers'); | |
| for (const v of [NaN, Infinity, -Infinity, null, undefined, '', 'abc', {}, [], -0]) { | |
| noThrow(`miss_range = ${JSON.stringify(v)} handled`, () => { | |
| CE.evaluate({ rulebook: maneuverRulebook, context: { conj: { min_range_km: v, probability: v, tca_ms: v, tle_age_days: v } }, now: NOW }); | |
| }); | |
| } | |
| { | |
| const r = CE.evaluate({ rulebook: maneuverRulebook, context: { conj: { min_range_km: NaN, probability: NaN } }, now: NOW }); | |
| ok('NaN inputs never produce SATISFIED on the rules that read them', | |
| !['FR-01', 'FR-12'].some((id) => r.rules.find((x) => x.id === id).state === CE.STATE.SATISFIED)); | |
| } | |
| sec('ENGINE — rollup edge cases'); | |
| noThrow('rollup(null)', () => CE.rollup(null)); | |
| noThrow('rollup([null, undefined])', () => CE.rollup([null, undefined])); | |
| eq('rollup of nothing has 0 items', CE.rollup([]).items, 0); | |
| { | |
| const one = CE.evaluate({ rulebook: { id: 'x', rules: [{ id: 'A', class: 'HARD', evaluate: () => CE.R.fail('no') }] }, context: {}, now: NOW }); | |
| eq('rollup takes the worst state', CE.rollup([one, one]).signal, CE.SIGNAL.BLOCKED); | |
| } | |
| // --------------------------------------------------------------------------- | |
| sec('CLASSIFY / RIGHT OF WAY — hostile names'); | |
| for (const n of [null, undefined, '', ' ', 123, {}, [], ' |