Initial upload with CI/CD workflow
Some checks failed
CI / test (push) Has been cancelled

This commit is contained in:
2026-01-31 12:26:01 +00:00
parent 4569f5855b
commit b6d14edd55

View File

@@ -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');
});
});
});