80 lines
2.6 KiB
TypeScript
80 lines
2.6 KiB
TypeScript
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');
|
|
});
|
|
});
|
|
});
|