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

async function runTests() {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  
  try {
    console.log('1. Navigating to http://localhost:3000...');
    await page.goto('http://localhost:3000', { waitUntil: 'networkidle' });
    
    console.log('2. Taking initial screenshot...');
    await page.screenshot({ path: 'screenshot-01-initial.png' });
    
    console.log('3. Looking for "통합 테스트" button...');
    // Wait for the integration test button - it should be in the sidebar
    const testButton = await page.locator('text=통합 테스트').first();
    
    if (await testButton.isVisible()) {
      console.log('4. Found "통합 테스트" button. Clicking it...');
      await testButton.click();
      
      console.log('5. Waiting for test dashboard to load...');
      await page.waitForLoadState('networkidle');
      await page.screenshot({ path: 'screenshot-02-test-dashboard.png' });
      
      console.log('6. Looking for test execution buttons...');
      
      // Get all visible text to understand the UI
      const pageText = await page.evaluate(() => document.body.innerText);
      console.log('Page content preview:', pageText.substring(0, 500));
      
      // Look for run test buttons
      const buttons = await page.locator('button').allTextContents();
      console.log('\nAvailable buttons:', buttons);
      
      // Try to find and click a "전체 통합 테스트 실행" or similar button
      const runAllButton = await page.locator('button:has-text("전체 통합 테스트 실행")').first();
      
      if (await runAllButton.isVisible()) {
        console.log('7. Found "전체 통합 테스트 실행" button. Clicking it...');
        await runAllButton.click();
        
        console.log('8. Waiting for tests to run...');
        // Wait for test results to appear
        await page.waitForTimeout(5000);
        await page.screenshot({ path: 'screenshot-03-test-results.png' });
        
        // Try to extract test results
        const resultText = await page.evaluate(() => document.body.innerText);
        console.log('\n=== TEST RESULTS ===');
        console.log(resultText);
      } else {
        // Try alternative button names
        const allButtons = await page.locator('button').all();
        let found = false;
        
        for (const btn of allButtons) {
          const text = await btn.textContent();
          if (text.includes('테스트') && text.includes('실행')) {
            console.log(`7. Found test button: "${text}". Clicking it...`);
            await btn.click();
            await page.waitForTimeout(5000);
            await page.screenshot({ path: 'screenshot-03-test-results.png' });
            found = true;
            break;
          }
        }
        
        if (!found) {
          console.log('No test execution button found. Taking screenshot for manual inspection...');
          await page.screenshot({ path: 'screenshot-03-test-dashboard-full.png' });
        }
      }
      
    } else {
      console.log('ERROR: Could not find "통합 테스트" button');
      await page.screenshot({ path: 'screenshot-02-error-not-found.png' });
    }
    
  } catch (error) {
    console.error('ERROR:', error);
    await page.screenshot({ path: 'screenshot-error.png' });
  } finally {
    await browser.close();
  }
}

runTests().catch(console.error);
