← back to Games Agentabrams

tests/play-flappy-sky.mjs

29 lines

import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import path from 'path'; import http from 'http'; import fs from 'fs';
const require = createRequire(import.meta.url);
let chromium; try { ({chromium}=require('/Users/macstudio3/.npm-global/lib/node_modules/playwright')); } catch { ({chromium}=require('playwright')); }
const root = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const MIME={'.html':'text/html','.json':'application/json','.png':'image/png','.js':'text/javascript'};
const server=http.createServer((req,res)=>{let p=decodeURIComponent(req.url.split('?')[0]);if(p.endsWith('/'))p+='index.html';const f=path.join(root,p);if(!f.startsWith(root)||!fs.existsSync(f)||fs.statSync(f).isDirectory()){res.writeHead(404);res.end('nf');return;}res.writeHead(200,{'Content-Type':MIME[path.extname(f)]||'text/plain'});fs.createReadStream(f).pipe(res);});
await new Promise(r=>server.listen(0,'127.0.0.1',r));
const base=`http://127.0.0.1:${server.address().port}`;
const browser=await chromium.launch();
const ctx=await browser.newContext({viewport:{width:480,height:720},recordVideo:{dir:'tests/rec',size:{width:480,height:720}}});
const page=await ctx.newPage();
const errs=[]; page.on('pageerror',e=>errs.push('PAGEERR '+e.message)); page.on('console',m=>{if(m.type()==='error')errs.push('CONSOLE '+m.text());});
await page.goto(base+'/games/flappy-sky/',{waitUntil:'networkidle'});
await page.waitForTimeout(500);
// read game state via a probe: we can't reach closure vars, so infer from DOM + a scripted play
const snap = async(label)=>{ const s=await page.evaluate(()=>({score:document.getElementById('scoreVal')?.textContent, overlayHidden:document.getElementById('overlay')?.classList.contains('hidden'), resultShown:document.getElementById('result')?.classList.contains('show'), prompt:document.getElementById('prompt')?.textContent, best:document.getElementById('bestVal')?.textContent})); console.log(label, JSON.stringify(s)); return s;};
await snap('READY');
// start + flap steadily like a real player for ~8s
await page.mouse.move(240,360);
for(let i=0;i<20;i++){ await page.mouse.click(240,360); await page.waitForTimeout(340); if(i%15===0) await snap('t'+i); }
await snap('AFTER-PLAY');
await page.screenshot({path:'tests/rec/flappy-sky-FIXED.png'});
await page.waitForTimeout(300);
await ctx.close(); // finalizes video
await browser.close(); server.close();
console.log('ERRORS:', errs.length? errs.join('\n') : 'none');