← back to Carnegie Reprice
test-weight-guard.mjs
56 lines
// test-weight-guard.mjs — TK-11547 test for the rollback weight guard in archive-redirect.mjs.
// Offline. The design here is deliberately DIFFERENT from every other TK-11547 site (heal-then-
// proceed-loudly rather than HOLD, because blocking an undo is worse than the defect), so the
// tests assert BOTH halves: that it heals, and that it still un-archives when healing fails.
import fs from 'node:fs';
import assert from 'node:assert';
const SRC = fs.readFileSync(new URL('./archive-redirect.mjs', import.meta.url), 'utf8');
const BLOCK = SRC.slice(SRC.indexOf('TK-11547 WEIGHT GO-LIVE GUARD'), SRC.indexOf('for(const r of map.redirects)'));
let pass = 0, fail = 0;
const t = (n, fn) => { try { fn(); console.log('PASS ' + n); pass++; } catch (e) { console.log('FAIL ' + n + ' — ' + e.message); fail++; } };
// the zero-detection predicate, lifted verbatim
const zero = (v) => { const w = Number(v.weight); const u = String(v.weight_unit || 'lb').toLowerCase();
const lb = u.startsWith('kg') ? w*2.20462 : (u==='g'||u.startsWith('gram')) ? w/453.59237 : u==='oz' ? w/16 : w;
return !(Number.isFinite(lb) && lb > 0); };
t('detects zero weight', () => assert.strictEqual(zero({ weight: 0 }), true));
t('detects null/absent weight', () => { assert.strictEqual(zero({}), true); assert.strictEqual(zero({ weight: null }), true); });
t('detects negative / NaN', () => { assert.strictEqual(zero({ weight: -1 }), true); assert.strictEqual(zero({ weight: 'x' }), true); });
t('accepts a positive lb weight', () => assert.strictEqual(zero({ weight: 3, weight_unit: 'lb' }), false));
t('kg / g / oz convert and are not falsely flagged', () => {
assert.strictEqual(zero({ weight: 1.36, weight_unit: 'kg' }), false);
assert.strictEqual(zero({ weight: 1360, weight_unit: 'g' }), false);
assert.strictEqual(zero({ weight: 4, weight_unit: 'oz' }), false);
});
t('STRUCTURAL: the guard RE-VERIFIES after writing (a 200 is not evidence)', () => {
assert.ok(/RE-VERIFY/.test(BLOCK) && /re\.variant\?\.weight/.test(BLOCK), 'no re-read after the heal write');
});
t('STRUCTURAL: it un-archives EVEN WHEN the heal fails — an undo is never blocked', () => {
const healIdx = BLOCK.indexOf('unhealed.push');
const unarchIdx = BLOCK.indexOf("status:'active'");
assert.ok(healIdx > -1 && unarchIdx > healIdx, 'the un-archive does not follow the failed-heal path');
assert.ok(!/continue;\s*\}\s*$/m.test(BLOCK.slice(healIdx, unarchIdx)), 'a failed heal appears to skip the un-archive');
});
t('STRUCTURAL: a failed heal is LOUD (console.error), never silent', () => {
assert.ok(/console\.error\(/.test(BLOCK) && /LIVE AT ZERO WEIGHT/.test(BLOCK), 'unhealed variants are not surfaced loudly');
});
t('STRUCTURAL: heal uses the REST variant field (narrow token lacks write_inventory)', () => {
assert.ok(/\/variants\/\$\{v\.id\}\.json/.test(BLOCK), 'not using the REST variant path');
// Strip comment lines before asserting absence. The first cut matched the word
// `inventoryItemUpdate` inside this guard's OWN explanatory comment (which says that path 403s
// on the narrow token) and failed on correct code — an assertion that reads prose as if it were
// behaviour. Worth locking down: it is the same imprecision that made an earlier structural
// check vacuously true by comparing against a constant's declaration instead of its call site.
const CODE = BLOCK.split('\n').filter(l => !/^\s*\/\//.test(l)).join('\n');
assert.ok(!/inventoryItemUpdate/.test(CODE), 'uses a GraphQL path the narrow token cannot call');
});
t('STRUCTURAL: samples get the sample default, sellables the type default', () => {
assert.ok(/W_SAMPLE_LB = 0\.25/.test(BLOCK) && /W_DEFAULT_LB = 3\.0/.test(BLOCK), 'approved defaults missing');
assert.ok(/isSample\(v\) \? W_SAMPLE_LB : W_DEFAULT_LB/.test(BLOCK), 'sample/sellable split not applied');
});
console.log(`\n${fail ? 'TESTS FAILED' : 'ALL TESTS PASS'} — ${pass} passed, ${fail} failed`);
process.exit(fail ? 1 : 0);