"""v3 审查：测试节点图编辑器各功能"""
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    context = browser.new_context(viewport={"width": 1400, "height": 900})
    page = context.new_page()

    errors = []
    console_msgs = []
    page.on("console", lambda msg: console_msgs.append(f"[{msg.type}] {msg.text}"))
    page.on("pageerror", lambda err: errors.append(str(err)))

    # 登录
    print("=== 登录 ===")
    page.goto("http://localhost:3000/login", wait_until="networkidle", timeout=30000)
    page.locator("input[name='email']").fill("t_chinese_1@xiaoxue.edu.cn")
    page.locator("input[name='password']").fill("123456")
    page.get_by_role("button", name="Sign In", exact=False).click()
    try:
        page.wait_for_url("**/dashboard**", timeout=15000)
    except Exception:
        page.wait_for_load_state("networkidle", timeout=10000)
    print(f"登录后: {page.url}")

    # 新建课案
    print("\n=== 新建课案 ===")
    page.goto("http://localhost:3000/teacher/lesson-plans/new", wait_until="networkidle", timeout=30000)
    page.locator("input[placeholder*='秋天']").fill("v3审查测试")
    page.locator("button[type='button']:has-text('常规课')").click()
    page.wait_for_timeout(500)
    page.get_by_role("button", name="创建课案", exact=False).click()
    try:
        page.wait_for_url("**/edit**", timeout=15000)
    except Exception:
        pass
    print(f"编辑页: {page.url}")

    if "/edit" in page.url:
        page.wait_for_timeout(5000)
        page.screenshot(path="e:/Desktop/CICD/bugs/v3_01_initial.png", full_page=True)

        # 测试1：节点渲染
        nodes = page.locator(".react-flow__node")
        edges = page.locator(".react-flow__edge")
        print(f"节点数: {nodes.count()}, 边数: {edges.count()}")

        # 测试2：节点选中
        print("\n=== 节点选中 ===")
        nodes.first.click()
        page.wait_for_timeout(1000)
        page.screenshot(path="e:/Desktop/CICD/bugs/v3_02_selected.png", full_page=True)
        # 检查侧边面板
        panel = page.locator("text=删除此节点")
        print(f"侧边面板可见: {panel.count() > 0}")

        # 测试3：编辑节点标题
        print("\n=== 编辑节点标题 ===")
        title_input = page.locator("input").nth(1)  # 侧边面板的标题输入
        if title_input.count() > 0:
            title_input.fill("修改后的标题")
            page.wait_for_timeout(500)
            print("标题已修改")

        # 测试4：添加节点
        print("\n=== 添加节点 ===")
        page.get_by_role("button", name="添加节点", exact=False).click()
        page.wait_for_timeout(500)
        add_items = page.locator("button:has-text('教学目标')")
        if add_items.count() > 0:
            add_items.first.click()
            page.wait_for_timeout(1000)
            nodes_after = page.locator(".react-flow__node")
            print(f"添加后节点数: {nodes_after.count()}")

        # 测试5：测试连线（拖拽创建）
        print("\n=== 测试连线 ===")
        # React Flow 的连线需要拖拽 handle
        handles = page.locator(".react-flow__handle")
        print(f"Handle 数量: {handles.count()}")

        # 测试6：版本抽屉
        print("\n=== 版本抽屉 ===")
        page.get_by_role("button", name="版本", exact=True).click()
        page.wait_for_timeout(2000)
        page.screenshot(path="e:/Desktop/CICD/bugs/v3_03_versions.png", full_page=True)
        loading = page.locator("text=加载中")
        no_version = page.locator("text=暂无版本")
        print(f"loading 可见: {loading.count() > 0}, 无版本: {no_version.count() > 0}")

        # 关闭抽屉
        page.locator(".fixed.inset-0 .flex-1").click()
        page.wait_for_timeout(500)

        # 测试7：保存版本
        print("\n=== 保存版本 ===")
        page.get_by_role("button", name="保存版本", exact=True).click()
        page.wait_for_timeout(2000)
        print(f"保存后 URL: {page.url}")

        page.screenshot(path="e:/Desktop/CICD/bugs/v3_04_final.png", full_page=True)

    # 错误输出
    print("\n=== 页面错误 ===")
    for e in errors:
        if "Performance" not in e and "measure" not in e:
            print(f"  ERROR: {e[:300]}")
    if not errors:
        print("  无（排除 Performance 测量噪声）")

    print("\n=== 控制台 error/warning ===")
    for m in console_msgs:
        if (m.startswith("[error]") or m.startswith("[warning]")) and "Performance" not in m:
            print(f"  {m[:300]}")

    browser.close()
    print("\n完成")
