import { expect, type Page } from "@playwright/test"; import { createAuthenticatedApiClient, createAuthenticatedBrowserStorageState, logout, type AuthenticatedApiClient, type E2EUser } from "./auth"; export type LiveSmokeRole = "admin" | "employee" | "manager" | "finance" | "financeMgr"; export type LiveSmokeUser = { role: LiveSmokeRole; identifier: string; password: string; name: string; }; function requireEnv(key: string) { const value = process.env[key]?.trim(); if (!value) { throw new Error(`缺少 live smoke 环境变量:${key}`); } return value; } function liveUrl(pathname: string) { const baseURL = process.env.LIVE_SMOKE_BASE_URL?.trim(); return baseURL ? new URL(pathname, baseURL).toString() : pathname; } export function getExpectedWecomStatus() { return process.env.LIVE_EXPECT_WECOM_STATUS?.trim() || "disabled"; } export function getLiveSmokeUser(role: LiveSmokeRole) { const envByRole: Record = { admin: { identifier: "LIVE_SMOKE_ADMIN_IDENTIFIER", password: "LIVE_SMOKE_ADMIN_PASSWORD", }, employee: { identifier: "LIVE_SMOKE_EMPLOYEE_IDENTIFIER", password: "LIVE_SMOKE_EMPLOYEE_PASSWORD", }, manager: { identifier: "LIVE_SMOKE_MANAGER_IDENTIFIER", password: "LIVE_SMOKE_MANAGER_PASSWORD", }, finance: { identifier: "LIVE_SMOKE_FINANCE_IDENTIFIER", password: "LIVE_SMOKE_FINANCE_PASSWORD", }, financeMgr: { identifier: "LIVE_SMOKE_FINANCE_MGR_IDENTIFIER", password: "LIVE_SMOKE_FINANCE_MGR_PASSWORD", }, }; const env = envByRole[role]; return { role, identifier: requireEnv(env.identifier), password: requireEnv(env.password), }; } async function readCurrentUserName(page: Page): Promise { const profileTrigger = page.getByTestId("topbar-profile-trigger").first(); await expect(profileTrigger).toBeVisible(); const name = (await profileTrigger.textContent())?.trim(); if (!name) { throw new Error("无法从顶栏读取当前登录用户名称"); } return name; } export async function loginLiveUser(page: Page, role: LiveSmokeRole): Promise { const credentials = getLiveSmokeUser(role); await page.goto(liveUrl("/login")); await expect(page.getByTestId("login-identifier")).toBeVisible(); await page.getByTestId("login-identifier").fill(credentials.identifier); await page.getByTestId("login-password").fill(credentials.password); await page.getByTestId("login-submit").click(); await expect(page).toHaveURL(/\/portal/, { timeout: 30_000 }).catch(async (error) => { const loginError = page.locator(".text-red-600, .text-red-700, [role='alert']").first(); const message = (await loginError.textContent().catch(() => ""))?.trim(); if (message) { throw new Error(`live 登录失败:${message}`); } throw error; }); const name = await readCurrentUserName(page); return { ...credentials, name, }; } export async function logoutLiveUser(page: Page, user?: Pick) { await logout(page, user?.name); } export function getRequiredLiveUser(role: LiveSmokeRole): E2EUser { const credentials = getLiveSmokeUser(role); return { identifier: credentials.identifier, password: credentials.password, name: role, }; } export async function loginLiveBrowserUser(page: Page, role: LiveSmokeRole): Promise { return loginLiveUser(page, role); } export async function createLiveApiClient(role: LiveSmokeRole): Promise { const baseURL = process.env.LIVE_SMOKE_BASE_URL?.trim() || "https://oa.hyinda.com"; return createAuthenticatedApiClient(getRequiredLiveUser(role), baseURL); } export async function createLiveBrowserStorageState(role: LiveSmokeRole) { const baseURL = process.env.LIVE_SMOKE_BASE_URL?.trim() || "https://oa.hyinda.com"; return createAuthenticatedBrowserStorageState(getRequiredLiveUser(role), baseURL); }