From 145d836b9f0738c5841d3b26031c61119d0eacfb Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Thu, 20 Feb 2025 09:41:59 -0500 Subject: [PATCH] test: unit test tool calls --- .../src/modules/puterai/lib/messages.test.js | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/src/backend/src/modules/puterai/lib/messages.test.js b/src/backend/src/modules/puterai/lib/messages.test.js index f7ca3695..53a4cb85 100644 --- a/src/backend/src/modules/puterai/lib/messages.test.js +++ b/src/backend/src/modules/puterai/lib/messages.test.js @@ -1,5 +1,6 @@ const { expect } = require('chai'); const Messages = require('./Messages.js'); +const OpenAIUtil = require('./OpenAIUtil.js'); describe('Messages', () => { describe('normalize_single_message', () => { @@ -70,4 +71,114 @@ describe('Messages', () => { }); } }); + describe('normalize OpenAI tool calls', () => { + const cases = [ + { + name: 'string message', + input: { + role: 'assistant', + tool_calls: [ + { + id: 'tool-1', + type: 'function', + function: { + name: 'tool-1-function', + arguments: {}, + } + } + ] + }, + output: { + role: 'assistant', + content: [ + { + type: 'tool_use', + id: 'tool-1', + name: 'tool-1-function', + input: {}, + } + ] + } + } + ]; + for ( const tc of cases ) { + it(`should normalize ${tc.name}`, () => { + const output = Messages.normalize_single_message(tc.input); + expect(output).to.deep.equal(tc.output); + }); + } + }); + describe('normalize Claude tool calls', () => { + const cases = [ + { + name: 'string message', + input: { + role: 'assistant', + content: [ + { + type: 'tool_use', + id: 'tool-1', + name: 'tool-1-function', + input: "{}", + } + ] + }, + output: { + role: 'assistant', + content: [ + { + type: 'tool_use', + id: 'tool-1', + name: 'tool-1-function', + input: "{}", + } + ] + } + } + ]; + for ( const tc of cases ) { + it(`should normalize ${tc.name}`, () => { + const output = Messages.normalize_single_message(tc.input); + expect(output).to.deep.equal(tc.output); + }); + } + }); + describe('OpenAI-ify normalized tool calls', () => { + const cases = [ + { + name: 'string message', + input: [{ + role: 'assistant', + content: [ + { + type: 'tool_use', + id: 'tool-1', + name: 'tool-1-function', + input: {}, + } + ] + }], + output: [{ + role: 'assistant', + content: null, + tool_calls: [ + { + id: 'tool-1', + type: 'function', + function: { + name: 'tool-1-function', + arguments: '{}', + } + } + ] + }] + } + ]; + for ( const tc of cases ) { + it(`should normalize ${tc.name}`, async () => { + const output = await OpenAIUtil.process_input_messages(tc.input); + expect(output).to.deep.equal(tc.output); + }); + } + }); });