File size: 20,851 Bytes
87bb7d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | /**
* 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.
*/
'use strict';
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, {}, [], ' |