From b6d14edd55583023f60789174d1d7844e54bfb09 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sat, 31 Jan 2026 12:26:01 +0000 Subject: [PATCH] Initial upload with CI/CD workflow --- tests/union-detection.test.ts | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/union-detection.test.ts diff --git a/tests/union-detection.test.ts b/tests/union-detection.test.ts new file mode 100644 index 0000000..6b810dd --- /dev/null +++ b/tests/union-detection.test.ts @@ -0,0 +1,79 @@ +import { TypeInferrer } from '../src/type-inferrer.js'; +import { DeclarationGenerator } from '../src/declaration-generator.js'; + +describe('Union Type Detection', () => { + describe('TypeInferrer union detection', () => { + it('should detect union types when field has different types across samples', () => { + const inferrer = new TypeInferrer({ rootName: 'Test' }); + const result = inferrer.inferFromMultiple([ + { value: 1 }, + { value: 'two' }, + ]); + expect(result.types.length).toBeGreaterThan(0); + }); + + it('should detect union with null type', () => { + const inferrer = new TypeInferrer({ rootName: 'Test' }); + const result = inferrer.inferFromMultiple([ + { value: 'string' }, + { value: null }, + ]); + expect(result.types.length).toBeGreaterThan(0); + }); + + it('should handle multiple union members', () => { + const inferrer = new TypeInferrer({ rootName: 'Test' }); + const result = inferrer.inferFromMultiple([ + { id: 1 }, + { id: 'abc' }, + { id: true }, + ]); + expect(result.types.length).toBeGreaterThan(0); + }); + + it('should not create union for single type', () => { + const inferrer = new TypeInferrer({ rootName: 'Test' }); + const result = inferrer.inferFromMultiple([ + { value: 1 }, + { value: 2 }, + { value: 3 }, + ]); + expect(result.types.length).toBeGreaterThan(0); + }); + + it('should detect union in nested objects', () => { + const inferrer = new TypeInferrer({ rootName: 'Test' }); + const result = inferrer.inferFromMultiple([ + { nested: { id: 1 } }, + { nested: { id: 'str' } }, + ]); + expect(result.types.length).toBeGreaterThan(0); + }); + + it('should detect union in arrays', () => { + const inferrer = new TypeInferrer({ rootName: 'Test' }); + const result = inferrer.inferFromMultiple([ + { items: [1, 2] }, + { items: ['a', 'b'] }, + ]); + expect(result.types.length).toBeGreaterThan(0); + }); + }); + + describe('Integration: infer and generate', () => { + it('should generate valid union type declarations', () => { + const inferrer = new TypeInferrer({ rootName: 'Response' }); + const result = inferrer.inferFromMultiple([ + { id: 1, type: 'user' }, + { id: '2', type: 'admin' }, + ]); + + const generator = new DeclarationGenerator(); + const declaration = generator.generate(result.types); + + expect(declaration).toContain('export interface Response'); + expect(declaration).toContain('id'); + expect(declaration).toContain('type'); + }); + }); +});