feat: 接通后台渲染异常上报与响应契约校验
This commit is contained in:
1 parent
330d67adc1
commit
adcaa12970
22 files changed
+845
-33
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test, expect, type Page, type Route } from "@playwright/test";
|
||||
|
||||
const adminOrigin = process.env.ADMIN_URL || "http://localhost:3005";
|
||||
@@ -19,12 +20,27 @@ interface MockOptions {
|
||||
memberships?: unknown;
|
||||
/** 品牌名称的异常返回 */
|
||||
brandName?: unknown;
|
||||
/** 始终返回的异常信任内容,用于验证契约错误态 */
|
||||
trustContent?: unknown;
|
||||
/** 只在首次请求时返回的异常信任内容,重新加载后恢复 */
|
||||
trustContentOnce?: unknown;
|
||||
/** 只在首次请求时返回的异常工作区数据,用于触发渲染异常 */
|
||||
workspaceOnce?: unknown;
|
||||
/** 上报接口返回的状态码 */
|
||||
clientErrorStatus?: number;
|
||||
}
|
||||
|
||||
type ClientErrorReport = {
|
||||
errorId?: string;
|
||||
message?: string;
|
||||
route?: string;
|
||||
componentStack?: string;
|
||||
};
|
||||
|
||||
async function mockAdminApi(page: Page, options: MockOptions = {}) {
|
||||
let servedTrustContent = false;
|
||||
let servedWorkspace = false;
|
||||
const clientErrors: ClientErrorReport[] = [];
|
||||
|
||||
await page.route("**/api/**", async (route: Route) => {
|
||||
const request = route.request();
|
||||
@@ -70,13 +86,35 @@ async function mockAdminApi(page: Page, options: MockOptions = {}) {
|
||||
});
|
||||
}
|
||||
if (path === "/admin/config/trust-content" && method === "GET") {
|
||||
if (options.trustContent !== undefined) {
|
||||
return route.fulfill({ json: options.trustContent });
|
||||
}
|
||||
if (!servedTrustContent && options.trustContentOnce !== undefined) {
|
||||
servedTrustContent = true;
|
||||
return route.fulfill({ json: options.trustContentOnce });
|
||||
}
|
||||
return route.fulfill({ json: [] });
|
||||
}
|
||||
if (path === "/admin/client-errors" && method === "POST") {
|
||||
clientErrors.push(JSON.parse(request.postData() ?? "{}") as ClientErrorReport);
|
||||
return route.fulfill({
|
||||
status: options.clientErrorStatus ?? 201,
|
||||
...(options.clientErrorStatus && options.clientErrorStatus >= 400
|
||||
? {
|
||||
json: {
|
||||
data: null,
|
||||
error: { code: "HTTP_500", message: "上报失败" },
|
||||
requestId: "req-client-error",
|
||||
},
|
||||
}
|
||||
: { json: { received: true, duplicate: false } }),
|
||||
});
|
||||
}
|
||||
if (path === "/admin/workspace") {
|
||||
if (!servedWorkspace && options.workspaceOnce !== undefined) {
|
||||
servedWorkspace = true;
|
||||
return route.fulfill({ json: options.workspaceOnce });
|
||||
}
|
||||
return route.fulfill({
|
||||
json: {
|
||||
brand: { id: "brand-current", name: "独立工坊" },
|
||||
@@ -92,6 +130,8 @@ async function mockAdminApi(page: Page, options: MockOptions = {}) {
|
||||
}
|
||||
return route.fulfill({ json: [] });
|
||||
});
|
||||
|
||||
return { clientErrors };
|
||||
}
|
||||
|
||||
test("品牌成员与品牌名称异常时后台外壳仍可正常使用", async ({ page }) => {
|
||||
@@ -109,14 +149,27 @@ test("品牌成员与品牌名称异常时后台外壳仍可正常使用", async
|
||||
await expect(page.getByRole("button", { name: "退出登录" })).toBeVisible();
|
||||
});
|
||||
|
||||
test("内容区渲染出错时保留导航,重新加载后恢复", async ({ page }) => {
|
||||
test("内容区渲染出错时上报错误编号并保留导航,重新加载后恢复", async ({ page }) => {
|
||||
await page.setViewportSize({ width: 1440, height: 1000 });
|
||||
await mockAdminApi(page, { trustContentOnce: [null] });
|
||||
const { clientErrors } = await mockAdminApi(page, {
|
||||
workspaceOnce: {
|
||||
brand: { id: "brand-current", name: "独立工坊" },
|
||||
installations: [],
|
||||
templateContent: { displayName: "独立创作者画册", trustContentTypes: [null] },
|
||||
offerings: [],
|
||||
forms: [],
|
||||
},
|
||||
});
|
||||
|
||||
await page.goto(`${adminOrigin}/admin/trust`);
|
||||
|
||||
await expect(page.getByText("页面渲染出错")).toBeVisible();
|
||||
await expect(page.getByText(/错误编号:admin-/)).toBeVisible();
|
||||
await expect.poll(() => clientErrors.length).toBe(1);
|
||||
const report = clientErrors[0];
|
||||
assert.match(report?.errorId ?? "", /^admin-/);
|
||||
assert.equal(report?.route, "/admin/trust");
|
||||
assert.ok(report?.message);
|
||||
|
||||
// 外壳导航仍然可用,不会因为单个页面崩溃而整站白屏
|
||||
await expect(page.getByRole("button", { name: "退出登录" })).toBeVisible();
|
||||
@@ -130,3 +183,38 @@ test("内容区渲染出错时保留导航,重新加载后恢复", async ({ pa
|
||||
await expect(page.getByRole("heading", { name: "信任内容" })).toBeVisible();
|
||||
await expect(page.getByText("页面渲染出错")).toBeHidden();
|
||||
});
|
||||
|
||||
test("响应契约不符时展示可读错误,不进入渲染错误边界", async ({ page }) => {
|
||||
await page.setViewportSize({ width: 1440, height: 1000 });
|
||||
const { clientErrors } = await mockAdminApi(page, { trustContent: [null] });
|
||||
|
||||
await page.goto(`${adminOrigin}/admin/trust`);
|
||||
|
||||
await expect(page.getByRole("alert")).toContainText("接口返回数据结构不符合契约");
|
||||
await expect(page.getByRole("alert")).toContainText("/admin/config/trust-content");
|
||||
await expect(page.getByText("页面渲染出错")).toBeHidden();
|
||||
assert.equal(clientErrors.length, 0);
|
||||
});
|
||||
|
||||
test("错误上报失败时错误页仍然可用", async ({ page }) => {
|
||||
await page.setViewportSize({ width: 1440, height: 1000 });
|
||||
const { clientErrors } = await mockAdminApi(page, {
|
||||
clientErrorStatus: 500,
|
||||
workspaceOnce: {
|
||||
brand: { id: "brand-current", name: "独立工坊" },
|
||||
installations: [],
|
||||
templateContent: { displayName: "独立创作者画册", trustContentTypes: [null] },
|
||||
offerings: [],
|
||||
forms: [],
|
||||
},
|
||||
});
|
||||
|
||||
await page.goto(`${adminOrigin}/admin/trust`);
|
||||
|
||||
await expect(page.getByText("页面渲染出错")).toBeVisible();
|
||||
await expect(page.getByRole("button", { name: "重新加载页面" })).toBeVisible();
|
||||
await expect.poll(() => clientErrors.length).toBe(1);
|
||||
// 上报失败不会让错误页递归崩溃,也不会产生未捕获异常
|
||||
await page.waitForTimeout(300);
|
||||
await expect(page.getByText("页面渲染出错")).toBeVisible();
|
||||
});
|
||||
Reference in new issue
Block a user