#!/usr/bin/env node

const { chromium } = require('playwright');

async function testPlaywright() {
  console.log('🧪 Testing Playwright installation...');

  try {
    // Launch browser
    console.log('  🚀 Launching Chromium...');
    const browser = await chromium.launch({
      headless: true
    });

    console.log('  ✅ Browser launched successfully');

    // Create page
    const page = await browser.newPage();
    console.log('  ✅ Page created');

    // Navigate to a simple page
    console.log('  🔗 Navigating to example.com...');
    await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });

    // Test selector
    const title = await page.title();
    console.log(`  ✅ Page title: "${title}"`);

    // Test waitForFunction
    await page.waitForFunction(() => document.title === 'Example Domain', { timeout: 5000 });
    console.log('  ✅ waitForFunction works');

    // Test locator
    const h1 = await page.locator('h1').textContent();
    console.log(`  ✅ Locator found: "${h1}"`);

    // Close browser
    await browser.close();
    console.log('  ✅ Browser closed');

    console.log('\n🎉 All tests passed! Playwright is ready to use.');
    return true;

  } catch (error) {
    console.error(`\n❌ Test failed: ${error.message}`);
    return false;
  }
}

testPlaywright()
  .then(success => {
    process.exit(success ? 0 : 1);
  })
  .catch(error => {
    console.error('Unexpected error:', error);
    process.exit(1);
  });
