import { describe, expect, it } from "vitest"; import type { GovernanceConfig } from "./governance.js"; import { evaluateGovernanceIntegrity, hasEffectiveComponentVocabulary, isEffectiveCanonicalSource, type GovernanceIntegrityInput, } from "./governance-integrity.js"; import { customerDefaultRuleStates } from "./rules/presets.js"; function evaluate( overrides: Partial ): ReturnType { return evaluateGovernanceIntegrity({ policy: undefined, policySource: "none", declared: false, ...overrides, }); } describe("isEffectiveCanonicalSource", () => { it("treats a directory source with no include as a wildcard (effective)", () => { expect(isEffectiveCanonicalSource({ kind: "directory", path: "ui" })).toBe(true); }); it("treats an npm/registry source with no include as NOT effective", () => { expect(isEffectiveCanonicalSource({ kind: "npm", specifier: "@acme/ui" })).toBe(false); }); it("treats any source with a non-empty include as effective", () => { expect( isEffectiveCanonicalSource({ kind: "npm", specifier: "@acme/ui", include: ["Button"] }) ).toBe(true); }); it("treats a source with an empty include as NOT effective", () => { expect(isEffectiveCanonicalSource({ kind: "directory", path: "ui", include: [] })).toBe(false); }); }); describe("hasEffectiveComponentVocabulary", () => { it("is false for undefined options", () => { expect(hasEffectiveComponentVocabulary(undefined)).toBe(false); }); it("is true when canonicalMappings is non-empty", () => { expect(hasEffectiveComponentVocabulary({ canonicalMappings: [{ name: "Button" }] })).toBe(true); }); it("is true when an effective canonical source is present", () => { expect( hasEffectiveComponentVocabulary({ canonicalSources: [{ kind: "directory", path: "ui" }], }) ).toBe(true); }); it("is false for a package source with no include", () => { expect( hasEffectiveComponentVocabulary({ canonicalSources: [{ kind: "npm", specifier: "@acme/ui" }], }) ).toBe(false); }); }); describe("evaluateGovernanceIntegrity", () => { it("1. no policy at all → inert, not fatal, not blocking-capable", () => { const verdict = evaluate({ policy: undefined, policySource: "none", declared: false }); expect(verdict.status).toBe("inert"); expect(verdict.fatalForCi).toBe(false); expect(verdict.blockingCapable).toBe(false); expect(verdict.armed).toEqual([]); }); it("2. customer-default shape (prefer-library enabled, NO vocabulary) → inert, NOT healthy", () => { const policy: GovernanceConfig = { rules: customerDefaultRuleStates() }; const verdict = evaluate({ policy, policySource: "preset", declared: true }); const components = verdict.families.find((f) => f.id === "components"); expect(components?.armed).toBe(false); expect(components?.reason).toContain("no effective canonical source"); expect(verdict.status).toBe("inert"); // declared + inert → CI-fatal expect(verdict.fatalForCi).toBe(true); }); it("3. directory canonical source (no include) → components armed, healthy, warn is not blocking", () => { const policy: GovernanceConfig = { canonicalSources: [{ kind: "directory", path: "ui" }], }; const verdict = evaluate({ policy, policySource: "config", declared: true }); expect(verdict.families.find((f) => f.id === "components")?.armed).toBe(true); expect(verdict.status).toBe("healthy"); expect(verdict.blockingCapable).toBe(false); expect(verdict.armed).toContain("components"); }); it("4a. directory source + prefer-library severity error → blocking-capable", () => { const policy: GovernanceConfig = { rules: { "components/prefer-library": { enabled: true, severity: "error" } }, canonicalSources: [{ kind: "directory", path: "ui" }], }; const verdict = evaluate({ policy, policySource: "config", declared: true }); expect(verdict.status).toBe("healthy"); expect(verdict.blockingCapable).toBe(true); expect(verdict.families.find((f) => f.id === "blocking-deny")?.rules).toContain( "components/prefer-library" ); }); it("4b. directory source + ci.failOnWarnings → blocking-capable even at warn", () => { const policy: GovernanceConfig = { canonicalSources: [{ kind: "directory", path: "ui" }], ci: { failOnWarnings: true }, }; const verdict = evaluate({ policy, policySource: "config", declared: true }); expect(verdict.status).toBe("healthy"); expect(verdict.blockingCapable).toBe(true); }); it("5. npm source with NO include → components NOT armed → inert (blocker-#2 lock)", () => { const policy: GovernanceConfig = { canonicalSources: [{ kind: "npm", specifier: "@acme/ui" }], }; const verdict = evaluate({ policy, policySource: "config", declared: true }); expect(verdict.families.find((f) => f.id === "components")?.armed).toBe(false); expect(verdict.status).toBe("inert"); expect(verdict.blockingCapable).toBe(false); }); it("6. npm source WITH include → components armed", () => { const policy: GovernanceConfig = { canonicalSources: [{ kind: "npm", specifier: "@acme/ui", include: ["Button"] }], }; const verdict = evaluate({ policy, policySource: "config", declared: true }); expect(verdict.families.find((f) => f.id === "components")?.armed).toBe(true); expect(verdict.status).toBe("healthy"); }); it("7. css-vars active + token vocabulary → tokens armed → healthy", () => { const policy: GovernanceConfig = { styles: [{ kind: "style.cssVars.mustBeDefined", severity: "warn" }], }; const verdict = evaluate({ policy, policySource: "config", declared: true, cssVarsActive: true, tokenVocabularySize: 5, }); expect(verdict.families.find((f) => f.id === "tokens")?.armed).toBe(true); expect(verdict.status).toBe("healthy"); expect(verdict.armed).toContain("tokens"); }); it("7b. css-vars active but empty token vocabulary → tokens NOT armed", () => { const policy: GovernanceConfig = { styles: [{ kind: "style.cssVars.mustBeDefined", severity: "warn" }], }; const verdict = evaluate({ policy, policySource: "config", declared: true, cssVarsActive: true, tokenVocabularySize: 0, }); const tokens = verdict.families.find((f) => f.id === "tokens"); expect(tokens?.armed).toBe(false); expect(tokens?.reason).toBe("no token vocabulary"); }); it("8. hygiene-only policy (raw-style rule enabled, no contract) → degraded", () => { const policy: GovernanceConfig = { rules: { "styles/no-raw-color": { enabled: true, severity: "warn" } }, }; const verdict = evaluate({ policy, policySource: "config", declared: true }); const hygiene = verdict.families.find((f) => f.id === "hygiene"); expect(hygiene?.armed).toBe(true); expect(hygiene?.rules).toContain("styles/no-raw-color"); expect(verdict.status).toBe("degraded"); // degraded is enforceable, so not CI-fatal expect(verdict.fatalForCi).toBe(false); }); });