- config.ts: DEFAULT_APP_CODE 改 testspace (统一测试场, 取代 developSystem 静默回落)
- app/login.ts|switch.ts|use.ts|logout.ts|create.ts: help/示例/AI hook 全部标注 testspace 是测试场, developSystem/workSystem 是 JNPF 只读 baseline, 禁止当测试目标
- tests/e2e/{smoke,auth.setup,baseline/template}.spec.ts: fixture 用 process.env.APP_CODE || 'testspace', 不再硬编码 developSystem
testspace app 已在 JNPF 创建 (systemId=841610403778660421), 新会话默认登录后, 自由 CRUD 不会污染 developSystem/workSystem baseline 0 差错基线。
Co-Authored-By: Claude <[email protected]>
51 lines
2.4 KiB
TypeScript
51 lines
2.4 KiB
TypeScript
/**
|
|
* 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 @ testspace (统一测试场)', 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 })
|
|
|
|
// 默认 testspace 测试场; developSystem/workSystem 是 JNPF 只读 baseline, 禁止用
|
|
const loginAppCode = process.env.PLAYWRIGHT_AUTH_APP_CODE || 'testspace'
|
|
const ok = await loginAsAdmin(page, { appCode: loginAppCode })
|
|
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 })
|
|
})
|