← back to Harlequin Sample Price Analysis
tests/plan.test.mjs
53 lines
// plan.test.mjs — data-integrity checks on the generated reprice plan.
// Validates artifacts/reprice-plan.json against the pricing formula + exclusion rules.
// Run: node --test (skips gracefully if the plan hasn't been generated yet).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, existsSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { computeRetail } from '../scripts/pricing.mjs';
const planPath = join(dirname(fileURLToPath(import.meta.url)), '..', 'artifacts', 'reprice-plan.json');
const EXCLUDED = ['DWHQ-335001', 'DWHQ-335002', 'DWHQ-335003', 'DWHQ-335024'];
test('reprice-plan.json exists and is well-formed', { skip: !existsSync(planPath) }, () => {
const p = JSON.parse(readFileSync(planPath, 'utf8'));
assert.equal(p.ticket, 'TK-10870');
assert.ok(Array.isArray(p.reprice_now) && p.reprice_now.length > 0);
assert.ok(Array.isArray(p.needs_roll_variant));
});
test('accounting closes: reprice_now + needs_roll_variant + excluded = 32 on_shopify', { skip: !existsSync(planPath) }, () => {
const p = JSON.parse(readFileSync(planPath, 'utf8'));
assert.equal(p.reprice_now.length + p.needs_roll_variant.length + p.excluded_from_32.length, 32);
assert.equal(p.reprice_now.length, 27);
assert.equal(p.needs_roll_variant.length, 3);
});
test('every reprice_now target_retail equals the DW formula from net_cost', { skip: !existsSync(planPath) }, () => {
const p = JSON.parse(readFileSync(planPath, 'utf8'));
for (const r of p.reprice_now) {
assert.equal(Number(r.target_retail), computeRetail(Number(r.net_cost)),
`${r.dwhq_sku}: target ${r.target_retail} != formula(${r.net_cost})`);
}
});
test('no excluded sku and no sample-only sku appears in reprice_now', { skip: !existsSync(planPath) }, () => {
const p = JSON.parse(readFileSync(planPath, 'utf8'));
const sampleOnly = new Set(p.needs_roll_variant);
for (const r of p.reprice_now) {
assert.ok(!EXCLUDED.includes(r.dwhq_sku), `excluded sku leaked: ${r.dwhq_sku}`);
assert.ok(!sampleOnly.has(r.dwhq_sku), `sample-only sku leaked: ${r.dwhq_sku}`);
assert.ok(!/-Sample$/.test(r.dwhq_sku), `reprice_now must not carry a -Sample sku: ${r.dwhq_sku}`);
}
});
test('plan carries the live-execution + variant-safety caveats', { skip: !existsSync(planPath) }, () => {
const p = JSON.parse(readFileSync(planPath, 'utf8'));
const blob = JSON.stringify(p.caveats).toLowerCase();
assert.ok(blob.includes('live'), 'missing live-enumeration caveat');
assert.ok(blob.includes('roll'), 'missing roll-variant caveat');
assert.ok(blob.includes('sample-only'), 'missing sample-only caveat');
});