test: unit test tool calls

This commit is contained in:
KernelDeimos
2025-02-20 09:41:59 -05:00
parent d66e38cc66
commit 145d836b9f

View File

@@ -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);
});
}
});
});