← back to Dw Dead Image Recovery

scripts/test-recover-versa.mjs

64 lines

#!/usr/bin/env node
// test-recover-versa.mjs — TK-11048 negative-case unit tests for the Versa
// image-recovery pure helpers. Imports the REAL functions from recover-versa.mjs
// (the direct-run guard means importing does NOT touch the database).
//
// The GATE is the deterministic, offline, $0 pure-function suite. Network checks
// for isLiveImage are best-effort: a network hiccup marks SKIP, never a false FAIL.
//
//   node scripts/test-recover-versa.mjs
import { isDataUri, extractImage, isLiveImage } from './recover-versa.mjs';

let pass = 0, fail = 0, skip = 0;
const fails = [];
function ok(cond, name) { if (cond) { pass++; } else { fail++; fails.push(name); console.log(`  FAIL ${name}`); } }

// ── isDataUri: must catch the exact original bug shape ───────────────────────
// The versa scraper stored the base64 lazy-load placeholder with the host
// prepended, e.g. https://host/data:image/jpeg;base64,....  All must be rejected.
ok(isDataUri('data:image/png;base64,AAAA') === true, 'isDataUri: bare data: URI');
ok(isDataUri('https://www.versadesignedsurfaces.com/data:image/jpeg;base64,/9j/4AAQ') === true, 'isDataUri: host-prefixed data: URI (the real bug)');
ok(isDataUri('anything;base64,ZZZ') === true, 'isDataUri: ;base64, marker anywhere');
ok(isDataUri('https://www.versadesignedsurfaces.com/fileadmin/_processed_/x/csm_A.jpg') === false, 'isDataUri: real fileadmin URL accepted');
ok(isDataUri('') === false, 'isDataUri: empty string not a data URI');
ok(isDataUri(null) === false, 'isDataUri: null not a data URI');

// ── extractImage: never persists a data: URI; needs the SKU core to match ────
const dataOnlyHtml = `<img src="data:image/jpeg;base64,/9j/4AAQSkZJRg" data-lazy="1">`;
ok(extractImage(dataOnlyHtml, 'AVF25-834') === null, 'extractImage: page with ONLY a data: URI -> null (never persists placeholder)');

const realHtml = `<picture><source srcset="/fileadmin/_processed_/d/6/csm_Terra_AVF25-834-Ice_df75.jpg 800w"></picture>`;
const got = extractImage(realHtml, 'AVF25-834');
ok(got === 'https://www.versadesignedsurfaces.com/fileadmin/_processed_/d/6/csm_Terra_AVF25-834-Ice_df75.jpg',
   'extractImage: real /fileadmin/ URL for matching SKU -> absolute URL');

const wrongSkuHtml = `<img src="/fileadmin/_processed_/x/csm_Other_ZZ99-000-Foo.jpg">`;
ok(extractImage(wrongSkuHtml, 'AVF25-834') === null, 'extractImage: no fileadmin path contains SKU core -> null');

const emptyHtml = `<html><body>no images here</body></html>`;
ok(extractImage(emptyHtml, 'AVF25-834') === null, 'extractImage: no images at all -> null');

// A fileadmin path that IS a data URI can never be produced by the /fileadmin/
// regex, but the belt-and-braces isDataUri reject inside extractImage still holds:
ok(extractImage(dataOnlyHtml, 'ZZ99-000') === null, 'extractImage: unmatched SKU on data-only page -> null');

// ── isLiveImage: network, best-effort (the "200 that is really HTML" class) ──
async function net() {
  try {
    // Negative: the site homepage returns 200 text/html — must NOT count as a live image.
    const htmlNotImage = await isLiveImage('https://www.versadesignedsurfaces.com/');
    ok(htmlNotImage === false, 'isLiveImage: 200 text/html homepage -> false (not an image)');
  } catch { skip++; console.log('  SKIP isLiveImage html-negative (network)'); }
  try {
    // Positive control: one already-recovered, liveness-verified URL from the map.
    const known = 'https://www.versadesignedsurfaces.com/fileadmin/_processed_/d/6/csm_Terra_AVF01-025-Ice_Blink_PVC-free_df75398070.jpg';
    const liveImg = await isLiveImage(known);
    if (liveImg) { pass++; } else { skip++; console.log('  SKIP isLiveImage image-positive (network/url drift)'); }
  } catch { skip++; console.log('  SKIP isLiveImage image-positive (network)'); }
}
await net();

console.log(`\n[test-recover-versa] PASS=${pass} FAIL=${fail} SKIP=${skip}`);
if (fail > 0) { console.log('FAILED:', fails.join('; ')); process.exit(1); }
process.exit(0);