import { chromium } from "@playwright/test";

(async () => {
  console.log("=== TEST WITH CACHE BYPASS ===\n");
  
  const b = await chromium.launch({ headless: false });
  const ctx = await b.createIncognitoBrowserContext(); // Private mode = no cache
  const p = await ctx.newPage();
  
  p.on("console", msg => {
    if (msg.text().includes("[Login]")) {
      console.log(`  ✓ ${msg.text()}`);
    }
  });
  
  try {
    // 캐시 무시하고 로드
    console.log("1. Loading login page (no cache)...");
    await p.goto("http://localhost:5265/login.html?nocache=" + Date.now(), { 
      waitUntil: "networkidle" 
    });
    console.log("   ✓ Loaded\n");
    
    console.log("2. Submitting login...");
    await p.fill("input[name='username']", "admin");
    await p.fill("input[name='password']", "admin");
    await p.click("button[type='submit']");
    console.log("   ✓ Submitted\n");
    
    console.log("3. Waiting 8 seconds...");
    for (let i = 1; i <= 8; i++) {
      await new Promise(r => setTimeout(r, 1000));
      const url = p.url();
      process.stdout.write(`   [${i}s] ${url}\r`);
    }
    
    console.log("\n\n4. FINAL CHECK:");
    const finalUrl = p.url();
    if (finalUrl.includes("/dashboard")) {
      console.log("   ✓✓✓ SUCCESS: Dashboard loaded!");
      const content = await p.content();
      if (content.includes("관리자 대시보드")) {
        console.log("   ✓ Dashboard content confirmed!");
      }
    } else if (finalUrl.includes("/not-found")) {
      console.log("   ✗ /not-found (auth failed)");
    } else {
      console.log("   URL: " + finalUrl);
    }
    
    await p.screenshot({ path: "./final-test-no-cache.png", fullPage: true });
    
  } catch (e) {
    console.error("Error:", e.message);
  } finally {
    await b.close();
  }
})();
