Skip to content

Commit 145d836

Browse files
committed
test: unit test tool calls
1 parent d66e38c commit 145d836

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

src/backend/src/modules/puterai/lib/messages.test.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { expect } = require('chai');
22
const Messages = require('./Messages.js');
3+
const OpenAIUtil = require('./OpenAIUtil.js');
34

45
describe('Messages', () => {
56
describe('normalize_single_message', () => {
@@ -70,4 +71,114 @@ describe('Messages', () => {
7071
});
7172
}
7273
});
74+
describe('normalize OpenAI tool calls', () => {
75+
const cases = [
76+
{
77+
name: 'string message',
78+
input: {
79+
role: 'assistant',
80+
tool_calls: [
81+
{
82+
id: 'tool-1',
83+
type: 'function',
84+
function: {
85+
name: 'tool-1-function',
86+
arguments: {},
87+
}
88+
}
89+
]
90+
},
91+
output: {
92+
role: 'assistant',
93+
content: [
94+
{
95+
type: 'tool_use',
96+
id: 'tool-1',
97+
name: 'tool-1-function',
98+
input: {},
99+
}
100+
]
101+
}
102+
}
103+
];
104+
for ( const tc of cases ) {
105+
it(`should normalize ${tc.name}`, () => {
106+
const output = Messages.normalize_single_message(tc.input);
107+
expect(output).to.deep.equal(tc.output);
108+
});
109+
}
110+
});
111+
describe('normalize Claude tool calls', () => {
112+
const cases = [
113+
{
114+
name: 'string message',
115+
input: {
116+
role: 'assistant',
117+
content: [
118+
{
119+
type: 'tool_use',
120+
id: 'tool-1',
121+
name: 'tool-1-function',
122+
input: "{}",
123+
}
124+
]
125+
},
126+
output: {
127+
role: 'assistant',
128+
content: [
129+
{
130+
type: 'tool_use',
131+
id: 'tool-1',
132+
name: 'tool-1-function',
133+
input: "{}",
134+
}
135+
]
136+
}
137+
}
138+
];
139+
for ( const tc of cases ) {
140+
it(`should normalize ${tc.name}`, () => {
141+
const output = Messages.normalize_single_message(tc.input);
142+
expect(output).to.deep.equal(tc.output);
143+
});
144+
}
145+
});
146+
describe('OpenAI-ify normalized tool calls', () => {
147+
const cases = [
148+
{
149+
name: 'string message',
150+
input: [{
151+
role: 'assistant',
152+
content: [
153+
{
154+
type: 'tool_use',
155+
id: 'tool-1',
156+
name: 'tool-1-function',
157+
input: {},
158+
}
159+
]
160+
}],
161+
output: [{
162+
role: 'assistant',
163+
content: null,
164+
tool_calls: [
165+
{
166+
id: 'tool-1',
167+
type: 'function',
168+
function: {
169+
name: 'tool-1-function',
170+
arguments: '{}',
171+
}
172+
}
173+
]
174+
}]
175+
}
176+
];
177+
for ( const tc of cases ) {
178+
it(`should normalize ${tc.name}`, async () => {
179+
const output = await OpenAIUtil.process_input_messages(tc.input);
180+
expect(output).to.deep.equal(tc.output);
181+
});
182+
}
183+
});
73184
});

0 commit comments

Comments
 (0)