import asyncio
from playwright.async_api import async_playwright

JS = """
() => {
    var app = window.__dzw;
    var spans = [];
    document.querySelectorAll('span').forEach(function(s){
        var t = s.textContent.trim();
        if (t && t.length <= 20) spans.push(t);
    });
    return {
        spans: spans.slice(0, 20),
        robotName: app && app.state && app.state.robotName,
        fileName: app && app.state && app.state.fileName,
        mode: app && app.state && app.state.mode,
        joints: app && app.state && app.state.joints,
        hasSGN: (app && app.poseRobot || '').toString().includes('SGN')
    };
}
"""

async def main():
    async with async_playwright() as p:
        br = await p.chromium.launch(headless=True)
        pg = await br.new_page()
        await pg.set_viewport_size({'width':1536,'height':1024})
        await pg.goto('http://localhost:8091/DOZIKWORKS_OS_v5_cleanpath.dc.html')
        await pg.wait_for_timeout(22000)
        r = await pg.evaluate(JS)
        print('=== v5 런타임 상태 (8091) ===')
        print(f'robotName: {r["robotName"]}')
        print(f'fileName:  {r["fileName"]}')
        print(f'mode:      {r["mode"]}')
        print(f'joints:    {r["joints"]}')
        print(f'hasSGN:    {r["hasSGN"]}')
        print(f'spans:     {r["spans"]}')
        # 스크린샷
        await pg.screenshot(path='state_v5_8091.png')
        print('screenshot: state_v5_8091.png')
        await br.close()

asyncio.run(main())
