|
1 | 1 | import {
|
2 | 2 | generateDefaultValue,
|
3 | 3 | formatFieldLabel,
|
| 4 | + normalizeUnionType, |
4 | 5 | cacheToolOutputSchemas,
|
5 | 6 | getToolOutputValidator,
|
6 | 7 | validateToolOutput,
|
@@ -142,6 +143,189 @@ describe("formatFieldLabel", () => {
|
142 | 143 | });
|
143 | 144 | });
|
144 | 145 |
|
| 146 | +describe("normalizeUnionType", () => { |
| 147 | + test("normalizes anyOf with string and null to string type", () => { |
| 148 | + const schema: JsonSchemaType = { |
| 149 | + anyOf: [{ type: "string" }, { type: "null" }], |
| 150 | + description: "Optional string parameter", |
| 151 | + }; |
| 152 | + |
| 153 | + const normalized = normalizeUnionType(schema); |
| 154 | + |
| 155 | + expect(normalized.type).toBe("string"); |
| 156 | + expect(normalized.anyOf).toBeUndefined(); |
| 157 | + expect(normalized.description).toBe("Optional string parameter"); |
| 158 | + }); |
| 159 | + |
| 160 | + test("normalizes anyOf with boolean and null to boolean type", () => { |
| 161 | + const schema: JsonSchemaType = { |
| 162 | + anyOf: [{ type: "boolean" }, { type: "null" }], |
| 163 | + description: "Optional boolean parameter", |
| 164 | + }; |
| 165 | + |
| 166 | + const normalized = normalizeUnionType(schema); |
| 167 | + |
| 168 | + expect(normalized.type).toBe("boolean"); |
| 169 | + expect(normalized.anyOf).toBeUndefined(); |
| 170 | + expect(normalized.description).toBe("Optional boolean parameter"); |
| 171 | + }); |
| 172 | + |
| 173 | + test("normalizes array type with string and null to string type", () => { |
| 174 | + const schema: JsonSchemaType = { |
| 175 | + type: ["string", "null"], |
| 176 | + description: "Optional string parameter", |
| 177 | + }; |
| 178 | + |
| 179 | + const normalized = normalizeUnionType(schema); |
| 180 | + |
| 181 | + expect(normalized.type).toBe("string"); |
| 182 | + expect(normalized.description).toBe("Optional string parameter"); |
| 183 | + }); |
| 184 | + |
| 185 | + test("normalizes array type with boolean and null to boolean type", () => { |
| 186 | + const schema: JsonSchemaType = { |
| 187 | + type: ["boolean", "null"], |
| 188 | + description: "Optional boolean parameter", |
| 189 | + }; |
| 190 | + |
| 191 | + const normalized = normalizeUnionType(schema); |
| 192 | + |
| 193 | + expect(normalized.type).toBe("boolean"); |
| 194 | + expect(normalized.description).toBe("Optional boolean parameter"); |
| 195 | + }); |
| 196 | + |
| 197 | + test("normalizes anyOf with number and null to number type", () => { |
| 198 | + const schema: JsonSchemaType = { |
| 199 | + anyOf: [{ type: "number" }, { type: "null" }], |
| 200 | + description: "Optional number parameter", |
| 201 | + }; |
| 202 | + |
| 203 | + const normalized = normalizeUnionType(schema); |
| 204 | + |
| 205 | + expect(normalized.type).toBe("number"); |
| 206 | + expect(normalized.anyOf).toBeUndefined(); |
| 207 | + expect(normalized.description).toBe("Optional number parameter"); |
| 208 | + }); |
| 209 | + |
| 210 | + test("normalizes anyOf with integer and null to integer type", () => { |
| 211 | + const schema: JsonSchemaType = { |
| 212 | + anyOf: [{ type: "integer" }, { type: "null" }], |
| 213 | + description: "Optional integer parameter", |
| 214 | + }; |
| 215 | + |
| 216 | + const normalized = normalizeUnionType(schema); |
| 217 | + |
| 218 | + expect(normalized.type).toBe("integer"); |
| 219 | + expect(normalized.anyOf).toBeUndefined(); |
| 220 | + expect(normalized.description).toBe("Optional integer parameter"); |
| 221 | + }); |
| 222 | + |
| 223 | + test("normalizes array type with number and null to number type", () => { |
| 224 | + const schema: JsonSchemaType = { |
| 225 | + type: ["number", "null"], |
| 226 | + description: "Optional number parameter", |
| 227 | + }; |
| 228 | + |
| 229 | + const normalized = normalizeUnionType(schema); |
| 230 | + |
| 231 | + expect(normalized.type).toBe("number"); |
| 232 | + expect(normalized.description).toBe("Optional number parameter"); |
| 233 | + }); |
| 234 | + |
| 235 | + test("normalizes array type with integer and null to integer type", () => { |
| 236 | + const schema: JsonSchemaType = { |
| 237 | + type: ["integer", "null"], |
| 238 | + description: "Optional integer parameter", |
| 239 | + }; |
| 240 | + |
| 241 | + const normalized = normalizeUnionType(schema); |
| 242 | + |
| 243 | + expect(normalized.type).toBe("integer"); |
| 244 | + expect(normalized.description).toBe("Optional integer parameter"); |
| 245 | + }); |
| 246 | + |
| 247 | + test("handles anyOf with reversed order (null first)", () => { |
| 248 | + const schema: JsonSchemaType = { |
| 249 | + anyOf: [{ type: "null" }, { type: "string" }], |
| 250 | + }; |
| 251 | + |
| 252 | + const normalized = normalizeUnionType(schema); |
| 253 | + |
| 254 | + expect(normalized.type).toBe("string"); |
| 255 | + expect(normalized.anyOf).toBeUndefined(); |
| 256 | + }); |
| 257 | + |
| 258 | + test("leaves non-union schemas unchanged", () => { |
| 259 | + const schema: JsonSchemaType = { |
| 260 | + type: "string", |
| 261 | + description: "Regular string parameter", |
| 262 | + }; |
| 263 | + |
| 264 | + const normalized = normalizeUnionType(schema); |
| 265 | + |
| 266 | + expect(normalized).toEqual(schema); |
| 267 | + }); |
| 268 | + |
| 269 | + test("leaves anyOf with non-matching types unchanged", () => { |
| 270 | + const schema: JsonSchemaType = { |
| 271 | + anyOf: [{ type: "string" }, { type: "number" }], |
| 272 | + }; |
| 273 | + |
| 274 | + const normalized = normalizeUnionType(schema); |
| 275 | + |
| 276 | + expect(normalized).toEqual(schema); |
| 277 | + }); |
| 278 | + |
| 279 | + test("leaves anyOf with more than two types unchanged", () => { |
| 280 | + const schema: JsonSchemaType = { |
| 281 | + anyOf: [{ type: "string" }, { type: "number" }, { type: "null" }], |
| 282 | + }; |
| 283 | + |
| 284 | + const normalized = normalizeUnionType(schema); |
| 285 | + |
| 286 | + expect(normalized).toEqual(schema); |
| 287 | + }); |
| 288 | + |
| 289 | + test("leaves array type with non-matching types unchanged", () => { |
| 290 | + const schema: JsonSchemaType = { |
| 291 | + type: ["string", "number"], |
| 292 | + }; |
| 293 | + |
| 294 | + const normalized = normalizeUnionType(schema); |
| 295 | + |
| 296 | + expect(normalized).toEqual(schema); |
| 297 | + }); |
| 298 | + |
| 299 | + test("handles schemas without type or anyOf", () => { |
| 300 | + const schema: JsonSchemaType = { |
| 301 | + description: "Schema without type", |
| 302 | + }; |
| 303 | + |
| 304 | + const normalized = normalizeUnionType(schema); |
| 305 | + |
| 306 | + expect(normalized).toEqual(schema); |
| 307 | + }); |
| 308 | + |
| 309 | + test("preserves other properties when normalizing", () => { |
| 310 | + const schema: JsonSchemaType = { |
| 311 | + anyOf: [{ type: "string" }, { type: "null" }], |
| 312 | + description: "Optional string", |
| 313 | + minLength: 1, |
| 314 | + maxLength: 100, |
| 315 | + pattern: "^[a-z]+$", |
| 316 | + }; |
| 317 | + |
| 318 | + const normalized = normalizeUnionType(schema); |
| 319 | + |
| 320 | + expect(normalized.type).toBe("string"); |
| 321 | + expect(normalized.anyOf).toBeUndefined(); |
| 322 | + expect(normalized.description).toBe("Optional string"); |
| 323 | + expect(normalized.minLength).toBe(1); |
| 324 | + expect(normalized.maxLength).toBe(100); |
| 325 | + expect(normalized.pattern).toBe("^[a-z]+$"); |
| 326 | + }); |
| 327 | +}); |
| 328 | + |
145 | 329 | describe("Output Schema Validation", () => {
|
146 | 330 | const mockTools: Tool[] = [
|
147 | 331 | {
|
|
0 commit comments