#!/usr/bin/env python3
"""
快速验证测试 - 验证系统基本功能
"""
from playwright.sync_api import sync_playwright
import time

def test_basic_flow():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context()
        page = context.new_page()
        
        try:
            print("1. 访问登录页...")
            page.goto("http://localhost:3002/login", timeout=10000)
            page.wait_for_load_state("networkidle", timeout=10000)
            print("✅ 登录页加载成功")
            
            print("\n2. 执行登录...")
            page.fill('input[type="text"]', 'admin')
            page.fill('input[type="password"]', 'admin123')
            page.click('button[type="submit"]')
            
            time.sleep(3)
            
            current_url = page.url
            print(f"当前URL: {current_url}")
            
            if 'dashboard' in current_url or current_url != 'http://localhost:3002/login':
                print("✅ 登录成功，已跳转")
                
                token = page.evaluate("localStorage.getItem('token')")
                if token:
                    print(f"✅ Token已保存: {token[:50]}...")
                else:
                    print("⚠️ Token未保存")
                
                print("\n3. 访问用户管理页...")
                page.goto("http://localhost:3002/users", timeout=10000)
                page.wait_for_load_state("networkidle", timeout=10000)
                
                current_url = page.url
                print(f"当前URL: {current_url}")
                
                if 'login' not in current_url:
                    print("✅ 用户管理页访问成功，未重定向到登录页")
                    
                    page_content = page.content()
                    if '用户管理' in page_content or 'Users' in page_content:
                        print("✅ 用户管理页面内容正确")
                    else:
                        print("⚠️ 用户管理页面内容可能不正确")
                else:
                    print("❌ 用户管理页访问失败，被重定向到登录页")
                
                return True
            else:
                print("❌ 登录失败，仍在登录页")
                return False
                
        except Exception as e:
            print(f"❌ 测试失败: {e}")
            return False
        finally:
            browser.close()

if __name__ == "__main__":
    print("=" * 60)
    print("系统快速验证测试")
    print("=" * 60)
    
    success = test_basic_flow()
    
    print("\n" + "=" * 60)
    if success:
        print("✅ 系统验证通过！")
    else:
        print("❌ 系统验证失败！")
    print("=" * 60)
