← back to Quadrille Showroom

proto/verify-v8.cjs

74 lines

const { chromium } = require('playwright');
const path = require('path');
const fs = require('fs');

(async () => {
  const shotsDir = path.join(__dirname, 'shots');
  fs.mkdirSync(shotsDir, { recursive: true });
  const fileUrl = 'file://' + path.join(__dirname, 'v8-swipe.html');

  const browser = await chromium.launch();
  const ctx = await browser.newContext({ viewport: { width: 1280, height: 860 }, deviceScaleFactor: 2 });
  const page = await ctx.newPage();

  const errors = [];
  page.on('console', m => { if (m.type() === 'error') errors.push(m.text()); });
  page.on('pageerror', e => errors.push('PAGEERROR: ' + e.message));

  // Fulfill the live-API request with REAL data fetched server-side (Node),
  // proving the page renders the actual showroom payload — bypasses the
  // file:// CORS wall that only exists for the standalone testing case.
  let liveOk = false, liveCount = 0;
  await page.route('**/api/showroom/products**', async route => {
    try {
      const res = await fetch('http://127.0.0.1:7690/api/showroom/products?limit=120');
      const body = await res.text();
      liveOk = true;
      try { liveCount = (JSON.parse(body).products || []).length; } catch(_) {}
      route.fulfill({ status: 200, contentType: 'application/json', body });
    } catch (e) {
      route.abort();
    }
  });

  await page.goto(fileUrl, { waitUntil: 'networkidle' });
  // wait for first card to render
  await page.waitForSelector('.card[data-depth="0"]', { timeout: 8000 });
  await page.waitForTimeout(900); // let swatch images load

  // initial screenshot
  await page.screenshot({ path: path.join(shotsDir, 'v8-swipe.png') });

  // drive: keep, keep, skip, keep, skip  -> expect 3 favorites
  const seq = ['keep','keep','skip','keep','skip'];
  for (const v of seq) {
    await page.click(v === 'keep' ? '#keepBtn' : '#skipBtn');
    await page.waitForTimeout(520);
  }

  const favCount = await page.$eval('#favCt', e => e.textContent.trim());
  const seenCount = await page.$eval('#seenCt', e => e.textContent.trim());
  const favRows = await page.$$eval('.fav', n => n.length);

  // test undo
  await page.click('#undoBtn');
  await page.waitForTimeout(400);
  const seenAfterUndo = await page.$eval('#seenCt', e => e.textContent.trim());

  // test keyboard arrow keep
  await page.keyboard.press('ArrowRight');
  await page.waitForTimeout(520);

  // final screenshot with favorites tray filled
  await page.screenshot({ path: path.join(shotsDir, 'v8-swipe-filled.png') });

  console.log(JSON.stringify({
    liveApiUsed: liveOk, liveApiCount: liveCount,
    favCount, seenCount, favRows, seenAfterUndo,
    consoleErrors: errors,
    pass: errors.length === 0 && favRows > 0 && liveOk
  }, null, 2));

  await browser.close();
})().catch(e => { console.error('VERIFY FAILED:', e); process.exit(1); });