import { describe, expect, it, vi } from "vitest"; import type { ExtensionAPI, ExtensionContext } from "@selesai/code"; import autoCompactExtension from "./auto-compact.ts"; function harness(tokens: number | undefined, compaction: Record | undefined) { let handler!: (e: unknown, ctx: ExtensionContext) => void; const compact = vi.fn(); const pi = { on: (_: string, h: typeof handler) => void (handler = h), getSettings: () => ({ compaction }), } as unknown as ExtensionAPI; const ctx = { getContextUsage: () => (tokens === undefined ? undefined : { tokens }), compact, ui: { notify: vi.fn() }, } as unknown as ExtensionContext; autoCompactExtension(pi); return { compact, settle: () => handler({}, ctx) }; } describe("auto-compact", () => { it("compacts only above thresholdTokens, once while in flight", () => { const t = { thresholdTokens: 196608 }; const below = harness(196608, t); below.settle(); expect(below.compact).not.toHaveBeenCalled(); const above = harness(196609, t); above.settle(); above.settle(); expect(above.compact).toHaveBeenCalledTimes(1); above.compact.mock.calls[0][0].onComplete(); above.settle(); expect(above.compact).toHaveBeenCalledTimes(2); }); it("is a no-op without threshold, usage, or when compaction disabled", () => { for (const h of [harness(999999, undefined), harness(undefined, { thresholdTokens: 1 }), harness(999999, { thresholdTokens: 1, enabled: false })]) { h.settle(); expect(h.compact).not.toHaveBeenCalled(); } }); });