import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import { CODES, EXPLAIN_URL_BASE, bridgeSourceViolation, byCode, byRuleId, } from "../index.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const registryPath = path.resolve(__dirname, "../../../../../docs/codes/registry.json"); describe("FUI code registry", () => { it("does not contain duplicate codes", () => { expect(new Set(CODES.map((entry) => entry.code)).size).toBe(CODES.length); }); it("does not contain duplicate rule IDs", () => { expect(new Set(CODES.map((entry) => entry.ruleId)).size).toBe(CODES.length); }); it("builds lookup maps from the registry", () => { for (const entry of CODES) { expect(byCode.get(entry.code)).toBe(entry); expect(byRuleId.get(entry.ruleId)).toBe(entry); expect(entry.explainUrl).toBe(`${EXPLAIN_URL_BASE}${entry.code}`); } }); it("matches the committed docs registry snapshot", () => { const snapshot = JSON.parse(readFileSync(registryPath, "utf8")); expect(snapshot).toEqual(CODES); }); it("bridges source violations into canonical findings with code metadata", () => { const bridged = bridgeSourceViolation({ validator: "scss", violation: { nodeId: "scss:src/app.css:2:4", nodeType: "ScssDeclaration", rule: "tokens/require-dual-fallback", severity: "serious", message: "Token fallback is missing", filePath: "src/app.css", line: 2, column: 4, rawValue: "var(--fui-color-accent)", }, }); expect(bridged.finding).toMatchObject({ ruleId: "tokens/require-dual-fallback", code: "FUI2003", helpUrl: `${EXPLAIN_URL_BASE}FUI2003`, location: { file: "src/app.css", line: 2, column: 4 }, attributes: { source: "source-validator", validator: "scss", rawValue: "var(--fui-color-accent)", }, }); expect(bridged.violation).toMatchObject({ code: "FUI2003", helpUrl: `${EXPLAIN_URL_BASE}FUI2003`, level: "error", }); }); });