← back to Dw Five Field Step0

scripts/verify-samples-live.js

131 lines

#!/usr/bin/env node
/**
 * Verify Phase 2 Results: Sample variants live on Shopify storefront
 * 
 * Spot-check 10 random SKUs from each of the 9 vendors:
 * - Verify variant exists (status 200)
 * - Verify price = $4.25
 * - Verify title includes "Sample"
 * - Verify no "Sample-Sample" suffix issues
 */
const fs = require('fs');
const path = require('path');
const { Pool } = require('pg');

const env = fs.readFileSync(path.join(process.env.HOME, '/Projects/secrets-manager/.env'), 'utf8');
const TOKEN = (env.match(/SHOPIFY_ADMIN_TOKEN=(\S+)/) || [])[1];
const DOMAIN = 'designer-laboratory-sandbox.myshopify.com';
const VER = '2024-10';

delete process.env.PGPASSWORD;
delete process.env.DATABASE_URL;
const pool = new Pool({ host: '/tmp', database: 'dw_unified' });

const VENDORS = [
  'Clarke And Clarke',
  'Gaston Y Daniela',
  'Lee Jofa',
  'Brunschwig & Fils',
  'GP & J Baker',
  'Threads',
  'Mulberry',
  'Kravet',
  'Phillipe Romano'
];

async function getRandomSKUsForVendor(vendor, limit = 10) {
  const res = await pool.query(
    `SELECT DISTINCT dw_sku, shopify_id
     FROM active_five_field_gaps 
     WHERE vendor = $1 AND fix_type = 'add-sample' AND priceable = true
     ORDER BY RANDOM()
     LIMIT $2`,
    [vendor, limit]
  );
  return res.rows;
}

async function verifySampleVariant(productId, dwSku) {
  const pid = productId.match(/\d+$/)[0];
  
  const result = await fetch(
    `https://${DOMAIN}/admin/api/${VER}/products/${pid}.json`,
    {
      headers: {
        'X-Shopify-Access-Token': TOKEN
      }
    }
  ).then(r => r.json());

  if (!result.product) {
    return { status: 'ERROR', reason: 'Product not found' };
  }

  const sampleVariant = result.product.variants.find(v =>
    v.sku && v.sku.includes(`${dwSku}-Sample`)
  );

  if (!sampleVariant) {
    return { status: 'MISSING', reason: 'Sample variant not found' };
  }

  const checks = {
    price: sampleVariant.price === '4.25' ? 'PASS' : `FAIL (${sampleVariant.price})`,
    title: sampleVariant.title && sampleVariant.title.includes('Sample') ? 'PASS' : `FAIL (${sampleVariant.title})`,
    skuFormat: !sampleVariant.sku.includes('Sample-Sample') ? 'PASS' : `FAIL (${sampleVariant.sku})`,
    inventory: sampleVariant.inventory_quantity === 0 ? 'PASS' : `WARN (${sampleVariant.inventory_quantity})`
  };

  return { status: 'FOUND', checks, sku: sampleVariant.sku };
}

(async () => {
  console.log('[VERIFY] Phase 2 Sample Variants Live\n');

  let totalSpots = 0, found = 0, missing = 0, errors = 0;

  for (const vendor of VENDORS) {
    console.log(`[${vendor}]`);
    const skus = await getRandomSKUsForVendor(vendor, 10);
    
    if (skus.length === 0) {
      console.log(`  No SKUs found\n`);
      continue;
    }

    for (const sku of skus) {
      const result = await verifySampleVariant(sku.shopify_id, sku.dw_sku);
      totalSpots++;

      if (result.status === 'FOUND') {
        found++;
        console.log(`  ✓ ${sku.dw_sku} (SKU: ${result.sku}) - ${result.checks.price}, ${result.checks.title}`);
      } else if (result.status === 'MISSING') {
        missing++;
        console.log(`  ✗ ${sku.dw_sku} - MISSING`);
      } else {
        errors++;
        console.log(`  ⚠ ${sku.dw_sku} - ERROR: ${result.reason}`);
      }
    }
    console.log('');
  }

  console.log(`[RESULTS]`);
  console.log(`  Spots checked: ${totalSpots}`);
  console.log(`  Found: ${found}/${totalSpots} (${((found/totalSpots)*100).toFixed(1)}%)`);
  console.log(`  Missing: ${missing}/${totalSpots}`);
  console.log(`  Errors: ${errors}/${totalSpots}`);
  console.log('');

  if (found === totalSpots) {
    console.log('✓ ALL CHECKS PASSED');
  } else if (found >= totalSpots * 0.95) {
    console.log('⚠ MOSTLY PASSED (>95%)');
  } else {
    console.log('✗ CHECKS FAILED (< 95%)');
  }

  process.exit(found >= totalSpots * 0.95 ? 0 : 1);
})().finally(() => pool.end());