← back to Dw Kravet Hires

scripts/cleanup-dupe-media.test.mjs

48 lines

#!/usr/bin/env node
// Negative tests for cleanup-dupe-media.mjs `decide()`.
// A delete-tool that has only been shown its happy path is untested where it matters.
import { decide } from './cleanup-dupe-media.mjs';

const A = 'gid://shopify/MediaImage/AAA';   // uploaded copy 1
const B = 'gid://shopify/MediaImage/BBB';   // uploaded copy 2
const OLD = 'gid://shopify/MediaImage/OLD'; // retained low-res anchor
const X = 'gid://shopify/MediaImage/XXX';   // something we never uploaded

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

// POSITIVE: both copies present, B featured -> delete A
t('deletes the non-featured copy',
  decide([B, A, OLD], B, [A, B]), { action: 'delete', id: A });

t('deletes the non-featured copy (order reversed)',
  decide([A, B, OLD], A, [A, B]), { action: 'delete', id: B });

// NEGATIVE: the cases that must NOT delete
t('refuses when featured is a media we never uploaded',
  decide([X, A, B, OLD], X, [A, B]),
  { action: 'skip', why: `featured ${X} is not one of the uploaded copies — REFUSING` });

t('skips when only one copy remains (already cleaned)',
  decide([B, OLD], B, [A, B]), { action: 'skip', why: 'only one copy present — nothing redundant' });

t('skips when neither copy is present',
  decide([OLD], OLD, [A, B]), { action: 'skip', why: 'neither uploaded copy present (already cleaned)' });

t('refuses an empty product (no media at all)',
  decide([], undefined, [A, B]), { action: 'skip', why: 'neither uploaded copy present (already cleaned)' });

// The critical one: never propose deleting the featured image itself.
const d = decide([A, B, OLD], A, [A, B]);
t('never proposes deleting the featured id', d.id !== A, true);

// And never proposes deleting the low-res rollback anchor.
t('never proposes deleting the anchor', d.id !== OLD, true);

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