diff --git a/src/agents/tools/image-tool.e2e.test.ts b/src/agents/tools/image-tool.e2e.test.ts index 400e2b7a822..b4bee9bb31e 100644 --- a/src/agents/tools/image-tool.e2e.test.ts +++ b/src/agents/tools/image-tool.e2e.test.ts @@ -92,6 +92,14 @@ async function expectImageToolExecOk( }); } +function requireImageTool(tool: T | null | undefined): T { + expect(tool).not.toBeNull(); + if (!tool) { + throw new Error("expected image tool"); + } + return tool; +} + function findSchemaUnionKeywords(schema: unknown, path = "root"): string[] { if (!schema || typeof schema !== "object") { return []; @@ -249,11 +257,7 @@ describe("image tool implicit imageModel config", () => { const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); try { const cfg = createMinimaxImageConfig(); - const tool = createImageTool({ config: cfg, agentDir }); - expect(tool).not.toBeNull(); - if (!tool) { - throw new Error("expected image tool"); - } + const tool = requireImageTool(createImageTool({ config: cfg, agentDir })); const violations = findSchemaUnionKeywords(tool.parameters, "image.parameters"); expect(violations).toEqual([]); @@ -279,11 +283,7 @@ describe("image tool implicit imageModel config", () => { const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-")); try { const cfg = createMinimaxImageConfig(); - const tool = createImageTool({ config: cfg, agentDir }); - expect(tool).not.toBeNull(); - if (!tool) { - throw new Error("expected image tool"); - } + const tool = requireImageTool(createImageTool({ config: cfg, agentDir })); expect(JSON.parse(JSON.stringify(tool.parameters))).toEqual({ type: "object", @@ -312,11 +312,7 @@ describe("image tool implicit imageModel config", () => { try { const cfg = createMinimaxImageConfig(); - const withoutWorkspace = createImageTool({ config: cfg, agentDir }); - expect(withoutWorkspace).not.toBeNull(); - if (!withoutWorkspace) { - throw new Error("expected image tool"); - } + const withoutWorkspace = requireImageTool(createImageTool({ config: cfg, agentDir })); await expect( withoutWorkspace.execute("t0", { prompt: "Describe the image.", @@ -324,11 +320,9 @@ describe("image tool implicit imageModel config", () => { }), ).rejects.toThrow(/Local media path is not under an allowed directory/i); - const withWorkspace = createImageTool({ config: cfg, agentDir, workspaceDir }); - expect(withWorkspace).not.toBeNull(); - if (!withWorkspace) { - throw new Error("expected image tool"); - } + const withWorkspace = requireImageTool( + createImageTool({ config: cfg, agentDir, workspaceDir }), + ); await expectImageToolExecOk(withWorkspace, imagePath); @@ -347,11 +341,7 @@ describe("image tool implicit imageModel config", () => { const cfg = createMinimaxImageConfig(); const tools = createOpenClawCodingTools({ config: cfg, agentDir }); - const tool = tools.find((candidate) => candidate.name === "image"); - expect(tool).not.toBeNull(); - if (!tool) { - throw new Error("expected image tool"); - } + const tool = requireImageTool(tools.find((candidate) => candidate.name === "image")); await expectImageToolExecOk(tool, imagePath); @@ -375,11 +365,7 @@ describe("image tool implicit imageModel config", () => { const cfg: OpenClawConfig = { agents: { defaults: { model: { primary: "minimax/MiniMax-M2.1" } } }, }; - const tool = createImageTool({ config: cfg, agentDir, sandbox }); - expect(tool).not.toBeNull(); - if (!tool) { - throw new Error("expected image tool"); - } + const tool = requireImageTool(createImageTool({ config: cfg, agentDir, sandbox })); await expect(tool.execute("t1", { image: "https://example.com/a.png" })).rejects.toThrow( /Sandboxed image tool does not allow remote URLs/i, @@ -405,18 +391,7 @@ describe("image tool implicit imageModel config", () => { Buffer.from(pngB64, "base64"), ); - const fetch = vi.fn().mockResolvedValue({ - ok: true, - status: 200, - statusText: "OK", - headers: new Headers(), - json: async () => ({ - content: "ok", - base_resp: { status_code: 0, status_msg: "" }, - }), - }); - global.fetch = withFetchPreconnect(fetch); - vi.stubEnv("MINIMAX_API_KEY", "minimax-test"); + const fetch = stubMinimaxOkFetch(); const cfg: OpenClawConfig = { agents: { @@ -427,11 +402,7 @@ describe("image tool implicit imageModel config", () => { }, }; const sandbox = { root: sandboxRoot, bridge: createHostSandboxFsBridge(sandboxRoot) }; - const tool = createImageTool({ config: cfg, agentDir, sandbox }); - expect(tool).not.toBeNull(); - if (!tool) { - throw new Error("expected image tool"); - } + const tool = requireImageTool(createImageTool({ config: cfg, agentDir, sandbox })); const res = await tool.execute("t1", { prompt: "Describe the image.", @@ -495,11 +466,7 @@ describe("image tool MiniMax VLM routing", () => { const cfg: OpenClawConfig = { agents: { defaults: { model: { primary: "minimax/MiniMax-M2.1" } } }, }; - const tool = createImageTool({ config: cfg, agentDir }); - expect(tool).not.toBeNull(); - if (!tool) { - throw new Error("expected image tool"); - } + const tool = requireImageTool(createImageTool({ config: cfg, agentDir })); return { fetch, tool }; }