feat: 增加模板版本独立预览入口

This commit is contained in:
que01 committed 2026-10-16 17:28:07 +08:00
1 parent 413413efda
commit 6edde5a8d6
15 files changed
+781 -14

No files matched your search

+1 -1
View File
@@ -218,7 +218,7 @@ export function Inquiry({
)}
<Button
className="submit-button"
disabled={sending}
disabled={sending || preview}
loading={sending}
onClick={() => void submit()}
>
@@ -0,0 +1,89 @@
import fs from "node:fs";
import path from "node:path";
import { execFileSync } from "node:child_process";
import { test, expect } from "@playwright/test";
// 使用真实包和生产投影生成 UI fixture;HTTP 授权与持久化由 API 集成测试覆盖。
const manifest = JSON.parse(
fs.readFileSync("../../packages/template-atelier-solopreneur/template.json", "utf8"),
);
const version = {
id: "preview-browser-version",
packageKey: manifest.packageKey,
version: manifest.version,
displayName: manifest.displayName,
status: "imported",
contentChecksum: "fixture-checksum",
manifestSnapshot: manifest,
importedBy: { displayName: "模板管理员" },
validationReport: {},
manifestSummary: { supportedIndustryTemplates: manifest.supportedIndustryTemplates },
};
test("模板管理可重试预览,并切换我的、详情和禁止提交的咨询内页", async ({ page }) => {
// 独立 Node ESM 进程避免 Playwright CJS transform 改写 API 的 import-only 依赖。
const fixture = JSON.parse(
execFileSync(
process.execPath,
[
"--input-type=module",
"-e",
`
import 'reflect-metadata';
import { AdminTemplatePreviewService } from './dist/modules/admin/admin-template-preview.service.js';
let input = '';
for await (const chunk of process.stdin) input += chunk;
const version = JSON.parse(input);
const service = new AdminTemplatePreviewService({ templatePackageVersion: { findUnique: async () => version } });
process.stdout.write(JSON.stringify(await service.preview(version.id, 'personal')));
`,
],
{ cwd: path.resolve("../api"), input: JSON.stringify(version), encoding: "utf8" },
),
);
await page.setViewportSize({ width: 1440, height: 1000 });
await page.addInitScript(() => {
localStorage.setItem("adminToken", "fixture-token");
localStorage.setItem("adminPlatformTemplateRole", "template_admin");
});
let attempts = 0;
const writes: string[] = [];
await page.route("**/api/**", async (route) => {
const url = new URL(route.request().url());
if (route.request().method() !== "GET") writes.push(url.pathname);
if (url.pathname.endsWith("/preview")) {
attempts++;
if (attempts === 1)
return route.fulfill({
status: 400,
json: { error: { code: "HTTP_400", message: "预览测试失败,请重试" } },
});
return route.fulfill({ json: fixture });
}
if (url.pathname.endsWith("/admin/templates"))
return route.fulfill({ json: { items: [version], page: 1, pageSize: 20, total: 1 } });
if (url.pathname.endsWith("/admin/brands")) return route.fulfill({ json: [] });
return route.fulfill({ json: { name: "测试工作区" } });
});
await page.goto(`${process.env.ADMIN_URL || "http://localhost:3016"}/admin/templates`);
await page.getByRole("button", { name: /预览/ }).click();
const modal = page.getByRole("dialog");
await expect(modal.getByText("预览测试失败,请重试")).toBeVisible();
await modal.getByRole("button", { name: /重试/ }).click();
await expect(modal.locator(".atelier-founder-name")).toHaveText(
manifest.sampleContent.personal.brandConfig.founder,
);
const choosePage = async (label: string) => {
await modal.getByLabel("预览页面").click();
await page.locator(".ant-select-item-option-content").getByText(label, { exact: true }).click();
};
await choosePage("我的");
await expect(modal.locator(".component-template-preview")).toContainText("我的咨询记录");
await choosePage("业务详情");
await expect(modal.locator(".component-template-preview")).toContainText(
manifest.sampleContent.personal.offerings[0].name,
);
await choosePage("咨询表单");
await expect(modal.locator(".inquiry-form")).toBeVisible();
await expect(modal.locator(".inquiry-form button").last()).toBeDisabled();
expect(writes).toEqual([]);
});