import { test, expect } from '@playwright/test';

test.describe('UAT阶段五：API交互与错误处理验证', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/');
    await page.waitForLoadState('networkidle');
    
    const usernameInput = page.locator('input[type="text"]').first();
    const passwordInput = page.locator('input[type="password"]').first();
    const loginButton = page.locator('button:has-text("登录")');
    
    await usernameInput.fill('admin');
    await passwordInput.fill('admin123');
    await loginButton.click();
    
    await page.waitForURL('**/dashboard', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
  });

  test('UAT-API-001: Token过期处理', async ({ page }) => {
    await page.evaluate(() => {
      localStorage.removeItem('token');
    });
    
    await page.goto('/users');
    await page.waitForTimeout(2000);
    
    const currentUrl = page.url();
    expect(currentUrl).toContain('/login');
  });

  test('UAT-API-002: 网络错误提示', async ({ page, context }) => {
    await context.route('**/api/**', route => route.abort('failed'));
    
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=用户管理');
    await page.waitForTimeout(2000);
    
    await context.unroute('**/api/**');
  });

  test('UAT-API-003: 权限不足提示', async ({ page }) => {
    await page.evaluate(() => {
      localStorage.setItem('token', 'user_token_without_admin_rights');
    });
    
    await page.goto('/roles');
    await page.waitForTimeout(1000);
    
    const errorMessage = page.locator('.el-message--error');
    if (await errorMessage.isVisible()) {
      await expect(errorMessage).toBeVisible();
    }
  });

  test('UAT-API-004: 并发请求处理', async ({ page }) => {
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    await page.click('text=用户管理');
    await page.waitForURL('**/users', { timeout: 30000 });
    
    const refreshButton = page.locator('button:has-text("刷新")').first();
    if (await refreshButton.isVisible()) {
      for (let i = 0; i < 3; i++) {
        await refreshButton.click();
        await page.waitForTimeout(100);
      }
      
      await page.waitForTimeout(1000);
      await expect(page.locator('.el-table')).toBeVisible();
    }
  });

  test('UAT-API-005: 数据加载状态显示', async ({ page }) => {
    const systemMenu = page.locator('.el-sub-menu__title:has-text("系统管理")');
    await systemMenu.click();
    await page.waitForTimeout(1000);
    
    const navigationPromise = page.click('text=用户管理');
    
    const loading = page.locator('.el-loading-mask');
    if (await loading.isVisible({ timeout: 100 }).catch(() => false)) {
      await expect(loading).toBeVisible();
    }
    
    await navigationPromise;
    await page.waitForURL('**/users', { timeout: 30000 });
    await page.waitForLoadState('networkidle');
    
    await expect(page.locator('.el-table')).toBeVisible();
  });
});
