import asyncio
import re
from playwright import async_api
from playwright.async_api import expect

async def run_test():
    pw = None
    browser = None
    context = None

    try:
        pw = await async_api.async_playwright().start()
        browser = await pw.chromium.launch(
            headless=True,
            args=[
                "--window-size=1280,720",
                "--disable-dev-shm-usage",
                "--ipc=host",
                "--single-process"
            ],
        )
        context = await browser.new_context()
        context.set_default_timeout(15000)
        page = await context.new_page()
        # -> navigate
        await page.goto("http://localhost:8890/login")
        try:
            await page.wait_for_load_state("domcontentloaded", timeout=5000)
        except Exception:
            pass
        
        # -> Fill the Email and Password fields with admin credentials and submit the Sign in button.
        # email input placeholder="agent@example.com"
        elem = page.locator("xpath=/html/body/div/div/div/div[2]/div[2]/form/div/input").nth(0)
        await elem.wait_for(state="visible", timeout=10000)
        await elem.fill("admin@example.com")
        
        # -> Fill the Email and Password fields with admin credentials and submit the Sign in button.
        # password input placeholder="••••••••"
        elem = page.locator("xpath=/html/body/div/div/div/div[2]/div[2]/form/div[2]/input").nth(0)
        await elem.wait_for(state="visible", timeout=10000)
        await elem.fill("Admin123!")
        
        # -> Fill the Email and Password fields with admin credentials and submit the Sign in button.
        # button "Sign in"
        elem = page.locator("xpath=/html/body/div/div/div/div[2]/div[2]/form/button").nth(0)
        await elem.wait_for(state="visible", timeout=10000)
        await elem.click()
        
        # -> Open the Channels page by clicking the 'Channels' item in the left navigation, then find the disconnected channel to reconnect.
        # link "Channels"
        elem = page.locator("xpath=/html/body/div/div/aside/nav/a[4]").nth(0)
        await elem.wait_for(state="visible", timeout=10000)
        await elem.click()
        
        # -> Click the 'Reconnect' button for the BritsMTN channel (element index 373), then observe the UI for a status update confirming reconnection.
        # button "Reconnect"
        elem = page.locator("xpath=/html/body/div/div/div/main/div/div[2]/div[2]/div/div[2]/button").nth(0)
        await elem.wait_for(state="visible", timeout=10000)
        await elem.click()
        
        # --> Test blocked (AST guard fallback)
        raise AssertionError("Test blocked during agent run: " + "TEST BLOCKED The test could not be run \u2014 reconnection requires a phone to scan the WhatsApp QR code which cannot be simulated in this environment. Observations: - Clicking the Reconnect button opened a 'Scan QR Code' modal showing 'Generating QR code\u2026' - The BritsMTN channel card still displays the 'disconnected' badge")
        await asyncio.sleep(5)
    finally:
        if context:
            await context.close()
        if browser:
            await browser.close()
        if pw:
            await pw.stop()

asyncio.run(run_test())
    