← back to Harlequin Sample Price Analysis

tests/integrity.test.mjs

77 lines

// integrity.test.mjs — unit tests for the reusable catalog-integrity detectors.
// Fixtures mirror the real Harlequin shapes found in TK-10870. No DB, no network.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  findProductIdCollisions,
  findMfrMisStamps,
  findJunkMfrSku,
  findCostGaps,
  integrityReport,
} from '../scripts/integrity.mjs';

test('findProductIdCollisions catches the Cranes Emerald/Marine shared product_id', () => {
  const rows = [
    { mfr_sku: 'cranes-in-flight-emerald-haw0065-04', shopify_product_id: '7787420680243' },
    { mfr_sku: 'cranes-in-flight-marine-haw0065-05', shopify_product_id: '7787420680243' },
    { mfr_sku: 'amazilia-gooseberry-haw0045-04', shopify_product_id: '7787420450867' },
    { mfr_sku: 'no-id', shopify_product_id: null },
  ];
  const g = findProductIdCollisions(rows);
  assert.equal(g.length, 1);
  assert.equal(g[0].shopify_product_id, '7787420680243');
  assert.equal(g[0].count, 2);
});

test('findMfrMisStamps catches Cranes + Demoiselle/Locronan, ignores junk', () => {
  const rows = [
    { sku: 'DWHQ-335001', mfr_sku: 'cranes-in-flight-marine-haw0065-05' },
    { sku: 'DWHQ-335002', mfr_sku: 'cranes-in-flight-marine-haw0065-05' },
    { sku: 'DWHQ-335003', mfr_sku: 'demoiselle-cornflower-first-light-honey-haw0067-09' },
    { sku: 'DWHQ-335024', mfr_sku: 'demoiselle-cornflower-first-light-honey-haw0067-09' },
    { sku: 'DWHQ-335007', mfr_sku: 'epitome-copper-gold-sepia-haw0076-01' }, // unique, ok
    { sku: 'X-1', mfr_sku: 'TRUE' }, { sku: 'X-2', mfr_sku: 'TRUE' },        // junk, must be ignored
  ];
  const g = findMfrMisStamps(rows);
  assert.equal(g.length, 2);
  assert.ok(g.every((x) => x.distinct_skus.length === 2));
  assert.ok(!g.some((x) => x.mfr_sku === 'TRUE'), 'junk must not be a mis-stamp');
});

test('findJunkMfrSku catches the TRUE-leak rows', () => {
  const rows = [
    { sku: 'A', mfr_sku: 'TRUE' }, { sku: 'B', mfr_sku: 'haw0065-05' }, { sku: 'C', mfr_sku: null },
  ];
  const junk = findJunkMfrSku(rows);
  assert.deepEqual(junk.map((r) => r.sku).sort(), ['A', 'C']);
});

test('findCostGaps flags on-shopify rows with no net cost', () => {
  const rows = [
    { mfr_sku: 'marine', on_shopify: true, price_trade: null },
    { mfr_sku: 'blush', on_shopify: true, price_trade: '140.00' },
    { mfr_sku: 'off', on_shopify: false, price_trade: null },
  ];
  assert.deepEqual(findCostGaps(rows).map((r) => r.mfr_sku), ['marine']);
});

test('integrityReport verdict: FAIL on collision, WARN on junk-only, PASS on clean', () => {
  const fail = integrityReport({
    catalogRows: [
      { shopify_product_id: '1', on_shopify: true, price_trade: '10' },
      { shopify_product_id: '1', on_shopify: true, price_trade: '10' },
    ], shopifyRows: [],
  });
  assert.equal(fail.verdict, 'FAIL');
  assert.equal(fail.productId_collisions, 1);

  const warn = integrityReport({ catalogRows: [], shopifyRows: [{ sku: 'A', mfr_sku: 'TRUE' }] });
  assert.equal(warn.verdict, 'WARN');

  const pass = integrityReport({
    catalogRows: [{ shopify_product_id: '9', on_shopify: true, price_trade: '10' }],
    shopifyRows: [{ sku: 'A', mfr_sku: 'real-haw0001-01' }],
  });
  assert.equal(pass.verdict, 'PASS');
});