import { expect, type APIRequestContext, type Page } from '@playwright/test'; export const uniqueId = (prefix: string) => `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`; const toFrontendUser = (user: any) => ({ id: user.id, username: user.username, role: user.role === 'doctor' ? 'user' : user.role, name: user.name, departmentId: user.departmentId, department: user.departmentName, status: user.status, signature: user.signature, signatureFileId: user.signatureFileId, visibleTemplates: [], manageableTemplates: [], }); export const apiRequest = async ( request: APIRequestContext, method: 'get' | 'post' | 'patch' | 'delete', path: string, data?: Record, ) => { const response = await request[method](path, data ? { data } : undefined); const text = await response.text(); expect(response.ok(), `${method.toUpperCase()} ${path} failed: ${text}`).toBe(true); return text ? (JSON.parse(text).data as T) : (null as T); }; export const loginByApi = async (page: Page, username: string, password = '123456') => { await page.goto('/'); await page.evaluate(() => { window.localStorage.clear(); window.sessionStorage.clear(); }); await page.request.post('/api/auth/logout').catch(() => undefined); const data = await apiRequest<{ user: any }>(page.request, 'post', '/api/auth/login', { username, password }); await page.evaluate((user) => { window.localStorage.setItem('currentUser', JSON.stringify(user)); }, toFrontendUser(data.user)); return data.user; }; export const createUserByApi = ( request: APIRequestContext, body: { username: string; name: string; role: 'admin' | 'user'; department?: string; departmentId?: string; visibleTemplates?: string[]; manageableTemplates?: string[]; }, ) => apiRequest<{ user: any }>(request, 'post', '/api/users', { password: '123456', status: 'active', ...body, }).then((data) => data.user); export const createDepartmentByApi = (request: APIRequestContext, name: string, code: string) => apiRequest<{ department: any }>(request, 'post', '/api/departments', { name, code }).then((data) => data.department); export const createReportByApi = ( request: APIRequestContext, body: { title: string; patientName?: string; hospitalId?: string; content?: string; status?: 'draft' | 'completed'; }, ) => apiRequest<{ report: any }>(request, 'post', '/api/reports', { patientName: 'E2E患者', hospitalId: uniqueId('H'), content: '

E2E报告内容

', status: 'completed', ...body, }).then((data) => data.report);