#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
模块名称：create_jikong_cases.py
模块描述：补充集控控制模块的UI自动化用例（设备列表、远程控制）

作者：czj
创建日期：2026-07-22
最后修改日期：2026-07-22
"""
import asyncio
import httpx
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

BASE_URL = "http://localhost:8001"
TARGET_URL = "https://192.168.5.44"

# 集控控制模块ID
JIKONG_MODULE_ID = "module_parent_jikong"

DEFAULT_CONFIG = {
    "timeout": 120000,
    "screenshot": True,
    "auto_login": True,
    "step_retry_count": 2,
}


def build_all_cases():
    """构建集控控制模块用例"""
    cases = []

    # ==================== 集控控制用例 ====================

    # 用例1: 设备列表-页面访问验证
    # 已探索：table=1, select=3, tab=7, canvas=1
    cases.append({
        "name": "设备列表-页面访问验证",
        "description": "验证通过功能中心访问设备列表页面，确认表格、Tab页和筛选器正常加载",
        "module_id": JIKONG_MODULE_ID,
        "priority": "high",
        "order": 100,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击设备列表", "action": "click", "params": {"selector": ".el-drawer >> text='设备列表'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证表格存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-table"}},
            {"order": 8, "name": "验证Tab页存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-tabs__item"}},
            {"order": 9, "name": "验证筛选器存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-select"}},
        ],
    })

    # 用例2: 设备列表-Tab切换验证
    # 已探索：tab=7，有多个Tab页可切换
    cases.append({
        "name": "设备列表-Tab切换验证",
        "description": "验证设备列表页面Tab页可正常切换，确认切换后内容区域更新",
        "module_id": JIKONG_MODULE_ID,
        "priority": "medium",
        "order": 101,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击设备列表", "action": "click", "params": {"selector": ".el-drawer >> text='设备列表'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": ".el-table", "timeout": 20000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证Tab页数量", "action": "assert", "params": {"type": "element_count_min", "selector": ".el-tabs__item", "min_count": 3}},
            {"order": 8, "name": "点击第二个Tab", "action": "click", "params": {"selector": ".el-tabs__item:nth-child(2)"}},
            {"order": 9, "name": "等待内容更新", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 10, "name": "验证内容区域存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-tabs__content"}},
        ],
    })

    # 用例3: 远程控制-页面访问验证
    # 已探索：select=3, btn=10, tab=9
    cases.append({
        "name": "远程控制-页面访问验证",
        "description": "验证通过功能中心访问远程控制页面，确认筛选器和Tab页正常加载",
        "module_id": JIKONG_MODULE_ID,
        "priority": "high",
        "order": 102,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击远程控制", "action": "click", "params": {"selector": ".el-drawer >> text='远程控制'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": "body", "timeout": 15000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证筛选器存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-select"}},
            {"order": 8, "name": "验证Tab页存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-tabs__item"}},
            {"order": 9, "name": "验证操作按钮存在", "action": "assert", "params": {"type": "element_exists", "selector": "button"}},
        ],
    })

    # 用例4: 远程控制-Tab切换验证
    # 已探索：tab=9，有9个Tab页
    cases.append({
        "name": "远程控制-Tab切换验证",
        "description": "验证远程控制页面Tab页可正常切换，确认切换后内容区域更新",
        "module_id": JIKONG_MODULE_ID,
        "priority": "medium",
        "order": 103,
        "config": DEFAULT_CONFIG,
        "steps": [
            {"order": 1, "name": "等待首页加载", "action": "wait", "params": {"selector": ".block", "timeout": 15000}},
            {"order": 2, "name": "点击功能中心图标", "action": "click", "params": {"selector": ".home_nav_left"}},
            {"order": 3, "name": "等待抽屉打开", "action": "wait", "params": {"selector": ".el-drawer", "timeout": 10000}},
            {"order": 4, "name": "点击远程控制", "action": "click", "params": {"selector": ".el-drawer >> text='远程控制'"}},
            {"order": 5, "name": "等待页面加载", "action": "wait", "params": {"selector": "body", "timeout": 15000}},
            {"order": 6, "name": "等待页面渲染", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 7, "name": "验证Tab页数量", "action": "assert", "params": {"type": "element_count_min", "selector": ".el-tabs__item", "min_count": 3}},
            {"order": 8, "name": "点击第二个Tab", "action": "click", "params": {"selector": ".el-tabs__item:nth-child(2)"}},
            {"order": 9, "name": "等待内容更新", "action": "wait", "params": {"selector": "body", "timeout": 3000}},
            {"order": 10, "name": "验证内容区域存在", "action": "assert", "params": {"type": "element_exists", "selector": ".el-tabs__content"}},
        ],
    })

    return cases


async def create_case(client: httpx.AsyncClient, case_data: dict):
    """创建单个测试用例"""
    try:
        response = await client.post(f"{BASE_URL}/api/cases", json=case_data, timeout=30.0)
        if response.status_code in (200, 201):
            result = response.json()
            print(f"  ✅ {case_data['name']} (ID: {result['id']})")
            return result
        else:
            print(f"  ❌ {case_data['name']} - {response.status_code}")
            return None
    except Exception as e:
        print(f"  ❌ {case_data['name']} - {e}")
        return None


async def main():
    print("=" * 60)
    print("补充集控控制模块用例（设备列表、远程控制）")
    print("=" * 60)

    cases = build_all_cases()
    print(f"\n计划创建: {len(cases)} 个用例")

    async with httpx.AsyncClient() as client:
        for i, case in enumerate(cases, 1):
            print(f"\n[{i}/{len(cases)}] ", end="")
            await create_case(client, case)

    print("\n" + "=" * 60)
    print("✅ 用例创建完成")
    print("=" * 60)


if __name__ == "__main__":
    asyncio.run(main())
