← back to Flappy Sky

test/e2e.mjs

56 lines

import { chromium } from 'playwright';
const errors = [], logs = [];
const browser = await chromium.launch();
const page = await browser.newPage({ viewport: { width: 480, height: 800 } });
page.on('console', m => { if (m.type()==='error') errors.push('console.error: '+m.text()); logs.push(m.type()+': '+m.text()); });
page.on('pageerror', e => errors.push('pageerror: '+e.message));
await page.goto('file://'+process.cwd()+'/index.html');
await page.waitForTimeout(400);

// helper to read internal state via a tiny hook we expose by evaluating
async function probe() {
  return await page.evaluate(() => {
    return { title: document.title, w: window.innerWidth };
  });
}
console.log('loaded:', JSON.stringify(await probe()));

// Start + flap a bunch via keyboard Space
await page.focus('canvas').catch(()=>{});
for (let i=0;i<12;i++){ await page.keyboard.press('Space'); await page.waitForTimeout(180); }
await page.waitForTimeout(200);
const scoreVisible = await page.evaluate(()=>{
  const s=document.getElementById('scoreTop');
  return { hidden: s.classList.contains('hidden'), score: document.getElementById('scoreVal').textContent };
});
console.log('after flaps:', JSON.stringify(scoreVisible));

// Let it fall to trigger game over
await page.waitForTimeout(2500);
const go = await page.evaluate(()=>({
  resultShown: document.getElementById('result').classList.contains('show'),
  overlayHidden: document.getElementById('overlay').classList.contains('hidden'),
  best: document.getElementById('bestVal').textContent
}));
console.log('gameover state:', JSON.stringify(go));

// Test pause
await page.keyboard.press('Space'); // restart -> ready or flap
await page.waitForTimeout(300);
await page.keyboard.press('Space'); // start playing
await page.waitForTimeout(200);
await page.keyboard.press('KeyP');
await page.waitForTimeout(200);
const paused = await page.evaluate(()=>document.getElementById('pauseVeil').classList.contains('show'));
console.log('pause veil shown:', paused);

// screenshot for a visual artifact
await page.keyboard.press('KeyP'); // resume
await page.waitForTimeout(300);
await page.screenshot({ path: '/tmp/flappy_shot.png' });

await browser.close();
console.log('CONSOLE ERRORS:', errors.length);
errors.forEach(e=>console.log('  '+e));
console.log(errors.length===0 ? 'PASS: no runtime errors' : 'FAIL');