import { describe, test, expect, beforeEach, afterEach } from 'bun:test'; import { generate } from '../../src/generator.js'; import { setupTest } from '../helpers/setup.js'; import { FIXTURES } from '../helpers/fixtures.js'; import { join } from 'path'; import { fileExists, readTextFile } from '../../src/utils/file.js'; import type { GeneratorConfig } from '../../src/config.js'; describe('Generator', () => { let tempDir: string; let cleanup: () => Promise; beforeEach(async () => { const setup = await setupTest(); tempDir = setup.tempDir; cleanup = setup.cleanup; }); afterEach(async () => { await cleanup(); }); describe('корректная генерация', () => { test('должен создать выходной файл', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.MINIMAL, outputPath, fileName: 'TestApi', }; await generate(config); const generatedFile = join(outputPath, 'TestApi.ts'); const exists = await fileExists(generatedFile); expect(exists).toBe(true); }, 30000); test('должен добавлять русскую подпись в generated файлы', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.MINIMAL, outputPath, fileName: 'TestApi', }; await generate(config); const generatedFile = join(outputPath, 'TestApi.ts'); const content = await readTextFile(generatedFile); expect(content.startsWith('/* eslint-disable */\n/* tslint:disable */\n// @ts-nocheck\n\n/*')).toBe(true); expect(content).toContain('АВТОМАТИЧЕСКИ СГЕНЕРИРОВАННЫЙ ФАЙЛ'); expect(content).toContain('Не редактируйте вручную: изменения будут перезаписаны.'); expect(content).toContain('Генератор: @gromlab/api-codegen'); expect(content).toContain('Репозиторий: https://gromlab.ru/gromov/api-codegen'); expect(content).not.toContain('THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API'); }, 30000); test('должен добавлять русскую подпись в split режиме', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.MINIMAL, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const generatedFile = join(outputPath, 'index.ts'); const content = await readTextFile(generatedFile); expect(content.startsWith('/* eslint-disable */\n/* tslint:disable */\n// @ts-nocheck\n\n/*')).toBe(true); expect(content).toContain('АВТОМАТИЧЕСКИ СГЕНЕРИРОВАННЫЙ ФАЙЛ'); expect(content).toContain('Не редактируйте вручную: изменения будут перезаписаны.'); expect(content).toContain('Генератор: @gromlab/api-codegen'); expect(content).not.toContain('THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API'); }, 30000); test('должен генерировать корректную структуру кода', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.MINIMAL, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const indexFile = join(outputPath, 'index.ts'); const httpClientFile = join(outputPath, 'http-client.ts'); const indexContent = await readTextFile(indexFile); const httpClientContent = await readTextFile(httpClientFile); // Проверяем наличие основных элементов expect(indexContent).toContain('createApiClient'); expect(httpClientContent).toContain('export class HttpClient'); }, 30000); test('должен обработать все HTTP методы', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.VALID, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const operationsIndex = join(outputPath, 'operations', 'index.ts'); const content = await readTextFile(operationsIndex); // Проверяем что методы UserController переименованы // UserController_getAll -> getAll // UserController_create -> create expect(content).toContain('getAll'); expect(content).toContain('create'); expect(content).toContain('getById'); expect(content).toContain('update'); expect(content).toContain('deleteUsersId'); }, 30000); test('должен генерировать типы для request и response', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.VALID, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const generatedFile = join(outputPath, 'data-contracts.ts'); const content = await readTextFile(generatedFile); // Проверяем наличие типов expect(content).toContain('User'); expect(content).toContain('CreateUserDto'); expect(content).toContain('UpdateUserDto'); }, 30000); test('должен генерировать enum типы', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.VALID, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const generatedFile = join(outputPath, 'data-contracts.ts'); const content = await readTextFile(generatedFile); // Проверяем наличие enum expect(content).toContain('type UserRole'); expect(content).toContain('admin'); expect(content).toContain('user'); expect(content).toContain('guest'); }, 30000); test('должен обработать Bearer authentication', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.WITH_AUTH, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const generatedFile = join(outputPath, 'http-client.ts'); const content = await readTextFile(generatedFile); // Проверяем наличие методов для работы с токеном expect(content).toContain('setSecurityData'); }, 30000); test('должен использовать baseUrl из servers', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.VALID, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const generatedFile = join(outputPath, 'http-client.ts'); const content = await readTextFile(generatedFile); // Проверяем что baseUrl установлен expect(content).toContain('https://api.example.com'); }, 30000); test('должен применять хук onFormatRouteName', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.VALID, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const generatedFile = join(outputPath, 'operations', 'index.ts'); const content = await readTextFile(generatedFile); // Проверяем что "Controller" удален из имен методов expect(content).not.toContain('UserControllerGetAll'); expect(content).not.toContain('UserControllerCreate'); // Проверяем что методы названы корректно expect(content).toContain('getAll'); expect(content).toContain('create'); }, 30000); }); describe('edge cases', () => { test('должен обработать пустую спецификацию', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.EMPTY, outputPath, fileName: 'TestApi', }; // Генерация должна пройти успешно await generate(config); const generatedFile = join(outputPath, 'TestApi.ts'); const exists = await fileExists(generatedFile); expect(exists).toBe(true); }, 30000); test('должен обработать минимальную спецификацию', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.MINIMAL, outputPath, fileName: 'TestApi', }; await generate(config); const generatedFile = join(outputPath, 'TestApi.ts'); const exists = await fileExists(generatedFile); expect(exists).toBe(true); }, 30000); test('должен обработать сложную спецификацию', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.COMPLEX, outputPath, fileName: 'TestApi', mode: 'split', }; await generate(config); const generatedFile = join(outputPath, 'operations', 'index.ts'); const content = await readTextFile(generatedFile); // Проверяем что все контроллеры присутствуют expect(content).toContain('list'); // ProductController_list -> list expect(content).toContain('create'); expect(content).toContain('getById'); }, 30000); test('должен обработать Unicode символы', async () => { const outputPath = join(tempDir, 'output'); const config: GeneratorConfig = { inputPath: FIXTURES.EDGE_CASES, outputPath, fileName: 'TestApi', }; // Генерация должна пройти успешно даже с Unicode await generate(config); const generatedFile = join(outputPath, 'TestApi.ts'); const exists = await fileExists(generatedFile); expect(exists).toBe(true); }, 30000); }); });