File size: 7,672 Bytes
07af8f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { convertParametersToTypes } from '../../src/utils/parameterConversion.js';

describe('Parameter Conversion Utilities', () => {
  describe('convertParametersToTypes', () => {
    it('should convert string to number when schema type is number', () => {
      const params = { count: '42' };
      const schema = {
        type: 'object',
        properties: {
          count: { type: 'number' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.count).toBe(42);
      expect(typeof result.count).toBe('number');
    });

    it('should convert string to integer when schema type is integer', () => {
      const params = { age: '25' };
      const schema = {
        type: 'object',
        properties: {
          age: { type: 'integer' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.age).toBe(25);
      expect(typeof result.age).toBe('number');
      expect(Number.isInteger(result.age)).toBe(true);
    });

    it('should convert string to boolean when schema type is boolean', () => {
      const params = { enabled: 'true', disabled: 'false', flag: '1' };
      const schema = {
        type: 'object',
        properties: {
          enabled: { type: 'boolean' },
          disabled: { type: 'boolean' },
          flag: { type: 'boolean' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.enabled).toBe(true);
      expect(result.disabled).toBe(false);
      expect(result.flag).toBe(true);
    });

    it('should convert comma-separated string to array when schema type is array', () => {
      const params = { tags: 'one,two,three' };
      const schema = {
        type: 'object',
        properties: {
          tags: { type: 'array' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(Array.isArray(result.tags)).toBe(true);
      expect(result.tags).toEqual(['one', 'two', 'three']);
    });

    it('should parse JSON string to object when schema type is object', () => {
      const params = { config: '{"key": "value", "nested": {"prop": 123}}' };
      const schema = {
        type: 'object',
        properties: {
          config: { type: 'object' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(typeof result.config).toBe('object');
      expect(result.config).toEqual({ key: 'value', nested: { prop: 123 } });
    });

    it('should keep values unchanged when they already have the correct type', () => {
      const params = { count: 42, enabled: true, tags: ['a', 'b'] };
      const schema = {
        type: 'object',
        properties: {
          count: { type: 'number' },
          enabled: { type: 'boolean' },
          tags: { type: 'array' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.count).toBe(42);
      expect(result.enabled).toBe(true);
      expect(result.tags).toEqual(['a', 'b']);
    });

    it('should keep string values unchanged when schema type is string', () => {
      const params = { name: 'John Doe' };
      const schema = {
        type: 'object',
        properties: {
          name: { type: 'string' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.name).toBe('John Doe');
      expect(typeof result.name).toBe('string');
    });

    it('should handle parameters without schema definition', () => {
      const params = { unknown: 'value' };
      const schema = {
        type: 'object',
        properties: {
          known: { type: 'string' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.unknown).toBe('value');
    });

    it('should return original params when schema has no properties', () => {
      const params = { key: 'value' };
      const schema = { type: 'object' };

      const result = convertParametersToTypes(params, schema);

      expect(result).toEqual(params);
    });

    it('should return original params when schema is null or undefined', () => {
      const params = { key: 'value' };

      const resultNull = convertParametersToTypes(params, null as any);
      const resultUndefined = convertParametersToTypes(params, undefined as any);

      expect(resultNull).toEqual(params);
      expect(resultUndefined).toEqual(params);
    });

    it('should handle invalid number conversion gracefully', () => {
      const params = { count: 'not-a-number' };
      const schema = {
        type: 'object',
        properties: {
          count: { type: 'number' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      // When conversion fails, it should keep original value
      expect(result.count).toBe('not-a-number');
    });

    it('should handle invalid JSON string for object gracefully', () => {
      const params = { config: '{invalid json}' };
      const schema = {
        type: 'object',
        properties: {
          config: { type: 'object' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      // When JSON parsing fails, it should keep original value
      expect(result.config).toBe('{invalid json}');
    });

    it('should handle mixed parameter types correctly', () => {
      const params = {
        name: 'Test',
        count: '10',
        price: '19.99',
        enabled: 'true',
        tags: 'tag1,tag2',
        config: '{"nested": true}',
      };
      const schema = {
        type: 'object',
        properties: {
          name: { type: 'string' },
          count: { type: 'integer' },
          price: { type: 'number' },
          enabled: { type: 'boolean' },
          tags: { type: 'array' },
          config: { type: 'object' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.name).toBe('Test');
      expect(result.count).toBe(10);
      expect(result.price).toBe(19.99);
      expect(result.enabled).toBe(true);
      expect(result.tags).toEqual(['tag1', 'tag2']);
      expect(result.config).toEqual({ nested: true });
    });

    it('should handle empty string values', () => {
      const params = { name: '', count: '', enabled: '' };
      const schema = {
        type: 'object',
        properties: {
          name: { type: 'string' },
          count: { type: 'number' },
          enabled: { type: 'boolean' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.name).toBe('');
      // Empty string should remain as empty string for number (NaN check keeps original)
      expect(result.count).toBe('');
      // Empty string converts to false for boolean
      expect(result.enabled).toBe(false);
    });

    it('should handle array that is already an array', () => {
      const params = { tags: ['existing', 'array'] };
      const schema = {
        type: 'object',
        properties: {
          tags: { type: 'array' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.tags).toEqual(['existing', 'array']);
    });

    it('should handle object that is already an object', () => {
      const params = { config: { key: 'value' } };
      const schema = {
        type: 'object',
        properties: {
          config: { type: 'object' },
        },
      };

      const result = convertParametersToTypes(params, schema);

      expect(result.config).toEqual({ key: 'value' });
    });
  });
});