Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 93 lines 2.87 KB
Raw
1 import { describe, expect, it } from 'vitest';
2 import { isExecutionSuccessful, normalizeResponse } from 'src/services/tools-executor';
3
4 describe('isExecutionSuccessful', () => {
5 it.each([
6 {
7 name: 'failed with a null error',
8 raw: { result_type: 'failed' as const, error: null },
9 expected: false,
10 },
11 {
12 name: 'failed with an empty error',
13 raw: { result_type: 'failed' as const, error: '' },
14 expected: false,
15 },
16 {
17 name: 'failed with a message',
18 raw: { result_type: 'failed' as const, error: 'Boom' },
19 expected: false,
20 },
21 { name: 'blocked', raw: { result_type: 'blocked' as const }, expected: false },
22 { name: 'completed', raw: { result_type: 'completed' as const, error: null }, expected: true },
23 ])('[Given] $name [Then] successful is $expected', ({ raw, expected }) => {
24 expect(isExecutionSuccessful(raw)).toBe(expected);
25 });
26 });
27
28 describe('normalizeResponse', () => {
29 const completed = {
30 result_type: 'completed' as const,
31 data: { ok: true },
32 error: null,
33 log_id: 'log_test',
34 };
35
36 it('[Given] a response with instant_charge [Then] it yields instantCharge', () => {
37 const response = normalizeResponse({
38 ...completed,
39 instant_charge: { amount: '0.005', currency: 'USD', charged_by: 'composio' },
40 });
41
42 expect(response.instantCharge).toEqual({
43 amount: '0.005',
44 currency: 'USD',
45 chargedBy: 'composio',
46 });
47 });
48
49 it('[Given] a response without instant_charge [Then] it has no instantCharge key', () => {
50 const response = normalizeResponse(completed);
51
52 expect(response).not.toHaveProperty('instantCharge');
53 expect(response).toEqual({
54 successful: true,
55 data: { ok: true },
56 error: null,
57 logId: 'log_test',
58 });
59 });
60
61 it('[Given] a call a session hook blocked [Then] it is a failed call with the hook message', () => {
62 const response = normalizeResponse({
63 result_type: 'blocked',
64 reason: { type: 'request_blocked', hook_id: 'no_external_email' },
65 message: 'Blocked by session policy: No external email',
66 });
67
68 expect(response).toEqual({
69 successful: false,
70 data: {},
71 error: 'Blocked by session policy: No external email',
72 logId: '',
73 });
74 });
75
76 it('[Given] a blocked result of a call that ran [Then] it keeps the log id and the charge', () => {
77 const response = normalizeResponse({
78 result_type: 'blocked',
79 reason: { type: 'response_blocked', hook_id: 'no_outside_data' },
80 message: 'Blocked by session policy',
81 log_id: 'log_456',
82 instant_charge: { amount: '0.01', currency: 'USD', charged_by: 'composio' },
83 });
84
85 expect(response).toEqual({
86 successful: false,
87 data: {},
88 error: 'Blocked by session policy',
89 logId: 'log_456',
90 instantCharge: { amount: '0.01', currency: 'USD', chargedBy: 'composio' },
91 });
92 });
93 });