e2e интеграционные тесты

This commit is contained in:
2025-10-28 10:51:14 +03:00
parent 6bffe6a9e1
commit 4e2d0f03de
9 changed files with 191 additions and 85 deletions

View File

@@ -38,7 +38,7 @@ describe('E2E Generation', () => {
});
describe('полный цикл генерации', () => {
test.skip('CLI генерация → создание файла → импорт → использование', async () => {
test('CLI генерация → создание файла → импорт → использование', async () => {
const outputPath = join(tempDir, 'output');
// 1. Генерация через CLI
@@ -135,10 +135,91 @@ describe('E2E Generation', () => {
const exists = await fileExists(generatedFile);
expect(exists).toBe(true);
}, 60000);
test('генерация из HTTP URL', async () => {
const outputPath = join(tempDir, 'output');
// Используем публичный OpenAPI spec
const { exitCode } = await execa('bun', [
'run',
CLI_PATH,
'--input',
'https://petstore3.swagger.io/api/v3/openapi.json',
'--output',
outputPath,
'--name',
'PetStore',
]);
expect(exitCode).toBe(0);
const generatedFile = join(outputPath, 'PetStore.ts');
const exists = await fileExists(generatedFile);
expect(exists).toBe(true);
// Проверяем что файл не пустой
const content = await readTextFile(generatedFile);
expect(content.length).toBeGreaterThan(1000);
}, 60000);
test('генерация с флагом --swr', async () => {
const outputPath = join(tempDir, 'output');
const { exitCode } = await execa('bun', [
'run',
CLI_PATH,
'--input',
FIXTURES.VALID,
'--output',
outputPath,
'--name',
'SwrApi',
'--swr',
]);
expect(exitCode).toBe(0);
const generatedFile = join(outputPath, 'SwrApi.ts');
const content = await readTextFile(generatedFile);
// Проверяем наличие импорта useSWR
expect(content).toContain('import useSWR from "swr"');
// Проверяем наличие use* хуков для GET запросов
expect(content).toContain('useGetAll');
expect(content).toContain('useGetById');
}, 30000);
test('генерация без флага --swr не содержит хуки', async () => {
const outputPath = join(tempDir, 'output');
const { exitCode } = await execa('bun', [
'run',
CLI_PATH,
'--input',
FIXTURES.VALID,
'--output',
outputPath,
'--name',
'NoSwrApi',
]);
expect(exitCode).toBe(0);
const generatedFile = join(outputPath, 'NoSwrApi.ts');
const content = await readTextFile(generatedFile);
// Проверяем отсутствие импорта useSWR
expect(content).not.toContain('import useSWR from "swr"');
// Проверяем отсутствие use* хуков
expect(content).not.toContain('useGetAll');
expect(content).not.toContain('useGetById');
}, 30000);
});
describe('HTTP запросы с mock сервером', () => {
test.skip('GET запрос без параметров', async () => {
test('GET запрос без параметров', async () => {
const outputPath = join(tempDir, 'output');
await execa('bun', [
@@ -152,26 +233,18 @@ describe('E2E Generation', () => {
'TestApi',
]);
// Создаем тестовый файл для вызова API
const testFile = join(tempDir, 'test-get.ts');
const testCode = `
import { Api } from '${join(outputPath, 'TestApi.ts')}';
const api = new Api();
const result = await api.user.getAll();
console.log(JSON.stringify(result));
`;
// Динамически импортируем сгенерированный API
const generatedFile = join(outputPath, 'TestApi.ts');
const { Api } = await import(generatedFile);
await Bun.write(testFile, testCode);
const api = new Api();
const result = await api.users.getAll();
const { stdout } = await execa('bun', ['run', testFile]);
const data = JSON.parse(stdout);
expect(Array.isArray(data)).toBe(true);
expect(data.length).toBe(2);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(2);
}, 60000);
test.skip('POST запрос с body', async () => {
test('POST запрос с body', async () => {
const outputPath = join(tempDir, 'output');
await execa('bun', [
@@ -185,28 +258,21 @@ describe('E2E Generation', () => {
'TestApi',
]);
const testFile = join(tempDir, 'test-post.ts');
const testCode = `
import { Api } from '${join(outputPath, 'TestApi.ts')}';
const api = new Api();
const result = await api.user.create({
email: 'new@example.com',
password: 'password123'
});
console.log(JSON.stringify(result));
`;
// Динамически импортируем сгенерированный API
const generatedFile = join(outputPath, 'TestApi.ts');
const { Api } = await import(generatedFile);
await Bun.write(testFile, testCode);
const api = new Api();
const result = await api.users.create({
email: 'new@example.com',
password: 'password123'
});
const { stdout } = await execa('bun', ['run', testFile]);
const data = JSON.parse(stdout);
expect(data.id).toBe('3');
expect(data.email).toBe('new@example.com');
expect(result.id).toBe('3');
expect(result.email).toBe('new@example.com');
}, 60000);
test.skip('обработка 404 статуса', async () => {
test('обработка 404 статуса', async () => {
const outputPath = join(tempDir, 'output');
await execa('bun', [
@@ -226,7 +292,7 @@ describe('E2E Generation', () => {
const api = new Api();
try {
await api.user.getById('999');
await api.users.getById('999');
} catch (error) {
console.log('error');
}
@@ -238,7 +304,7 @@ describe('E2E Generation', () => {
expect(stdout).toContain('error');
}, 60000);
test.skip('Bearer token authentication', async () => {
test('Bearer token authentication', async () => {
const outputPath = join(tempDir, 'output');
await execa('bun', [
@@ -252,32 +318,38 @@ describe('E2E Generation', () => {
'AuthApi',
]);
const testFile = join(tempDir, 'test-auth.ts');
const testCode = `
import { Api } from '${join(outputPath, 'AuthApi.ts')}';
const api = new Api();
// Логин
const { token } = await api.auth.login({
email: 'test@example.com',
password: 'password'
});
// Установка токена
api.instance.setSecurityData(token);
// Запрос с токеном
const profile = await api.profile.get();
console.log(JSON.stringify(profile));
`;
// Динамически импортируем сгенерированный API
const generatedFile = join(outputPath, 'AuthApi.ts');
const { Api, HttpClient } = await import(generatedFile);
await Bun.write(testFile, testCode);
// Создаем HttpClient с securityWorker для добавления Bearer токена
const httpClient = new HttpClient({
securityWorker: (securityData: string | null) => {
if (securityData) {
return {
headers: {
Authorization: `Bearer ${securityData}`
}
};
}
}
});
const { stdout } = await execa('bun', ['run', testFile]);
const data = JSON.parse(stdout);
const api = new Api(httpClient);
expect(data.email).toBe('test@example.com');
// Логин
const loginResult = await api.auth.login({
email: 'test@example.com',
password: 'password'
});
// Установка токена
httpClient.setSecurityData(loginResult.token);
// Запрос с токеном
const profile = await api.profile.get();
expect(profile.email).toBe('test@example.com');
}, 60000);
});
});