import { describe, it, expect } from "vitest"; import { describeModel, compactSessionContext, COMPACTION_FALLBACK_INSTRUCTIONS } from "./pi.js"; import type { AgentSession } from "@mariozechner/pi-coding-agent"; describe("describeModel", () => { it('returns "provider/modelId" when session has a model', () => { const fakeSession = { model: { provider: "anthropic", id: "claude-sonnet-4-5", name: "Claude Sonnet", }, } as unknown as AgentSession; expect(describeModel(fakeSession)).toBe("anthropic/claude-sonnet-4-5"); }); it('returns "unknown model" when session model is undefined', () => { const fakeSession = { model: undefined, } as unknown as AgentSession; expect(describeModel(fakeSession)).toBe("unknown model"); }); it("handles different providers", () => { const fakeSession = { model: { provider: "openai", id: "gpt-4o", name: "GPT-4o", }, } as unknown as AgentSession; expect(describeModel(fakeSession)).toBe("openai/gpt-4o"); }); }); describe("COMPACTION_FALLBACK_INSTRUCTIONS", () => { it("is a non-empty string", () => { expect(COMPACTION_FALLBACK_INSTRUCTIONS).toBeTruthy(); expect(typeof COMPACTION_FALLBACK_INSTRUCTIONS).toBe("string"); expect(COMPACTION_FALLBACK_INSTRUCTIONS.length).toBeGreaterThan(0); }); it("mentions summarizing completed steps", () => { expect(COMPACTION_FALLBACK_INSTRUCTIONS).toContain("completed steps"); }); }); describe("compactSessionContext", () => { it("returns null when session does not have compact method", async () => { const session = {} as AgentSession; const result = await compactSessionContext(session); expect(result).toBeNull(); }); it("calls session.compact with default instructions when no custom instructions provided", async () => { const compact = async (instructions: string) => ({ summary: "Compacted", tokensBefore: 100000, }); const session = { compact } as unknown as AgentSession; const result = await compactSessionContext(session); expect(result).toEqual({ summary: "Compacted", tokensBefore: 100000, }); }); it("calls session.compact with custom instructions when provided", async () => { let capturedInstructions: string | undefined; const compact = async (instructions: string) => { capturedInstructions = instructions; return { summary: "Custom", tokensBefore: 50000 }; }; const session = { compact } as unknown as AgentSession; const result = await compactSessionContext(session, "Focus on step 3"); expect(capturedInstructions).toBe("Focus on step 3"); expect(result).toEqual({ summary: "Custom", tokensBefore: 50000, }); }); it("returns null when session.compact throws", async () => { const compact = async () => { throw new Error("compaction failed"); }; const session = { compact } as unknown as AgentSession; const result = await compactSessionContext(session); expect(result).toBeNull(); }); it("returns null when session.compact returns null", async () => { const compact = async () => null; const session = { compact } as unknown as AgentSession; const result = await compactSessionContext(session); expect(result).toBeNull(); }); it("returns result with empty summary when session.compact returns object without summary", async () => { const compact = async () => ({}); const session = { compact } as unknown as AgentSession; const result = await compactSessionContext(session); // Should still return a result with empty summary since the guard checks for object expect(result).toEqual({ summary: "", tokensBefore: 0 }); }); });