Files
digital-psychology/apps/admin-h5/e2e/admin-ops-smoke.spec.ts
T
jackyu66gitandCursor 7e9023f0a8
ci / h5 (push) Canceled after 0s
ci / api (push) Canceled after 0s
ci / ess-docs (push) Canceled after 0s
test(e2e): 前后台联调 Playwright 与 admin 冒烟
新增 e2e-live 真 API 一致性全路径,并为 admin-h5 增加 mock 浏览器冒烟。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-06 22:41:43 +08:00

105 lines
3.4 KiB
TypeScript

import { test, expect } from '@playwright/test'
/** Admin Phase A: login → users → detail grant → audit (API mocked). */
test('admin login users grant and audit with mocked API', async ({ page }) => {
const userId = '11111111-1111-1111-1111-111111111111'
let granted = false
await page.route('**/api/v1/admin/**', async (route) => {
const req = route.request()
const url = req.url()
const method = req.method()
const ok = (data: unknown) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ code: 0, message: 'success', data }),
})
if (url.includes('/auth/login') && method === 'POST') {
await ok({
token: 'adm_e2e_token',
expires_at: new Date(Date.now() + 3600_000).toISOString(),
admin: { id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', username: 'admin' },
})
return
}
if (url.includes('/auth/logout') && method === 'POST') {
await ok({ ok: true })
return
}
if (url.endsWith('/me') || url.includes('/me?')) {
await ok({ id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', username: 'admin' })
return
}
if (url.includes(`/users/${userId}/membership/grant`) && method === 'POST') {
granted = true
await ok({ ok: true })
return
}
if (url.includes(`/users/${userId}`) && method === 'GET') {
await ok({
id: userId,
status: 'active',
created_at: '2026-08-01T00:00:00Z',
profiles: [{ id: 'p1', relation: 'self', display_name: '我' }],
membership: {
active: granted,
plan: granted ? 'month' : undefined,
status: granted ? 'active' : 'none',
expires_at: granted ? '2026-09-01T00:00:00Z' : undefined,
},
recent_orders: [],
})
return
}
if (url.includes('/users') && method === 'GET') {
await ok({
items: [{ id: userId, status: 'active', created_at: '2026-08-01T00:00:00Z' }],
})
return
}
if (url.includes('/orders')) {
await ok({ items: [] })
return
}
if (url.includes('/audit-logs')) {
await ok({
items: granted
? [
{
id: 'aud1',
admin_id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
action: 'membership.grant',
target_type: 'user',
target_id: userId,
meta: { plan: 'month' },
created_at: '2026-08-06T00:00:00Z',
},
]
: [],
})
return
}
await ok({})
})
await page.goto('/login')
await expect(page.getByRole('heading', { name: '运营后台' })).toBeVisible()
await page.locator('input[type="password"]').fill('change-me')
await page.getByRole('button', { name: '登录' }).click()
await expect(page.getByRole('heading', { name: '用户' })).toBeVisible()
await expect(page.getByText(userId)).toBeVisible()
await page.getByRole('link', { name: userId }).click()
await expect(page.getByRole('heading', { name: '用户详情' })).toBeVisible()
await page.getByRole('button', { name: '授予 / 延长' }).click()
await expect(page.getByText('已授予')).toBeVisible()
await page.getByRole('link', { name: '审计' }).click()
await expect(page.getByRole('heading', { name: '审计' })).toBeVisible()
await expect(page.getByText('membership.grant')).toBeVisible()
})