import { describe, expect, it } from "vitest"; import { compareByVocabularyRank, compileGlobalGovernanceFacts, contractTierRuleIds, customerDefaultRuleStates, FactIndex, FRAGMENTS_INTERNAL_RULE_IDS, fragmentsPresetRuleStates, isContractTierRule, makeContractTokenFact, makeStyleCssVarsMustBeDefinedFact, makeStyleDeclarationFact, makeUsageNodeFact, makeUsageTextChildFact, RULE_TIER, RULES, runRules, tierFor, } from "../index.js"; import type { Fact } from "../index.js"; /** * Brief 02 — rule taxonomy. ACCEPTANCE §1–§3. * * The customer default preset enables only Tier A (contract vocabulary), which * is inert until a contract is authored; Tier B (generic hygiene) is off and the * Fragments-internal `tokens/require-dual-fallback` is excluded entirely. The * full `fragments` preset keeps every rule for Fragments' own dogfooding. */ // --- fixtures -------------------------------------------------------------- /** Raw-value declarations — the shape that floods the dashboard today. */ function rawValueFixture(): Fact[] { const file = "src/Widget.module.scss"; return [ makeStyleDeclarationFact({ file, selector: ".a", declarationPath: "0", property: "color", value: "#3b82f6", location: { file, line: 1, column: 2 }, }), makeStyleDeclarationFact({ file, selector: ".a", declarationPath: "1", property: "padding", value: "13px", location: { file, line: 2, column: 2 }, }), makeStyleDeclarationFact({ file, selector: ".a", declarationPath: "2", property: "background", // a CSS token var with no SCSS fallback — the dual-fallback shape value: "var(--fui-surface-secondary)", location: { file, line: 3, column: 2 }, }), ]; } function index(facts: Fact[]): FactIndex { const ix = new FactIndex(); ix.addMany(facts); return ix; } // --- taxonomy SSOT coverage ------------------------------------------------ describe("rule taxonomy SSOT", () => { it("classifies every rule in RULES, with no stray ids", () => { const ruleIds = RULES.map((rule) => rule.id).sort(); const tieredIds = Object.keys(RULE_TIER).sort(); expect(tieredIds).toEqual(ruleIds); }); it("treats exactly the two-choice-contract rules as Tier A", () => { expect(contractTierRuleIds().sort()).toEqual([ "components/prefer-library", "tokens/css-vars-must-be-defined", ]); }); it("fails safe to hygiene for unknown rule ids", () => { expect(tierFor("some/unknown-rule")).toBe("hygiene"); expect(isContractTierRule("some/unknown-rule")).toBe(false); }); }); // --- §1 — customer + no contract → zero findings --------------------------- describe("§1 customer default, no contract", () => { it("produces zero findings over a raw-value fixture (not the flood)", () => { const ix = index([ ...compileGlobalGovernanceFacts({ rules: customerDefaultRuleStates() }), ...rawValueFixture(), ]); const findings = runRules(ix); expect(findings).toHaveLength(0); }); it("contrast: the same fixture DOES flood under the full fragments preset", () => { // The customer default's 0 is the taxonomy doing its job, not an empty // fixture: the config-gated dual-fallback rule fires under `fragments` on the // exact same facts, and is precisely what the customer default drops. const fragments = index([ ...compileGlobalGovernanceFacts({ rules: fragmentsPresetRuleStates() }), ...rawValueFixture(), ]); expect(runRules(fragments).some((f) => f.ruleId === "tokens/require-dual-fallback")).toBe(true); }); }); // --- §2 — require-dual-fallback absent from the customer preset ------------ describe("§2 tokens/require-dual-fallback is Fragments-internal", () => { const file = "src/Card.module.scss"; const decl = makeStyleDeclarationFact({ file, selector: ".c", declarationPath: "0", property: "color", value: "var(--fui-surface-secondary)", location: { file, line: 1, column: 0 }, }); it("is excluded from the customer default entirely", () => { expect(FRAGMENTS_INTERNAL_RULE_IDS.has("tokens/require-dual-fallback")).toBe(true); expect(customerDefaultRuleStates()).not.toHaveProperty("tokens/require-dual-fallback"); }); it("customer preset = 0, fragments preset = 1", () => { const customer = index([ ...compileGlobalGovernanceFacts({ rules: customerDefaultRuleStates() }), decl, ]); expect(runRules(customer)).toHaveLength(0); const fragments = index([ ...compileGlobalGovernanceFacts({ rules: fragmentsPresetRuleStates() }), decl, ]); const fragmentsFindings = runRules(fragments); expect(fragmentsFindings).toHaveLength(1); expect(fragmentsFindings[0].ruleId).toBe("tokens/require-dual-fallback"); }); }); // --- §3 — contract authored → exactly the Tier-A drift, vocabulary-first --- describe("§3 contract authored", () => { // A contract: canonical Button declared (npm source) + a token source file // declaring --color-accent. An agent writes a raw