F1-4: schema 页全量接入——错误边界、健康度扫描、抽样渲染实测

渲染链路加固:
- PageRenderPage 加 AmisErrorBoundary class component + SchemaErrorCard,覆盖
  schema 拉取失败/JSON 异常/amis 渲染抛错三种路径,不白屏不崩 Tab
- schema 加载成功后调用 recordRecentPage 写入最近打开
- src/api/schema.ts mock 模式下未配置 templateId 自动 fallback 后端接口

全量健康度扫描 (scripts/schema-scan.mjs):
- 登录→psql 拉取 1489 个 template_id→并发≤8 扫描→产出报告
- 结果: 1489/1489 HTTP 200,JSON 可解析率 100%,顶层 type 全为 page,零失败

抽样渲染实测 (e2e/schema-samples.spec.ts):
- 9 种类型 26 个样本,Node 侧预登录注入真实 JWT,断言 amis-host 挂载且无错误卡片
- 结果: 26/26 全部通过,截图存 e2e/artifacts/

配置更新:
- playwright.config.ts 后端反代改为 8093 (dev profile,含所有端点)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 20:32:53 +08:00
parent effc21d929
commit 0d8593e022
33 changed files with 597 additions and 18 deletions

124
e2e/schema-samples.spec.ts Normal file
View File

@@ -0,0 +1,124 @@
/**
* F1-4 抽样渲染实测 —— 27 个样本模板跨 9 类型真实浏览器渲染
*
* 断言目标:渲染成功(amis-host 挂载 + 无 schema-error-card),不断言有无业务数据。
* 登录方式:Node 侧预登录拿真实 JWT,通过 addInitScript 直接注入 localStorage。
*/
import { expect, test, type Page, type TestInfo } from '@playwright/test';
import * as http from 'node:http';
const BACKEND_NODE = 'http://localhost:8093';
// ---- Node 侧登录(一次,所有 test 共用) ----
let _tokenCache: string | null = null;
async function getToken(): Promise<string> {
if (_tokenCache) return _tokenCache;
return new Promise((resolve, reject) => {
const body = JSON.stringify({ account: 'admin', password: '111111' });
const req = http.request(
{
hostname: 'localhost',
port: 8093,
path: '/api/auth/login',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
},
},
(res) => {
let data = '';
res.on('data', (c) => (data += c));
res.on('end', () => {
try {
const parsed = JSON.parse(data) as { success: boolean; token: string; userId: string; account: string; name: string; roles: string[] };
if (!parsed.success) reject(new Error('登录失败'));
_tokenCache = parsed.token;
resolve(parsed.token);
} catch (e) {
reject(e);
}
});
},
);
req.on('error', reject);
req.write(body);
req.end();
});
}
// 种入真实 JWT(Node 侧已拿到 token,直接注入为常量)
async function seedAuth(page: Page) {
const token = await getToken();
await page.addInitScript(
({ t }: { t: string }) => {
localStorage.setItem('eip4_token', t);
localStorage.setItem(
'eip4_profile',
JSON.stringify({ userId: 'ADMIN', account: 'admin', name: '系统管理员', roles: [] }),
);
},
{ t: token },
);
}
// 27 个样本(9 种 type,各 ≥2 个)
const SAMPLES: Array<{ type: string; id: string }> = [
// type1
{ type: 'type1', id: '00551ef5-1c76-4015-bec9-2e1c8b631deb' },
{ type: 'type1', id: '009c4f52-636a-4688-8148-75cabe83fd83' },
{ type: 'type1', id: '00e7a83b-dd1f-4d7a-a574-bc362bc785d2' },
// type3
{ type: 'type3', id: '0211a0e2-2073-4b24-b59f-e6757e5dcff0' },
{ type: 'type3', id: '0246d4bd-232b-465d-9265-8c1fc1ecbdf2' },
{ type: 'type3', id: '082f27b4-7ae9-4b21-acc9-2b2a6f7d9797' },
// type4
{ type: 'type4', id: '19e418d1-ea62-47c1-9fb4-c33f94c306c4' },
{ type: 'type4', id: '1b7af860-f11d-4b64-931c-13264207d57c' },
{ type: 'type4', id: '21d82b1c-1db0-499a-b2c7-48befd42a9d7' },
// type5
{ type: 'type5', id: '062041e3-0862-49b1-a304-34c873ac5b9e' },
{ type: 'type5', id: 'f4446608-68e3-40f3-8546-2511de73fbdc' },
// type6
{ type: 'type6', id: '05226af5-5c77-470f-b039-d894fc55714d' },
{ type: 'type6', id: '08158341-1b41-48be-9226-90a206960e8f' },
{ type: 'type6', id: '1d9e666d-8923-4d9a-8644-05ba3fa533aa' },
// type7
{ type: 'type7', id: '00511ac1-ee64-4ecd-a550-22842ee5be72' },
{ type: 'type7', id: '00a64619-0014-412f-8ca9-95edcf988c2f' },
{ type: 'type7', id: '0267be11-591a-4533-b7cc-079cef81086e' },
// type100
{ type: 'type100', id: '00ff3f9d-b744-4e55-a731-f98d16d6f7fe' },
{ type: 'type100', id: '06a1d7d2-a202-4b8f-aef3-2e25e3db0ed7' },
{ type: 'type100', id: '06fe97fa-6ebe-41e7-9f88-c6a635c1887e' },
// type101
{ type: 'type101', id: '138b7ca7-befb-4a0c-bfc6-ed8fb4d4e434' },
{ type: 'type101', id: '42861f01-14ea-482d-8031-f30612dfccf4' },
{ type: 'type101', id: '43d2e233-08cf-4019-850e-5c2c073d1f76' },
// type200
{ type: 'type200', id: '00654052-f899-45d6-bbd4-04457c13025a' },
{ type: 'type200', id: '03ec5566-7d99-4eba-85ce-31e25f5e48ab' },
{ type: 'type200', id: '063fdf2a-3032-4220-9974-32f0a5932aa6' },
];
for (const sample of SAMPLES) {
test(`[${sample.type}] ${sample.id}`, async ({ page }, testInfo: TestInfo) => {
await seedAuth(page);
await page.goto(`/page/${sample.id}`);
// 等待渲染完成:amis-host 出现(amis SDK 加载需要时间)
await expect(page.locator('[data-testid="amis-host"]')).toBeVisible({ timeout: 45_000 });
// 确认无错误边界卡片(schema 加载失败/amis 崩溃时会出现)
await expect(page.locator('[data-testid="schema-error-card"]')).not.toBeVisible();
// 截图存 e2e/artifacts/
const screenshotPath = `e2e/artifacts/${sample.type}-${sample.id}.png`;
await page.screenshot({ path: screenshotPath, fullPage: false });
await testInfo.attach(`${sample.type}-${sample.id}`, {
path: screenshotPath,
contentType: 'image/png',
});
});
}