from playwright.sync_api import sync_playwright


URL = "http://localhost:8765/view/全局分类树-需求热度与关系分析.html"


def wait_until_ready(page):
    page.goto(URL)
    page.locator("#demand-count").wait_for(state="visible")
    page.wait_for_function(
        "document.querySelector('#demand-count').textContent.trim() !== '—'"
    )


def test_level_scroll(page):
    wait_until_ready(page)
    page.locator('[data-start-depth="3"]').click()
    viewport = page.locator("#icicle-viewport")
    dimensions = viewport.evaluate(
        "(element) => ({clientHeight: element.clientHeight, scrollHeight: element.scrollHeight})"
    )
    assert dimensions["scrollHeight"] > dimensions["clientHeight"], dimensions
    assert page.locator("#map-context").text_content().startswith("从第 3 级开始")
    assert page.locator("body").get_attribute("data-start-depth") == "3"
    page.screenshot(path="/tmp/supplyagent-level-tree.png")

    page.locator("#icicle-canvas").hover(position={"x": 120, "y": 220})
    page.mouse.wheel(0, 900)
    page.wait_for_timeout(300)
    scroll_top = viewport.evaluate("(element) => element.scrollTop")
    assert scroll_top > 0, f"ordinary wheel did not scroll the long tree: {scroll_top}"
    before_scale = float(page.locator("body").get_attribute("data-view-scale"))
    page.keyboard.down("Control")
    page.mouse.wheel(0, -120)
    page.keyboard.up("Control")
    page.wait_for_timeout(100)
    after_scale = float(page.locator("body").get_attribute("data-view-scale"))
    assert after_scale > before_scale, (before_scale, after_scale)


def test_demand_path_navigation(page):
    wait_until_ready(page)
    page.locator("#global-search").fill("节日")
    page.locator(".demand-card", has_text="节日").first.click()
    page.locator(".attachment-button").first.click()

    path_buttons = page.locator("[data-path-info-node]")
    count = path_buttons.count()
    assert count > 1, f"expected an ancestor path, got {count} nodes"
    parent_button = path_buttons.nth(count - 2)
    parent_name = parent_button.locator("strong").text_content().strip()
    parent_button.click()

    assert "visible" in page.locator(".demand-attachments").get_attribute("class")
    assert "节日" in page.locator(".demand-card.selected").text_content()
    current_name = page.locator("#breadcrumb-bar button.current").text_content().strip()
    assert current_name == parent_name, (current_name, parent_name)

    page.locator("#parent-view").click()
    higher_parent_name = (
        page.locator("#breadcrumb-bar button.current").text_content().strip()
    )
    assert higher_parent_name != parent_name
    assert "节日" in page.locator(".demand-card.selected").text_content()
    assert (
        page.locator("[data-path-info-node].active strong").text_content().strip()
        == higher_parent_name
    )
    page.screenshot(path="/tmp/supplyagent-demand-path.png", full_page=True)


with sync_playwright() as playwright:
    browser = playwright.chromium.launch(
        headless=True,
        executable_path="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
    )
    page = browser.new_page(viewport={"width": 1600, "height": 1000})
    console_errors = []
    page.on(
        "console",
        lambda message: console_errors.append(message.text)
        if message.type == "error"
        else None,
    )
    test_level_scroll(page)
    test_demand_path_navigation(page)
    assert not console_errors, console_errors
    browser.close()
