Skip to content

Commit c238aa1

Browse files
authored
fix: make tools return strings (#57)
* fix: make tools return strings * fix tests * fix tests
1 parent 41144e5 commit c238aa1

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

packages/toolbox-core/src/toolbox_core/tool.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ function ToolboxTool(
162162
const response: AxiosResponse = await session.post(toolUrl, payload, {
163163
headers,
164164
});
165-
return response.data;
165+
return typeof response.data === 'string'
166+
? response.data
167+
: JSON.stringify(response.data);
166168
} catch (error) {
167169
logApiError(`Error posting data to ${toolUrl}:`, error);
168170
throw error;

packages/toolbox-core/test/e2e/test.e2e.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ describe('ToolboxClient E2E Tests', () => {
3737
describe('invokeTool', () => {
3838
it('should invoke the getNRowsTool', async () => {
3939
const response = await getNRowsTool({num_rows: '2'});
40-
const result = response['result'];
41-
expect(typeof result).toBe('string');
42-
expect(result).toContain('row1');
43-
expect(result).toContain('row2');
44-
expect(result).not.toContain('row3');
40+
expect(typeof response).toBe('string');
41+
expect(response).toContain('row1');
42+
expect(response).toContain('row2');
43+
expect(response).not.toContain('row3');
4544
});
4645

4746
it('should invoke the getNRowsTool with missing params', async () => {
@@ -136,29 +135,26 @@ describe('ToolboxClient E2E Tests', () => {
136135
it('should successfully bind a parameter with bindParam and invoke', async () => {
137136
const newTool = getNRowsTool.bindParam('num_rows', '3');
138137
const response = await newTool(); // Invoke with no args
139-
const result = response['result'];
140-
expect(result).toContain('row1');
141-
expect(result).toContain('row2');
142-
expect(result).toContain('row3');
143-
expect(result).not.toContain('row4');
138+
expect(response).toContain('row1');
139+
expect(response).toContain('row2');
140+
expect(response).toContain('row3');
141+
expect(response).not.toContain('row4');
144142
});
145143

146144
it('should successfully bind parameters with bindParams and invoke', async () => {
147145
const newTool = getNRowsTool.bindParams({num_rows: '3'});
148146
const response = await newTool(); // Invoke with no args
149-
const result = response['result'];
150-
expect(result).toContain('row1');
151-
expect(result).toContain('row2');
152-
expect(result).toContain('row3');
153-
expect(result).not.toContain('row4');
147+
expect(response).toContain('row1');
148+
expect(response).toContain('row2');
149+
expect(response).toContain('row3');
150+
expect(response).not.toContain('row4');
154151
});
155152

156153
it('should successfully bind a synchronous function value', async () => {
157154
const newTool = getNRowsTool.bindParams({num_rows: () => '1'});
158155
const response = await newTool();
159-
const result = response['result'];
160-
expect(result).toContain('row1');
161-
expect(result).not.toContain('row2');
156+
expect(response).toContain('row1');
157+
expect(response).not.toContain('row2');
162158
});
163159

164160
it('should successfully bind an asynchronous function value', async () => {
@@ -169,23 +165,19 @@ describe('ToolboxClient E2E Tests', () => {
169165

170166
const newTool = getNRowsTool.bindParams({num_rows: asyncNumProvider});
171167
const response = await newTool();
172-
const result = response['result'];
173-
174-
expect(result).toContain('row1');
175-
176-
expect(result).not.toContain('row2');
168+
expect(response).toContain('row1');
169+
expect(response).not.toContain('row2');
177170
});
178171

179172
it('should successfully bind parameters at load time', async () => {
180173
const tool = await commonToolboxClient.loadTool('get-n-rows', null, {
181174
num_rows: '3',
182175
});
183176
const response = await tool();
184-
const result = response['result'];
185-
expect(result).toContain('row1');
186-
expect(result).toContain('row2');
187-
expect(result).toContain('row3');
188-
expect(result).not.toContain('row4');
177+
expect(response).toContain('row1');
178+
expect(response).toContain('row2');
179+
expect(response).toContain('row3');
180+
expect(response).not.toContain('row4');
189181
});
190182

191183
it('should throw an error when re-binding an existing parameter', () => {
@@ -268,7 +260,7 @@ describe('ToolboxClient E2E Tests', () => {
268260
'my-test-auth': authToken1Getter,
269261
});
270262
const response = await authTool({id: '2'});
271-
expect(response.result).toContain('row2');
263+
expect(response).toContain('row2');
272264
});
273265

274266
it('should succeed when running a tool with correct async auth', async () => {
@@ -280,7 +272,7 @@ describe('ToolboxClient E2E Tests', () => {
280272
'my-test-auth': getAsyncToken,
281273
});
282274
const response = await authTool({id: '2'});
283-
expect(response.result).toContain('row2');
275+
expect(response).toContain('row2');
284276
});
285277

286278
it('should fail when a tool with a param requiring auth is run without auth', async () => {
@@ -295,9 +287,9 @@ describe('ToolboxClient E2E Tests', () => {
295287
'my-test-auth': authToken1Getter,
296288
});
297289
const response = await tool();
298-
expect(response.result).toContain('row4');
299-
expect(response.result).toContain('row5');
300-
expect(response.result).toContain('row6');
290+
expect(response).toContain('row4');
291+
expect(response).toContain('row5');
292+
expect(response).toContain('row6');
301293
});
302294

303295
it('should fail when a tool with a param requiring auth is run with insufficient auth claims', async () => {

packages/toolbox-core/test/test.tool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ describe('ToolboxTool', () => {
307307
expect(mockAxiosPost).toHaveBeenCalledWith(expectedUrl, validArgs, {
308308
headers: {},
309309
});
310-
expect(result).toEqual(mockApiResponseData);
310+
expect(result).toEqual(JSON.stringify(mockApiResponseData));
311311
});
312312

313313
it('should re-throw the error and log to console.error if API call fails', async () => {

0 commit comments

Comments
 (0)