test(image-tool): share temp agent dirs and table-drive validation cases

This commit is contained in:
Peter Steinberger
2026-02-21 23:46:32 +00:00
parent 150c048b0a
commit a353dae14f

View File

@@ -18,6 +18,15 @@ async function writeAuthProfiles(agentDir: string, profiles: unknown) {
); );
} }
async function withTempAgentDir<T>(run: (agentDir: string) => Promise<T>): Promise<T> {
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-"));
try {
return await run(agentDir);
} finally {
await fs.rm(agentDir, { recursive: true, force: true });
}
}
const ONE_PIXEL_PNG_B64 = const ONE_PIXEL_PNG_B64 =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/woAAn8B9FD5fHAAAAAASUVORK5CYII="; "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/woAAn8B9FD5fHAAAAAASUVORK5CYII=";
const ONE_PIXEL_GIF_B64 = "R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs="; const ONE_PIXEL_GIF_B64 = "R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=";
@@ -141,16 +150,17 @@ describe("image tool implicit imageModel config", () => {
}); });
it("stays disabled without auth when no pairing is possible", async () => { it("stays disabled without auth when no pairing is possible", async () => {
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); await withTempAgentDir(async (agentDir) => {
const cfg: OpenClawConfig = { const cfg: OpenClawConfig = {
agents: { defaults: { model: { primary: "openai/gpt-5.2" } } }, agents: { defaults: { model: { primary: "openai/gpt-5.2" } } },
}; };
expect(resolveImageModelConfigForTool({ cfg, agentDir })).toBeNull(); expect(resolveImageModelConfigForTool({ cfg, agentDir })).toBeNull();
expect(createImageTool({ config: cfg, agentDir })).toBeNull(); expect(createImageTool({ config: cfg, agentDir })).toBeNull();
}); });
});
it("pairs minimax primary with MiniMax-VL-01 (and fallbacks) when auth exists", async () => { it("pairs minimax primary with MiniMax-VL-01 (and fallbacks) when auth exists", async () => {
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); await withTempAgentDir(async (agentDir) => {
vi.stubEnv("MINIMAX_API_KEY", "minimax-test"); vi.stubEnv("MINIMAX_API_KEY", "minimax-test");
vi.stubEnv("OPENAI_API_KEY", "openai-test"); vi.stubEnv("OPENAI_API_KEY", "openai-test");
vi.stubEnv("ANTHROPIC_API_KEY", "anthropic-test"); vi.stubEnv("ANTHROPIC_API_KEY", "anthropic-test");
@@ -163,9 +173,10 @@ describe("image tool implicit imageModel config", () => {
}); });
expect(createImageTool({ config: cfg, agentDir })).not.toBeNull(); expect(createImageTool({ config: cfg, agentDir })).not.toBeNull();
}); });
});
it("pairs zai primary with glm-4.6v (and fallbacks) when auth exists", async () => { it("pairs zai primary with glm-4.6v (and fallbacks) when auth exists", async () => {
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); await withTempAgentDir(async (agentDir) => {
vi.stubEnv("ZAI_API_KEY", "zai-test"); vi.stubEnv("ZAI_API_KEY", "zai-test");
vi.stubEnv("OPENAI_API_KEY", "openai-test"); vi.stubEnv("OPENAI_API_KEY", "openai-test");
vi.stubEnv("ANTHROPIC_API_KEY", "anthropic-test"); vi.stubEnv("ANTHROPIC_API_KEY", "anthropic-test");
@@ -178,9 +189,10 @@ describe("image tool implicit imageModel config", () => {
}); });
expect(createImageTool({ config: cfg, agentDir })).not.toBeNull(); expect(createImageTool({ config: cfg, agentDir })).not.toBeNull();
}); });
});
it("pairs a custom provider when it declares an image-capable model", async () => { it("pairs a custom provider when it declares an image-capable model", async () => {
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); await withTempAgentDir(async (agentDir) => {
await writeAuthProfiles(agentDir, { await writeAuthProfiles(agentDir, {
version: 1, version: 1,
profiles: { profiles: {
@@ -206,9 +218,10 @@ describe("image tool implicit imageModel config", () => {
}); });
expect(createImageTool({ config: cfg, agentDir })).not.toBeNull(); expect(createImageTool({ config: cfg, agentDir })).not.toBeNull();
}); });
});
it("prefers explicit agents.defaults.imageModel", async () => { it("prefers explicit agents.defaults.imageModel", async () => {
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); await withTempAgentDir(async (agentDir) => {
const cfg: OpenClawConfig = { const cfg: OpenClawConfig = {
agents: { agents: {
defaults: { defaults: {
@@ -221,13 +234,14 @@ describe("image tool implicit imageModel config", () => {
primary: "openai/gpt-5-mini", primary: "openai/gpt-5-mini",
}); });
}); });
});
it("keeps image tool available when primary model supports images (for explicit requests)", async () => { it("keeps image tool available when primary model supports images (for explicit requests)", async () => {
// When the primary model supports images, we still keep the tool available // When the primary model supports images, we still keep the tool available
// because images are auto-injected into prompts. The tool description is // because images are auto-injected into prompts. The tool description is
// adjusted via modelHasVision to discourage redundant usage. // adjusted via modelHasVision to discourage redundant usage.
vi.stubEnv("OPENAI_API_KEY", "test-key"); vi.stubEnv("OPENAI_API_KEY", "test-key");
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); await withTempAgentDir(async (agentDir) => {
const cfg: OpenClawConfig = { const cfg: OpenClawConfig = {
agents: { agents: {
defaults: { defaults: {
@@ -250,7 +264,10 @@ describe("image tool implicit imageModel config", () => {
}); });
const tool = createImageTool({ config: cfg, agentDir, modelHasVision: true }); const tool = createImageTool({ config: cfg, agentDir, modelHasVision: true });
expect(tool).not.toBeNull(); expect(tool).not.toBeNull();
expect(tool?.description).toContain("Only use this tool when images were NOT already provided"); expect(tool?.description).toContain(
"Only use this tool when images were NOT already provided",
);
});
}); });
it("exposes an Anthropic-safe image schema without union keywords", async () => { it("exposes an Anthropic-safe image schema without union keywords", async () => {
@@ -598,41 +615,50 @@ describe("image tool response validation", () => {
}; };
} }
it("caps image-tool max tokens by model capability", () => { it.each([
expect(__testing.resolveImageToolMaxTokens(4000)).toBe(4000); {
name: "caps image-tool max tokens by model capability",
maxOutputTokens: 4000,
expected: 4000,
},
{
name: "keeps requested image-tool max tokens when model capability is higher",
maxOutputTokens: 8192,
expected: 4096,
},
{
name: "falls back to requested image-tool max tokens when model capability is missing",
maxOutputTokens: undefined,
expected: 4096,
},
])("$name", ({ maxOutputTokens, expected }) => {
expect(__testing.resolveImageToolMaxTokens(maxOutputTokens)).toBe(expected);
}); });
it("keeps requested image-tool max tokens when model capability is higher", () => { it.each([
expect(__testing.resolveImageToolMaxTokens(8192)).toBe(4096); {
}); name: "rejects image-model responses with no final text",
it("falls back to requested image-tool max tokens when model capability is missing", () => {
expect(__testing.resolveImageToolMaxTokens(undefined)).toBe(4096);
});
it("rejects image-model responses with no final text", () => {
expect(() =>
__testing.coerceImageAssistantText({
provider: "openai",
model: "gpt-5-mini",
message: createAssistantMessage({ message: createAssistantMessage({
content: [{ type: "thinking", thinking: "hmm" }], content: [{ type: "thinking", thinking: "hmm" }],
}) as never, }) as never,
}), expectedError: /returned no text/i,
).toThrow(/returned no text/i); },
}); {
name: "surfaces provider errors from image-model responses",
it("surfaces provider errors from image-model responses", () => {
expect(() =>
__testing.coerceImageAssistantText({
provider: "openai",
model: "gpt-5-mini",
message: createAssistantMessage({ message: createAssistantMessage({
stopReason: "error", stopReason: "error",
errorMessage: "boom", errorMessage: "boom",
}) as never, }) as never,
expectedError: /boom/i,
},
])("$name", ({ message, expectedError }) => {
expect(() =>
__testing.coerceImageAssistantText({
provider: "openai",
model: "gpt-5-mini",
message,
}), }),
).toThrow(/boom/i); ).toThrow(expectedError);
}); });
it("returns trimmed text from image-model responses", () => { it("returns trimmed text from image-model responses", () => {