Files
dacli/tests/e2e/auth.setup.ts
T
ssw c98ad4f425 init: TS 版 dacli v0.7 baseline (8 父命令/26+ 子命令, 0.4MB, .claude/skills/dacli-ts)
参考 boot/ 仓库结构初始化, 同步到 gitea:3000/ssw/dacli (private).

包括:
- src/  8 父命令 (form/menu/dm/dict/di/apis/doc/workflow + login/status) + core (client/crypto/help/log/types/config)
- src/schema/ FieldBuilder (从 JNPF web 源码提取, 避免 53 个手写 builder)
- tests/ vitest 单元 + playwright E2E (含 workSystem/developSystem baseline regression)
- scripts/ extract-all.mjs (从 jnpf-web 源码生成 schema) + seed-tds-baseline.ts
- doc/  architecture/plan/api/extractors/baseline/RESUME/help-system + v0.6/v0.7 changelog
- .claude/skills/dacli-ts/  Claude 集成 skill (SKILL.md + 5 references + 7 examples + 3 baseline)
- CLAUDE.md (项目说明) + README.md + prompt.md (开发任务说明书)
- 配置文件: package.json/lock, tsconfig, vitest.config, playwright.config
2026-06-12 18:26:39 +08:00

49 lines
2.2 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 @ tds', 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 })
const ok = await loginAsAdmin(page, { appCode: 'tds' })
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 })
})