import { test, expect } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { DashboardPage } from './pages/DashboardPage';
import { UserManagementPage } from './pages/UserManagementPage';
import { RoleManagementPage } from './pages/RoleManagementPage';
import { MenuManagementPage } from './pages/MenuManagementPage';
import { SystemConfigPage } from './pages/SystemConfigPage';
import { FileManagementPage } from './pages/FileManagementPage';
import { OperationLogPage } from './pages/OperationLogPage';
import { NotificationPage } from './pages/NotificationPage';
import { DictionaryManagementPage } from './pages/DictionaryManagementPage';

test.describe('E2E完整业务流程测试', () => {
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;
  let userManagementPage: UserManagementPage;
  let roleManagementPage: RoleManagementPage;
  let menuManagementPage: MenuManagementPage;
  let systemConfigPage: SystemConfigPage;
  let fileManagementPage: FileManagementPage;
  let operationLogPage: OperationLogPage;
  let notificationPage: NotificationPage;
  let dictionaryManagementPage: DictionaryManagementPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    dashboardPage = new DashboardPage(page);
    userManagementPage = new UserManagementPage(page);
    roleManagementPage = new RoleManagementPage(page);
    menuManagementPage = new MenuManagementPage(page);
    systemConfigPage = new SystemConfigPage(page);
    fileManagementPage = new FileManagementPage(page);
    operationLogPage = new OperationLogPage(page);
    notificationPage = new NotificationPage(page);
    dictionaryManagementPage = new DictionaryManagementPage(page);
  });

  test('E2E-001: 用户完整生命周期流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建新角色', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.clickCreateRole();

      const roleData = {
        roleName: `测试角色_${timestamp}`,
        roleKey: `test_role_${timestamp}`,
        roleSort: '1',
        status: 'ACTIVE',
        remark: `测试角色备注_${timestamp}`,
      };

      await roleManagementPage.fillRoleForm(roleData);
      await roleManagementPage.submitForm();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 为角色分配权限', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission('user:view');
      await roleManagementPage.selectPermission('user:create');
      await roleManagementPage.selectPermission('user:edit');
      await roleManagementPage.selectPermission('user:delete');
      await roleManagementPage.savePermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 创建新用户', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();

      const userData = {
        username: `testuser_${timestamp}`,
        nickname: `测试用户${timestamp}`,
        email: `test_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };

      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 为用户分配角色', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.editUser(1);
      await page.click('.role-select');
      await page.click('option:has-text("测试角色")');
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('6. 用户登录验证', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login(`testuser_${timestamp}`, 'Test123!@#');
      await expect(page).toHaveURL(/.*dashboard/);
      const username = await dashboardPage.getUsername();
      expect(username).toContain(`testuser_${timestamp}`);
    });

    await test.step('7. 修改用户信息', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.editUser(1);
      const dialog = page.locator('.el-dialog');
      const nicknameInput = dialog.locator('.el-form-item').filter({ hasText: '昵称' }).locator('input');
      await nicknameInput.fill(`更新用户_${timestamp}`);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('8. 禁用用户', async () => {
      await userManagementPage.clickStatusButton(1);
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('9. 启用用户', async () => {
      await userManagementPage.clickStatusButton(1);
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('10. 删除用户', async () => {
      await userManagementPage.deleteUser(1);
      await userManagementPage.confirmDelete();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('11. 删除角色', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.deleteRole(1);
      await roleManagementPage.confirmDelete();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });
  });

  test('E2E-002: 角色权限分配完整流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建新角色', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.clickCreateRole();

      const roleData = {
        roleName: `UAT角色_${timestamp}`,
        roleKey: `uat_role_${timestamp}`,
        roleSort: '1',
        status: '1',
        remark: 'UAT测试角色',
      };

      await roleManagementPage.fillRoleForm(roleData);
      await roleManagementPage.submitForm();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 为角色分配菜单权限', async () => {
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission('system:user:view');
      await roleManagementPage.selectPermission('system:user:add');
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 为角色分配API权限', async () => {
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission('api:user:list');
      await roleManagementPage.selectPermission('api:user:create');
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 创建新用户', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();

      const userData = {
        username: `uatuser_${timestamp}`,
        nickname: `UAT用户${timestamp}`,
        email: `uat_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };

      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('6. 为用户分配角色', async () => {
      await userManagementPage.editUser(1);
      await page.click('.role-select');
      await page.click('option:has-text("UAT角色")');
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('7. 用户登录验证权限', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login(`uatuser_${timestamp}`, 'Test123!@#');
      await expect(page).toHaveURL(/.*dashboard/);

      await dashboardPage.navigateToUserManagement();
      await expect(page).toHaveURL(/.*users/);

      await page.goto('/users/create');
      await expect(page).toHaveURL(/.*users/);
    });

    await test.step('8. 撤销角色权限', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.deselectPermission('system:user:add');
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('9. 删除角色', async () => {
      await roleManagementPage.deleteRole(1);
      await roleManagementPage.confirmDelete();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });
  });

  test('E2E-003: 菜单树构建与权限控制流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建父级菜单', async () => {
      await dashboardPage.navigateToMenuManagement();
      await menuManagementPage.clickCreateMenu();

      const menuData = {
        menuName: `父级菜单_${timestamp}`,
        parentId: '0',
        orderNum: '1',
        menuType: 'M',
        component: `parent_${timestamp}`,
        perms: `parent:view_${timestamp}`,
        status: '1',
      };

      await menuManagementPage.fillMenuForm(menuData);
      await menuManagementPage.submitForm();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 创建子级菜单', async () => {
      await menuManagementPage.clickCreateMenu();

      const menuData = {
        menuName: `子级菜单_${timestamp}`,
        parentId: '1',
        orderNum: '1',
        menuType: 'C',
        component: `child_${timestamp}`,
        perms: `child:view_${timestamp}`,
        status: '1',
      };

      await menuManagementPage.fillMenuForm(menuData);
      await menuManagementPage.submitForm();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 配置菜单权限', async () => {
      await menuManagementPage.editMenu(1);
      await menuManagementPage.selectPermission('menu:view');
      await menuManagementPage.submitForm();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 验证菜单树显示', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`父级菜单_${timestamp}`);
      await expect(page.locator('table')).toContainText(`子级菜单_${timestamp}`);
    });

    await test.step('6. 为角色分配菜单权限', async () => {
      await dashboardPage.navigateToRoleManagement();
      await roleManagementPage.openPermissionDialog(1);
      await roleManagementPage.selectPermission(`parent:view_${timestamp}`);
      await roleManagementPage.submitPermissions();
      await expect(roleManagementPage.successMessage).toBeVisible();
    });

    await test.step('7. 用户登录验证菜单访问', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
      await expect(page.locator('.menu-item')).toContainText(`父级菜单_${timestamp}`);
    });

    await test.step('8. 删除子级菜单', async () => {
      await dashboardPage.navigateToMenuManagement();
      await menuManagementPage.deleteMenu(2);
      await menuManagementPage.confirmDelete();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });

    await test.step('9. 删除父级菜单', async () => {
      await menuManagementPage.deleteMenu(1);
      await menuManagementPage.confirmDelete();
      await expect(menuManagementPage.successMessage).toBeVisible();
    });
  });

  test('E2E-004: 系统配置管理流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 查看当前配置', async () => {
      await dashboardPage.navigateToSystemConfig();
      await expect(systemConfigPage.table).toBeVisible();
    });

    await test.step('3. 修改配置值', async () => {
      await systemConfigPage.editConfig(1);
      await page.fill('input[name="configValue"]', `test_value_${timestamp}`);
      await systemConfigPage.submitForm();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });

    await test.step('4. 验证配置生效', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`test_value_${timestamp}`);
    });

    await test.step('5. 刷新配置缓存', async () => {
      await systemConfigPage.refreshCache();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });

    await test.step('6. 恢复默认配置', async () => {
      await systemConfigPage.editConfig(1);
      await page.fill('input[name="configValue"]', 'default_value');
      await systemConfigPage.submitForm();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });

    await test.step('7. 批量修改配置', async () => {
      await systemConfigPage.editConfig(2);
      await page.fill('input[name="configValue"]', `batch_value_${timestamp}`);
      await systemConfigPage.submitForm();
      await expect(systemConfigPage.successMessage).toBeVisible();
    });
  });

  test('E2E-005: 文件管理完整流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 上传文件', async () => {
      await dashboardPage.navigateToFileManagement();
      await fileManagementPage.clickUploadFile();
      
      const fileInput = page.locator('input[type="file"]');
      await fileInput.setInputFiles('./e2e/fixtures/test-file.txt');
      await fileManagementPage.submitUpload();
      await expect(fileManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 验证文件信息', async () => {
      await expect(page.locator('table')).toContainText('test-file.txt');
    });

    await test.step('4. 预览文件', async () => {
      await fileManagementPage.previewFile(1);
      await expect(page.locator('.file-preview')).toBeVisible();
    });

    await test.step('5. 下载文件', async () => {
      const downloadPromise = page.waitForEvent('download');
      await fileManagementPage.downloadFile(1);
      const download = await downloadPromise;
      expect(download.suggestedFilename()).toBe('test-file.txt');
    });

    await test.step('6. 设置文件权限', async () => {
      await fileManagementPage.editFile(1);
      await page.selectOption('select[name="permission"]', 'private');
      await fileManagementPage.submitForm();
      await expect(fileManagementPage.successMessage).toBeVisible();
    });

    await test.step('7. 删除文件', async () => {
      await fileManagementPage.deleteFile(1);
      await fileManagementPage.confirmDelete();
      await expect(fileManagementPage.successMessage).toBeVisible();
    });
  });

  test('E2E-006: 审计日志记录与查询流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 执行各种操作', async () => {
      await dashboardPage.navigateToUserManagement();
      await page.waitForTimeout(1000);
      
      await dashboardPage.navigateToRoleManagement();
      await page.waitForTimeout(1000);
      
      await dashboardPage.navigateToMenuManagement();
      await page.waitForTimeout(1000);
    });

    await test.step('3. 查看操作日志', async () => {
      await dashboardPage.navigateToOperationLog();
      await expect(operationLogPage.table).toBeVisible();
      await expect(page.locator('table')).toContainText('用户管理');
    });

    await test.step('4. 查看登录日志', async () => {
      await operationLogPage.switchToLoginLog();
      await expect(page.locator('table')).toContainText('admin');
    });

    await test.step('5. 查看异常日志', async () => {
      await operationLogPage.switchToExceptionLog();
      await expect(operationLogPage.table).toBeVisible();
    });

    await test.step('6. 搜索日志', async () => {
      await operationLogPage.search('用户管理');
      await page.waitForTimeout(2000);
      await expect(page.locator('table')).toContainText('用户管理');
    });

    await test.step('7. 导出日志', async () => {
      const downloadPromise = page.waitForEvent('download');
      await operationLogPage.exportLogs();
      const download = await downloadPromise;
      expect(download.suggestedFilename()).toMatch(/logs.*\.xlsx/);
    });
  });

  test('E2E-007: 通知发布与推送流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 发布系统通知', async () => {
      await dashboardPage.navigateToNotification();
      await notificationPage.clickCreateNotification();

      const notificationData = {
        title: `系统通知_${timestamp}`,
        content: `这是一条测试通知内容_${timestamp}`,
        type: 'system',
        status: '1',
      };

      await notificationPage.fillNotificationForm(notificationData);
      await notificationPage.submitForm();
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('3. 发布用户消息', async () => {
      await notificationPage.clickCreateNotification();

      const notificationData = {
        title: `用户消息_${timestamp}`,
        content: `这是一条测试用户消息_${timestamp}`,
        type: 'user',
        status: '1',
      };

      await notificationPage.fillNotificationForm(notificationData);
      await notificationPage.submitForm();
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('4. 推送实时消息', async () => {
      await notificationPage.pushRealTimeMessage(1);
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('5. 用户查看通知', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
      await expect(page.locator('.notification-badge')).toBeVisible();
    });

    await test.step('6. 标记通知已读', async () => {
      await dashboardPage.navigateToNotification();
      await notificationPage.markAsRead(1);
      await expect(notificationPage.successMessage).toBeVisible();
    });

    await test.step('7. 删除通知', async () => {
      await notificationPage.deleteNotification(1);
      await notificationPage.confirmDelete();
      await expect(notificationPage.successMessage).toBeVisible();
    });
  });

  test('E2E-008: 字典数据管理流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建字典类型', async () => {
      await dashboardPage.navigateToDictionary();
      await dictionaryManagementPage.clickCreateDictType();

      const dictTypeData = {
        dictName: `测试字典_${timestamp}`,
        dictType: `test_dict_${timestamp}`,
        status: '1',
        remark: `测试字典类型_${timestamp}`,
      };

      await dictionaryManagementPage.fillDictTypeForm(dictTypeData);
      await dictionaryManagementPage.submitForm();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 添加字典数据', async () => {
      await dictionaryManagementPage.clickCreateDictData();

      const dictData = {
        dictLabel: `测试数据1_${timestamp}`,
        dictValue: `value1_${timestamp}`,
        dictSort: '1',
        status: '1',
      };

      await dictionaryManagementPage.fillDictDataForm(dictData);
      await dictionaryManagementPage.submitForm();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 修改字典数据', async () => {
      await dictionaryManagementPage.editDictData(1);
      await page.fill('input[name="dictLabel"]', `更新数据_${timestamp}`);
      await dictionaryManagementPage.submitForm();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });

    await test.step('5. 查询字典数据', async () => {
      await dictionaryManagementPage.search(`更新数据_${timestamp}`);
      await page.waitForTimeout(2000);
      await expect(page.locator('table')).toContainText(`更新数据_${timestamp}`);
    });

    await test.step('6. 删除字典数据', async () => {
      await dictionaryManagementPage.deleteDictData(1);
      await dictionaryManagementPage.confirmDelete();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });

    await test.step('7. 删除字典类型', async () => {
      await dictionaryManagementPage.deleteDictType(1);
      await dictionaryManagementPage.confirmDelete();
      await expect(dictionaryManagementPage.successMessage).toBeVisible();
    });
  });

  test('E2E-009: 多用户并发操作流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 创建测试用户', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToUserManagement();
      
      for (let i = 1; i <= 2; i++) {
        await userManagementPage.clickCreateUser();
        const userData = {
          username: `concurrent_user_${i}_${timestamp}`,
          nickname: `并发用户${i}_${timestamp}`,
          email: `concurrent_${i}_${timestamp}@example.com`,
          phone: '13800138000',
          password: 'Test123!@#',
          confirmPassword: 'Test123!@#',
        };
        await userManagementPage.fillUserForm(userData);
        await userManagementPage.submitForm();
        await expect(userManagementPage.successMessage).toBeVisible();
      }
    });

    await test.step('2. 用户A创建数据', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login(`concurrent_user_1_${timestamp}`, 'Test123!@#');
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();
      const userData = {
        username: `user_a_data_${timestamp}`,
        nickname: `用户A数据_${timestamp}`,
        email: `user_a_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };
      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 用户B同时创建数据', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login(`concurrent_user_2_${timestamp}`, 'Test123!@#');
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();
      const userData = {
        username: `user_b_data_${timestamp}`,
        nickname: `用户B数据_${timestamp}`,
        email: `user_b_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };
      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('4. 验证数据一致性', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`user_a_data_${timestamp}`);
      await expect(page.locator('table')).toContainText(`user_b_data_${timestamp}`);
    });

    await test.step('5. 清理测试数据', async () => {
      await loginPage.logout();
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await dashboardPage.navigateToUserManagement();
      
      await userManagementPage.search(`concurrent_user_1_${timestamp}`);
      await page.waitForTimeout(1000);
      const rows = await page.locator('table tbody tr').count();
      if (rows > 0) {
        await userManagementPage.deleteUser(1);
        await userManagementPage.confirmDelete();
      }
      
      await userManagementPage.search(`concurrent_user_2_${timestamp}`);
      await page.waitForTimeout(1000);
      const rows2 = await page.locator('table tbody tr').count();
      if (rows2 > 0) {
        await userManagementPage.deleteUser(1);
        await userManagementPage.confirmDelete();
      }
    });
  });

  test('E2E-010: 系统异常恢复流程', async ({ page }) => {
    const timestamp = Date.now();

    await test.step('1. 管理员登录', async () => {
      await loginPage.goto();
      await loginPage.login('admin', 'admin123');
      await expect(page).toHaveURL(/.*dashboard/);
    });

    await test.step('2. 创建测试数据', async () => {
      await dashboardPage.navigateToUserManagement();
      await userManagementPage.clickCreateUser();
      const userData = {
        username: `recovery_test_${timestamp}`,
        nickname: `恢复测试用户_${timestamp}`,
        email: `recovery_${timestamp}@example.com`,
        phone: '13800138000',
        password: 'Test123!@#',
        confirmPassword: 'Test123!@#',
      };
      await userManagementPage.fillUserForm(userData);
      await userManagementPage.submitForm();
      await expect(userManagementPage.successMessage).toBeVisible();
    });

    await test.step('3. 记录数据状态', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`recovery_test_${timestamp}`);
    });

    await test.step('4. 模拟网络中断', async () => {
      await page.context().setOffline(true);
      await page.waitForTimeout(2000);
    });

    await test.step('5. 恢复网络连接', async () => {
      await page.context().setOffline(false);
      await page.waitForTimeout(2000);
    });

    await test.step('6. 验证数据完整性', async () => {
      await page.reload();
      await expect(page.locator('table')).toContainText(`recovery_test_${timestamp}`);
    });

    await test.step('7. 验证会话恢复', async () => {
      await expect(page).toHaveURL(/.*dashboard/);
      const username = await dashboardPage.getUsername();
      expect(username).toContain('admin');
    });

    await test.step('8. 验证操作继续', async () => {
      await dashboardPage.navigateToUserManagement();
      await expect(page).toHaveURL(/.*users/);
      await expect(page.locator('table')).toBeVisible();
    });

    await test.step('9. 清理测试数据', async () => {
      await userManagementPage.search(`recovery_test_${timestamp}`);
      await page.waitForTimeout(1000);
      const rows = await page.locator('table tbody tr').count();
      if (rows > 0) {
        await userManagementPage.deleteUser(1);
        await userManagementPage.confirmDelete();
        await expect(userManagementPage.successMessage).toBeVisible();
      }
    });
  });
});