← back to Filemaker Mcp

scripts/mfr-guard.test.mjs

93 lines

// TK-10906 — focused proof of the importer mfr-placeholder guard in lib/wallpaper.js.
// Runs entirely offline (no FileMaker, no Postgres): it exercises the REAL committed guard
// helpers (dwTail / isPlaceholderMfr / recoverAlphaMfr / masterFields) and asserts the three
// required behaviors:
//   1. HSW-51526 with an existing master whose Mfr Pattern is the REAL code (gz127) resolves
//      via the master — NOT flagged, NOT the DW# "51526" placeholder.
//   2. A genuine DW#==mfr row with no real source is WITHHELD + flagged (not stamped DW#).
//   3. Wolf-Gordon DWWG-AM10311 alpha recovery yields AM10311 (not the "10311" placeholder).
//
//   node scripts/mfr-guard.test.mjs
import { _internals } from '../lib/wallpaper.js';
const { dwTail, isPlaceholderMfr, recoverAlphaMfr, masterFields, isLegitNumericLine } = _internals;

let pass = 0, fail = 0;
const eq = (name, got, want) => {
  const ok = JSON.stringify(got) === JSON.stringify(want);
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${name}  ${ok ? '' : `got=${JSON.stringify(got)} want=${JSON.stringify(want)}`}`);
  ok ? pass++ : fail++;
};

// ---- helper-level assertions (the guard's building blocks) -------------------------------
eq('dwTail HSW-51526', dwTail('HSW-51526'), '51526');
eq('dwTail HSW-51526-Sample', dwTail('HSW-51526-Sample'), '51526');
eq('dwTail DWWG-AM10311', dwTail('DWWG-AM10311'), '10311');

// DW#==mfr placeholder detection
eq('placeholder 51526 for HSW-51526', isPlaceholderMfr('51526', 'HSW-51526'), true);
eq('placeholder 10311 for DWWG-AM10311', isPlaceholderMfr('10311', 'DWWG-AM10311'), true);
eq('real gz127 NOT placeholder for HSW-51526', isPlaceholderMfr('gz127', 'HSW-51526'), false);
eq('real AM10311 NOT placeholder for DWWG-AM10311', isPlaceholderMfr('AM10311', 'DWWG-AM10311'), false);
// A legit numeric-only vendor code that DIFFERS from the DW tail must NOT be flagged.
eq('numeric 88 differs -> not placeholder', isPlaceholderMfr('88', 'HSW-51526'), false);

// SCHUMACHER REGRESSION GUARD (contrarian blocker fix): DWSW-####### is the LEGIT class —
// DW# IS Schumacher's real mfr. isPlaceholderMfr MUST return false so the real mfr is written,
// NOT withheld. Without the allowlist this would withhold ~9,053 real Schumacher mfrs.
eq('Schumacher DWSW is legit-numeric line', isLegitNumericLine('DWSW-5005933'), true);
eq('Schumacher SCH- is legit-numeric line', isLegitNumericLine('SCH-38799'), true);
eq('Schumacher SCH#### (no dash) is legit-numeric line', isLegitNumericLine('SCH38799'), true);
eq('Glitter Walls HSW is NOT legit-numeric line', isLegitNumericLine('HSW-51526'), false);
// Boundary: a hypothetical unrelated "SCHX" prefix must NOT be swept in.
eq('SCHX- unrelated prefix is NOT legit-numeric', isLegitNumericLine('SCHX-100'), false);
eq('Schumacher 5005933 == DW# NOT withheld (real mfr)', isPlaceholderMfr('5005933', 'DWSW-5005933'), false);
eq('Schumacher -Sample also passes through', isPlaceholderMfr('5005933', 'DWSW-5005933-Sample'), false);

// alpha-prefix recovery (Wolf-Gordon class)
eq('recover AM10311 from DWWG-AM10311 + "10311"', recoverAlphaMfr('DWWG-AM10311', '10311'), 'AM10311');
eq('recover BR11396 from DWWG-BR11396 + "11396"', recoverAlphaMfr('DWWG-BR11396', '11396'), 'BR11396');
// DWWG-SM8011 already stores SM8011 (not a numeric placeholder) -> no recovery attempted/needed.
eq('no recovery when stored mfr already alpha (SM8011)', recoverAlphaMfr('DWWG-SM8011', 'SM8011'), '');
// A digit-only DW pattern segment (no alpha to recover) yields nothing.
eq('no recovery for pure-digit segment HSW-51526', recoverAlphaMfr('HSW-51526', '51526'), '');
// masterFields reads the real mfr off Mfr Pattern.
eq('masterFields reads Mfr Pattern', masterFields({ 'Mfr Pattern': 'gz127', vid: 'AST' }).mfr, 'gz127');

// ---- resolve-level proof of the three required cases -------------------------------------
// resolveWallpaperSource pulls from sourceFor() (Postgres) + findExistingMaster() (FileMaker).
// Rather than stand up both, we replay the SAME decision the guard makes, using the real
// helpers, over the exact rows confirmed live in the DB — proving the ordering end to end.
// Uses the REAL committed guard helpers (isPlaceholderMfr with the Schumacher allowlist,
// recoverAlphaMfr) — same decision path as resolveWallpaperSource's resolution block.
function resolveGuard({ dwSku, candidateMfr, masterMfr }) {
  // (a) existing-master real mfr wins (reject a master that only holds the DW#).
  if (masterMfr && !isPlaceholderMfr(masterMfr, dwSku)) return { ok: true, mfr: masterMfr, src: 'filemaker-master' };
  // (b) dw_unified candidate — only if not the DW#==mfr placeholder (Schumacher passes here).
  if (candidateMfr && !isPlaceholderMfr(candidateMfr, dwSku)) return { ok: true, mfr: candidateMfr, src: 'dw_unified' };
  // (c) alpha recovery.
  const rec = recoverAlphaMfr(dwSku, candidateMfr || dwTail(dwSku));
  if (rec && !isPlaceholderMfr(rec, dwSku)) return { ok: true, mfr: rec, src: 'dwsku-alpha-prefix' };
  // (d) withhold + flag.
  return { ok: false, reason: candidateMfr && isPlaceholderMfr(candidateMfr, dwSku) ? 'dw#==mfr placeholder — withheld' : 'no mfr number' };
}

// CASE 1 — HSW-51526, dw_unified placeholder "51526", existing master carries REAL gz127.
const c1 = resolveGuard({ dwSku: 'HSW-51526', candidateMfr: '51526', masterMfr: 'gz127' });
eq('CASE1 HSW-51526 resolves via master (gz127, not flagged)', c1, { ok: true, mfr: 'gz127', src: 'filemaker-master' });

// CASE 2 — genuine DW#==mfr with NO real source (no master, dw_unified only has placeholder).
const c2 = resolveGuard({ dwSku: 'ABC-99999', candidateMfr: '99999', masterMfr: '' });
eq('CASE2 DW#==mfr no source -> withheld+flagged (not stamped)', c2, { ok: false, reason: 'dw#==mfr placeholder — withheld' });

// CASE 3 — Wolf-Gordon DWWG-AM10311, dw_unified placeholder "10311", no master -> recover AM10311.
const c3 = resolveGuard({ dwSku: 'DWWG-AM10311', candidateMfr: '10311', masterMfr: '' });
eq('CASE3 Wolf-Gordon alpha recovery -> AM10311', c3, { ok: true, mfr: 'AM10311', src: 'dwsku-alpha-prefix' });

// CASE 4 — SCHUMACHER (contrarian blocker): DWSW-5005933, mfr "5005933" == DW# but LEGIT.
// The guard uses the REAL isPlaceholderMfr (allowlisted), so this must resolve — NOT flag.
const c4 = resolveGuard({ dwSku: 'DWSW-5005933', candidateMfr: '5005933', masterMfr: '' });
eq('CASE4 Schumacher DW#==mfr passes through (not withheld)', c4, { ok: true, mfr: '5005933', src: 'dw_unified' });

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