import OpenAI from 'openai'; import { createImageRecognizeTool } from '../src/modules/netaclaw/tools/builtin/image_recognize.js'; jest.mock('openai'); describe('createImageRecognizeTool', () => { const createMock = jest.fn().mockResolvedValue({ choices: [{ message: { content: 'ok' } }], }); beforeEach(() => { createMock.mockClear(); (OpenAI as unknown as jest.Mock).mockImplementation(() => ({ chat: { completions: { create: createMock, }, }, })); }); it('converts localhost image urls to data urls before sending to the model', async () => { const fetchMock = jest.fn().mockResolvedValue({ ok: true, headers: { get: jest.fn().mockReturnValue('image/png') }, arrayBuffer: jest.fn().mockResolvedValue(Uint8Array.from([1, 2, 3]).buffer), }); (globalThis as any).fetch = fetchMock; const tool = createImageRecognizeTool({ baseUrl: 'https://ark.cn-beijing.volces.com/api/v3', apiKey: 'test-key', supplier: 'volcengine', modelId: 'doubao-seed-2-0-lite-260215', promptHint: null, }); await tool.execute('call-1', { image: 'http://127.0.0.1:8003/upload/demo.png', } as any); expect(fetchMock).toHaveBeenCalledWith('http://127.0.0.1:8003/upload/demo.png'); expect(createMock).toHaveBeenCalledWith(expect.objectContaining({ messages: [expect.objectContaining({ content: expect.arrayContaining([ expect.objectContaining({ type: 'image_url', image_url: expect.objectContaining({ url: 'data:image/png;base64,AQID', }), }), ]), })], })); }); });