# JBI 파일피커 클릭 재현 — 상단 토글 버튼 실좌표 클릭 후 드롭다운이 뜨는지
import asyncio
from playwright.async_api import async_playwright

async def main():
    errors = []
    async with async_playwright() as p:
        br = await p.chromium.launch(headless=True)
        pg = await br.new_page()
        await pg.set_viewport_size({'width':1400,'height':900})
        pg.on('console', lambda m: errors.append(m.text) if m.type=='error' and 'Failed to load resource' not in m.text else None)
        pg.on('pageerror', lambda e: errors.append('[pageerror] '+str(e)))
        await pg.goto('http://localhost:8090/DOZIKWORKS_OS_v5_locked.dc.html')
        await pg.wait_for_timeout(16000)

        info = await pg.evaluate("""() => {
            const wrap=document.getElementById('dzw-jbi-picker-wrap');
            if(!wrap) return {wrap:false};
            const btn=wrap.firstElementChild;
            const r=btn.getBoundingClientRect();
            return {wrap:true, x:r.x+r.width/2, y:r.y+r.height/2, w:r.width, h:r.height,
                    pick:btn.getAttribute('data-dzw-pick'),
                    stateBefore: window.__dzw ? window.__dzw.state.jbiPickerOpen : 'no-app',
                    hasMethod: !!(window.__dzw && window.__dzw.toggleJbiPicker)};
        }""")
        print('버튼 정보:', info)
        if info.get('wrap') and info.get('w',0)>0:
            await pg.mouse.click(info['x'], info['y'])
            await pg.wait_for_timeout(1200)
            after = await pg.evaluate("""() => ({
                state: window.__dzw.state.jbiPickerOpen,
                dropdownVisible: !!document.querySelector('#dzw-jbi-picker-wrap sc-if div') ||
                    [...document.querySelectorAll('#dzw-jbi-picker-wrap *')].some(e=>e.textContent.includes('없음 (JBI 해제)') && e.offsetParent)
            })""")
            print('클릭 후:', after)
            # 무엇이 클릭을 받았는지
            hit = await pg.evaluate(f"""() => {{
                const el=document.elementFromPoint({info['x']},{info['y']});
                let path=[]; let n=el;
                while(n&&path.length<6){{ path.push(n.tagName+(n.id?'#'+n.id:'')+(n.getAttribute&&n.getAttribute('data-dzw-pick')?'[pick='+n.getAttribute('data-dzw-pick')+']':'')); n=n.parentElement; }}
                return path;
            }}""")
            print('클릭 지점 요소 경로:', hit)
        print('콘솔에러:', errors[:5])
        await br.close()

asyncio.run(main())
