import assert from "node:assert/strict"; import test from "node:test"; import { classifyContractRefresh } from "./republish-stale-template-contracts.mjs"; function snapshot(overrides = {}) { return { generatedAt: "2026-10-20T00:00:00.000Z", templateKey: "doors", templateDefinition: { key: "doors" }, visualTemplateDefinition: { supportedIndustryTemplates: ["personal", "doors"] }, visualTemplateManifest: { pages: [] }, templatePackageManifest: { supportedIndustryTemplates: ["personal", "doors"] }, installation: { templateVersion: "1.0.6" }, brand: { name: "林境门作" }, offerings: [{ name: "入户门" }], ...overrides, }; } const staleContract = () => snapshot({ generatedAt: "2026-10-20T01:00:00.000Z", visualTemplateDefinition: { supportedIndustryTemplates: ["doors"] }, templatePackageManifest: { supportedIndustryTemplates: ["doors"] }, installation: { templateVersion: "1.0.7" }, }); test("detects a contract-only stale publication", () => { assert.deepEqual(classifyContractRefresh(snapshot(), staleContract()), { status: "contract-stale", changedKeys: ["visualTemplateDefinition", "templatePackageManifest", "installation"], blockingKeys: [], allowedChanges: [], }); }); test("ignores generated timestamps when the publication is current", () => { assert.deepEqual( classifyContractRefresh(snapshot(), snapshot({ generatedAt: "2026-10-21T00:00:00.000Z" })), { status: "current", changedKeys: [], blockingKeys: [], allowedChanges: [] }, ); }); test("blocks automatic publication when draft content has diverged", () => { const current = snapshot({ installation: { templateVersion: "1.0.7" }, brand: { name: "尚未发布的品牌名" }, }); assert.deepEqual(classifyContractRefresh(snapshot(), current), { status: "content-diverged", changedKeys: ["installation", "brand"], blockingKeys: ["brand"], allowedChanges: [], }); }); test("treats a missing key and an explicit null as the same value", () => { const current = snapshot({ brand: { name: "林境门作", miniAppSettings: null } }); assert.equal(classifyContractRefresh(snapshot(), current).status, "current"); }); test("allows an explicitly permitted legacy field removal", () => { const published = snapshot({ brand: { name: "林境门作", config: { visualTemplateKey: "doors" } }, }); const current = snapshot({ brand: { name: "林境门作", config: {} } }); assert.deepEqual( classifyContractRefresh(published, current, { allowedRemovedKeys: ["brand.config.visualTemplateKey"], }), { status: "contract-stale", changedKeys: ["brand"], blockingKeys: [], allowedChanges: ["brand.config.visualTemplateKey"], }, ); }); test("does not allow an unlisted legacy field removal", () => { const published = snapshot({ brand: { name: "林境门作", config: { visualTemplateKey: "doors" } }, }); const current = snapshot({ brand: { name: "林境门作", config: {} } }); assert.deepEqual(classifyContractRefresh(published, current), { status: "content-diverged", changedKeys: ["brand"], blockingKeys: ["brand"], allowedChanges: [], }); }); test("does not allow a permitted legacy field to change value", () => { const published = snapshot({ brand: { name: "林境门作", config: { visualTemplateKey: "doors" } }, }); const current = snapshot({ brand: { name: "林境门作", config: { visualTemplateKey: "other" } } }); assert.deepEqual( classifyContractRefresh(published, current, { allowedRemovedKeys: ["brand.config.visualTemplateKey"], }), { status: "content-diverged", changedKeys: ["brand"], blockingKeys: ["brand"], allowedChanges: [], }, ); }); test("blocks when a permitted removal is combined with other content changes", () => { const published = snapshot({ brand: { name: "林境门作", config: { visualTemplateKey: "doors" } }, }); const current = snapshot({ brand: { name: "林境门作", config: {}, miniAppSettings: { appId: "wx-test" } }, }); const result = classifyContractRefresh(published, current, { allowedRemovedKeys: ["brand.config.visualTemplateKey"], }); assert.equal(result.status, "content-diverged"); assert.deepEqual(result.blockingKeys, ["brand"]); });