← back to Harlequin Sample Price Analysis

tests/pricing.test.mjs

144 lines

// pricing.test.mjs — dependency-free unit tests for the pure pricing library.
// Run: node --test    (no DB, no network, no npm install)
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  computeRetail,
  computeMapFloor,
  retailMatchesFormula,
  classifyRow,
  summarize,
  roundCents,
  patternKeyFromMfrSku,
  inferSiblingCostCandidate,
  isJunkMfrSku,
  SAMPLE_PRICE,
  RETAIL_PLACEHOLDER,
} from '../scripts/pricing.mjs';

test('computeRetail applies cost/0.65/0.85 == 1.810x and rounds to cents', () => {
  // Real cohort values verified against the live mirror on 2026-08-30.
  assert.equal(computeRetail(140), 253.39);   // Amazilia / Kamanu / Lotus
  assert.equal(computeRetail(115), 208.14);   // Demoiselle / Salice / Houndstooth
  assert.equal(computeRetail(149), 269.68);   // Epitome / Flores / Gardinum
  assert.equal(computeRetail(126), 228.05);   // Lucielle
});

test('computeRetail multiplier is exactly the DW 1.8100 factor', () => {
  const r = computeRetail(100);
  assert.equal(r, 181.0);                     // 100/0.65/0.85 = 181.00
});

test('computeRetail guards missing / non-positive cost', () => {
  assert.equal(computeRetail(null), null);
  assert.equal(computeRetail(undefined), null);
  assert.equal(computeRetail(0), null);
  assert.equal(computeRetail(-5), null);
});

test('computeMapFloor is 2x cost (S1 SSP=2xTRADE hypothesis)', () => {
  assert.equal(computeMapFloor(149), 298.0);
  assert.equal(computeMapFloor(null), null);
});

test('DW formula retail is BELOW a 2x-cost MAP floor (margin flag)', () => {
  // 1.81x < 2.00x always, so every computed retail trips the MAP-floor flag.
  const cost = 149;
  assert.ok(computeRetail(cost) < computeMapFloor(cost));
});

test('retailMatchesFormula tolerates half-cent rounding', () => {
  assert.equal(retailMatchesFormula(149, 269.68), true);
  assert.equal(retailMatchesFormula(149, 269.69), false);
  assert.equal(retailMatchesFormula(null, 150), false);
});

test('classifyRow flags a real priced on-Shopify row as computable', () => {
  const c = classifyRow({ dw_sku: 'DWHA-570211', mfr_sku: 'epitome-...-01', price_trade: '149.00', price_retail: '269.68', on_shopify: true });
  assert.equal(c.sellPriceComputable, true);
  assert.equal(c.costGap, false);
  assert.equal(c.dwskuGap, false);
  assert.equal(c.formulaMatch, true);
  assert.equal(c.computedRetail, 269.68);
  assert.equal(c.belowMapFloor, true);        // 269.68 < 298.00
});

test('classifyRow flags the cost-gap placeholder row (Cranes In Flight/Marine)', () => {
  const c = classifyRow({ dw_sku: 'DWHA-570138', mfr_sku: 'cranes-in-flight-marine-haw0065-05', price_trade: null, price_retail: '150.00', on_shopify: true });
  assert.equal(c.costGap, true);
  assert.equal(c.sellPriceComputable, false);
  assert.equal(c.retailPlaceholder, true);
  assert.equal(c.computedRetail, null);
});

test('classifyRow flags a null-dw_sku row (Houndstooth)', () => {
  const c = classifyRow({ dw_sku: null, mfr_sku: 'houndstooth-taupe-soft-focus-haw0289-03', price_trade: '115.00', price_retail: '208.14', on_shopify: true });
  assert.equal(c.dwskuGap, true);
  assert.equal(c.sellPriceComputable, true);  // priced, just missing the DW SKU
});

