#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
重新创建优化的登录模块测试用例
"""

import requests

BASE_URL = "http://localhost:8001/api"

# 获取登录模块
print("获取登录模块...")
response = requests.get(f"{BASE_URL}/modules")
modules = response.json()['items']

login_module = None
for m in modules:
    if 'login' in m['name'].lower() or '登录' in m['name']:
        login_module = m
        break

if not login_module:
    print("创建登录模块...")
    response = requests.post(f"{BASE_URL}/modules", json={
        "name": "登录模块",
        "description": "统一管理平台登录功能测试",
        "order": 1
    })
    login_module = response.json()

module_id = login_module['id']
print(f"登录模块ID: {module_id}")

# 删除旧用例
print("\n删除旧用例...")
response = requests.get(f"{BASE_URL}/cases?module_id={module_id}")
old_cases = response.json()['items']
for case in old_cases:
    requests.delete(f"{BASE_URL}/cases/{case['id']}")
    print(f"  删除: {case['name']}")

print(f"\n创建了 {len(old_cases)} 个旧用例")

# 创建优化的测试用例
print("\n创建新的优化用例...")
test_cases = [
    {
        "name": "登录功能-页面访问验证",
        "description": "验证登录页面是否可以正常访问",
        "priority": "high",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待页面加载", "action": "wait",
             "params": {"selector": "body", "timeout": 10000}},
            {"order": 3, "name": "验证页面标题", "action": "assert",
             "params": {"type": "title"}, "expected": "统一管理平台"}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    },
    {
        "name": "登录功能-正常登录",
        "description": "使用正确的用户名和密码登录",
        "priority": "high",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待登录表单加载", "action": "wait",
             "params": {"selector": "input", "timeout": 10000}},
            {"order": 3, "name": "输入用户名", "action": "fill",
             "params": {"selector": "input[type='text']", "value": "admin@xty"}},
            {"order": 4, "name": "输入密码", "action": "fill",
             "params": {"selector": "input[type='password']", "value": "Ubains@13579"}},
            {"order": 5, "name": "输入验证码", "action": "fill",
             "params": {"selector": "input[type='text']:nth-of-type(3)", "value": "csba"}},
            {"order": 6, "name": "点击登录按钮", "action": "click",
             "params": {"selector": "button"}},
            {"order": 7, "name": "等待页面跳转", "action": "wait",
             "params": {"selector": "body", "timeout": 5000}},
            {"order": 8, "name": "验证登录成功", "action": "assert",
             "params": {"type": "url"}, "expected": "platform"}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    },
    {
        "name": "登录功能-用户名为空",
        "description": "用户名为空时点击登录",
        "priority": "medium",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待页面加载", "action": "wait",
             "params": {"selector": "input[type='password']", "timeout": 10000}},
            {"order": 3, "name": "输入密码", "action": "fill",
             "params": {"selector": "input[type='password']", "value": "Ubains@13579"}},
            {"order": 4, "name": "点击登录按钮", "action": "click",
             "params": {"selector": "button"}},
            {"order": 5, "name": "验证错误提示", "action": "wait",
             "params": {"selector": ".el-message", "timeout": 5000}}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    },
    {
        "name": "登录功能-密码为空",
        "description": "密码为空时点击登录",
        "priority": "medium",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待页面加载", "action": "wait",
             "params": {"selector": "input[type='text']", "timeout": 10000}},
            {"order": 3, "name": "输入用户名", "action": "fill",
             "params": {"selector": "input[type='text']", "value": "admin@xty"}},
            {"order": 4, "name": "点击登录按钮", "action": "click",
             "params": {"selector": "button"}},
            {"order": 5, "name": "验证错误提示", "action": "wait",
             "params": {"selector": ".el-message", "timeout": 5000}}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    },
    {
        "name": "登录功能-错误密码",
        "description": "使用错误的密码登录",
        "priority": "medium",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待页面加载", "action": "wait",
             "params": {"selector": "input", "timeout": 10000}},
            {"order": 3, "name": "输入用户名", "action": "fill",
             "params": {"selector": "input[type='text']", "value": "admin@xty"}},
            {"order": 4, "name": "输入错误密码", "action": "fill",
             "params": {"selector": "input[type='password']", "value": "WrongPassword"}},
            {"order": 5, "name": "输入验证码", "action": "fill",
             "params": {"selector": "input[type='text']:nth-of-type(3)", "value": "csba"}},
            {"order": 6, "name": "点击登录按钮", "action": "click",
             "params": {"selector": "button"}},
            {"order": 7, "name": "验证错误提示", "action": "wait",
             "params": {"selector": ".el-message", "timeout": 5000}}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    },
    {
        "name": "登录功能-记住密码勾选框",
        "description": "验证记住密码勾选框功能",
        "priority": "low",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待页面加载", "action": "wait",
             "params": {"selector": "input[type='checkbox']", "timeout": 10000}},
            {"order": 3, "name": "验证勾选框存在", "action": "assert",
             "params": {"type": "visible", "selector": "input[type='checkbox']"}},
            {"order": 4, "name": "勾选记住密码", "action": "check",
             "params": {"selector": "input[type='checkbox']"}}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    },
    {
        "name": "登录功能-忘记密码链接",
        "description": "验证忘记密码链接是否可用",
        "priority": "low",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待页面加载", "action": "wait",
             "params": {"selector": "body", "timeout": 10000}},
            {"order": 3, "name": "验证忘记密码链接存在", "action": "assert",
             "params": {"type": "visible", "selector": "text=忘记密码"}}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    },
    {
        "name": "登录功能-立即注册链接",
        "description": "验证立即注册链接是否可用",
        "priority": "low",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate",
             "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "等待页面加载", "action": "wait",
             "params": {"selector": "body", "timeout": 10000}},
            {"order": 3, "name": "验证立即注册链接存在", "action": "assert",
             "params": {"type": "visible", "selector": "text=立即注册"}}
        ],
        "config": {"timeout": 60000, "retry": 0, "screenshot": True, "auto_login": False}
    }
]

created_count = 0
for case_data in test_cases:
    case_payload = {
        "module_id": module_id,
        "name": case_data["name"],
        "description": case_data["description"],
        "priority": case_data["priority"],
        "steps": case_data["steps"],
        "config": case_data["config"]
    }

    response = requests.post(f"{BASE_URL}/cases", json=case_payload)
    if response.status_code == 200:
        created_count += 1
        print(f"  [OK] {case_data['name']}")
    else:
        print(f"  [FAIL] {case_data['name']} - {response.text}")

print(f"\n[SUCCESS] 创建了 {created_count}/{len(test_cases)} 个测试用例")
print(f"模块ID: {module_id}")
