// Quick visual check: title, gameplay, mid-swing, slime hit.
import { chromium } from '@playwright/test';

const url = process.env.URL ?? 'http://localhost:4189';
const browser = await chromium.launch({
  args: ['--enable-unsafe-swiftshader', '--mute-audio'],
});
const page = await browser.newPage({ viewport: { width: 390, height: 844 }, hasTouch: true });
page.on('console', (m) => { if (m.type() === 'error') console.log('CONSOLE ERROR:', m.text()); });
page.on('pageerror', (e) => console.log('PAGE ERROR:', e));

await page.goto(url);
await page.waitForFunction(() => window.__game?.phase === 'title');
await page.waitForTimeout(1200);
await page.screenshot({ path: 'shots/1-title.png' });

await page.locator('#title-screen').click();
await page.waitForTimeout(900);
await page.screenshot({ path: 'shots/2-field.png' });

// walk into the grass patch and swing — catch the trail mid-sweep
await page.evaluate(() => {
  const g = window.__game.debug.nearestGrass();
  if (g) window.__game.teleport(g[0], g[1] + 1.0);
});
await page.waitForTimeout(150);
await page.evaluate(() => window.__game.attack());
await page.waitForTimeout(110);
await page.screenshot({ path: 'shots/3-swing-a.png' });
await page.screenshot({ path: 'shots/3-swing-b.png' });
await page.waitForTimeout(350);
await page.screenshot({ path: 'shots/4-after-cut.png' });
console.log('grassCut:', await page.evaluate(() => window.__game.grassCut));

// slime hit
await page.evaluate(() => {
  const s = window.__game.debug.nearestSlime();
  if (s) window.__game.teleport(s[0], s[1] + 1.1);
});
await page.evaluate(() => window.__game.attack());
await page.waitForTimeout(170);
await page.screenshot({ path: 'shots/5-slime-hit.png' });
console.log('slimeHits:', await page.evaluate(() => window.__game.debug.slimeHits()));

// ruin + pots
await page.evaluate(() => window.__game.teleport(10.5, -9.6));
await page.waitForTimeout(400);
await page.screenshot({ path: 'shots/6-ruin.png' });

// desktop framing
await page.setViewportSize({ width: 1280, height: 800 });
await page.waitForTimeout(500);
await page.screenshot({ path: 'shots/7-desktop.png' });

await browser.close();
console.log('done');
