feat: Refactor codebase, improve types, attempt test fixes

This commit is contained in:
leonardsellem
2025-03-31 11:20:05 +02:00
parent d16ad72b22
commit ecd9133437
38 changed files with 829 additions and 811 deletions

View File

@@ -2,33 +2,40 @@
* N8nApiClient unit tests
*/
import '@jest/globals';
import axios from 'axios';
import { N8nApiClient } from '../../../src/api/client.js';
import { EnvConfig } from '../../../src/config/environment.js';
import { N8nApiError } from '../../../src/errors/index.js';
import { createMockAxiosInstance, createMockAxiosResponse } from '../../mocks/axios-mock.js';
import { mockApiResponses } from '../../mocks/n8n-fixtures.js';
import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals'; // Explicit import
import axios, { AxiosInstance } from 'axios'; // Import AxiosInstance type
import { N8nApiClient } from '../../../src/api/client.js'; // Add .js
import { EnvConfig } from '../../../src/config/environment.js'; // Add .js
import { N8nApiError } from '../../../src/errors/index.js'; // Add .js
import { createMockAxiosInstance, createMockAxiosResponse } from '../../mocks/axios-mock.js'; // Add .js
import { mockApiResponses } from '../../mocks/n8n-fixtures.js'; // Add .js
// We will spy on axios.create instead of mocking the whole module
// jest.mock('axios');
// const mockedAxios = axios as jest.Mocked<typeof axios>;
// Mock axios
jest.mock('axios', () => ({
create: jest.fn(),
}));
describe('N8nApiClient', () => {
// Mock configuration
const mockConfig: EnvConfig = {
n8nApiUrl: 'https://n8n.example.com/api/v1',
n8nApiKey: 'test-api-key',
n8nWebhookUsername: 'test-user', // Added missing property
n8nWebhookPassword: 'test-password', // Added missing property
debug: false,
};
// Define a type for the mock axios instance based on axios-mock.ts
type MockAxiosInstance = ReturnType<typeof createMockAxiosInstance>;
// Mock axios instance
let mockAxios;
let mockAxios: MockAxiosInstance;
beforeEach(() => {
// Create the mock instance
mockAxios = createMockAxiosInstance();
(axios.create as jest.Mock).mockReturnValue(mockAxios);
// Spy on axios.create and mock its return value
jest.spyOn(axios, 'create').mockReturnValue(mockAxios as any);
});
afterEach(() => {
@@ -42,7 +49,7 @@ describe('N8nApiClient', () => {
new N8nApiClient(mockConfig);
// Assert
expect(axios.create).toHaveBeenCalledWith({
expect(axios.create).toHaveBeenCalledWith({ // Check the spy
baseURL: mockConfig.n8nApiUrl,
headers: {
'X-N8N-API-KEY': mockConfig.n8nApiKey,
@@ -80,6 +87,7 @@ describe('N8nApiClient', () => {
const client = new N8nApiClient(mockConfig);
mockAxios.addMockResponse('get', '/workflows', {
status: 200,
statusText: 'OK', // Added statusText
data: { data: [] },
});
@@ -92,6 +100,7 @@ describe('N8nApiClient', () => {
const client = new N8nApiClient(mockConfig);
mockAxios.addMockResponse('get', '/workflows', {
status: 500,
statusText: 'Internal Server Error', // Added statusText
data: { message: 'Server error' },
});
@@ -116,6 +125,7 @@ describe('N8nApiClient', () => {
const mockWorkflows = mockApiResponses.workflows.list;
mockAxios.addMockResponse('get', '/workflows', {
status: 200,
statusText: 'OK', // Added statusText
data: mockWorkflows,
});
@@ -132,6 +142,7 @@ describe('N8nApiClient', () => {
const client = new N8nApiClient(mockConfig);
mockAxios.addMockResponse('get', '/workflows', {
status: 200,
statusText: 'OK', // Added statusText
data: {},
});
@@ -160,6 +171,7 @@ describe('N8nApiClient', () => {
const mockWorkflow = mockApiResponses.workflows.single(workflowId);
mockAxios.addMockResponse('get', `/workflows/${workflowId}`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockWorkflow,
});
@@ -193,6 +205,7 @@ describe('N8nApiClient', () => {
mockAxios.addMockResponse('post', `/workflows/${workflowId}/execute`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockResponse,
});
@@ -214,6 +227,7 @@ describe('N8nApiClient', () => {
mockAxios.addMockResponse('post', '/workflows', {
status: 200,
statusText: 'OK', // Added statusText
data: mockResponse,
});
@@ -236,6 +250,7 @@ describe('N8nApiClient', () => {
mockAxios.addMockResponse('put', `/workflows/${workflowId}`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockResponse,
});
@@ -257,6 +272,7 @@ describe('N8nApiClient', () => {
mockAxios.addMockResponse('delete', `/workflows/${workflowId}`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockResponse,
});
@@ -278,6 +294,7 @@ describe('N8nApiClient', () => {
mockAxios.addMockResponse('post', `/workflows/${workflowId}/activate`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockResponse,
});
@@ -299,6 +316,7 @@ describe('N8nApiClient', () => {
mockAxios.addMockResponse('post', `/workflows/${workflowId}/deactivate`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockResponse,
});
@@ -318,6 +336,7 @@ describe('N8nApiClient', () => {
const mockExecutions = mockApiResponses.executions.list;
mockAxios.addMockResponse('get', '/executions', {
status: 200,
statusText: 'OK', // Added statusText
data: mockExecutions,
});
@@ -338,6 +357,7 @@ describe('N8nApiClient', () => {
const mockExecution = mockApiResponses.executions.single(executionId);
mockAxios.addMockResponse('get', `/executions/${executionId}`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockExecution,
});
@@ -359,6 +379,7 @@ describe('N8nApiClient', () => {
mockAxios.addMockResponse('delete', `/executions/${executionId}`, {
status: 200,
statusText: 'OK', // Added statusText
data: mockResponse,
});

View File

@@ -1,54 +0,0 @@
/**
* Simple HTTP client tests without complex dependencies
*/
import { describe, it, expect } from '@jest/globals';
// Create a simple HTTP client class to test
class SimpleHttpClient {
constructor(private baseUrl: string, private apiKey: string) {}
getBaseUrl(): string {
return this.baseUrl;
}
getApiKey(): string {
return this.apiKey;
}
buildAuthHeader(): Record<string, string> {
return {
'X-N8N-API-KEY': this.apiKey
};
}
formatUrl(path: string): string {
return `${this.baseUrl}${path.startsWith('/') ? path : '/' + path}`;
}
}
describe('SimpleHttpClient', () => {
it('should store baseUrl and apiKey properly', () => {
const baseUrl = 'https://n8n.example.com/api/v1';
const apiKey = 'test-api-key';
const client = new SimpleHttpClient(baseUrl, apiKey);
expect(client.getBaseUrl()).toBe(baseUrl);
expect(client.getApiKey()).toBe(apiKey);
});
it('should create proper auth headers', () => {
const client = new SimpleHttpClient('https://n8n.example.com/api/v1', 'test-api-key');
const headers = client.buildAuthHeader();
expect(headers).toEqual({ 'X-N8N-API-KEY': 'test-api-key' });
});
it('should format URLs correctly', () => {
const baseUrl = 'https://n8n.example.com/api/v1';
const client = new SimpleHttpClient(baseUrl, 'test-api-key');
expect(client.formatUrl('workflows')).toBe(`${baseUrl}/workflows`);
expect(client.formatUrl('/workflows')).toBe(`${baseUrl}/workflows`);
});
});