mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 16:14:58 +00:00
test(agents): centralize AgentMessage fixtures and remove unsafe casts
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { castAgentMessage } from "../../test-helpers/agent-message-fixtures.js";
|
||||
import {
|
||||
selectCompactionTimeoutSnapshot,
|
||||
shouldFlagCompactionTimeout,
|
||||
@@ -32,8 +32,8 @@ describe("compaction-timeout helpers", () => {
|
||||
});
|
||||
|
||||
it("uses pre-compaction snapshot when compaction timeout occurs", () => {
|
||||
const pre = [{ role: "assistant", content: "pre" } as unknown as AgentMessage] as const;
|
||||
const current = [{ role: "assistant", content: "current" } as unknown as AgentMessage] as const;
|
||||
const pre = [castAgentMessage({ role: "assistant", content: "pre" })] as const;
|
||||
const current = [castAgentMessage({ role: "assistant", content: "current" })] as const;
|
||||
const selected = selectCompactionTimeoutSnapshot({
|
||||
timedOutDuringCompaction: true,
|
||||
preCompactionSnapshot: [...pre],
|
||||
@@ -47,7 +47,7 @@ describe("compaction-timeout helpers", () => {
|
||||
});
|
||||
|
||||
it("falls back to current snapshot when pre-compaction snapshot is unavailable", () => {
|
||||
const current = [{ role: "assistant", content: "current" } as unknown as AgentMessage] as const;
|
||||
const current = [castAgentMessage({ role: "assistant", content: "current" })] as const;
|
||||
const selected = selectCompactionTimeoutSnapshot({
|
||||
timedOutDuringCompaction: true,
|
||||
preCompactionSnapshot: null,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { ImageContent } from "@mariozechner/pi-ai";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { castAgentMessage } from "../../test-helpers/agent-message-fixtures.js";
|
||||
import { PRUNED_HISTORY_IMAGE_MARKER, pruneProcessedHistoryImages } from "./history-image-prune.js";
|
||||
|
||||
describe("pruneProcessedHistoryImages", () => {
|
||||
@@ -8,14 +9,14 @@ describe("pruneProcessedHistoryImages", () => {
|
||||
|
||||
it("prunes image blocks from user messages that already have assistant replies", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
castAgentMessage({
|
||||
role: "user",
|
||||
content: [{ type: "text", text: "See /tmp/photo.png" }, { ...image }],
|
||||
} as AgentMessage,
|
||||
{
|
||||
}),
|
||||
castAgentMessage({
|
||||
role: "assistant",
|
||||
content: "got it",
|
||||
} as unknown as AgentMessage,
|
||||
}),
|
||||
];
|
||||
|
||||
const didMutate = pruneProcessedHistoryImages(messages);
|
||||
@@ -31,10 +32,10 @@ describe("pruneProcessedHistoryImages", () => {
|
||||
|
||||
it("does not prune latest user message when no assistant response exists yet", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
castAgentMessage({
|
||||
role: "user",
|
||||
content: [{ type: "text", text: "See /tmp/photo.png" }, { ...image }],
|
||||
} as AgentMessage,
|
||||
}),
|
||||
];
|
||||
|
||||
const didMutate = pruneProcessedHistoryImages(messages);
|
||||
@@ -50,10 +51,10 @@ describe("pruneProcessedHistoryImages", () => {
|
||||
|
||||
it("does not change messages when no assistant turn exists", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
castAgentMessage({
|
||||
role: "user",
|
||||
content: "noop",
|
||||
} as AgentMessage,
|
||||
}),
|
||||
];
|
||||
|
||||
const didMutate = pruneProcessedHistoryImages(messages);
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { castAgentMessage } from "../test-helpers/agent-message-fixtures.js";
|
||||
import { dropThinkingBlocks, isAssistantMessageWithContent } from "./thinking.js";
|
||||
|
||||
describe("isAssistantMessageWithContent", () => {
|
||||
it("accepts assistant messages with array content and rejects others", () => {
|
||||
const assistant = {
|
||||
const assistant = castAgentMessage({
|
||||
role: "assistant",
|
||||
content: [{ type: "text", text: "ok" }],
|
||||
} as AgentMessage;
|
||||
const user = { role: "user", content: "hi" } as AgentMessage;
|
||||
const malformed = { role: "assistant", content: "not-array" } as unknown as AgentMessage;
|
||||
});
|
||||
const user = castAgentMessage({ role: "user", content: "hi" });
|
||||
const malformed = castAgentMessage({ role: "assistant", content: "not-array" });
|
||||
|
||||
expect(isAssistantMessageWithContent(assistant)).toBe(true);
|
||||
expect(isAssistantMessageWithContent(user)).toBe(false);
|
||||
@@ -20,8 +21,8 @@ describe("isAssistantMessageWithContent", () => {
|
||||
describe("dropThinkingBlocks", () => {
|
||||
it("returns the original reference when no thinking blocks are present", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{ role: "user", content: "hello" } as AgentMessage,
|
||||
{ role: "assistant", content: [{ type: "text", text: "world" }] } as AgentMessage,
|
||||
castAgentMessage({ role: "user", content: "hello" }),
|
||||
castAgentMessage({ role: "assistant", content: [{ type: "text", text: "world" }] }),
|
||||
];
|
||||
|
||||
const result = dropThinkingBlocks(messages);
|
||||
@@ -30,13 +31,13 @@ describe("dropThinkingBlocks", () => {
|
||||
|
||||
it("drops thinking blocks while preserving non-thinking assistant content", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
castAgentMessage({
|
||||
role: "assistant",
|
||||
content: [
|
||||
{ type: "thinking", thinking: "internal" },
|
||||
{ type: "text", text: "final" },
|
||||
],
|
||||
} as unknown as AgentMessage,
|
||||
}),
|
||||
];
|
||||
|
||||
const result = dropThinkingBlocks(messages);
|
||||
@@ -47,10 +48,10 @@ describe("dropThinkingBlocks", () => {
|
||||
|
||||
it("keeps assistant turn structure when all content blocks were thinking", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
castAgentMessage({
|
||||
role: "assistant",
|
||||
content: [{ type: "thinking", thinking: "internal-only" }],
|
||||
} as unknown as AgentMessage,
|
||||
}),
|
||||
];
|
||||
|
||||
const result = dropThinkingBlocks(messages);
|
||||
|
||||
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user