import type { Page, Route } from '@playwright/test' import { captureScreenshot, expect, test } from './fixtures' const json = (route: Route, data: unknown) => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ code: 0, message: '成功', data, timestamp: Date.now(), requestId: 'zentao-user-regression' }), }) const roles = [ { id: '1', code: 'admin', name: '管理员', shortName: '管', enabled: true, builtIn: true, isSuperAdmin: true, dataScopeCode: 'ALL', version: 1 }, { id: '2', code: 'teacher', name: '教员', shortName: '教', enabled: true, builtIn: true, isSuperAdmin: false, dataScopeCode: 'SYSTEM', version: 1 }, ] async function mockUsers(page: Page) { let userRequests = 0 let statsRequests = 0 await page.route(/\/api\/(?:auth|tran)\//, async (route) => { const path = new URL(route.request().url()).pathname if (path.endsWith('/auth/me')) return json(route, { userId: '1', username: 'admin', displayName: '系统管理员', departmentId: '100', departmentName: '数字车间', activeRoleId: '1', roles, authorizationMode: 'SINGLE_ACTIVE', mustChangePassword: false, permissions: ['system.users', 'system.users.update', 'system.users.reset-password'], }) if (path.endsWith('/departments/tree')) return json(route, [{ id: '100', code: 'ORG-100', name: '数字车间', parentId: null, enabled: true, isRoot: true, sortOrder: 10, directUserCount: 1, directChildCount: 0, version: 1, children: [], }]) if (path.endsWith('/roles/all')) return json(route, roles) if (path.endsWith('/users/stats')) { statsRequests += 1 const pending = statsRequests === 1 ? 2 : 1 return json(route, { total: 12, enabled: 12, disabled: 0, passwordPending: pending, mustChangePasswordCount: pending }) } if (path.endsWith('/users')) { userRequests += 1 return json(route, { records: [{ id: '9', username: 'teacher09', displayName: '测试教员', departmentId: '100', departmentName: '数字车间', roleIds: ['2', '1'], defaultRoleId: '1', enabled: true, isProtected: false, credentialConfigured: true, mustChangePassword: userRequests === 1, lastLoginAt: null, dataVersion: 1, }], total: 1, page: 1, size: 10, }) } if (path.endsWith('/menus/navigation')) return json(route, []) if (path.endsWith('/system-config/public')) return json(route, { systemName: '数字车间' }) return json(route, {}) }) await page.addInitScript(() => { window.sessionStorage.setItem('unreal-tran:web:access-token:v1', 'zentao-user-regression-token') }) return { statsRequests: () => statsRequests, } } test('Bug 860、862:默认角色置顶且搜索同步刷新全局待改密统计', async ({ page }, testInfo) => { const requests = await mockUsers(page) await page.goto('/system/users') const row = page.locator('.users-table .el-table__row').filter({ hasText: '测试教员' }) const roleTags = row.locator('.role-tags .el-tag') await expect(roleTags).toHaveCount(2) await expect(roleTags.first()).toContainText('管理员') await expect(roleTags.first()).toContainText('默认') await expect(page.getByTestId('user-inline-statistics')).toContainText('全局待改密 2') const searchInput = page.getByPlaceholder('请输入姓名、账号、部门或角色') await searchInput.fill('teacher09') await searchInput.press('Enter') await expect.poll(requests.statsRequests).toBe(2) await expect(page.getByTestId('user-inline-statistics')).toContainText('全局待改密 1') await expect(row.getByText('待修改密码', { exact: true })).toHaveCount(0) await captureScreenshot(page, testInfo, 'bugs-860-862-user-role-and-pending-statistics') })