import fs from 'node:fs';
import { chromium } from 'playwright';

const url = process.env.AGENT_WORKBENCH_URL || 'http://127.0.0.1:4319';
fs.mkdirSync('.qa/screenshots', { recursive: true });
const browser = await chromium.launch({ channel: 'msedge', headless: true });
try {
  const page = await browser.newPage({ viewport: { width: 1440, height: 960 }, deviceScaleFactor: 1 });
  const errors = [];
  page.on('pageerror', error => errors.push(error.message));
  await page.goto(url, { waitUntil: 'domcontentloaded' });
  await page.waitForLoadState('networkidle', { timeout: 6000 }).catch(() => {});
  await page.locator('.connection.connected').waitFor({ timeout: 8000 });
  await page.screenshot({ path: '.qa/screenshots/initial-desktop.png', fullPage: true });
  fs.writeFileSync('.qa/initial-dom.html', await page.content());
  console.log(JSON.stringify({ url, errors, buttons: await page.getByRole('button').allTextContents(),
    labeledButtons: await page.locator('button[aria-label]').evaluateAll(nodes => nodes.map(node => node.getAttribute('aria-label'))),
    text: (await page.locator('body').innerText()).slice(0, 3500) }, null, 2));
} finally { await browser.close(); }
