Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { normalizeToolArguments } from '../../src/utils/toolArguments'; |
| 3 | import { ComposioInvalidToolArgumentsError } from '../../src/errors/ToolErrors'; |
| 4 | |
| 5 | describe('normalizeToolArguments (issue #2406)', () => { |
| 6 | it('returns a plain object unchanged', () => { |
| 7 | const input = { to: 'a@b.com', subject: 'hi' }; |
| 8 | expect(normalizeToolArguments(input)).toBe(input); |
| 9 | }); |
| 10 | |
| 11 | it('parses a stringified JSON object into an object', () => { |
| 12 | const params = { to: 'a@b.com', subject: 'hi', body: 'Hello' }; |
| 13 | expect(normalizeToolArguments(JSON.stringify(params))).toEqual(params); |
| 14 | }); |
| 15 | |
| 16 | it.each([null, undefined])('coerces %s to an empty object', value => { |
| 17 | expect(normalizeToolArguments(value)).toEqual({}); |
| 18 | }); |
| 19 | |
| 20 | it.each(['', ' ', '\n\t '])( |
| 21 | 'coerces an empty/whitespace string %j to an empty object', |
| 22 | value => { |
| 23 | expect(normalizeToolArguments(value)).toEqual({}); |
| 24 | } |
| 25 | ); |
| 26 | |
| 27 | it('throws a typed error when the string is not valid JSON', () => { |
| 28 | expect(() => normalizeToolArguments('{"to": "a@b.com"', 'GMAIL_SEND')).toThrow( |
| 29 | ComposioInvalidToolArgumentsError |
| 30 | ); |
| 31 | try { |
| 32 | normalizeToolArguments('not json at all', 'GMAIL_SEND'); |
| 33 | } catch (error) { |
| 34 | expect(error).toBeInstanceOf(ComposioInvalidToolArgumentsError); |
| 35 | expect((error as Error).message).toContain("Tool 'GMAIL_SEND'"); |
| 36 | // The original SyntaxError is preserved as the cause for debugging. |
| 37 | expect((error as { cause?: unknown }).cause).toBeInstanceOf(SyntaxError); |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | it.each([ |
| 42 | ['a JSON array string', '[1, 2, 3]'], |
| 43 | ['a JSON number string', '42'], |
| 44 | ['a JSON string literal', '"hello"'], |
| 45 | ])('throws when the parsed JSON %s is not an object', (_label, value) => { |
| 46 | expect(() => normalizeToolArguments(value)).toThrow(ComposioInvalidToolArgumentsError); |
| 47 | }); |
| 48 | |
| 49 | it.each([ |
| 50 | ['an array', [1, 2, 3]], |
| 51 | ['a number', 42], |
| 52 | ['a boolean', true], |
| 53 | ])('throws when given %s directly', (_label, value) => { |
| 54 | expect(() => normalizeToolArguments(value)).toThrow(ComposioInvalidToolArgumentsError); |
| 55 | }); |
| 56 | |
| 57 | it('mentions the tool slug in the error message when provided', () => { |
| 58 | expect(() => normalizeToolArguments(42, 'SLACK_SEND_MESSAGE')).toThrow( |
| 59 | /Tool 'SLACK_SEND_MESSAGE'/ |
| 60 | ); |
| 61 | }); |
| 62 | |
| 63 | it('falls back to a generic label when no slug is provided', () => { |
| 64 | expect(() => normalizeToolArguments(42)).toThrow(/^.*\bTool expected arguments/); |
| 65 | }); |
| 66 | }); |