import * as fs from "node:fs"; import * as path from "node:path"; import * as os from "node:os"; import { PhaseResearcherAgent } from "../agents/phase-researcher.js"; describe("PhaseResearcherAgent", () => { let tempDir: string; beforeEach(() => { tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "ciagent-phase-researcher-test-")); fs.mkdirSync(path.join(tempDir, ".git"), { recursive: true }); }); afterEach(() => { fs.rmSync(tempDir, { recursive: true, force: true }); }); it("extracts decisions from commit log content", () => { const agent = new PhaseResearcherAgent(); const result = agent.extractDecisions( `some commit\n---ci---\nphase: 1\ndecisions:\n - D-101: Use SQLite for storage confidence: 0.9\n - D-102: Retry on failure confidence: 0.3\n---/ci---\n` ); expect(result.length).toBe(2); expect(result[0].id).toBe("D-101"); expect(result[0].confidence).toBe(0.9); expect(result[1].id).toBe("D-102"); expect(result[1].confidence).toBe(0.3); }); it("extracts lessons from commit log content", () => { const agent = new PhaseResearcherAgent(); const result = agent.extractLessons( `some commit\n---ci---\nphase: 1\nlessons:\n - testing: Flaky tests are a problem\n - build: CI timeouts\n---/ci---\n` ); expect(result.length).toBe(2); expect(result[0]).toContain("testing"); }); it("identifies risks from low-confidence decisions and repeated lessons", () => { const agent = new PhaseResearcherAgent(); const decisions = [ { id: "D-1", decision: "Risky choice", confidence: 0.4 }, { id: "D-2", decision: "Safe choice", confidence: 0.9 }, ]; const lessons = [ "testing: Flaky tests", "testing: More flaky tests", "build: CI timeouts", ]; const risks = agent.identifyRisks(decisions, lessons); const highRisk = risks.filter((r) => r.severity === "high"); expect(highRisk.length).toBeGreaterThan(0); expect(highRisk.some((r) => r.description.includes("D-1"))).toBe(true); }); it("handles empty git log gracefully", () => { const agent = new PhaseResearcherAgent(); const result = agent.mechanicalPhaseResearch(tempDir, 1); expect(result.phase).toBe(1); expect(result.decisions).toEqual([]); expect(result.lessons).toEqual([]); expect(result.risks).toEqual([]); }); it("agent name is phase-researcher", () => { const agent = new PhaseResearcherAgent(); expect(agent.name).toBe("phase-researcher"); }); });