File size: 3,076 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { getInputSchemaFromSourceCode } from '@/logic-function/get-input-schema-from-source-code';
import { DEFAULT_TOOL_INPUT_SCHEMA } from '@/logic-function/constants/DefaultToolInputSchema';

describe('getInputSchemaFromSourceCode', () => {
  it('should return empty input if not parameter', async () => {
    const fileContent = 'function testFunction() { return }';
    const result = await getInputSchemaFromSourceCode(fileContent);
    expect(result).toEqual(DEFAULT_TOOL_INPUT_SCHEMA);
  });
  it('should return first input if multiple parameters', async () => {
    const fileContent =
      'function testFunction(params1: {}, params2: {}) { return }';
    const result = await getInputSchemaFromSourceCode(fileContent);
    expect(result).toEqual(DEFAULT_TOOL_INPUT_SCHEMA);
  });
  it('should return empty input if wrong parameter', async () => {
    const fileContent = 'function testFunction(params: string) { return }';
    const result = await getInputSchemaFromSourceCode(fileContent);
    expect(result).toEqual(DEFAULT_TOOL_INPUT_SCHEMA);
  });
  it('should fall back to empty when the params type cannot be inferred', async () => {
    const fileContent =
      'function testFunction(params: ImportedAlias) { return }';
    const result = await getInputSchemaFromSourceCode(fileContent);
    expect(result).toEqual(DEFAULT_TOOL_INPUT_SCHEMA);
  });
  it('should infer record schemas from TwentyRecord markers', async () => {
    const fileContent = `
        function testFunction(params: {
          companies: TwentyRecord<'company-universal-identifier'>[];
        }) { return }
      `;
    const result = await getInputSchemaFromSourceCode(fileContent);
    expect(result).toEqual({
      type: 'object',
      properties: {
        companies: {
          type: 'records',
          objectUniversalIdentifier: 'company-universal-identifier',
        },
      },
    });
  });
  it('should return input from source code', async () => {
    const fileContent = `
        function testFunction(
          params: {
            param1: string;
            param2: number;
            param3: boolean;
            param4: object;
            param5: { subParam1: string };
            param6: "my" | "enum";
            param7: string[];
          }
        ): void {
          return
        }
      `;

    const result = await getInputSchemaFromSourceCode(fileContent);
    expect(result).toEqual({
      properties: {
        param1: {
          type: 'string',
        },
        param2: {
          type: 'number',
        },
        param3: {
          type: 'boolean',
        },
        param4: {
          type: 'object',
        },
        param5: {
          properties: {
            subParam1: {
              type: 'string',
            },
          },
          type: 'object',
        },
        param6: {
          enum: ['my', 'enum'],
          type: 'string',
        },
        param7: {
          items: {
            type: 'string',
          },
          type: 'array',
        },
      },
      type: 'object',
    });
  });
});