test(agents): centralize AgentMessage fixtures and remove unsafe casts

This commit is contained in:
Peter Steinberger
2026-03-03 02:13:43 +00:00
parent 15a0455d04
commit 70db52de71
14 changed files with 216 additions and 139 deletions

View File

@@ -1,5 +1,6 @@
import type { AgentMessage } from "@mariozechner/pi-agent-core";
import { describe, expect, it } from "vitest";
import { castAgentMessage } from "../test-helpers/agent-message-fixtures.js";
import {
CONTEXT_LIMIT_TRUNCATION_NOTICE,
PREEMPTIVE_TOOL_RESULT_COMPACTION_PLACEHOLDER,
@@ -7,35 +8,35 @@ import {
} from "./tool-result-context-guard.js";
function makeUser(text: string): AgentMessage {
return {
return castAgentMessage({
role: "user",
content: text,
timestamp: Date.now(),
} as unknown as AgentMessage;
});
}
function makeToolResult(id: string, text: string): AgentMessage {
return {
return castAgentMessage({
role: "toolResult",
toolCallId: id,
toolName: "read",
content: [{ type: "text", text }],
isError: false,
timestamp: Date.now(),
} as unknown as AgentMessage;
});
}
function makeLegacyToolResult(id: string, text: string): AgentMessage {
return {
return castAgentMessage({
role: "tool",
tool_call_id: id,
tool_name: "read",
content: text,
} as unknown as AgentMessage;
});
}
function makeToolResultWithDetails(id: string, text: string, detailText: string): AgentMessage {
return {
return castAgentMessage({
role: "toolResult",
toolCallId: id,
toolName: "read",
@@ -49,7 +50,7 @@ function makeToolResultWithDetails(id: string, text: string, detailText: string)
},
isError: false,
timestamp: Date.now(),
} as unknown as AgentMessage;
});
}
function getToolResultText(msg: AgentMessage): string {
@@ -199,11 +200,10 @@ describe("installToolResultContextGuard", () => {
it("wraps an existing transformContext and guards the transformed output", async () => {
const agent = makeGuardableAgent((messages) => {
return messages.map(
(msg) =>
({
...(msg as unknown as Record<string, unknown>),
}) as unknown as AgentMessage,
return messages.map((msg) =>
castAgentMessage({
...(msg as unknown as Record<string, unknown>),
}),
);
});
const contextForNextCall = makeTwoToolResultOverflowContext();
@@ -254,10 +254,10 @@ describe("installToolResultContextGuard", () => {
await agent.transformContext?.(contextForNextCall, new AbortController().signal);
const oldResult = contextForNextCall[1] as unknown as {
const oldResult = contextForNextCall[1] as {
details?: unknown;
};
const newResult = contextForNextCall[2] as unknown as {
const newResult = contextForNextCall[2] as {
details?: unknown;
};
const oldResultText = getToolResultText(contextForNextCall[1]);