import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { collectArchitectureGuardrailViolations, countLines, evaluateRule, rules, } from "./check-architecture-guardrails.mjs"; describe("architecture guardrails", () => { it("counts lines consistently for maxLines checks", () => { assert.equal(countLines("a\nb\nc"), 3); }); it("reports required, forbidden, and maxLines violations together", () => { const violations = evaluateRule( { file: "apps/web/src/example.ts", maxLines: 2, required: [{ pattern: /\bexpectedHelper\b/, message: "must call the extracted helper" }], forbidden: [{ pattern: /\binlineLogic\b/, message: "must not re-inline complex logic" }], }, "inlineLogic();\nconst a = 1;\nconst b = 2;\n", ); assert.deepEqual(violations, [ "apps/web/src/example.ts: missing guardrail anchor: must call the extracted helper", "apps/web/src/example.ts: forbidden pattern matched: must not re-inline complex logic", "apps/web/src/example.ts: file grew to 4 lines and exceeds maxLines=2; split the ownership surface before expanding it further", ]); }); it("returns no violations when a rule is satisfied", () => { const violations = evaluateRule( { file: "packages/api/src/example.ts", maxLines: 4, required: [{ pattern: /\bderiveThing\b/, message: "must keep derivation centralized" }], forbidden: [{ pattern: /\bunsafeThing\b/, message: "must not use unsafe helper" }], }, "export function deriveThing() {\n return true;\n}\n", ); assert.deepEqual(violations, []); }); it("reads sources through the injected reader when collecting violations", async () => { const violations = await collectArchitectureGuardrailViolations( [ { file: "apps/web/src/example.ts", maxLines: 2, required: [], forbidden: [], }, ], { workspaceRoot: "/virtual/repo", readSource: async (filePath) => { assert.equal(filePath, "/virtual/repo/apps/web/src/example.ts"); return "line1\nline2\nline3\n"; }, }, ); assert.deepEqual(violations, [ "apps/web/src/example.ts: file grew to 4 lines and exceeds maxLines=2; split the ownership surface before expanding it further", ]); }); it("guards the extracted timeline live preview ownership boundary", () => { const dragRule = rules.find((rule) => rule.file === "apps/web/src/hooks/useTimelineDrag.ts"); const livePreviewRule = rules.find((rule) => rule.file === "apps/web/src/hooks/timelineLivePreview.ts"); const touchRule = rules.find((rule) => rule.file === "apps/web/src/hooks/timelineTouch.ts"); const multiSelectRule = rules.find((rule) => rule.file === "apps/web/src/hooks/timelineMultiSelect.ts"); const rangeRule = rules.find((rule) => rule.file === "apps/web/src/hooks/timelineRangeSelection.ts"); assert.ok(dragRule); assert.ok(livePreviewRule); assert.ok(touchRule); assert.ok(multiSelectRule); assert.ok(rangeRule); assert.deepEqual(evaluateRule(dragRule, "function clearLivePreview() {}\n"), [ "apps/web/src/hooks/useTimelineDrag.ts: missing guardrail anchor: timeline drag must keep live preview behavior delegated to the extracted helper module", "apps/web/src/hooks/useTimelineDrag.ts: missing guardrail anchor: timeline drag must keep touch fallback and drag disambiguation delegated to the extracted helper module", "apps/web/src/hooks/useTimelineDrag.ts: missing guardrail anchor: timeline drag must keep multi-select rectangle lifecycle delegated to the extracted helper module", "apps/web/src/hooks/useTimelineDrag.ts: missing guardrail anchor: timeline drag must keep range preview and finalization delegated to the extracted helper module", "apps/web/src/hooks/useTimelineDrag.ts: forbidden pattern matched: timeline drag must not re-inline live preview helper implementations", ]); assert.deepEqual(evaluateRule(livePreviewRule, "export function scheduleLivePreview() {}\n"), [ "apps/web/src/hooks/timelineLivePreview.ts: missing guardrail anchor: timeline live preview helpers must keep preview reset logic centralized", "apps/web/src/hooks/timelineLivePreview.ts: missing guardrail anchor: timeline live preview helpers must keep snapshot preservation centralized", ]); assert.deepEqual(evaluateRule(touchRule, "export function getTouchPoint() {}\n"), [ "apps/web/src/hooks/timelineTouch.ts: missing guardrail anchor: timeline touch helpers must keep scroll-vs-drag disambiguation centralized", ]); assert.deepEqual(evaluateRule(multiSelectRule, "export function createMultiSelectState() {}\n"), [ "apps/web/src/hooks/timelineMultiSelect.ts: missing guardrail anchor: timeline multi-select helpers must keep minimal-drag reset logic centralized", ]); assert.deepEqual(evaluateRule(rangeRule, "export function updateRangeSelectionDraft() {}\n"), [ "apps/web/src/hooks/timelineRangeSelection.ts: missing guardrail anchor: timeline range helpers must keep ordered range finalization centralized", ]); }); });