// The Penguin Test (penguins/site/index.html): errors, overflow, controls clickable, panels and tables filled, at 8 viewports;
// then behaviour: the first selection, the globe and North-pole counts, the speed-limit slider, playback, colony switch, whole-map view.
// CH= node penguins.mjs [url] (default: the built file; LIVE=1 for https://penguins.aethercosmology.com)
import { chromium } from 'playwright-core';
import { fileURLToPath } from 'url';
import path from 'path';
import fs from 'fs';
const here = path.dirname(fileURLToPath(import.meta.url));
const url = process.argv[2] || (process.env.LIVE ? 'https://penguins.aethercosmology.com/' : 'file://' + path.join(here, '../penguins/site/index.html'));
const VP = [[1920, 1080], [1680, 1050], [1440, 900], [1280, 720], [1024, 768], [820, 1180], [390, 844], [360, 740]];
const b = await chromium.launch({ executablePath: process.env.CH });
let fails = 0;
const fail = (...a) => { fails++; console.log('FAIL', ...a); };
for (const [w, h] of VP) {
const p = await b.newPage({ viewport: { width: w, height: h } });
const errs = [];
p.on('pageerror', e => errs.push(e.message));
p.on('console', m => m.type() === 'error' && errs.push(m.text()));
await p.goto(url, { waitUntil: 'load' });
await p.waitForTimeout(1200);
const r = await p.evaluate(() => {
const out = { overflow: document.documentElement.scrollWidth - innerWidth, blocked: [], empty: [], clipped: [] };
for (const el of document.querySelectorAll('button, input, select, a')) {
el.scrollIntoView({ block: 'center' });
const b = el.getClientRects()[0];
if (!b || !b.width) continue;
const hit = document.elementFromPoint(b.left + b.width / 2, b.top + b.height / 2);
if (hit !== el && !el.contains(hit) && !(hit && hit.closest('label') && hit.closest('label') === el.closest('label')))
out.blocked.push((el.id || el.textContent.trim().slice(0, 20)) + ' <- ' + (hit && (hit.id || hit.className || hit.tagName)));
}
for (const c of document.querySelectorAll('.map canvas')) {
const g = c.getContext('2d'), d = g.getImageData(0, 0, c.width, c.height).data; let lit = 0;
for (let i = 0; i < d.length; i += 4 * 97) if (d[i] + d[i + 1] + d[i + 2] > 60) lit++;
if (!c.width || lit < 20) out.empty.push('canvas ' + c.closest('.panel').dataset.key);
}
for (const dd of document.querySelectorAll('.ro dd')) { if (dd.textContent.trim() === '—') out.empty.push('readout'); if (dd.scrollWidth > dd.clientWidth + 1) out.clipped.push(dd.textContent); }
if (document.querySelectorAll('#animals tbody tr').length < 1) out.empty.push('animals table');
if (document.querySelectorAll('#species tbody tr').length < 5) out.empty.push('species table');
window.scrollTo(0, 0);
return out;
});
if (errs.length) fail(w, h, 'errors', errs.slice(0, 3));
if (r.overflow > 0) fail(w, h, 'horizontal overflow', r.overflow);
if (r.blocked.length) fail(w, h, 'blocked', r.blocked.slice(0, 5));
if (r.empty.length) fail(w, h, 'empty', [...new Set(r.empty)]);
if (r.clipped.length) fail(w, h, 'clipped', r.clipped.slice(0, 4));
await p.screenshot({ path: path.join(here, `out/penguins_${w}x${h}.png`) });
await p.close();
}
// behaviour
const DATA = JSON.parse(fs.readFileSync(path.join(here, '../penguins/data/penguins.json'), 'utf8'));
const [FSP, FCOL, FYR] = DATA.default.split('|');
const FIRST = DATA.tracks.filter(t => t.sp === FSP && t.colony === FCOL && t.year === FYR);
const p = await b.newPage({ viewport: { width: 1440, height: 900 } });
const errs = []; p.on('pageerror', e => errs.push(e.message));
await p.goto(url, { waitUntil: 'load' });
await p.waitForTimeout(800);
const n = k => p.evaluate(k => document.querySelector(`.panel[data-key="${k}"] [data-f="n"]`).textContent, k);
const first = await p.evaluate(() => [document.getElementById('grp').selectedOptions[0].textContent, document.getElementById('clock').textContent]);
console.log('first selection:', first.join(' | '), '| globe', await n('globe'), '| AE', await n('ae'));
if (!/Pointe Géologie, Terre Adélie, 2005 \(11\)/.test(first[0])) fail('first selection is not the 2005 Pointe Géologie emperors', first[0]);
if (!/^0 of 11/.test(await n('globe'))) fail('globe should have no track beyond the limit', await n('globe'));
if (!/^[1-9]\d* of 11/.test(await n('ae'))) fail('North-pole AE should have tracks beyond the limit', await n('ae'));
if (!/Day (\d+) of \1$/.test(first[1])) fail('page should open with the full tracks drawn', first[1]);
// the slider: each readout must equal the count from the data at that limit
for (const v of [6, 11.9, 18, 24]) {
await p.evaluate(v => { const s = document.getElementById('lim'); s.value = v; s.dispatchEvent(new Event('input')); }, v);
await p.waitForTimeout(120);
const got = await p.evaluate(() => [...document.querySelectorAll('.panel')].map(x => parseInt(x.querySelector('[data-f="n"]').textContent)));
const want = ['globe', 'ae', 'dualpole', 'mercator'].map(m => FIRST.filter(t => t.st[m].need !== null && t.st[m].need > v).length);
console.log(`limit ${v} km/h: beyond`, got.join(' / '));
if (want && want.join() !== got.join()) fail('readout differs from the data at', v, got, want);
}
await p.evaluate(() => { const s = document.getElementById('lim'); s.value = s.defaultValue; s.dispatchEvent(new Event('input')); });
// playback
await p.click('#play'); await p.waitForTimeout(1500);
const playing = await p.evaluate(() => document.getElementById('clock').textContent);
console.log('playing:', playing);
if (!/^Day [0-9] of 80/.test(playing)) fail('play should restart from day 0 and advance', playing);
await p.click('#play');
// another colony and the whole-map view
const opt = await p.evaluate(() => { const o = [...document.querySelectorAll('#grp option')].find(o => /Cape Crozier/.test(o.textContent)); return o && o.value; });
if (opt) { await p.selectOption('#grp', opt); await p.waitForTimeout(400); console.log('Cape Crozier:', await p.evaluate(() => document.getElementById('grp').selectedOptions[0].textContent), '| AE', await n('ae'), '| globe', await n('globe')); }
else fail('no Cape Crozier option');
await p.click('#view button[data-v="all"]'); await p.waitForTimeout(400);
await p.locator('.grid').screenshot({ path: path.join(here, 'out/penguins_whole.png') });
if (errs.length) fail('errors', errs.slice(0, 3));
console.log(fails ? `penguins: ${fails} FAILS` : 'penguins OK');
await b.close();
process.exit(fails ? 1 : 0);