test(agents): fix context-pruning malformed-block fixture typing

This commit is contained in:
Shakker
2026-03-05 05:46:06 +00:00
parent 0e834a9169
commit 50cbec60d5

View File

@@ -1,60 +1,111 @@
import type { AgentMessage } from "@mariozechner/pi-agent-core";
import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
import { describe, expect, it } from "vitest";
import { pruneContextMessages } from "./pruner.js";
import { DEFAULT_CONTEXT_PRUNING_SETTINGS } from "./settings.js";
const MODEL = { contextWindow: 1_000_000 };
type AssistantMessage = Extract<AgentMessage, { role: "assistant" }>;
type AssistantContentBlock = AssistantMessage["content"][number];
const CONTEXT_WINDOW_1M = {
model: { contextWindow: 1_000_000 },
} as unknown as ExtensionContext;
function makeUser(text: string): AgentMessage {
return {
role: "user",
content: text,
timestamp: Date.now(),
};
}
function makeAssistant(content: AssistantMessage["content"]): AgentMessage {
return {
role: "assistant",
content,
api: "openai-responses",
provider: "openai",
model: "test-model",
usage: {
input: 1,
output: 1,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 2,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
total: 0,
},
},
stopReason: "stop",
timestamp: Date.now(),
};
}
describe("pruneContextMessages", () => {
it("does not crash on assistant message with malformed thinking block (missing thinking string)", () => {
const messages: AgentMessage[] = [
{ role: "user", content: "hello" },
{
role: "assistant",
content: [{ type: "thinking" } as never, { type: "text", text: "ok" }],
},
makeUser("hello"),
makeAssistant([
{ type: "thinking" } as unknown as AssistantContentBlock,
{ type: "text", text: "ok" },
]),
];
expect(() =>
pruneContextMessages({
messages,
settings: DEFAULT_CONTEXT_PRUNING_SETTINGS,
ctx: { model: MODEL },
ctx: CONTEXT_WINDOW_1M,
}),
).not.toThrow();
});
it("does not crash on assistant message with null content entries", () => {
const messages: AgentMessage[] = [
{ role: "user", content: "hello" },
{
role: "assistant",
content: [null as never, { type: "text", text: "world" }],
},
makeUser("hello"),
makeAssistant([null as unknown as AssistantContentBlock, { type: "text", text: "world" }]),
];
expect(() =>
pruneContextMessages({
messages,
settings: DEFAULT_CONTEXT_PRUNING_SETTINGS,
ctx: { model: MODEL },
ctx: CONTEXT_WINDOW_1M,
}),
).not.toThrow();
});
it("does not crash on assistant message with malformed text block (missing text string)", () => {
const messages: AgentMessage[] = [
makeUser("hello"),
makeAssistant([
{ type: "text" } as unknown as AssistantContentBlock,
{ type: "thinking", thinking: "still fine" },
]),
];
expect(() =>
pruneContextMessages({
messages,
settings: DEFAULT_CONTEXT_PRUNING_SETTINGS,
ctx: CONTEXT_WINDOW_1M,
}),
).not.toThrow();
});
it("handles well-formed thinking blocks correctly", () => {
const messages: AgentMessage[] = [
{ role: "user", content: "hello" },
{
role: "assistant",
content: [
{ type: "thinking", thinking: "let me think" },
{ type: "text", text: "here is the answer" },
],
},
makeUser("hello"),
makeAssistant([
{ type: "thinking", thinking: "let me think" },
{ type: "text", text: "here is the answer" },
]),
];
const result = pruneContextMessages({
messages,
settings: DEFAULT_CONTEXT_PRUNING_SETTINGS,
ctx: { model: MODEL },
ctx: CONTEXT_WINDOW_1M,
});
expect(result).toHaveLength(2);
});