77 lines
2.6 KiB
TypeScript
77 lines
2.6 KiB
TypeScript
import * as path from 'path';
|
|
import * as fs from 'fs';
|
|
import * as os from 'os';
|
|
|
|
describe('CLI Integration', () => {
|
|
const tempDir = path.join(os.tmpdir(), 'type-from-json-cli-test');
|
|
|
|
beforeAll(() => {
|
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
});
|
|
|
|
afterAll(() => {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('should process a JSON file and generate declaration', async () => {
|
|
const jsonPath = path.join(tempDir, 'cli-test.json');
|
|
const dtsPath = path.join(tempDir, 'cli-test.d.ts');
|
|
|
|
fs.writeFileSync(jsonPath, JSON.stringify({
|
|
id: 1,
|
|
name: 'Test User',
|
|
active: true
|
|
}));
|
|
|
|
const { readJsonFile, writeDeclarationFile } = await import('../src/file-watcher.js');
|
|
const { TypeInferrer } = await import('../src/type-inferrer.js');
|
|
const { DeclarationGenerator } = await import('../src/declaration-generator.js');
|
|
|
|
const data = await readJsonFile(jsonPath);
|
|
const inferrer = new TypeInferrer({ rootName: 'CliTest' });
|
|
const result = inferrer.infer(data);
|
|
|
|
const generator = new DeclarationGenerator();
|
|
const declaration = generator.generate(result.types);
|
|
|
|
await writeDeclarationFile(dtsPath, declaration);
|
|
|
|
expect(fs.existsSync(dtsPath)).toBe(true);
|
|
|
|
const content = fs.readFileSync(dtsPath, 'utf-8');
|
|
expect(content).toContain('export interface CliTest');
|
|
expect(content).toContain('id: number');
|
|
expect(content).toContain('name: string');
|
|
expect(content).toContain('active: boolean');
|
|
});
|
|
|
|
it('should handle multiple JSON samples', async () => {
|
|
const jsonPath = path.join(tempDir, 'multi-sample.json');
|
|
const dtsPath = path.join(tempDir, 'multi-sample.d.ts');
|
|
|
|
fs.writeFileSync(jsonPath, JSON.stringify([
|
|
{ id: 1, status: 'active' },
|
|
{ id: 2, status: 'pending' }
|
|
]));
|
|
|
|
const { readJsonFile, writeDeclarationFile } = await import('../src/file-watcher.js');
|
|
const { extractJsonSamplesFromFile } = await import('../src/file-watcher.js');
|
|
const { TypeInferrer } = await import('../src/type-inferrer.js');
|
|
const { DeclarationGenerator } = await import('../src/declaration-generator.js');
|
|
|
|
const samples = extractJsonSamplesFromFile(jsonPath);
|
|
const inferrer = new TypeInferrer({ rootName: 'MultiSample' });
|
|
const result = inferrer.inferFromMultiple(samples);
|
|
|
|
const generator = new DeclarationGenerator();
|
|
const declaration = generator.generate(result.types);
|
|
|
|
await writeDeclarationFile(dtsPath, declaration);
|
|
|
|
expect(fs.existsSync(dtsPath)).toBe(true);
|
|
|
|
const content = fs.readFileSync(dtsPath, 'utf-8');
|
|
expect(content).toContain('export interface MultiSample');
|
|
});
|
|
});
|