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

def test_type_and_check():
    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)
        
        textarea = page.locator('textarea[placeholder*="Nhắn tin"]').first
        textarea.click()
        time.sleep(1)
        
        # Use page.keyboard.type or type()
        textarea.press_sequentially("Xin chao Muse, day la tin nhan kiem tra", delay=20)
        time.sleep(2)
        
        info = page.evaluate("""() => {
            const btns = Array.from(document.querySelectorAll('button')).map(b => ({
                text: b.innerText,
                aria: b.getAttribute('aria-label'),
                pel: b.getAttribute('data-pel-click'),
                disabled: b.disabled,
                html: b.outerHTML
            }));
            return btns.filter(b => b.aria || b.pel || b.text);
        }""")
        print(f"Total buttons after typing: {len(info)}")
        for b in info:
            if any(k in str(b).lower() for k in ["gửi", "send", "chat", "dừng", "stop", "submit"]):
                print("MATCH:", b)
                
        page.screenshot(path="04_Assets/after_type_buttons.png")

if __name__ == '__main__':
    test_type_and_check()
