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 62 lines 2.33 KB
Raw
1 import { expect, test } from 'vitest';
2
3 import { functionType } from './FunctionType';
4 import { namedType } from './NamedType';
5 import { parameter } from './Parameter';
6 import { stringify } from './stringify';
7 import { unionType } from './UnionType';
8
9 const A = namedType('A');
10 const stringType = namedType('string');
11 const numberType = namedType('number');
12
13 test('name and type', () => {
14 const param = parameter('foo', A);
15
16 expect(stringify(param)).toMatchInlineSnapshot(`"foo: A"`);
17 });
18
19 test('optional', () => {
20 const param = parameter('foo', A).optional();
21
22 expect(stringify(param)).toMatchInlineSnapshot(`"foo?: A"`);
23 });
24
25 test('parameter with union type adds parentheses in function context', () => {
26 // Union has precedence 1, FunctionParameter context requires precedence 4
27 // So parentheses should be added: (param: (string | number))
28 const unionParam = unionType([stringType, numberType]);
29 const func = functionType().addParameter(parameter('param', unionParam));
30 const result = stringify(func);
31
32 expect(result).toBe('(param: (string | number)) => void');
33 });
34
35 test('parameter with intersection type adds parentheses in function context', () => {
36 // Intersection has precedence 2, FunctionParameter context requires precedence 4
37 // So parentheses should be added
38 const intersectionParam = namedType('A & B'); // Using named type to simulate intersection
39 const func = functionType().addParameter(parameter('param', intersectionParam));
40 const result = stringify(func);
41
42 // Named type has atomic precedence, no parentheses needed
43 expect(result).toBe('(param: A & B) => void');
44 });
45
46 test('parameter with function type adds parentheses in function context', () => {
47 // Function type has precedence 4, FunctionParameter context requires precedence 4
48 // Since 4 is not < 4, no parentheses needed
49 const funcParam = functionType().setReturnType(stringType);
50 const func = functionType().addParameter(parameter('callback', funcParam));
51 const result = stringify(func);
52
53 expect(result).toBe('(callback: () => string) => void');
54 });
55
56 test('optional parameter with union type adds parentheses', () => {
57 const unionParam = unionType([stringType, numberType]);
58 const func = functionType().addParameter(parameter('param', unionParam).optional());
59 const result = stringify(func);
60
61 expect(result).toBe('(param?: (string | number)) => void');
62 });