/**
 * Minimal Playwright test — debug the "missing ) after argument list" error.
 */
import { chromium } from 'file:///C:/Users/Zjz/AppData/Roaming/npm/node_modules/@playwright/mcp/node_modules/playwright/index.mjs';

const BASE = 'http://127.0.0.1:8000';

async function main() {
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage();

  // Track ALL errors
  page.on('pageerror', err => {
    console.log('PAGE_ERROR:', err.message);
    console.log('  Stack:', err.stack?.substring(0, 200));
  });
  page.on('console', msg => {
    if (msg.type() === 'error') {
      const text = msg.text();
      console.log('CONSOLE_ERROR:', text.substring(0, 300));
      // Try to get the stack trace
      try {
        const stack = msg.stackTrace?.();
        if (stack && stack.length > 0) {
          console.log('  At:', stack[0].url?.substring(0, 100), stack[0].lineNumber, stack[0].columnNumber);
        }
      } catch(e) {}
    }
  });
  page.on('weberror', err => {
    console.log('WEB_ERROR:', err.message);
  });

  console.log('Loading page...');
  await page.goto(`${BASE}/simple/`, { waitUntil: 'load', timeout: 15000 });

  console.log('Waiting for scripts to settle...');
  await new Promise(r => setTimeout(r, 2000));

  // Check if key functions exist
  const exists = await page.evaluate(() => ({
    typeofSA: typeof SA,
    typeofGoStep: typeof goStep,
    typeofHandleFiles: typeof handleFiles,
    typeofShowError: typeof showError,
    typeofEscapeHtml: typeof escapeHtml,
    leafletExists: typeof L !== 'undefined',
    chartExists: typeof Chart !== 'undefined',
  }));
  console.log('Window globals:', JSON.stringify(exists, null, 2));

  await browser.close();
}

main().catch(e => console.error(e));
