mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-12 03:33:43 +00:00
test: move embedded and tool agent suites out of e2e
This commit is contained in:
108
src/agents/pi-embedded-runner/run/attempt.test.ts
Normal file
108
src/agents/pi-embedded-runner/run/attempt.test.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { ImageContent } from "@mariozechner/pi-ai";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { injectHistoryImagesIntoMessages, resolvePromptBuildHookResult } from "./attempt.js";
|
||||
|
||||
describe("injectHistoryImagesIntoMessages", () => {
|
||||
const image: ImageContent = { type: "image", data: "abc", mimeType: "image/png" };
|
||||
|
||||
it("injects history images and converts string content", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
role: "user",
|
||||
content: "See /tmp/photo.png",
|
||||
} as AgentMessage,
|
||||
];
|
||||
|
||||
const didMutate = injectHistoryImagesIntoMessages(messages, new Map([[0, [image]]]));
|
||||
|
||||
expect(didMutate).toBe(true);
|
||||
const firstUser = messages[0] as Extract<AgentMessage, { role: "user" }> | undefined;
|
||||
expect(Array.isArray(firstUser?.content)).toBe(true);
|
||||
const content = firstUser?.content as Array<{ type: string; text?: string; data?: string }>;
|
||||
expect(content).toHaveLength(2);
|
||||
expect(content[0]?.type).toBe("text");
|
||||
expect(content[1]).toMatchObject({ type: "image", data: "abc" });
|
||||
});
|
||||
|
||||
it("avoids duplicating existing image content", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
role: "user",
|
||||
content: [{ type: "text", text: "See /tmp/photo.png" }, { ...image }],
|
||||
} as AgentMessage,
|
||||
];
|
||||
|
||||
const didMutate = injectHistoryImagesIntoMessages(messages, new Map([[0, [image]]]));
|
||||
|
||||
expect(didMutate).toBe(false);
|
||||
const first = messages[0] as Extract<AgentMessage, { role: "user" }> | undefined;
|
||||
if (!first || !Array.isArray(first.content)) {
|
||||
throw new Error("expected array content");
|
||||
}
|
||||
expect(first.content).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("ignores non-user messages and out-of-range indices", () => {
|
||||
const messages: AgentMessage[] = [
|
||||
{
|
||||
role: "assistant",
|
||||
content: "noop",
|
||||
} as unknown as AgentMessage,
|
||||
];
|
||||
|
||||
const didMutate = injectHistoryImagesIntoMessages(messages, new Map([[1, [image]]]));
|
||||
|
||||
expect(didMutate).toBe(false);
|
||||
const firstAssistant = messages[0] as Extract<AgentMessage, { role: "assistant" }> | undefined;
|
||||
expect(firstAssistant?.content).toBe("noop");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolvePromptBuildHookResult", () => {
|
||||
it("reuses precomputed legacy before_agent_start result without invoking hook again", async () => {
|
||||
const hookRunner = {
|
||||
hasHooks: vi.fn(
|
||||
(hookName: "before_prompt_build" | "before_agent_start") =>
|
||||
hookName === "before_agent_start",
|
||||
),
|
||||
runBeforePromptBuild: vi.fn(async () => undefined),
|
||||
runBeforeAgentStart: vi.fn(async () => ({ prependContext: "from-hook" })),
|
||||
};
|
||||
const result = await resolvePromptBuildHookResult({
|
||||
prompt: "hello",
|
||||
messages: [],
|
||||
hookCtx: {},
|
||||
hookRunner,
|
||||
legacyBeforeAgentStartResult: { prependContext: "from-cache", systemPrompt: "legacy-system" },
|
||||
});
|
||||
|
||||
expect(hookRunner.runBeforeAgentStart).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({
|
||||
prependContext: "from-cache",
|
||||
systemPrompt: "legacy-system",
|
||||
});
|
||||
});
|
||||
|
||||
it("calls legacy hook when precomputed result is absent", async () => {
|
||||
const hookRunner = {
|
||||
hasHooks: vi.fn(
|
||||
(hookName: "before_prompt_build" | "before_agent_start") =>
|
||||
hookName === "before_agent_start",
|
||||
),
|
||||
runBeforePromptBuild: vi.fn(async () => undefined),
|
||||
runBeforeAgentStart: vi.fn(async () => ({ prependContext: "from-hook" })),
|
||||
};
|
||||
const messages = [{ role: "user", content: "ctx" }];
|
||||
const result = await resolvePromptBuildHookResult({
|
||||
prompt: "hello",
|
||||
messages,
|
||||
hookCtx: {},
|
||||
hookRunner,
|
||||
});
|
||||
|
||||
expect(hookRunner.runBeforeAgentStart).toHaveBeenCalledTimes(1);
|
||||
expect(hookRunner.runBeforeAgentStart).toHaveBeenCalledWith({ prompt: "hello", messages }, {});
|
||||
expect(result.prependContext).toBe("from-hook");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user