← back to Consulting Designerwallcoverings Com

verification/sort-reset.e2e.cjs

31 lines

#!/usr/bin/env node
const { chromium } = require('/Users/macstudio3/Projects/Designer-Wallcoverings/node_modules/playwright');

(async () => {
  const base = process.env.BASE_URL || 'http://127.0.0.1:19622';
  const browser = await chromium.launch({
    executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    headless: true,
  });
  const context = await browser.newContext({ httpCredentials: { username: 'admin', password: 'DW2024!' } });
  const page = await context.newPage();
  const errors = [];
  page.on('pageerror', error => errors.push(error.message));
  page.on('console', message => message.type() === 'error' && errors.push(message.text()));
  await page.goto(`${base}/versions/atelier.html`, { waitUntil: 'domcontentloaded' });
  await page.waitForSelector('.gcard');
  const titles = () => page.locator('.gcard').evaluateAll(cards => cards.map(card => card.dataset.title));
  const natural = await titles();
  await page.selectOption('#sortsel', 'title');
  const sorted = await titles();
  await page.selectOption('#sortsel', 'newest');
  const restored = await titles();
  const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
  if (same(natural, sorted)) throw new Error('Title sort did not change the natural order');
  if (!same(natural, restored)) throw new Error('Newest did not restore the natural order');
  const appErrors = errors.filter(message => !/favicon|ERR_FAILED|fonts\.googleapis/i.test(message));
  if (appErrors.length) throw new Error(`Application errors: ${appErrors.join(' | ')}`);
  console.log(JSON.stringify({ ok: true, cards: natural.length, title_sort_changed: true, newest_restored: true, ignored_external_errors: errors.length - appErrors.length }));
  await browser.close();
})().catch(error => { console.error(error.stack || error.message); process.exit(1); });