test(agents): dedupe agent and cron test scaffolds

This commit is contained in:
Peter Steinberger
2026-03-02 06:40:42 +00:00
parent 281494ae52
commit 7e29d604ba
38 changed files with 3114 additions and 4486 deletions

View File

@@ -1,89 +1,28 @@
import type { AgentMessage } from "@mariozechner/pi-agent-core";
import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
import * as piCodingAgent from "@mariozechner/pi-coding-agent";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { buildCompactionSummarizationInstructions, summarizeInStages } from "./compaction.js";
vi.mock("@mariozechner/pi-coding-agent", async (importOriginal) => {
const actual = await importOriginal<typeof piCodingAgent>();
return {
...actual,
generateSummary: vi.fn(),
};
});
const mockGenerateSummary = vi.mocked(piCodingAgent.generateSummary);
function makeMessage(index: number, size = 1200): AgentMessage {
return {
role: "user",
content: `m${index}-${"x".repeat(size)}`,
timestamp: index,
};
}
import { describe, expect, it } from "vitest";
import { buildCompactionSummarizationInstructions } from "./compaction.js";
describe("compaction identifier policy", () => {
const testModel = {
provider: "anthropic",
model: "claude-3-opus",
contextWindow: 200_000,
} as unknown as NonNullable<ExtensionContext["model"]>;
beforeEach(() => {
mockGenerateSummary.mockReset();
mockGenerateSummary.mockResolvedValue("summary");
it("defaults to strict identifier preservation", () => {
const built = buildCompactionSummarizationInstructions();
expect(built).toContain("Preserve all opaque identifiers exactly as written");
expect(built).toContain("UUIDs");
});
it("defaults to strict identifier preservation", async () => {
await summarizeInStages({
messages: [makeMessage(1), makeMessage(2)],
model: testModel,
apiKey: "test-key",
signal: new AbortController().signal,
reserveTokens: 4000,
maxChunkTokens: 8000,
contextWindow: 200_000,
it("can disable identifier preservation with off policy", () => {
const built = buildCompactionSummarizationInstructions(undefined, {
identifierPolicy: "off",
});
const firstCall = mockGenerateSummary.mock.calls[0];
expect(firstCall?.[5]).toContain("Preserve all opaque identifiers exactly as written");
expect(firstCall?.[5]).toContain("UUIDs");
expect(built).toBeUndefined();
});
it("can disable identifier preservation with off policy", async () => {
await summarizeInStages({
messages: [makeMessage(1), makeMessage(2)],
model: testModel,
apiKey: "test-key",
signal: new AbortController().signal,
reserveTokens: 4000,
maxChunkTokens: 8000,
contextWindow: 200_000,
summarizationInstructions: { identifierPolicy: "off" },
it("supports custom identifier instructions", () => {
const built = buildCompactionSummarizationInstructions(undefined, {
identifierPolicy: "custom",
identifierInstructions: "Keep ticket IDs unchanged.",
});
const firstCall = mockGenerateSummary.mock.calls[0];
expect(firstCall?.[5]).toBeUndefined();
});
it("supports custom identifier instructions", async () => {
await summarizeInStages({
messages: [makeMessage(1), makeMessage(2)],
model: testModel,
apiKey: "test-key",
signal: new AbortController().signal,
reserveTokens: 4000,
maxChunkTokens: 8000,
contextWindow: 200_000,
summarizationInstructions: {
identifierPolicy: "custom",
identifierInstructions: "Keep ticket IDs unchanged.",
},
});
const firstCall = mockGenerateSummary.mock.calls[0];
expect(firstCall?.[5]).toContain("Keep ticket IDs unchanged.");
expect(firstCall?.[5]).not.toContain("Preserve all opaque identifiers exactly as written");
expect(built).toContain("Keep ticket IDs unchanged.");
expect(built).not.toContain("Preserve all opaque identifiers exactly as written");
});
it("falls back to strict text when custom policy is missing instructions", () => {
@@ -94,24 +33,10 @@ describe("compaction identifier policy", () => {
expect(built).toContain("Preserve all opaque identifiers exactly as written");
});
it("avoids duplicate additional-focus headers in split+merge path", async () => {
await summarizeInStages({
messages: [makeMessage(1), makeMessage(2), makeMessage(3), makeMessage(4)],
model: testModel,
apiKey: "test-key",
signal: new AbortController().signal,
reserveTokens: 4000,
maxChunkTokens: 1000,
contextWindow: 200_000,
parts: 2,
minMessagesForSplit: 4,
customInstructions: "Prioritize customer-visible regressions.",
it("keeps custom focus text when identifier policy is off", () => {
const built = buildCompactionSummarizationInstructions("Track release blockers.", {
identifierPolicy: "off",
});
const mergedCall = mockGenerateSummary.mock.calls.at(-1);
const instructions = mergedCall?.[5] ?? "";
expect(instructions).toContain("Merge these partial summaries into a single cohesive summary.");
expect(instructions).toContain("Prioritize customer-visible regressions.");
expect((instructions.match(/Additional focus:/g) ?? []).length).toBe(1);
expect(built).toBe("Additional focus:\nTrack release blockers.");
});
});