import { describe, expect, test } from "bun:test"; import forgetExtension, { __test } from "../index.ts"; const { formatInspect, validateTarget, forgetSummary, preview, REMINDER } = __test; function msg(id: string, role: string, text: string) { return { type: "message", id, parentId: null, timestamp: "", message: { role, content: text }, }; } describe("forget pure helpers", () => { test("registers short before-agent reminder", () => { const handlers = new Map(); forgetExtension({ on: (name: string, handler: unknown) => handlers.set(name, handler), registerTool: () => undefined, } as never); const handler = handlers.get("before_agent_start"); const result = handler({ systemPrompt: "base" }); expect(result.systemPrompt).toContain(REMINDER); expect(REMINDER.length).toBeLessThan(100); }); test("inspect formats active branch ids without previews by default", () => { const text = formatInspect([ msg("u1", "user", "remember alpha secret"), msg("a1", "assistant", "ok"), ]); expect(text).toContain("Forget inspect: 2 active branch entries"); expect(text).toContain("entry_id=u1 kind=user"); expect(text).not.toContain("remember alpha secret"); expect(text).toContain("entry_id=a1 kind=assistant"); }); test("inspect can show previews when explicitly requested", () => { const text = formatInspect( [msg("u1", "user", "remember alpha secret")], 80, true, ); expect(text).toContain("remember alpha secret"); }); test("inspect limit omits older entries", () => { const text = formatInspect( [ msg("u1", "user", "one"), msg("u2", "user", "two"), msg("u3", "user", "three"), ], 2, ); expect(text).toContain("1 older omitted"); expect(text).not.toContain("u1"); expect(text).toContain("u2"); expect(text).toContain("u3"); }); test("target validation accepts active user branch target", () => { const branch = [msg("u1", "user", "one"), msg("a1", "assistant", "two")]; const result = validateTarget(branch, "u1"); expect(result.ok).toBe(true); expect(result.before).toBe(0); expect(result.after).toBe(1); }); test("target validation accepts numeric inspect row", () => { const branch = [msg("u1", "user", "one"), msg("a1", "assistant", "two")]; const result = validateTarget(branch, "000"); expect(result.ok).toBe(true); expect(result.entry?.id).toBe("u1"); }); test("target validation rejects stale user row", () => { const branch = [ msg("u1", "user", "old"), msg("a1", "assistant", "answer"), msg("u2", "user", "new"), ]; const result = validateTarget(branch, "u1"); expect(result.ok).toBe(false); expect(result.error).toContain("current_user=yes"); }); test("target validation rejects missing and toolResult targets", () => { const branch = [ msg("u1", "user", "one"), msg("t1", "toolResult", "tool output"), ]; expect(validateTarget(branch, "missing").ok).toBe(false); const tool = validateTarget(branch, "t1"); expect(tool.ok).toBe(false); expect(tool.error).toContain("toolResult"); }); test("forget summary is explicit tombstone not hidden data", () => { const branch = [msg("u1", "user", "secret")]; const validation = validateTarget(branch, "u1"); const summary = forgetSummary(validation); expect(summary).toContain("intentionally removed"); expect(summary).toContain("u1"); expect(summary).not.toContain("secret"); }); test("preview compacts whitespace and truncates", () => { expect(preview("a\n\t b c", 20)).toBe("a b c"); expect(preview("x".repeat(200), 10)).toBe(`${"x".repeat(9)}…`); }); });