/** * tests/e2e/auth.setup.ts — Playwright projects.setup 模式 * * 跑一次 UI 登录 (admin/123456 @ tds), 把 cookie + localStorage 持久化到 .auth/admin.json, * 后续 spec 通过 use.storageState 复用登录态, 不必每个 test 重输密码. * * 跑法: * npx playwright test --project=setup # 单独刷新登录态 * npx playwright test # 自动先跑 setup 再跑 chromium * * TTL 优化: 6 小时内复用, 超时则本次重登 (远短于 dacli 7 天 token). * * 关键路径 (来自 ssw-e2e-test 验证): * 登录页: /jnpf_app_tds/login * 登录后: /jnpf_app_tds/home * 会话共享: 同一 host (nginx) 下所有 jnpf_app_X 路径共用 storage (浏览器层 host-wide) */ import { test as setup, expect } from '@playwright/test' import { isAuthFresh, loginAsAdmin, AUTH_FILE, APP_HOME_PATH } from './_helpers/auth' import { mkdirSync } from 'node:fs' import { dirname } from 'node:path' setup('authenticate as admin/123456 @ workSystem (v17 verify)', async ({ page }) => { // 新鲜则跳过 — Playwright 仍会写 storageState 给后续 project 用, 我们手动保留旧文件 if (isAuthFresh()) { setup.info().annotations.push({ type: 'note', description: 'auth fresh, skip UI login' }) // 必须依然 storageState 写一次到 fixture, 否则 Playwright 会用空 state // 用 page.context() 加载现有 file: 我们让 context 直接从 file 初始化 — 但 setup project 没法在 fixture 之前换 context // 折中: 不在 setup project 用 storageState fixture, 改在 chromium project use.storageState 引用同 file // 故此处直接 return — chromium project 会从 AUTH_FILE 读 return } mkdirSync(dirname(AUTH_FILE), { recursive: true }) // v17: 改 workSystem (tds DB 已重置, 2026-06-15 用户清空其他应用) const ok = await loginAsAdmin(page, { appCode: 'developSystem' }) expect(ok, 'UI 登录应跳到 /home').toBe(true) // 健壮性: 验证 storage 有 access token const hasAccess = await page.evaluate(() => { return Object.keys(localStorage).some( (k) => k.includes('core-access') && (localStorage.getItem(k)?.length ?? 0) > 50 ) }) expect(hasAccess, '登录后 localStorage 应有加密 access token').toBe(true) await page.context().storageState({ path: AUTH_FILE }) })