import asyncio
from playwright.async_api import async_playwright

async def run():
    async with async_playwright() as p:
        br = await p.chromium.launch(channel='msedge', headless=True)
        pg = await br.new_page(viewport={'width':1280,'height':800})
        await pg.goto('http://localhost:8090/DOZIKWORKS_OS_v5_locked.dc.html', timeout=10000)
        await pg.wait_for_timeout(3000)

        # 모듈 존재 확인
        r = await pg.evaluate('''() => {
            const ep = window.DZW5_EDGEPATH;
            return {
                module: typeof ep,
                initBtn: typeof ep?.initBtn,
                buildEdgeCache: typeof ep?.buildEdgeCache,
                showEdgeChain: typeof ep?.showEdgeChain,
                handlePointerDown: typeof ep?.handlePointerDown,
                btn: document.getElementById("dzw-edge-mode-btn")?.textContent
            };
        }''')
        print('모듈:', r)

        # 버튼 클릭 ON
        await pg.locator('#dzw-edge-mode-btn').click()
        await pg.wait_for_timeout(400)
        btn_text = await pg.locator('#dzw-edge-mode-btn').text_content()
        edge_mode = await pg.evaluate('() => window.__dzw?._edgeMode')
        print('클릭후 버튼:', btn_text, '/ _edgeMode:', edge_mode)
        await pg.screenshot(path='edgepath_on.png')
        print('스크린샷: edgepath_on.png')

        # 버튼 클릭 OFF
        await pg.locator('#dzw-edge-mode-btn').click()
        await pg.wait_for_timeout(300)
        print('OFF후:', await pg.locator('#dzw-edge-mode-btn').text_content(),
              '/ _edgeMode:', await pg.evaluate('() => window.__dzw?._edgeMode'))

        await br.close()
        print('검증 완료')

asyncio.run(run())
