import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; import { ensureCondenseDefaults } from "../extensions/condense-config.ts"; const EXPECTED_DEFAULTS = { enabled: true, pruneOn: "agent-message", batchingMode: "turn", autoBudgetThreshold: 0.735, budgetTurnDelta: 0.04, summarizerModel: "default", summarizerThinking: "default", summarizerIdleTimeoutMs: 90_000, summarizerMaxTimeoutMs: 300_000, quietOversizedSkips: false, }; async function withAgentDir(run: (agentDir: string) => Promise): Promise { const agentDir = await mkdtemp(join(tmpdir(), "my-pi-condense-")); try { await run(agentDir); } finally { await rm(agentDir, { recursive: true, force: true }); } } test("pi-condense writes the complete bundle defaults when contextPrune is absent", async () => { await withAgentDir(async (agentDir) => { assert.equal(ensureCondenseDefaults(agentDir), "updated"); assert.deepEqual(JSON.parse(await readFile(join(agentDir, "settings.json"), "utf8")), { contextPrune: EXPECTED_DEFAULTS, }); }); }); test("pi-condense fills only missing defaults and preserves unrelated settings", async () => { await withAgentDir(async (agentDir) => { const path = join(agentDir, "settings.json"); await writeFile( path, JSON.stringify({ theme: "dark", contextPrune: { minBatchChars: 4000, budgetTurnDelta: 0.03 } }), "utf8", ); assert.equal(ensureCondenseDefaults(agentDir), "updated"); assert.deepEqual(JSON.parse(await readFile(path, "utf8")), { theme: "dark", contextPrune: { ...EXPECTED_DEFAULTS, minBatchChars: 4000, budgetTurnDelta: 0.03 }, }); }); }); test("pi-condense preserves an explicit disabled choice while filling other defaults", async () => { await withAgentDir(async (agentDir) => { const path = join(agentDir, "settings.json"); await writeFile(path, JSON.stringify({ contextPrune: { enabled: false } }), "utf8"); assert.equal(ensureCondenseDefaults(agentDir), "updated"); assert.deepEqual(JSON.parse(await readFile(path, "utf8")), { contextPrune: { ...EXPECTED_DEFAULTS, enabled: false }, }); }); }); test("pi-condense does not rewrite a complete explicit configuration", async () => { await withAgentDir(async (agentDir) => { const path = join(agentDir, "settings.json"); const original = `${JSON.stringify( { contextPrune: { ...EXPECTED_DEFAULTS, enabled: false, quietOversizedSkips: true } }, null, 2, )}\n`; await writeFile(path, original, "utf8"); assert.equal(ensureCondenseDefaults(agentDir), "unchanged"); assert.equal(await readFile(path, "utf8"), original); }); }); test("pi-condense defaults do not overwrite malformed settings", async () => { await withAgentDir(async (agentDir) => { const path = join(agentDir, "settings.json"); await writeFile(path, "{not-json", "utf8"); assert.equal(ensureCondenseDefaults(agentDir), "skipped-invalid"); assert.equal(await readFile(path, "utf8"), "{not-json"); }); }); test("pi-condense defaults reject a non-object contextPrune value", async () => { await withAgentDir(async (agentDir) => { const path = join(agentDir, "settings.json"); const original = `${JSON.stringify({ contextPrune: false }, null, 2)}\n`; await writeFile(path, original, "utf8"); assert.equal(ensureCondenseDefaults(agentDir), "skipped-invalid"); assert.equal(await readFile(path, "utf8"), original); }); });