import { chromium } from 'playwright';
import { spawn } from 'child_process';
const PORT = 4215;
const server = spawn('node', ['node_modules/vite/bin/vite.js', 'preview', '--port', String(PORT), '--strictPort'], { cwd: process.cwd(), stdio: 'pipe' });
const waitServer = () => new Promise((res, rej) => { const t=setTimeout(()=>rej('timeout'),30000); server.stdout.on('data',d=>{if(d.toString().includes('Local:')){clearTimeout(t);res();}}); server.stderr.on('data',d=>{if(d.toString().includes('Local:')){clearTimeout(t);res();}});});
const checks={};
const getActive = (page) => page.evaluate(() => window.game.scene.scenes.filter(s=>s.scene.isActive()).map(s=>s.scene.key));
async function walkToScene(page, dir, wantScene, maxSteps=20){
  await page.keyboard.down(dir);
  for (let i=0;i<maxSteps;i++){ await page.waitForTimeout(250); const a=await getActive(page); if(a.includes(wantScene)){ await page.keyboard.up(dir); return true; } }
  await page.keyboard.up(dir); return false;
}
try {
  await waitServer();
  const browser = await chromium.launch();
  const page = await browser.newPage({ viewport: { width: 960, height: 540 } });
  page.on('pageerror', (e) => console.log('PAGEERR', e.message));
  await page.goto(`http://localhost:${PORT}/`, { waitUntil: 'load', timeout: 15000 });
  await page.waitForTimeout(800);
  await page.keyboard.press('Enter'); await page.waitForTimeout(1200);
  for (let i=0;i<4;i++){ await page.keyboard.press('e'); await page.waitForTimeout(150); }
  await page.click('canvas'); await page.waitForTimeout(300);

  // house -> plaza (walk down to bottom edge door)
  checks.house_to_plaza = await walkToScene(page, 's', 'PlazaScene');
  await page.waitForTimeout(600);
  // plaza -> bakery (walk left to left edge). Player spawns at door_house bottom, offset upward.
  checks.plaza_to_bakery = await walkToScene(page, 'a', 'BakeryScene');
  await page.waitForTimeout(600);
  // bakery -> plaza (walk down)
  checks.bakery_to_plaza = await walkToScene(page, 's', 'PlazaScene');
  await page.waitForTimeout(600);
  // plaza -> shop (walk right)
  checks.plaza_to_shop = await walkToScene(page, 'd', 'ShopScene');
  await page.waitForTimeout(600);
  // shop -> plaza
  checks.shop_to_plaza = await walkToScene(page, 's', 'PlazaScene');
  await page.waitForTimeout(600);
  // plaza -> clinic (walk up to top edge)
  checks.plaza_to_clinic = await walkToScene(page, 'w', 'ClinicScene');
  await page.waitForTimeout(600);
  // clinic -> plaza
  checks.clinic_to_plaza = await walkToScene(page, 's', 'PlazaScene');
  await page.waitForTimeout(600);
  // plaza -> lake (walk left - lake at col0,row10, bakery at col0,row9; both left edge)
  // player may hit bakery first; let's walk up a bit then left
  await page.keyboard.down('w'); await page.waitForTimeout(800); await page.keyboard.up('w');
  // now walk down to row 10 area then left
  await page.keyboard.down('s'); await page.waitForTimeout(400); await page.keyboard.up('s');
  checks.plaza_to_lake = await walkToScene(page, 'a', 'LakeScene');
  await page.waitForTimeout(600);
  // lake -> plaza (door_plaza at right edge col 22,8 — walk right)
  checks.lake_to_plaza = await walkToScene(page, 'd', 'PlazaScene');
  await page.waitForTimeout(600);
  // plaza -> field (walk right, field at col29 row10)
  checks.plaza_to_field = await walkToScene(page, 'd', 'FieldScene');
  await page.waitForTimeout(600);
  // field -> plaza
  checks.field_to_plaza = await walkToScene(page, 'a', 'PlazaScene');
  await page.waitForTimeout(600);

  await browser.close();
} catch(e){ console.log('FAIL', e.message); } finally { server.kill('SIGTERM'); }
console.log('CHECKS:', JSON.stringify(checks, null, 2));
