← back to Dw Unbuyable Recovery Pilot

tk11041-innovations-reconcile/test/provenance.test.mjs

64 lines

// TK-11041 — provenance-guard regression test.
// Proves the source-of-truth hardening on synthetic positive/negative cases AND
// against the LIVE corrupted rows in innovations_catalog (read-only).
//
// Run: node --test test/provenance.test.mjs
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { classifyCatalogSource, isTrustworthySource } from '../lib/provenance.mjs';
import { queryRows } from '../../lib/db.mjs';

// ---- POSITIVE: a real, corroborated Innovations code with provenance -------
test('trustworthy: well-shaped code + product_url + price', () => {
  const row = { mfr_sku: 'ZIO-088', product_url: 'https://innovationsusa.com/p/zio-088',
                pattern_name: 'Alchemy Helio', price_trade: 42 };
  const r = classifyCatalogSource(row);
  assert.equal(r.trustworthy, true, JSON.stringify(r));
  assert.deepEqual(r.reasons, []);
});

// ---- NEGATIVE 1: the truncated-tail fabrication signature -------------------
test('rejects fabricated lowercase-tail code (Sumatra -> atra-1)', () => {
  const r = classifyCatalogSource({ mfr_sku: 'atra-1', pattern_name: 'Sumatra Wallcovering', product_url: '' });
  assert.equal(r.trustworthy, false);
  assert.ok(r.reasons.includes('corrupted_lowercase_tail'));
  assert.ok(r.reasons.includes('code_is_pattern_tail'));
  assert.ok(r.reasons.includes('no_provenance_url'));
});

// ---- NEGATIVE 2: placeholder code leaked from a boolean scrape --------------
test('rejects TRUE placeholder code', () => {
  const r = classifyCatalogSource({ mfr_sku: 'TRUE', pattern_name: 'Whatever', product_url: 'https://x' });
  assert.equal(r.trustworthy, false);
  assert.ok(r.reasons.includes('placeholder_code'));
});

// ---- NEGATIVE 3: shape OK but NO provenance URL -> still untrusted ----------
test('rejects well-shaped code that lacks provenance', () => {
  const r = classifyCatalogSource({ mfr_sku: 'SOA-008', pattern_name: 'Agate', product_url: '' });
  assert.equal(r.trustworthy, false);
  assert.deepEqual(r.reasons, ['no_provenance_url']);
});

// ---- NEGATIVE 4: a pattern-tail code that HAPPENS to have a url still fails -
test('code_is_pattern_tail fires even with a url (Mazarin -> arin-3)', () => {
  const r = classifyCatalogSource({ mfr_sku: 'arin-3', pattern_name: 'Mazarin', product_url: 'https://x' });
  assert.equal(r.trustworthy, false);
  assert.ok(r.reasons.includes('code_is_pattern_tail'));
});

// ---- boolean convenience ----------------------------------------------------
test('isTrustworthySource mirrors classify', () => {
  assert.equal(isTrustworthySource({ mfr_sku: 'ZIO-1', product_url: 'https://x', pattern_name: 'Aerial' }), true);
  assert.equal(isTrustworthySource({ mfr_sku: 'arin-1', product_url: '', pattern_name: 'Mazarin' }), false);
});

// ---- LIVE: every known-corrupted row in innovations_catalog is flagged ------
test('LIVE: all lowercase-tail catalog rows are rejected (0 false-trust)', () => {
  const rows = queryRows(
    `select mfr_sku, product_url, pattern_name from innovations_catalog where mfr_sku ~ '^[a-z]+-[0-9]+$'`);
  assert.ok(rows.length >= 1, 'expected at least one corrupted row to exist');
  const leaked = rows.filter(isTrustworthySource);
  assert.equal(leaked.length, 0, `fabricated rows wrongly trusted: ${JSON.stringify(leaked.slice(0,3))}`);
});