Files
pi-auto-reinject/test/auto-compact.test.ts
grayhook bf862656ae Phase 6: add compaction coexistence hint — one-time notify when both compactors run.
Shows ui.notify once when pi-auto-compact is detected while Pi compaction.enabled stays true.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-17 11:47:27 +07:00

109 lines
3.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
detectAndCachePiAutoCompact,
detectPiAutoCompact,
maybeNotifyCompactionCoexistenceHint,
PI_AUTO_COMPACT_FOLLOW_UP_PREFIXES,
resolveDeliveryMode,
resolvePiDefaultCompactionEnabled,
} from "../src/auto-compact";
import { createDefaultSettings } from "../src/settings";
import { createRuntimeFlags } from "../src/state";
describe("detectPiAutoCompact", () => {
it("returns true when auto-compact command is registered", () => {
const pi = {
getCommands: () => [{ name: "auto-compact" }],
};
expect(detectPiAutoCompact(pi as never)).toBe(true);
});
it("returns false when auto-compact command is absent", () => {
const pi = {
getCommands: () => [{ name: "skill-reinject" }],
};
expect(detectPiAutoCompact(pi as never)).toBe(false);
});
});
describe("detectAndCachePiAutoCompact", () => {
it("writes detect result into runtime flags", () => {
const runtime = createRuntimeFlags();
const pi = {
getCommands: () => [{ name: "auto-compact" }],
};
expect(detectAndCachePiAutoCompact(pi as never, runtime)).toBe(true);
expect(runtime.autoCompactDetected).toBe(true);
});
});
describe("resolveDeliveryMode", () => {
it("defers when pi-auto-compact is cached as detected", () => {
const runtime = createRuntimeFlags();
runtime.autoCompactDetected = true;
expect(resolveDeliveryMode(createDefaultSettings(), runtime)).toBe("defer");
});
});
describe("PI_AUTO_COMPACT_FOLLOW_UP_PREFIXES", () => {
it("documents pi-auto-compact follow-up phrases from SPEC §16.9", () => {
expect([...PI_AUTO_COMPACT_FOLLOW_UP_PREFIXES]).toEqual([
"Auto-compact ran before this turn.",
"Auto-compact ran mid-turn.",
"Emergency auto-compact ran.",
"Auto-compact ran on session resume.",
]);
});
});
describe("resolvePiDefaultCompactionEnabled", () => {
it("defaults to enabled when compaction settings are absent", () => {
expect(resolvePiDefaultCompactionEnabled({}, {})).toBe(true);
});
it("lets project override global compaction.enabled", () => {
expect(
resolvePiDefaultCompactionEnabled(
{ compaction: { enabled: true } },
{ compaction: { enabled: false } },
),
).toBe(false);
});
});
describe("maybeNotifyCompactionCoexistenceHint", () => {
it("notifies once when pi-auto-compact and Pi compaction are both active", () => {
const runtime = createRuntimeFlags();
runtime.autoCompactDetected = true;
const notifications: string[] = [];
const ctx = {
hasUI: true,
ui: {
notify: (message: string) => {
notifications.push(message);
},
},
};
maybeNotifyCompactionCoexistenceHint(ctx as never, runtime, true);
maybeNotifyCompactionCoexistenceHint(ctx as never, runtime, true);
expect(notifications).toHaveLength(1);
expect(runtime.compactionCoexistenceHintShown).toBe(true);
});
it("skips notify when pi-auto-compact is not detected", () => {
const runtime = createRuntimeFlags();
const notifications: string[] = [];
const ctx = {
hasUI: true,
ui: { notify: (message: string) => notifications.push(message) },
};
maybeNotifyCompactionCoexistenceHint(ctx as never, runtime, true);
expect(notifications).toHaveLength(0);
expect(runtime.compactionCoexistenceHintShown).toBe(false);
});
});