import sys
import time
from pathlib import Path
sys.path.append(str(Path("05_Production_Pipeline").resolve()))
from invisible_playwright import InvisiblePlaywright
from muse_invpw_driver import sync_worker_profile, DEFAULT_PROFILE

def test_click_new_chat():
    sync_worker_profile()
    with InvisiblePlaywright(profile_dir=DEFAULT_PROFILE, headless=True) as ctx:
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        page.goto("https://muse.ai", wait_until="domcontentloaded", timeout=30000)
        time.sleep(3)
        
        new_chat_btn = page.locator('button[aria-label="Đoạn chat phụ mới"]').first
        print(f"Count: {new_chat_btn.count()}, visible: {new_chat_btn.is_visible()}")
        new_chat_btn.click()
        time.sleep(3)
        
        screenshot_path = Path("04_Assets/after_click_new_chat.png").resolve()
        page.screenshot(path=str(screenshot_path))
        print("Screenshot saved to:", screenshot_path)
        
        print("Page URL:", page.url)
        print("Active element:", page.evaluate("() => document.activeElement ? document.activeElement.outerHTML : 'none'"))

if __name__ == "__main__":
    test_click_new_chat()
