const { connect } = require("puppeteer-real-browser");

async function run() {
  console.log("=== Testing app.weavy.ai/signin with puppeteer-real-browser ===");
  const { browser, page } = await connect({
    headless: false,
    turnstile: true,
    connectOption: { defaultViewport: null }
  });

  try {
    await page.goto("https://app.weavy.ai/signin", { waitUntil: "domcontentloaded" });
    console.log("Waiting 30 seconds for automatic Turnstile solver to pass...");
    await new Promise(r => setTimeout(r, 30000));
    
    const cookies = await page.cookies();
    const cfClearance = cookies.find(c => c.name === 'cf_clearance');
    
    // Check elements
    const buttons = await page.evaluate(() => {
      return Array.from(document.querySelectorAll('button, a')).map(el => el.textContent.trim());
    });
    
    console.log("Cookies length:", cookies.length);
    console.log("cf_clearance exists:", !!cfClearance);
    console.log("Found text contents on page elements:", buttons);
    
    // Take screenshot
    await page.screenshot({ path: "weavy_puppeteer_test.png" });
    console.log("Screenshot saved to weavy_puppeteer_test.png");
  } catch (err) {
    console.error("Error:", err);
  } finally {
    await browser.close();
  }
}

run().catch(console.error);
