syntaxai/tdd.md · main · src / b32_sama_v2_verify.test.ts
import { describe, test, expect } from "bun:test";
import { verifySamaV2 } from "./b32_sama_v2_verify.ts";
import type { ProfileSpec, SamaV2Input } from "./a31_sama_v2.ts";
// Minimal fixture profile mirroring the shape this repo's
// sama.profile.toml declares, but with synthetic prefixes so tests
// don't change when the live profile evolves.
const FIXTURE_PROFILE: ProfileSpec = {
samaVersion: "2.0",
profile: "test-fixture",
layers: {
0: { sublayers: [{ name: "default", prefix: "p0_", index: 0 }] },
1: {
sublayers: [
{ name: "logic", prefix: "p1a_", index: 0 },
{ name: "render", prefix: "p1b_", index: 1 },
],
},
2: {
sublayers: [
{ name: "data", prefix: "p2a_", index: 0 },
{ name: "io", prefix: "p2b_", index: 1 },
],
},
3: {
sublayers: [
{ name: "handlers", prefix: "p3a_", index: 0 },
{ name: "server", prefix: "p3b_", index: 1 },
],
},
},
};
const mk = (entries: Array<[string, string]>): SamaV2Input => ({
profile: FIXTURE_PROFILE,
files: new Map(entries),
});
describe("c32_sama_v2_verify — overall", () => {
test("empty repo: every check passes with examined=0 for content-bearing checks", () => {
const report = verifySamaV2(mk([]));
expect(report.overallPassed).toBe(true);
expect(report.checks).toHaveLength(7);
for (const c of report.checks) expect(c.passed).toBe(true);
});
test("a minimal Layer-0-only repo conforms", () => {
const report = verifySamaV2(mk([
["src/p0_types.ts", "export const x = 1;\n"],
]));
expect(report.overallPassed).toBe(true);
});
});
describe("c32_sama_v2_verify — Sorted (#1)", () => {
test("a file without a profile-recognised prefix is flagged", () => {
const report = verifySamaV2(mk([
["src/unknown_x.ts", "export const x = 1;\n"],
]));
const sorted = report.checks.find((c) => c.id === 1)!;
expect(sorted.passed).toBe(false);
expect(sorted.violations.some((v) => v.file === "src/unknown_x.ts")).toBe(true);
});
test("a profile whose prefixes lex-sort against layer order is flagged", () => {
// Swap: Layer 0 prefix sorts AFTER Layer 1 prefix.
const bad: ProfileSpec = {
samaVersion: "2.0", profile: "bad",
layers: {
0: { sublayers: [{ name: "default", prefix: "z0_", index: 0 }] },
1: { sublayers: [{ name: "default", prefix: "a1_", index: 0 }] },
2: { sublayers: [{ name: "default", prefix: "b2_", index: 0 }] },
3: { sublayers: [{ name: "default", prefix: "c3_", index: 0 }] },
},
};
const report = verifySamaV2({ profile: bad, files: new Map() });
const sorted = report.checks.find((c) => c.id === 1)!;
expect(sorted.passed).toBe(false);
expect(sorted.violations.length).toBeGreaterThan(0);
});
});
describe("c32_sama_v2_verify — Architecture (#2)", () => {
test("an unprefixed src/*.ts file is flagged with a clear reason", () => {
const report = verifySamaV2(mk([
["src/random.ts", "export const x = 1;\n"],
]));
const arch = report.checks.find((c) => c.id === 2)!;
expect(arch.passed).toBe(false);
const vio = arch.violations.find((v) => v.file === "src/random.ts")!;
expect(vio.detail).toContain("unprefixed");
});
test("a properly-prefixed file is not flagged", () => {
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "export const x = 1;\n"],
]));
expect(report.checks.find((c) => c.id === 2)!.passed).toBe(true);
});
});
describe("c32_sama_v2_verify — Modeled tests (#3)", () => {
test("a Layer 1 file without a sibling test is flagged", () => {
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "export const x = 1;\n"],
]));
const modeled = report.checks.find((c) => c.id === 3)!;
expect(modeled.passed).toBe(false);
const vio = modeled.violations[0]!;
expect(vio.file).toBe("src/p1a_logic.ts");
expect(vio.detail).toContain("p1a_logic.test.ts");
});
test("a Layer 1 file with its sibling passes", () => {
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "export const x = 1;\n"],
["src/p1a_logic.test.ts", "import {expect, test} from \"bun:test\"; test(\"x\", () => { expect(1).toBe(1); });\n"],
]));
expect(report.checks.find((c) => c.id === 3)!.passed).toBe(true);
});
test("Layer 0 files don't require sibling tests", () => {
const report = verifySamaV2(mk([
["src/p0_types.ts", "export const x = 1;\n"],
]));
expect(report.checks.find((c) => c.id === 3)!.passed).toBe(true);
});
});
describe("c32_sama_v2_verify — Modeled boundary (#4)", () => {
test("JSON.parse in Layer 1 is flagged", () => {
const report = verifySamaV2(mk([
["src/p1a_naughty.ts", "export const f = (s: string) => JSON.parse(s);\n"],
]));
const boundary = report.checks.find((c) => c.id === 4)!;
expect(boundary.passed).toBe(false);
expect(boundary.violations[0]!.detail).toContain("JSON.parse");
});
test("JSON.parse in Layer 2 is OK (Layer 2 IS the boundary)", () => {
const report = verifySamaV2(mk([
["src/p2b_adapter.ts", "export const f = (s: string) => JSON.parse(s);\n"],
]));
expect(report.checks.find((c) => c.id === 4)!.passed).toBe(true);
});
test("string literals containing JSON.parse don't false-positive", () => {
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "const explainer = \"to fix, call JSON.parse(input) in Layer 2\";\nexport const x = explainer.length;\n"],
]));
expect(report.checks.find((c) => c.id === 4)!.passed).toBe(true);
});
});
describe("c32_sama_v2_verify — Atomic (#5)", () => {
test("a file over the 700-line cap is flagged", () => {
const fat = Array.from({ length: 720 }, (_, i) => `// line ${i}`).join("\n");
const report = verifySamaV2(mk([
["src/p1a_fat.ts", fat],
]));
const atomic = report.checks.find((c) => c.id === 5)!;
expect(atomic.passed).toBe(false);
expect(atomic.violations[0]!.detail).toContain("over the 700-line cap");
});
test("a barrel re-export file is flagged", () => {
const report = verifySamaV2(mk([
["src/p1a_barrel.ts", "export * from \"./p1a_a.ts\";\nexport * from \"./p1a_b.ts\";\n"],
]));
const atomic = report.checks.find((c) => c.id === 5)!;
expect(atomic.passed).toBe(false);
expect(atomic.violations[0]!.detail).toContain("barrel");
});
});
describe("c32_sama_v2_verify — Law §1.2 (#6)", () => {
test("upward import (Layer 1 → Layer 2) is flagged", () => {
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "import { x } from \"./p2a_data.ts\";\nexport const y = x;\n"],
["src/p1a_logic.test.ts", "import { test, expect } from \"bun:test\"; test(\"y\", () => { expect(1).toBe(1); });\n"],
["src/p2a_data.ts", "export const x = 1;\n"],
["src/p2a_data.test.ts","import { test, expect } from \"bun:test\"; test(\"x\", () => { expect(1).toBe(1); });\n"],
]));
const law = report.checks.find((c) => c.id === 6)!;
expect(law.passed).toBe(false);
expect(law.violations.some((v) => v.detail.includes("upward"))).toBe(true);
});
test("downward import (Layer 2 → Layer 0) passes", () => {
const report = verifySamaV2(mk([
["src/p2a_data.ts", "import type { X } from \"./p0_types.ts\";\nexport const f = (): X => ({} as X);\n"],
["src/p2a_data.test.ts", "import { test, expect } from \"bun:test\"; test(\"f\", () => { expect(1).toBe(1); });\n"],
["src/p0_types.ts", "export interface X { id: number }\n"],
]));
expect(report.checks.find((c) => c.id === 6)!.passed).toBe(true);
});
test("same-layer reversed sublayer is flagged", () => {
// p1a_logic is sublayer index 0 (logic), p1b_render is sublayer
// index 1 (render). Logic importing render is reverse order.
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "import { r } from \"./p1b_render.ts\";\nexport const y = r;\n"],
["src/p1a_logic.test.ts", "import { test, expect } from \"bun:test\"; test(\"y\", () => { expect(1).toBe(1); });\n"],
["src/p1b_render.ts", "export const r = 1;\n"],
["src/p1b_render.test.ts","import { test, expect } from \"bun:test\"; test(\"r\", () => { expect(1).toBe(1); });\n"],
]));
const law = report.checks.find((c) => c.id === 6)!;
expect(law.passed).toBe(false);
expect(law.violations.some((v) => v.detail.includes("sublayer"))).toBe(true);
});
test("an import cycle is flagged", () => {
const report = verifySamaV2(mk([
["src/p1a_a.ts", "import { y } from \"./p1a_b.ts\";\nexport const x = y;\n"],
["src/p1a_a.test.ts", "import { test, expect } from \"bun:test\"; test(\"x\", () => { expect(1).toBe(1); });\n"],
["src/p1a_b.ts", "import { x } from \"./p1a_a.ts\";\nexport const y = x;\n"],
["src/p1a_b.test.ts", "import { test, expect } from \"bun:test\"; test(\"y\", () => { expect(1).toBe(1); });\n"],
]));
const law = report.checks.find((c) => c.id === 6)!;
expect(law.passed).toBe(false);
expect(law.violations.some((v) => v.detail.includes("cycle"))).toBe(true);
});
});
describe("c32_sama_v2_verify — Consistency §3 (#7)", () => {
test("Layer 1 file reaching Layer 2 contradicts its declared prefix", () => {
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "import { f } from \"./p2a_data.ts\";\nexport const y = f;\n"],
["src/p1a_logic.test.ts", "import { test, expect } from \"bun:test\"; test(\"y\", () => { expect(1).toBe(1); });\n"],
["src/p2a_data.ts", "export const f = 1;\n"],
["src/p2a_data.test.ts", "import { test, expect } from \"bun:test\"; test(\"f\", () => { expect(1).toBe(1); });\n"],
]));
const consistency = report.checks.find((c) => c.id === 7)!;
expect(consistency.passed).toBe(false);
expect(consistency.violations[0]!.detail).toContain("declared Layer 1");
expect(consistency.violations[0]!.detail).toContain("Layer 2");
});
test("downward-only imports are consistent", () => {
const report = verifySamaV2(mk([
["src/p1a_logic.ts", "import type { X } from \"./p0_types.ts\";\nexport const y = (a: X) => a;\n"],
["src/p1a_logic.test.ts", "import { test, expect } from \"bun:test\"; test(\"y\", () => { expect(1).toBe(1); });\n"],
["src/p0_types.ts", "export interface X { id: number }\n"],
]));
expect(report.checks.find((c) => c.id === 7)!.passed).toBe(true);
});
});