feat: 分离模板演示数据并校验预览资源
This commit is contained in:
1 parent
6edde5a8d6
commit
b60174885e
31 files changed
+1785
-69
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, readFileSync, readdirSync } from "node:fs";
|
||||
import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { after, before, test } from "node:test";
|
||||
@@ -41,22 +41,50 @@ function multipartArchive(archive) {
|
||||
};
|
||||
}
|
||||
|
||||
function createTemplateArchive(template = "acheng-campus-food") {
|
||||
function createTemplateArchive(template = "acheng-campus-food", mutateManifest) {
|
||||
const output = mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-pack-"));
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
path.resolve(process.cwd(), "../../scripts/template-pack.cjs"),
|
||||
"--template",
|
||||
template,
|
||||
"--out",
|
||||
output,
|
||||
],
|
||||
{ cwd: path.resolve(process.cwd(), "../.."), stdio: "ignore" },
|
||||
);
|
||||
const filename = readdirSync(output).find((item) => item.endsWith(".tar.gz"));
|
||||
assert.ok(filename);
|
||||
return readFileSync(path.join(output, filename));
|
||||
try {
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
path.resolve(process.cwd(), "../../scripts/template-pack.cjs"),
|
||||
"--template",
|
||||
template,
|
||||
"--out",
|
||||
output,
|
||||
],
|
||||
{ cwd: path.resolve(process.cwd(), "../.."), stdio: "ignore" },
|
||||
);
|
||||
const filename = readdirSync(output).find((item) => item.endsWith(".tar.gz"));
|
||||
assert.ok(filename);
|
||||
if (mutateManifest) {
|
||||
const packageName = filename.slice(0, -7);
|
||||
const directory = path.join(output, packageName);
|
||||
const manifestPath = path.join(directory, "template.json");
|
||||
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
||||
mutateManifest(manifest);
|
||||
const content = Buffer.from(JSON.stringify(manifest));
|
||||
writeFileSync(manifestPath, content);
|
||||
const checksumPath = path.join(directory, "checksums.json");
|
||||
const checksums = JSON.parse(readFileSync(checksumPath, "utf8"));
|
||||
const entry = checksums.files.find((item) => item.path === "template.json");
|
||||
entry.bytes = content.length;
|
||||
entry.sha256 = createHash("sha256").update(content).digest("hex");
|
||||
const canonicalEntries = checksums.files.map((item) => ({
|
||||
bytes: item.bytes,
|
||||
path: item.path,
|
||||
sha256: item.sha256,
|
||||
}));
|
||||
checksums.contentChecksum = createHash("sha256")
|
||||
.update(JSON.stringify(canonicalEntries))
|
||||
.digest("hex");
|
||||
writeFileSync(checksumPath, JSON.stringify(checksums));
|
||||
execFileSync("tar", ["-czf", path.join(output, filename), "-C", output, packageName]);
|
||||
}
|
||||
return readFileSync(path.join(output, filename));
|
||||
} finally {
|
||||
rmSync(output, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
test("模板目录只允许平台模板管理员访问,并支持安全、幂等导入", async () => {
|
||||
@@ -190,6 +218,19 @@ test("平台模板管理员可以按行业预览版本,且预览不写入品
|
||||
});
|
||||
assert.equal(login.statusCode, 201);
|
||||
const headers = { authorization: `Bearer ${JSON.parse(login.body).token}` };
|
||||
const invalidArchive = createTemplateArchive("atelier-solopreneur", (manifest) => {
|
||||
manifest.previewFixture.personal.safety.submissions = "enabled";
|
||||
});
|
||||
const invalidMultipart = multipartArchive(invalidArchive);
|
||||
const versionCount = await prisma.templatePackageVersion.count();
|
||||
const rejectedImport = await request("/admin/templates/import", {
|
||||
method: "POST",
|
||||
headers: { ...headers, ...invalidMultipart.headers },
|
||||
rawPayload: invalidMultipart.payload,
|
||||
});
|
||||
assert.equal(rejectedImport.statusCode, 400, rejectedImport.body);
|
||||
assert.match(JSON.parse(rejectedImport.body).error.message, /previewFixture/);
|
||||
assert.equal(await prisma.templatePackageVersion.count(), versionCount);
|
||||
const archive = createTemplateArchive("atelier-solopreneur");
|
||||
const multipart = multipartArchive(archive);
|
||||
const imported = await request("/admin/templates/import", {
|
||||
@@ -213,7 +254,7 @@ test("平台模板管理员可以按行业预览版本,且预览不写入品
|
||||
]);
|
||||
const before = await businessCounts();
|
||||
const preview = await request(`/admin/templates/${versionId}/preview?industry=personal`, {
|
||||
headers,
|
||||
headers: { ...headers, "x-request-id": "independent-template-preview" },
|
||||
});
|
||||
assert.equal(preview.statusCode, 200, preview.body);
|
||||
const body = JSON.parse(preview.body);
|
||||
@@ -226,11 +267,85 @@ test("平台模板管理员可以按行业预览版本,且预览不写入品
|
||||
assert.equal(body.workspace.brand.config.visualTemplateKey, "atelier-solopreneur");
|
||||
assert.equal(body.template.pages.includes("profile"), true);
|
||||
assert.equal(body.template.pages.includes("inquiry"), true);
|
||||
assert.equal(body.preview.fixtureSource, "previewFixture");
|
||||
assert.equal(body.workspace.brand.name, "独立工坊 · 演示品牌");
|
||||
assert.equal(body.workspace.brand.founder, "林予 · 示例主理人");
|
||||
assert.equal(body.workspace.brand.contactPhone, null);
|
||||
assert.equal(body.preview.safety.submissions, "disabled");
|
||||
assert.ok(body.preview.pages.find((page) => page.key === "home").modules.length > 0);
|
||||
assert.ok(
|
||||
body.preview.assets.some(
|
||||
(asset) => asset.binding === "brand.avatarUrl" && asset.status === "missing",
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
body.preview.assets.some(
|
||||
(asset) => asset.binding === "offering.coverUrl" && asset.status === "ready",
|
||||
),
|
||||
);
|
||||
assert.match(
|
||||
body.preview.assetFiles["/assets/cases/campus.jpg"].dataUrl,
|
||||
/^data:image\/jpeg;base64,/,
|
||||
);
|
||||
assert.match(body.preview.assetFiles["/assets/cases/campus.jpg"].sha256, /^[a-f0-9]{64}$/);
|
||||
const previewAudit = await request(`/admin/templates/${versionId}/audit`, { headers });
|
||||
assert.equal(previewAudit.statusCode, 200);
|
||||
const event = JSON.parse(previewAudit.body).find((entry) => entry.action === "previewed");
|
||||
assert.equal(event.requestId, "independent-template-preview");
|
||||
assert.deepEqual(event.metadata, {
|
||||
industry: "personal",
|
||||
contentChecksum: body.template.contentChecksum,
|
||||
fixtureSource: "previewFixture",
|
||||
});
|
||||
assert.ok(event.actor.id);
|
||||
const stored = await prisma.templatePackageVersion.findUniqueOrThrow({
|
||||
where: { id: versionId },
|
||||
});
|
||||
const failures = [
|
||||
[{ ...stored.manifestSnapshot, sampleContent: {} }, /缺少必需数据/],
|
||||
[{ ...stored.manifestSnapshot, previewFixture: {} }, /previewFixture/],
|
||||
[{ ...stored.manifestSnapshot, previewFixture: { personal: null } }, /previewFixture/],
|
||||
[
|
||||
{
|
||||
...stored.manifestSnapshot,
|
||||
previewFixture: {
|
||||
personal: {
|
||||
...stored.manifestSnapshot.previewFixture.personal,
|
||||
safety: { submissions: "enabled" },
|
||||
},
|
||||
},
|
||||
},
|
||||
/previewFixture/,
|
||||
],
|
||||
[
|
||||
{
|
||||
...stored.manifestSnapshot,
|
||||
previewFixture: {
|
||||
personal: {
|
||||
...stored.manifestSnapshot.previewFixture.personal,
|
||||
brand: {
|
||||
...stored.manifestSnapshot.previewFixture.personal.brand,
|
||||
contactPhone: "13800000000",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
/previewFixture/,
|
||||
],
|
||||
[
|
||||
{
|
||||
...stored.manifestSnapshot,
|
||||
previewFixture: {
|
||||
personal: {
|
||||
...stored.manifestSnapshot.previewFixture.personal,
|
||||
content: {
|
||||
...stored.manifestSnapshot.previewFixture.personal.content,
|
||||
templateKey: "food",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
/所属行业/,
|
||||
],
|
||||
[{ ...stored.manifestSnapshot, requiredCapabilities: ["unknown"] }, /未注册的 capability/],
|
||||
[{ ...stored.manifestSnapshot, visualTemplateKey: "unknown" }, /renderer/],
|
||||
[{ ...stored.manifestSnapshot, runtimeContract: "9.0" }, /契约/],
|
||||
@@ -241,6 +356,41 @@ test("平台模板管理员可以按行业预览版本,且预览不写入品
|
||||
[{ ...stored.manifestSnapshot, admin: { sections: [{ fields: [null] }] } }, /契约/],
|
||||
];
|
||||
try {
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
data: { archiveChecksum: "0".repeat(64) },
|
||||
});
|
||||
const corrupt = await request(`/admin/templates/${versionId}/preview?industry=personal`, {
|
||||
headers,
|
||||
});
|
||||
assert.equal(corrupt.statusCode, 409, corrupt.body);
|
||||
assert.match(JSON.parse(corrupt.body).error.message, /摘要不一致/);
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
data: { archiveChecksum: stored.archiveChecksum },
|
||||
});
|
||||
// 独立演示内容不依赖 bootstrap;旧包则显式标明兼容来源。
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
data: { manifestSnapshot: { ...stored.manifestSnapshot, sampleContent: {} } },
|
||||
});
|
||||
const independent = await request(`/admin/templates/${versionId}/preview?industry=personal`, {
|
||||
headers,
|
||||
});
|
||||
assert.equal(independent.statusCode, 200, independent.body);
|
||||
assert.equal(JSON.parse(independent.body).workspace.brand.founder, "林予 · 示例主理人");
|
||||
const legacyManifest = { ...stored.manifestSnapshot };
|
||||
delete legacyManifest.previewFixture;
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
data: { manifestSnapshot: legacyManifest },
|
||||
});
|
||||
const legacy = await request(`/admin/templates/${versionId}/preview?industry=personal`, {
|
||||
headers,
|
||||
});
|
||||
assert.equal(legacy.statusCode, 200, legacy.body);
|
||||
assert.equal(JSON.parse(legacy.body).preview.fixtureSource, "legacy-sampleContent");
|
||||
assert.equal(JSON.parse(legacy.body).preview.diagnostics[0].code, "LEGACY_PREVIEW_SAMPLE");
|
||||
for (const [manifestSnapshot, reason] of failures) {
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
@@ -255,7 +405,7 @@ test("平台模板管理员可以按行业预览版本,且预览不写入品
|
||||
} finally {
|
||||
await prisma.templatePackageVersion.update({
|
||||
where: { id: versionId },
|
||||
data: { manifestSnapshot: stored.manifestSnapshot },
|
||||
data: { manifestSnapshot: stored.manifestSnapshot, archiveChecksum: stored.archiveChecksum },
|
||||
});
|
||||
}
|
||||
const noIndustry = await request(`/admin/templates/${versionId}/preview`, { headers });
|
||||
|
||||
Reference in new issue
Block a user