Files
CapaKraken/scripts/check-architecture-guardrails.test.mjs
T

70 lines
2.4 KiB
JavaScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
collectArchitectureGuardrailViolations,
countLines,
evaluateRule,
} 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",
]);
});
});