← back to Sanderson Onboard

scripts/verify_cadence_weights.test.mjs

63 lines

// verify_cadence_weights.test.mjs — TK-11471 NEGATIVE TEST.
// A positive-only test on a detector proves nothing (CLAUDE.md TK-11431 amendment 3): the whole
// purpose of this verifier is to go RED when the SDG cadence ships a zero-weight product live, so
// the red path is what must be proven. Offline, zero network, zero DB.
//
// Why not "point it at a known-bad pre-fix run": every pre-fix cadence run has SINCE been healed by
// the TK-11414 backfill, so live history can no longer produce a red. Faults are injected instead.
import { classify } from './verify_cadence_weights.mjs';
import assert from 'node:assert';

let pass = 0, fail = 0;
const t = (name, fn) => { try { fn(); console.log('PASS ' + name); pass++; }
                          catch (e) { console.log('FAIL ' + name + ' — ' + e.message); fail++; } };

const sellable = (iid) => ({ dw: 'DWXX-1', sku: 'DWXX-1',        iid, isSample: false });
const sample   = (iid) => ({ dw: 'DWXX-1', sku: 'DWXX-1-Sample', iid, isSample: true  });

t('injected ZERO-weight SELLABLE => FAIL (the 190-sellable leak)', () => {
  const r = classify([sellable(1), sample(2)], new Map([[1, 0], [2, 0.25]]));
  assert.strictEqual(r.verdict, 'FAIL');
  assert.strictEqual(r.zero_weight_sellable, 1);
  assert.strictEqual(r.zero_weight_sample, 0);
});

t('injected ZERO-weight SAMPLE => FAIL (samples count — the canary FAILs on them too)', () => {
  const r = classify([sellable(1), sample(2)], new Map([[1, 3], [2, 0]]));
  assert.strictEqual(r.verdict, 'FAIL');
  assert.strictEqual(r.zero_weight_sample, 1);
});

t('NULL weight (measurement absent) => FAIL, never silently OK', () => {
  const r = classify([sellable(1)], new Map([[1, null]]));
  assert.strictEqual(r.verdict, 'FAIL');
});

t('UNMEASURED variant (id not returned by Shopify) => UNKNOWN/WARN, NEVER PASS', () => {
  const r = classify([sellable(1), sellable(2)], new Map([[1, 3]]));   // iid 2 missing
  assert.strictEqual(r.verdict, 'UNKNOWN');
  assert.strictEqual(r.status, 'WARN');
  assert.strictEqual(r.unmeasured, 1);
});

t('EMPTY weight map (whole read failed) => UNKNOWN, never a false green', () => {
  const r = classify([sellable(1), sample(2)], new Map());
  assert.notStrictEqual(r.verdict, 'PASS');
  assert.strictEqual(r.unmeasured, 2);
});

t('all variants positively weighted => PASS', () => {
  const r = classify([sellable(1), sample(2)], new Map([[1, 3], [2, 0.25]]));
  assert.strictEqual(r.verdict, 'PASS');
  assert.strictEqual(r.status, 'PASS');
  assert.strictEqual(r.zero_weight, 0);
});

t('negative / NaN weight => FAIL (not treated as a number that happens to be falsy-safe)', () => {
  assert.strictEqual(classify([sellable(1)], new Map([[1, -1]])).verdict, 'FAIL');
  assert.strictEqual(classify([sellable(1)], new Map([[1, NaN]])).verdict, 'FAIL');
});

console.log(`\n${fail ? 'TESTS FAILED' : 'ALL TESTS PASS'} — ${pass} passed, ${fail} failed`);
process.exit(fail ? 1 : 0);