Skip to content

Commit f72112b

Browse files
committed
test: cover tool invocation append
1 parent 5a0ddaf commit f72112b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/utils/functionCalling/__tests__/handleFunctionCalling.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,70 @@ describe('handleFunctionCalling utils (browser/jsdom)', () => {
188188
})
189189
})
190190

191+
it('handleFunctionCall appends tool invocations when message.tools already exists', async () => {
192+
const availableTools: any[] = [
193+
{ id: 'w1', name: 'my_tool', readableName: 'My Tool', description: 'd' },
194+
]
195+
const existingTool: any = {
196+
id: 'prev',
197+
name: 'prev_tool',
198+
readableName: 'Prev Tool',
199+
invocationId: 'prev1',
200+
}
201+
const message: any = {
202+
id: 'u1',
203+
role: 'user',
204+
content: 'hi',
205+
tools: [existingTool],
206+
}
207+
const conversation: any = {
208+
id: 'c1',
209+
model: { id: 'gpt-4o', name: 'm', tokenLimit: 10, enabled: true },
210+
messages: [message],
211+
}
212+
213+
vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(
214+
new Response(
215+
JSON.stringify({
216+
choices: [
217+
{
218+
message: {
219+
tool_calls: [
220+
{
221+
id: 'call1',
222+
function: { name: 'my_tool', arguments: '{"x":1}' },
223+
},
224+
],
225+
},
226+
},
227+
],
228+
}),
229+
{ status: 200 },
230+
),
231+
)
232+
233+
const tools = await handleFunctionCall(
234+
message,
235+
availableTools,
236+
[],
237+
'',
238+
conversation,
239+
'openai-key',
240+
'CS101',
241+
)
242+
243+
expect(tools).toHaveLength(1)
244+
expect(conversation.messages[0].tools).toHaveLength(2)
245+
expect(conversation.messages[0].tools[0]).toMatchObject({
246+
id: 'prev',
247+
invocationId: 'prev1',
248+
})
249+
expect(conversation.messages[0].tools[1]).toMatchObject({
250+
id: 'w1',
251+
invocationId: 'call1',
252+
})
253+
})
254+
191255
it('handleFunctionCall uses OpenAICompatible and lowercases modelId for OpenRouter', async () => {
192256
const conversation = {
193257
id: 'c1',

0 commit comments

Comments
 (0)