import { describe, expect, it } from "vitest"; import { defaultPermissionSeeds } from "@hivegrid/db/permissions-seed.server"; import { shkTemplateRoleSeeds } from "@hivegrid/db/shk-template-roles-seed.server"; import { digestShkV2TenantBootstrapConfig, SHK_V2_TENANT_BOOTSTRAP_CONFIG, shkV2TenantBootstrapConfigSchema } from "./shk-v2.config"; describe("SHK v2 tenant bootstrap config", () => { it("is schema-valid, versioned and references the single global Admin owner role", () => { expect(shkV2TenantBootstrapConfigSchema.safeParse(SHK_V2_TENANT_BOOTSTRAP_CONFIG).success).toBe(true); expect(SHK_V2_TENANT_BOOTSTRAP_CONFIG.templateKey).toBe("shk-v2"); expect(SHK_V2_TENANT_BOOTSTRAP_CONFIG.ownerRole).toEqual({ name: "Admin", type: "app", ownership: "global" }); }); it("defines exactly the five decided editable tenant roles from conservative registered permissions", () => { expect(SHK_V2_TENANT_BOOTSTRAP_CONFIG.tenantRoles.map((role) => role.name)).toEqual([ "Vertrieb", "Marketing", "Innendienst", "Buchhaltung/Verwaltung", "Monteur", ]); const validPermissionKeys = new Set(defaultPermissionSeeds); for (const role of SHK_V2_TENANT_BOOTSTRAP_CONFIG.tenantRoles) { expect(role.isTemplateOrigin).toBe(true); expect( role.permissionKeys.every((key) => validPermissionKeys.has(key)), `${role.name} references an unknown permission` ).toBe(true); expect( role.permissionKeys.some((key) => key.startsWith("admin.")), `${role.name} references an operator permission` ).toBe(false); expect(new Set(role.permissionKeys).size).toBe(role.permissionKeys.length); } }); it("derives Vertrieb, Marketing, Innendienst and Monteur from the existing validated role baselines", () => { const existing = new Map(shkTemplateRoleSeeds.map((role) => [role.name, role.permissionKeys])); const configured = new Map(SHK_V2_TENANT_BOOTSTRAP_CONFIG.tenantRoles.map((role) => [role.name, role.permissionKeys])); expect(configured.get("Vertrieb")).toEqual(existing.get("Vertriebler")); expect(configured.get("Marketing")).toEqual(existing.get("Marketing")); expect(configured.get("Monteur")).toEqual(existing.get("Monteur")); expect(configured.get("Innendienst")).toEqual(existing.get("Büro/Backoffice")?.filter((key) => key !== "app.settings.subscription.view")); expect(configured.get("Innendienst")?.some((key) => key.includes("subscription") || key.includes("invoice"))).toBe(false); }); it("keeps Buchhaltung/Verwaltung read-only and limited to existing administration and subscription views", () => { const accounting = SHK_V2_TENANT_BOOTSTRAP_CONFIG.tenantRoles.find((role) => role.name === "Buchhaltung/Verwaltung"); expect(accounting?.permissionKeys).toEqual([ "app.settings.members.view", "app.settings.account.view", "app.settings.subscription.view", "app.settings.subscription.invoices.view", "dashboard.read", ]); expect(accounting?.permissionKeys.some((key) => /(?:create|update|delete|write|manage|admin|set)$/.test(key))).toBe(false); }); it("defines three sales pipelines with the validated stage and lost-reason baselines", () => { expect(SHK_V2_TENANT_BOOTSTRAP_CONFIG.salesPipelines.map(({ name, isDefault }) => ({ name, isDefault }))).toEqual([ { name: "Vertrieb Wärmepumpe", isDefault: true }, { name: "Vertrieb Photovoltaik", isDefault: false }, { name: "Vertrieb Badezimmer", isDefault: false }, ]); const expectedStages = [ { name: "Neu", stageKind: "offen", winProbability: 10, color: "#64748b", position: 10 }, { name: "Qualifiziert", stageKind: "offen", winProbability: 30, color: "#0ea5e9", position: 20 }, { name: "Angebot", stageKind: "offen", winProbability: 60, color: "#f59e0b", position: 30 }, { name: "Gewonnen", stageKind: "gewonnen", winProbability: 100, color: "#16a34a", position: 40 }, { name: "Verloren", stageKind: "verloren", winProbability: 0, color: "#dc2626", position: 50 }, ]; for (const pipeline of SHK_V2_TENANT_BOOTSTRAP_CONFIG.salesPipelines) { expect(pipeline.stages).toEqual(expectedStages); } expect(SHK_V2_TENANT_BOOTSTRAP_CONFIG.salesLostReasons).toEqual(["Preis", "Zeitpunkt", "Kein Bedarf", "Wettbewerber", "Nicht erreichbar"]); }); it("keeps all three automation placeholders unpublished and non-executable behind explicit gates", () => { expect(SHK_V2_TENANT_BOOTSTRAP_CONFIG.automationPlaceholders.map((placeholder) => placeholder.templateKey)).toEqual([ "shk.order-thank-you.v1", "shk.kwp-project-routing.v1", "shk.follow-up-reminder.v1", ]); for (const placeholder of SHK_V2_TENANT_BOOTSTRAP_CONFIG.automationPlaceholders) { expect(placeholder.isPublished).toBe(false); expect(placeholder.isExecutable).toBe(false); expect(placeholder.nodes).toEqual([]); expect(placeholder.activationGates.length).toBeGreaterThan(0); } }); it("produces a canonical SHA-256 digest independent of object key insertion order", () => { const digest = digestShkV2TenantBootstrapConfig(); expect(digest).toBe("6f37bd4b1f71174dcc677e6111161aa558d5d5a08fc9fb4e2a19136d4afdcdf9"); const reordered = { automationPlaceholders: SHK_V2_TENANT_BOOTSTRAP_CONFIG.automationPlaceholders, salesLostReasons: SHK_V2_TENANT_BOOTSTRAP_CONFIG.salesLostReasons, salesPipelines: SHK_V2_TENANT_BOOTSTRAP_CONFIG.salesPipelines, tenantRoles: SHK_V2_TENANT_BOOTSTRAP_CONFIG.tenantRoles, ownerRole: SHK_V2_TENANT_BOOTSTRAP_CONFIG.ownerRole, schemaVersion: SHK_V2_TENANT_BOOTSTRAP_CONFIG.schemaVersion, templateKey: SHK_V2_TENANT_BOOTSTRAP_CONFIG.templateKey, }; expect(digestShkV2TenantBootstrapConfig(reordered)).toBe(digest); }); });