import { describe, expect, test } from "vitest"; import ctxExtension, { __test } from "../index.ts"; const { compactRead, handleToolResult } = __test; describe("ctx read compactor", () => { test("registers no tools", () => { const tools: string[] = []; const handlers: string[] = []; ctxExtension({ registerTool(tool: { name: string }) { tools.push(tool.name); }, on(name: string) { handlers.push(name); }, }); expect(tools).toEqual([]); expect(handlers).toEqual(["tool_result"]); }); test("keeps head and tail only", () => { const compacted = compactRead( Array.from({ length: 300 }, (_, i) => `line ${i + 1}`).join("\n"), ); expect(compacted).toContain("line 1"); expect(compacted).toContain("line 300"); expect(compacted).toContain("[ctx compacted read:"); expect(compacted).not.toContain("line 100"); }); test("skips explicit read window", () => { const text = "x".repeat(25_000); expect( handleToolResult({ toolName: "read", input: { path: "x", offset: 1, limit: 10 }, content: [{ type: "text", text }], }), ).toBeUndefined(); }); test("does not touch bash/grep/find/ls", () => { const text = "x".repeat(25_000); for (const toolName of ["bash", "grep", "find", "ls"]) { expect( handleToolResult({ toolName, content: [{ type: "text", text }] }), ).toBeUndefined(); } }); test("compacts large read results", () => { const text = Array.from({ length: 400 }, (_, i) => `line ${i + 1}`).join("\n") + "\n" + "x".repeat(25_000); const patch = handleToolResult({ toolName: "read", input: { path: "x" }, content: [{ type: "text", text }], }); expect(patch?.content[0].text).toContain("[ctx compacted read:"); expect(patch?.details?.ctxCompactedRead).toBe(true); }); });