/**
* WordPress Client Unit Tests
*/
import { WordPressClient } from '../../src/services/wordpress-client.js';
import { WordPressConfig } from '../../src/types/wordpress.js';
import axios from 'axios';
// Mock axios
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
describe('WordPressClient', () => {
let client: WordPressClient;
let config: WordPressConfig;
beforeEach(() => {
config = {
baseUrl: 'https://test-site.com',
username: 'testuser',
applicationPassword: 'testpass'
};
client = new WordPressClient(config);
// Reset mocks
jest.clearAllMocks();
});
describe('constructor', () => {
it('should create client with correct configuration', () => {
expect(client).toBeInstanceOf(WordPressClient);
});
it('should set up authentication header', () => {
const expectedAuth = Buffer.from(`${config.username}:${config.applicationPassword}`).toString('base64');
expect(mockedAxios.create).toHaveBeenCalledWith({
baseURL: config.baseUrl,
timeout: 30000,
headers: {
'Authorization': `Basic ${expectedAuth}`,
'Content-Type': 'application/json'
}
});
});
});
describe('getAxiosInstance', () => {
it('should return axios instance', () => {
const instance = client.getAxiosInstance();
expect(instance).toBeDefined();
});
});
describe('request interceptor', () => {
it('should log request details in debug mode', () => {
const mockRequest = {
method: 'GET',
url: '/wp-json/wp/v2/posts',
headers: {}
};
// Mock process.env.DEBUG_MODE
const originalDebugMode = process.env.DEBUG_MODE;
process.env.DEBUG_MODE = 'true';
// Test would require more complex setup to test interceptors
// This is a placeholder for the actual test
process.env.DEBUG_MODE = originalDebugMode;
});
});
describe('response interceptor', () => {
it('should handle successful responses', () => {
const mockResponse = {
data: { id: 1, title: 'Test Post' },
status: 200,
statusText: 'OK'
};
// Test would require more complex setup to test interceptors
// This is a placeholder for the actual test
});
it('should handle error responses', () => {
const mockError = {
response: {
status: 404,
statusText: 'Not Found',
data: { message: 'Post not found' }
}
};
// Test would require more complex setup to test interceptors
// This is a placeholder for the actual test
});
});
});