/** * complexity.test.ts — integration tests for the complexity hygiene check. * * Validates: * 1. A function with cyclomatic complexity 50+ is flagged and refactored * 2. A function with complexity 35-49 is flagged with justification required * 3. Deep nesting and over-abstraction are simplified */ import { describe, expect, it, beforeAll, afterAll, beforeEach, afterEach, } from "bun:test"; import * as fs from "node:fs/promises"; import * as path from "node:path"; import { tmpdir } from "node:os"; import { complexityCheck, buildComplexityScanTask, buildComplexityFixTask, } from "../src/checks/complexity.js"; import { registerCheck, clearChecks } from "../src/checks/registry.js"; import { runAgentTask, setAgentRunner, resetAgentRunner, fakeAgentRunner, } from "../src/agent-runner.js"; describe("complexity check", () => { let tempDir: string; beforeEach(async () => { tempDir = await fs.mkdtemp(path.join(tmpdir(), "pygienium-test-")); }); afterEach(async () => { await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {}); }); it("is registered with correct properties", () => { expect(complexityCheck.name).toBe("complexity"); expect(complexityCheck.label).toBe("Complexity"); expect(complexityCheck.agentName).toBe("scanner"); expect(complexityCheck.fixAgentName).toBe("fixer"); }); it("builds scan task with complexity thresholds", () => { const scope = { cwd: tempDir, target: tempDir, fix: false, rest: [], }; const task = buildComplexityScanTask(tempDir, scope); expect(task).toContain("cyclomatic complexity"); expect(task).toContain("50+"); expect(task).toContain("35-49"); expect(task).toContain("MUST refactor"); expect(task).toContain("findings.md"); }); it("builds fix task from findings", () => { const scope = { cwd: tempDir, target: tempDir, fix: true, rest: [], }; const findings = "# complexity findings\n\n- myFunction: complexity 65 - must refactor"; const task = buildComplexityFixTask(tempDir, scope, findings); expect(task).toContain("complexity fix"); expect(task).toContain("myFunction"); expect(task).toContain("changes.md"); }); }); describe("agent loading", () => { it("loads scanner agent from agents directory", async () => { const { loadAgents } = await import("../src/agents.js"); const agents = await loadAgents(); expect(agents.has("scanner")).toBe(true); const scanner = agents.get("scanner"); expect(scanner).toBeDefined(); expect(scanner?.systemPrompt).toContain("scanner"); }); it("loads fixer agent from agents directory", async () => { const { loadAgents } = await import("../src/agents.js"); const agents = await loadAgents(); expect(agents.has("fixer")).toBe(true); const fixer = agents.get("fixer"); expect(fixer).toBeDefined(); expect(fixer?.allowedTools).toContain("edit"); }); });