test('summarize reproduces the 32-row cohort headline counts', () => {
  // Synthetic mini-cohort mirroring the live shape: 31 priced (1 also null-sku), 1 cost-gap.
  const rows = [];
  for (let i = 0; i < 26; i++) rows.push({ dw_sku: `DWHA-${i}`, mfr_sku: `p-${i}`, price_trade: '115.00', price_retail: '208.14', on_shopify: true });
  for (let i = 0; i < 5; i++) rows.push({ dw_sku: null, mfr_sku: `houndstooth-${i}`, price_trade: '115.00', price_retail: '208.14', on_shopify: true });
  rows.push({ dw_sku: 'DWHA-570138', mfr_sku: 'cranes', price_trade: null, price_retail: '150.00', on_shopify: true });
  const s = summarize(rows);
  assert.equal(s.total, 32);
  assert.equal(s.computable, 31);
  assert.equal(s.costGap, 1);
  assert.equal(s.dwskuGap, 5);
  assert.equal(s.retailPlaceholder, 1);
  assert.equal(s.formulaMatch, 31);
});

test('patternKeyFromMfrSku extracts the shared HAW pattern code', () => {
  // All four Cranes In Flight colorways share haw0065.
  assert.equal(patternKeyFromMfrSku('cranes-in-flight-marine-haw0065-05'), 'haw0065');
  assert.equal(patternKeyFromMfrSku('cranes-in-flight-blush-haw0065-03'), 'haw0065');
  assert.equal(patternKeyFromMfrSku('houndstooth-taupe-soft-focus-haw0289-03'), 'haw0289');
  assert.equal(patternKeyFromMfrSku(null), null);
});

test('inferSiblingCostCandidate returns a CONFIDENT $140 candidate for Cranes In Flight/Marine', () => {
  // Exact live rows: 3 priced siblings all $140, Marine (target) NULL — all share haw0065.
  const rows = [
    { mfr_sku: 'cranes-in-flight-blush-haw0065-03', price_trade: '140.00' },
    { mfr_sku: 'cranes-in-flight-emerald-haw0065-04', price_trade: '140.00' },
    { mfr_sku: 'cranes-in-flight-platinum-haw0065-01', price_trade: '140.00' },
    { mfr_sku: 'cranes-in-flight-marine-haw0065-05', price_trade: null },
  ];
  const res = inferSiblingCostCandidate(rows, 'cranes-in-flight-marine-haw0065-05');
  assert.equal(res.candidateCost, 140);
  assert.equal(res.confident, true);
  assert.equal(res.siblingCount, 3);
  assert.equal(computeRetail(res.candidateCost), 253.39); // implied retail matches live siblings
});

test('inferSiblingCostCandidate is NOT confident with a lone or disagreeing sibling', () => {
  const disagree = [
    { mfr_sku: 'x-haw1-01', price_trade: '140.00' },
    { mfr_sku: 'x-haw1-02', price_trade: '150.00' },
    { mfr_sku: 'x-haw1-03', price_trade: null },
  ];
  assert.equal(inferSiblingCostCandidate(disagree, 'x-haw1-03').confident, false); // siblings disagree
  const lone = [
    { mfr_sku: 'y-haw2-01', price_trade: '99.00' },
    { mfr_sku: 'y-haw2-02', price_trade: null },
  ];
  assert.equal(inferSiblingCostCandidate(lone, 'y-haw2-02').confident, false); // only 1 sibling
});

test('isJunkMfrSku flags leaked booleans and null-ish tokens, not real codes', () => {
  for (const j of ['TRUE', 'false', 'null', 'N/A', '-', '', '  ', null, undefined, '#REF!'])
    assert.equal(isJunkMfrSku(j), true, `expected junk: ${j}`);
  for (const real of ['haw0065-05', 'wp81388m', 'NCW4186', 'cranes-in-flight-marine-haw0065-05'])
    assert.equal(isJunkMfrSku(real), false, `expected real: ${real}`);
});

test('constants are the DW canonical values', () => {
  assert.equal(SAMPLE_PRICE, 4.25);
  assert.equal(RETAIL_PLACEHOLDER, 150.0);
  assert.equal(roundCents(1.005), 1.01); // EPSILON nudge gives correct half-up rounding (not the naive 1.00)
});