import { assert, assertEquals, assertStringIncludes } from "@std/assert"; import { testExtension } from "@berry/api/testing.ts"; import misc from "./mod.ts"; // the cap is server-wide module state, so each test sets what it needs first, // except this one, which runs on the fresh import and pins the default Deno.test("the context cap is off by default", () => { assertEquals(testExtension(misc).compactionCap, undefined); }); Deno.test("the threshold and background offset are tunable", async () => { const harness = testExtension(misc); const report = await harness.command("context-cap", "131072 16384"); assertEquals(harness.compactionCap, { maxContextTokens: 131_072, backgroundTokens: 16_384 }); assertStringIncludes(report as string, "131072"); assertStringIncludes(report as string, "114688"); }); Deno.test("counts accept k and m suffixes, so 2^18 reads 256k", async () => { const harness = testExtension(misc); await harness.command("context-cap", "256k 32k"); assertEquals( harness.compactionCap, { maxContextTokens: 262_144, backgroundTokens: 32_768 }, ); await harness.command("context-cap", "1m"); assertEquals(harness.compactionCap?.maxContextTokens, 1_048_576); }); Deno.test("counts it cannot parse leave the cap alone", async () => { const harness = testExtension(misc); await harness.command("context-cap", "100000 1000"); const before = harness.compactionCap; let failed = 0; for (const args of ["soon", "-5", "0", "100000 soon", "1 2 3", "1000 2000"]) { try { await harness.command("context-cap", args); } catch { failed++; } } assertEquals(failed, 6); assertEquals(harness.compactionCap, before); }); Deno.test("the cap switches off and back on", async () => { const harness = testExtension(misc); await harness.command("context-cap", "100000 1000"); assert(await harness.command("context-cap", "off")); assertEquals(harness.compactionCap, undefined); const report = await harness.command("context-cap", "on"); assertEquals(harness.compactionCap, { maxContextTokens: 100_000, backgroundTokens: 1_000 }); assertStringIncludes(report as string, "100000"); await harness.command("context-cap", "256k 32k"); });