feat: 收敛行业模板身份为品牌单一真相

- BrandProfile.templateKey 为唯一真相,安装记录降级为同模板的版本台账
- 安装只允许补建当前模板台账,跨行业模板请求返回 400,杜绝身份漂移
- preview 不再强制要求台账存在,台账缺失时以模板目录版本为准
- seed 按品牌身份补齐模板台账,消除「身份有、台账无」
- 记录 ADR 0026
This commit is contained in:
que01 committed 2026-10-14 13:55:54 +08:00
1 parent 896f3240f9
commit 6073e72940
4 files changed
+298 -40

No files matched your search

+157 -13
View File
@@ -1092,6 +1092,11 @@ test("admin release lifecycle is authorized, brand-scoped, and versioned", async
assert.equal((await publishReleaseForTests("恢复集成测试基线")).statusCode, 201);
});
function errorMessage(body) {
const parsed = JSON.parse(body);
return parsed?.error?.message ?? parsed?.message ?? "";
}
async function request(url, options = {}) {
return app
.getHttpAdapter()
@@ -1182,6 +1187,114 @@ test("visual template compatibility is enforced by catalog, preview, and brand s
assert.equal(compatibleSwitch.statusCode, 200);
});
test("industry template state has a single source of truth", async () => {
const brand = await prisma.brandProfile.findUnique({
where: { id: brandId },
select: { templateKey: true, config: true },
});
assert.ok(brand);
const templateKey = brand.templateKey;
const visualTemplateKey = brand.config?.visualTemplateKey;
const installationBefore = await prisma.brandTemplateInstallation.findFirst({
where: { brandId },
});
const fieldsBefore = await prisma.brandFieldDefinition.findMany({ where: { brandId } });
const unauthorized = await request("/admin/workspace/template/preview");
assert.equal(unauthorized.statusCode, 401);
const forbidden = await request("/admin/workspace/template/preview", {
headers: { authorization: `Bearer ${customerToken}` },
});
assert.equal(forbidden.statusCode, 403);
const login = await request("/admin/session/login", {
method: "POST",
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
});
const headers = {
authorization: `Bearer ${JSON.parse(login.body).data?.token ?? JSON.parse(login.body).token}`,
};
try {
// 品牌身份来自 BrandProfile.templateKey;即使版本台账缺失,预览也必须可用而不是 400。
const preview = await request("/admin/workspace/template/preview", { headers });
assert.equal(preview.statusCode, 200);
const previewBody = JSON.parse(preview.body);
assert.equal(previewBody.templateKey, templateKey);
assert.equal(previewBody.currentVersion, previewBody.latestVersion);
assert.equal(previewBody.canUpgrade, false);
assert.equal(previewBody.canRollback, Boolean(installationBefore?.previousSnapshots?.length));
const otherKey = templateKey === "personal" ? "food" : "personal";
// 安装其他行业模板必须被拒绝,且不能改写品牌身份或视觉模板。
const rejected = await request("/admin/workspace/install", {
method: "POST",
headers,
payload: { templateKey: otherKey },
});
assert.equal(rejected.statusCode, 400);
assert.match(errorMessage(rejected.body), /更换行业模板不在本版本范围内/);
const afterRejected = await prisma.brandProfile.findUnique({
where: { id: brandId },
select: { templateKey: true, config: true },
});
assert.equal(afterRejected.templateKey, templateKey);
assert.equal(afterRejected.config?.visualTemplateKey, visualTemplateKey);
// 台账已存在时重复安装当前模板应被拒绝。
if (installationBefore) {
const repeated = await request("/admin/workspace/install", {
method: "POST",
headers,
payload: { templateKey },
});
assert.equal(repeated.statusCode, 400);
assert.match(errorMessage(repeated.body), /已经安装/);
// 缺失的版本台账可以由安装补齐,且不改动品牌身份与视觉模板。
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
const repaired = await request("/admin/workspace/install", {
method: "POST",
headers,
payload: { templateKey },
});
assert.equal(repaired.statusCode, 201, repaired.body);
const installationAfter = await prisma.brandTemplateInstallation.findFirst({
where: { brandId },
});
assert.equal(installationAfter.templateKey, templateKey);
const afterRepair = await prisma.brandProfile.findUnique({
where: { id: brandId },
select: { templateKey: true, config: true },
});
assert.equal(afterRepair.templateKey, templateKey);
assert.equal(afterRepair.config?.visualTemplateKey, visualTemplateKey);
}
} finally {
const createdFieldIds = (await prisma.brandFieldDefinition.findMany({ where: { brandId } }))
.filter((item) => !fieldsBefore.some((current) => current.id === item.id))
.map((item) => item.id);
if (createdFieldIds.length)
await prisma.brandFieldDefinition.deleteMany({ where: { id: { in: createdFieldIds } } });
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
if (installationBefore) {
await prisma.brandTemplateInstallation.create({
data: {
brandId,
templateKey: installationBefore.templateKey,
templateVersion: installationBefore.templateVersion,
configSnapshot: installationBefore.configSnapshot,
},
});
}
await prisma.brandProfile.update({
where: { id: brandId },
data: { templateKey, config: brand.config },
});
}
});
test("seeded public workspaces keep personal, food, and doors brands isolated", async () => {
const expected = [
{
@@ -1536,7 +1649,8 @@ test("admin capability list includes template defaults and brand overrides", asy
await prisma.capabilityInstallation.deleteMany({ where: { brandId } });
});
test("admin can install doors industry template through the shared runtime contract", async () => {
test("installing another industry template is rejected without drifting brand identity", async () => {
const originalBrand = await prisma.brandProfile.findUniqueOrThrow({ where: { id: brandId } });
await prisma.inquirySubmission.deleteMany({ where: { brandId } });
await prisma.inquiryForm.deleteMany({ where: { brandId } });
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
@@ -1546,27 +1660,40 @@ test("admin can install doors industry template through the shared runtime contr
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
});
const token = JSON.parse(login.body).data?.token ?? JSON.parse(login.body).token;
const headers = { authorization: `Bearer ${token}` };
try {
// 品牌身份是 food;装 doors 属于「更换行业模板」,不在本版本范围内。
const installed = await request("/admin/workspace/install", {
method: "POST",
headers: { authorization: `Bearer ${token}` },
headers,
payload: { templateKey: "doors" },
});
assert.equal(installed.statusCode, 201);
assert.equal(JSON.parse(installed.body).template.key, "doors");
assert.equal(installed.statusCode, 400, installed.body);
assert.match(errorMessage(installed.body), /更换行业模板不在本版本范围内/);
// 拒绝之后不能留下任何漂移:品牌身份、视觉模板、台账都不变。
const brandAfter = await prisma.brandProfile.findUniqueOrThrow({ where: { id: brandId } });
assert.equal(brandAfter.templateKey, originalBrand.templateKey);
assert.deepEqual(brandAfter.config, originalBrand.config);
assert.equal(await prisma.brandTemplateInstallation.count({ where: { brandId } }), 0);
// 公开运行时仍按品牌身份走 food 模板,没有被 doors 污染。
const workspace = await request("/public/workspace");
assert.equal(workspace.statusCode, 200);
const body = JSON.parse(workspace.body);
assert.equal(body.template.key, "food");
assert.equal(body.publication.status, "published");
const samplePreview = await request("/admin/workspace/sample-content/preview", {
headers: { authorization: `Bearer ${token}` },
});
const samplePreview = await request("/admin/workspace/sample-content/preview", { headers });
assert.equal(samplePreview.statusCode, 200);
assert.equal(JSON.parse(samplePreview.body).templateKey, "doors");
assert.equal(JSON.parse(samplePreview.body).templateKey, originalBrand.templateKey);
} finally {
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
await prisma.brandProfile.update({
where: { id: brandId },
data: { templateKey: originalBrand.templateKey, config: originalBrand.config },
});
}
});
@@ -1671,7 +1798,7 @@ test("admin can preview and import sample content idempotently", async () => {
});
});
test("admin can install a template and public workspace exposes enabled configuration", async () => {
test("template ledger install, upgrade preview, and rollback stay brand-scoped", async () => {
await prisma.inquirySubmission.deleteMany({ where: { brandId } });
await prisma.inquiryForm.deleteMany({ where: { brandId } });
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
@@ -1687,14 +1814,31 @@ test("admin can install a template and public workspace exposes enabled configur
headers: { authorization: `Bearer ${customerToken}` },
});
assert.equal(customerAccess.statusCode, 403);
const currentTemplateKey = (
await prisma.brandProfile.findUniqueOrThrow({
where: { id: brandId },
select: { templateKey: true },
})
).templateKey;
const crossTemplate = await request("/admin/workspace/install", {
method: "POST",
headers: { authorization: `Bearer ${token}` },
payload: { templateKey: currentTemplateKey === "personal" ? "food" : "personal" },
});
assert.equal(crossTemplate.statusCode, 400, crossTemplate.body);
const installed = await request("/admin/workspace/install", {
method: "POST",
headers: { authorization: `Bearer ${token}` },
payload: { templateKey: "personal" },
payload: { templateKey: currentTemplateKey },
});
assert.equal(installed.statusCode, 201);
assert.equal(JSON.parse(installed.body).template.key, "personal");
assert.equal(JSON.parse(installed.body).visualTemplate.key, "linjing-editorial");
assert.equal(installed.statusCode, 201, installed.body);
assert.equal(JSON.parse(installed.body).template.key, currentTemplateKey);
assert.equal(
JSON.parse(installed.body).visualTemplate.supportedIndustryTemplates.includes(
currentTemplateKey,
),
true,
);
assert.equal(JSON.parse(installed.body).installation.configSnapshot.schemaVersion, "1.0");
try {
const previewUnauthorized = await request("/admin/workspace/template/preview");