#!/usr/bin/env python3
"""
检查X-User-Id header
"""

from playwright.sync_api import sync_playwright
import time

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    context = browser.new_context()
    page = context.new_page()
    
    # 监听网络请求
    def handle_request(request):
        if '/api/users/page' in request.url:
            headers = request.headers
            print(f"\n请求: {request.method} {request.url}")
            print(f"Headers:")
            for key in ['authorization', 'x-user-id', 'x-username']:
                if key in headers:
                    print(f"  {key}: {headers[key]}")
                else:
                    print(f"  {key}: 不存在")
    
    def handle_response(response):
        if '/api/users/page' in response.url:
            print(f"\n响应: {response.status} {response.url}")
    
    page.on('request', handle_request)
    page.on('response', handle_response)
    
    # 登录
    print("登录...")
    page.goto("http://localhost:3002/login")
    page.wait_for_load_state("networkidle")
    page.fill('input[placeholder="请输入用户名"]', 'admin')
    page.fill('input[placeholder="请输入密码"]', 'admin123')
    page.click('button:has-text("登录")')
    
    # 等待Token
    for i in range(10):
        time.sleep(1)
        token = page.evaluate("localStorage.getItem('token')")
        if token:
            print(f"\n登录成功，Token: {token[:50]}...")
            break
    
    # 访问用户管理
    print("\n\n访问用户管理...")
    page.goto("http://localhost:3002/users")
    page.wait_for_load_state("networkidle")
    time.sleep(2)
    
    print(f"\n最终URL: {page.url}")
    
    browser.close()
