← back to Dw Image Shrink

verify-urlgap.js

28 lines

// VERIFY Cody's URL-gap claim: do 'done' products' body_html (or metafields)
// hard-reference the now-deleted original image URL? Read-only.
import fs from 'fs';
import pg from 'pg';
const SHOP='designer-laboratory-sandbox.myshopify.com';
const ATOK=fs.readFileSync('/Users/macstudio3/Projects/secrets-manager/.env','utf8').split('\n').find(l=>l.startsWith('SHOPIFY_ADMIN_TOKEN=')).split('=').slice(1).join('=').replace(/["' ]/g,'');
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function get(p){const r=await fetch(`https://${SHOP}/admin/api/2024-10/${p}`,{headers:{'X-Shopify-Access-Token':ATOK}});await sleep(120);return r.json();}
const db=new pg.Client({host:'/tmp',database:'dw_unified'}); await db.connect();
const {rows}=await db.query("SELECT product_id, orig_src FROM img_shrink_ledger WHERE state='done' AND orig_src IS NOT NULL ORDER BY id DESC LIMIT 40");
let hard=0, checked=0, mfHard=0;
for(const r of rows){
  // stem = the CDN filename without path/query/size-suffix/ext
  const stem=(r.orig_src.split('?')[0].split('/').pop()||'').replace(/\.(jpe?g|png|webp)$/i,'').replace(/_\d+x\d*$/,'');
  if(!stem) continue;
  const pj=await get(`products/${r.product_id}.json?fields=body_html`);
  const bh=pj.product?.body_html||'';
  checked++;
  if(bh.toLowerCase().includes(stem.toLowerCase())){ hard++; console.log(`  HARDREF body_html product ${r.product_id} → "${stem}"`); }
  // metafields (raw-url storage)
  const mf=await get(`products/${r.product_id}/metafields.json`);
  const blob=JSON.stringify(mf.metafields||[]).toLowerCase();
  if(blob.includes(stem.toLowerCase())){ mfHard++; console.log(`  HARDREF metafield product ${r.product_id} → "${stem}"`); }
}
console.log(`\nRESULT: checked ${checked} done-products | body_html hardrefs=${hard} | metafield hardrefs=${mfHard}`);
console.log(hard+mfHard===0 ? 'VERDICT: URL-gap NOT reproduced — descriptions/metafields do NOT embed the product\'s own image CDN URL. Cody\'s risk is PHANTOM for DW → drop.' : 'VERDICT: URL-gap REAL — need orig_src→new_src rewrite for the affected fields.');
await db.end();