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() })