fix(regression): resolve dogfood findings across marketing, auth, e2e and docs
- Fix list-to-detail navigation on product/service/solution/case pages - Fix soft 404 on service detail by removing (marketing)/loading.tsx and using force-dynamic - Fix contact form submission feedback and news placeholder image handling - Unify SSR/client authentication state in auth.ts - Add "新闻动态" to main navigation - Fix Playwright storageState path and Firefox footer link flakiness - Add E2E coverage for nav dropdown, cases filter and auth token parsing - Update visual regression baselines (desktop/tablet/mobile, chromium/webkit/firefox) - Update README, lessons-learned and add REGRESSION_REPORT_2026-07-27.md - Ignore .lighthouseci/ and heading-hierarchy-report.json
This commit is contained in:
@@ -1,4 +1,13 @@
|
||||
import { test, expect, APIRequestContext } from '@playwright/test';
|
||||
import { config as dotenvConfig } from 'dotenv';
|
||||
import path from 'path';
|
||||
|
||||
// 加载项目根目录的 .env.local,使 E2E 测试与 dev/preview 服务器使用相同的 CMS_REVALIDATE_SECRET
|
||||
// Playwright 配置已负责设置 process.env,此处仅做兜底加载,不再 override 以避免覆盖 webServer 环境变量
|
||||
dotenvConfig({ path: path.resolve(__dirname, '../.env.local') });
|
||||
|
||||
// CMS 工作流依赖共享数据库与角色权限,浏览器差异可忽略,仅在 chromium 项目运行以避免跨 project 数据竞争
|
||||
test.skip(({ browserName }) => browserName !== 'chromium', 'CMS workflow test runs only in chromium');
|
||||
|
||||
/**
|
||||
* CMS 内容发布工作流 E2E 测试
|
||||
@@ -13,8 +22,60 @@ const REVIEWER_CREDENTIALS = { username: 'e2e_reviewer', password: 'reviewer123'
|
||||
const BASE_URL = process.env.E2E_BASE_URL || 'http://localhost:3000';
|
||||
const REVALIDATE_SECRET = process.env.CMS_REVALIDATE_SECRET || 'e2e-test-secret';
|
||||
|
||||
// 跟踪当前测试创建的内容条目 ID,afterEach 只清理当前测试的数据,避免并行/跨 project 误删
|
||||
const createdItemIds: string[] = [];
|
||||
|
||||
test.setTimeout(120000);
|
||||
|
||||
async function deleteItemsByIds(request: APIRequestContext, token: string, ids: string[]): Promise<void> {
|
||||
if (ids.length === 0) return;
|
||||
|
||||
await Promise.all(
|
||||
ids.map((id) =>
|
||||
request.delete(`${BASE_URL}/api/admin/items?id=${id}`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
async function cleanupStaleE2ENews(request: APIRequestContext, token: string): Promise<void> {
|
||||
const response = await request.get(`${BASE_URL}/api/admin/items`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
params: { modelCode: 'news', pageSize: '1000' },
|
||||
});
|
||||
|
||||
if (!response.ok()) {
|
||||
console.warn('Failed to fetch news items for cleanup:', await response.text());
|
||||
return;
|
||||
}
|
||||
|
||||
const body = await response.json();
|
||||
const items = (body.items || body.data || []) as Array<{ id: string; title: string }>;
|
||||
const staleItems = items.filter((item) => item.title?.startsWith('E2E 测试新闻'));
|
||||
|
||||
await deleteItemsByIds(
|
||||
request,
|
||||
token,
|
||||
staleItems.map((item) => item.id)
|
||||
);
|
||||
}
|
||||
|
||||
test.beforeAll(async ({ request }) => {
|
||||
// 清理历史残留 E2E 测试数据(仅在 chromium 项目执行,不会误删其他 project 数据)
|
||||
const token = await loginAdmin(request);
|
||||
await cleanupStaleE2ENews(request, token);
|
||||
});
|
||||
|
||||
test.afterEach(async ({ request }) => {
|
||||
const token = await loginAdmin(request);
|
||||
// 优先按 ID 清理当前测试创建的条目,避免误删其他并行测试的数据
|
||||
const ids = createdItemIds.splice(0);
|
||||
await deleteItemsByIds(request, token, ids);
|
||||
// 再兜底清理历史残留
|
||||
await cleanupStaleE2ENews(request, token);
|
||||
});
|
||||
|
||||
async function login(
|
||||
request: APIRequestContext,
|
||||
credentials: { username: string; password: string }
|
||||
@@ -83,6 +144,7 @@ async function createNewsDraft(
|
||||
const body = await response.json();
|
||||
expect(body.status).toBe('draft');
|
||||
|
||||
createdItemIds.push(body.id);
|
||||
return { id: body.id, slug, title };
|
||||
}
|
||||
|
||||
@@ -108,7 +170,7 @@ async function approveItem(
|
||||
request: APIRequestContext,
|
||||
token: string,
|
||||
itemId: string
|
||||
): Promise<void> {
|
||||
): Promise<{ status: string; slug: string }> {
|
||||
const response = await request.post(`${BASE_URL}/api/admin/items/${itemId}/workflow`, {
|
||||
data: { action: 'approve' },
|
||||
headers: {
|
||||
@@ -120,6 +182,7 @@ async function approveItem(
|
||||
expect(response.ok()).toBeTruthy();
|
||||
const body = await response.json();
|
||||
expect(body.status).toBe('published');
|
||||
return body;
|
||||
}
|
||||
|
||||
async function revalidateNews(request: APIRequestContext, slug: string): Promise<void> {
|
||||
@@ -133,8 +196,11 @@ async function revalidateNews(request: APIRequestContext, slug: string): Promise
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
const responseBody = await response.text();
|
||||
console.log('[DEBUG] revalidate status=%s body=%s', response.status(), responseBody);
|
||||
|
||||
expect(response.ok()).toBeTruthy();
|
||||
const body = await response.json();
|
||||
const body = JSON.parse(responseBody);
|
||||
expect(body.code).toBe(0);
|
||||
expect(body.data.revalidatedPaths).toContain('/news');
|
||||
expect(body.data.revalidatedPaths).toContain(`/news/${slug}`);
|
||||
@@ -162,18 +228,28 @@ async function ensureRolePermissions(
|
||||
|
||||
async function verifyNewsVisibleOnFrontend(
|
||||
page: import('@playwright/test').Page,
|
||||
request: APIRequestContext,
|
||||
slug: string,
|
||||
title: string,
|
||||
suffix: string
|
||||
): Promise<void> {
|
||||
// 先用 API context 拉取本地页面,确认服务端渲染正常
|
||||
const apiResponse = await request.get(`${BASE_URL}/news/${slug}`);
|
||||
const apiHtml = await apiResponse.text();
|
||||
console.log('[DEBUG] api status=%s title=%s', apiResponse.status(), apiHtml.match(/<title>([^<]+)<\/title>/)?.[1]);
|
||||
|
||||
// 直接访问详情页验证 ISR 刷新后的内容可见(比列表页更稳定,避免整页缓存波动)
|
||||
await page.goto(`/news/${slug}`, { waitUntil: 'domcontentloaded', timeout: 30000 });
|
||||
|
||||
const currentUrl = page.url();
|
||||
const h1Text = (await page.locator('h1').first().textContent().catch(() => '')) || '';
|
||||
console.log('[DEBUG] page.url=%s h1=%s expected title=%s', currentUrl, h1Text, title);
|
||||
|
||||
await expect
|
||||
.poll(
|
||||
async () => {
|
||||
const h1Text = (await page.locator('h1').first().textContent().catch(() => '')) || '';
|
||||
return h1Text.includes(title);
|
||||
const text = (await page.locator('h1').first().textContent().catch(() => '')) || '';
|
||||
return text.includes(title);
|
||||
},
|
||||
{ timeout: 20000, intervals: [1000, 2000, 2000] }
|
||||
)
|
||||
@@ -183,15 +259,21 @@ async function verifyNewsVisibleOnFrontend(
|
||||
}
|
||||
|
||||
test.describe('CMS 内容发布工作流', () => {
|
||||
// 同一 worker 内串行执行,避免共享 DB/角色权限在并行时相互影响
|
||||
test.describe.configure({ mode: 'serial' });
|
||||
|
||||
test('admin 可完成 创建草稿 → 提交审核 → 发布 → 前台可见', async ({ request, page }) => {
|
||||
const suffix = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
|
||||
const token = await loginAdmin(request);
|
||||
const { id, slug, title } = await createNewsDraft(request, token, suffix);
|
||||
console.log('[DEBUG] created item id=%s slug=%s title=%s', id, slug, title);
|
||||
await submitForReview(request, token, id);
|
||||
await approveItem(request, token, id);
|
||||
const afterApprove = await approveItem(request, token, id);
|
||||
console.log('[DEBUG] after approve status=%s slug=%s', afterApprove.status, afterApprove.slug);
|
||||
await revalidateNews(request, slug);
|
||||
await verifyNewsVisibleOnFrontend(page, slug, title, suffix);
|
||||
console.log('[DEBUG] revalidate done, navigating to /news/%s', slug);
|
||||
await verifyNewsVisibleOnFrontend(page, request, slug, title, suffix);
|
||||
});
|
||||
|
||||
test('非法状态流转会被拦截', async ({ request }) => {
|
||||
@@ -255,6 +337,6 @@ test.describe('CMS 内容发布工作流', () => {
|
||||
|
||||
// 5. 刷新 ISR 缓存并验证前台可见
|
||||
await revalidateNews(request, slug);
|
||||
await verifyNewsVisibleOnFrontend(page, slug, title, suffix);
|
||||
await verifyNewsVisibleOnFrontend(page, request, slug, title, suffix);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user