← back to Dw Domain Fleet

scripts/test-pull-catalog.js

80 lines

#!/usr/bin/env node
/**
 * test-pull-catalog.js — prove the sellable-variant selection rule (TK-11463).
 *
 * The bug this guards: pull-catalog.js flattened each Shopify product to
 * variants[0], and DW puts the $4.25 memo Sample at position 1 on most products,
 * so 903 sellable goods store-wide were recorded wearing a sample's SKU and
 * price. The fix must rescue those WITHOUT mislabelling the 3,585 products that
 * are genuinely sample-only — so the negative cases (a sample-only product MUST
 * still resolve to its sample; "Sampler" MUST NOT be treated as a sample) matter
 * as much as the positive one.
 *
 * Every case is drawn from a shape actually observed on designerwallcoverings.com.
 * Run: node scripts/test-pull-catalog.js
 */
const { isSampleVariant, pickVariant } = require('./pull-catalog.js');

let pass = 0, fail = 0;
function eq(name, got, want) {
  const ok = JSON.stringify(got) === JSON.stringify(want);
  if (ok) { console.log(`  ok   ${name}`); pass++; }
  else { console.log(`  FAIL ${name}\n         got  ${JSON.stringify(got)}\n         want ${JSON.stringify(want)}`); fail++; }
}

console.log('==> isSampleVariant');
eq('-Sample suffix is a sample',            isSampleVariant({ sku: 'DWQW-61260-Sample', title: 'Sample' }), true);
eq('-SAMPLE uppercase is a sample (Thibaut)', isSampleVariant({ sku: 'DWTT-81110-SAMPLE', title: 'Sample' }), true);
eq('variant titled exactly "Sample"',       isSampleVariant({ sku: 'ODD-SKU-9', title: 'Sample' }), true);
eq('roll variant is NOT a sample',          isSampleVariant({ sku: 'DWQW-61260', title: 'Sold Per Roll' }), false);
eq('per-yard variant is NOT a sample',      isSampleVariant({ sku: 'DWPF-605557', title: 'Sold per Yard' }), false);
// Anchoring: a pattern or size legitimately containing the word must not be eaten.
eq('"Sampler" is NOT a sample (anchored)',  isSampleVariant({ sku: 'DW-SAMPLER-12', title: 'Sold Per Roll' }), false);
eq('mid-string "sample" is NOT a sample',   isSampleVariant({ sku: 'DW-SAMPLE-KIT-3', title: 'Kit' }), false);
eq('"Sample Book" title is NOT bare Sample', isSampleVariant({ sku: 'DW-9', title: 'Sample Book' }), false);
eq('empty/missing variant is not a sample', isSampleVariant(null), false);

console.log('==> pickVariant — THE RESCUE (sample at position 0, sellable behind it)');
// Real shape: designerwallcoverings.com/products/wool-weave-...-malibu-wallpaper.json
eq('Malibu roll rescued from $4.25 sample', pickVariant({ variants: [
    { sku: 'DWQW-61260-Sample', title: 'Sample', price: '4.25' },
    { sku: 'DWQW-61260', title: 'Sold Per Roll', price: '150.43' },
  ]}), { sku: 'DWQW-61260', title: 'Sold Per Roll', price: '150.43' });
// Real shape: /products/la-desirade-palmeraie-pierre-frey.json — $532.90, not $4.25
eq('Pierre Frey per-yard rescued',          pickVariant({ variants: [
    { sku: 'DWPF-605557-Sample', title: 'Sample', price: '4.25' },
    { sku: 'DWPF-605557', title: 'Sold per Yard', price: '532.90' },
  ]}), { sku: 'DWPF-605557', title: 'Sold per Yard', price: '532.90' });
// Real shape: /products/dwtt-81110-... — uppercase SAMPLE
eq('Thibaut uppercase SAMPLE rescued',      pickVariant({ variants: [
    { sku: 'DWTT-81110-SAMPLE', title: 'Sample', price: '4.25' },
    { sku: 'DWTT-81110', title: 'Sold Per (27" x 4.5yds)', price: '65.16' },
  ]}), { sku: 'DWTT-81110', title: 'Sold Per (27" x 4.5yds)', price: '65.16' });

console.log('==> pickVariant — THE GUARD (must NOT mislabel genuinely sample-only goods)');
// Real shape: /products/bromonte-taupe-on-tint-dwqc-600488.json — one variant, sample only.
eq('sample-only Quadrille keeps its sample', pickVariant({ variants: [
    { sku: 'DWQC-600488-Sample', title: 'Sample', price: '4.25' },
  ]}), { sku: 'DWQC-600488-Sample', title: 'Sample', price: '4.25' });
// Real shape: /products/toban-commercial-wallcovering-toban-tob-102.json
eq('sample-only Newmor keeps its sample',    pickVariant({ variants: [
    { sku: 'DWNM-581175-Sample', title: 'Sample', price: '4.25' },
  ]}), { sku: 'DWNM-581175-Sample', title: 'Sample', price: '4.25' });
eq('all-sample multi-variant falls back to [0]', pickVariant({ variants: [
    { sku: 'A-Sample', title: 'Sample', price: '4.25' },
    { sku: 'B-Sample', title: 'Sample', price: '4.25' },
  ]}), { sku: 'A-Sample', title: 'Sample', price: '4.25' });

console.log('==> pickVariant — degenerate input must not throw');
eq('no variants array',  pickVariant({}), {});
eq('empty variants',     pickVariant({ variants: [] }), {});
eq('null product',       pickVariant(null), {});
eq('sellable-first is unchanged (no regression on the 9,589 normal products)',
   pickVariant({ variants: [
     { sku: 'DWGR-100', title: 'Sold Per Roll', price: '89.00' },
     { sku: 'DWGR-100-Sample', title: 'Sample', price: '4.25' },
   ]}), { sku: 'DWGR-100', title: 'Sold Per Roll', price: '89.00' });

console.log(`==> ${pass} passed, ${fail} failed`);
process.exit(fail ? 1 : 0);