repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/forget/__tests__/unit.test.ts
RawFile outline
- { formatInspect, validateTarget, forgetSummary, preview, REMINDER } line 4
- msg line 7
- forget pure helpers line 17
- registers short before-agent reminder line 18
- inspect formats active branch ids without previews by default line 31
- inspect can show previews when explicitly requested line 42
- inspect limit omits older entries line 51
- target validation accepts active user branch target line 66
- target validation accepts numeric inspect row line 74
- target validation rejects stale user row line 81
- target validation rejects missing and toolResult targets line 92
- forget summary is explicit tombstone not hidden data line 103
- preview compacts whitespace and truncates line 112
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<string, any>();
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)}…`);
});
});