/**
* WordPress Tools Unit Tests
*/
import { createWordPressTools } from '../../src/tools/wordpress-tools.js';
import { WordPressService } from '../../src/services/wordpress/wordpress-service.js';
// Mock the WordPressService
jest.mock('../../src/services/wordpress/wordpress-service.js');
describe('WordPress Tools', () => {
let mockWordPressService: jest.Mocked<WordPressService>;
let tools: any[];
beforeEach(() => {
// Create mock WordPressService
mockWordPressService = {
posts: {
listPosts: jest.fn(),
getPost: jest.fn(),
createPost: jest.fn(),
updatePost: jest.fn(),
deletePost: jest.fn(),
searchPosts: jest.fn()
},
pages: {
listPages: jest.fn(),
getPage: jest.fn(),
createPage: jest.fn(),
updatePage: jest.fn(),
deletePage: jest.fn(),
getPageIdBySlug: jest.fn(),
listAllContent: jest.fn()
},
media: {
listMedia: jest.fn(),
getMedia: jest.fn(),
uploadMedia: jest.fn()
},
users: {
listUsers: jest.fn(),
getUser: jest.fn(),
createUser: jest.fn(),
updateUser: jest.fn(),
deleteUser: jest.fn(),
searchUsers: jest.fn()
},
taxonomies: {
getCategories: jest.fn(),
getCategory: jest.fn(),
createCategory: jest.fn(),
updateCategory: jest.fn(),
deleteCategory: jest.fn(),
getTags: jest.fn(),
getTag: jest.fn(),
createTag: jest.fn(),
updateTag: jest.fn(),
deleteTag: jest.fn()
}
} as any;
tools = createWordPressTools(mockWordPressService);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('get_posts tool', () => {
it('should call listPosts with correct parameters', async () => {
const mockResponse = [{ id: 1, title: { rendered: 'Test Post' } }];
mockWordPressService.posts.listPosts.mockResolvedValue(mockResponse);
const getPostsTool = tools.find(tool => tool.name === 'get_posts');
expect(getPostsTool).toBeDefined();
const result = await getPostsTool.handler({
per_page: 5,
status: 'publish',
search: 'test'
});
expect(mockWordPressService.posts.listPosts).toHaveBeenCalledWith({
per_page: 5,
status: 'publish',
search: 'test'
});
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
it('should handle errors in get_posts', async () => {
const mockError = new Error('API Error');
mockWordPressService.posts.listPosts.mockRejectedValue(mockError);
const getPostsTool = tools.find(tool => tool.name === 'get_posts');
await expect(getPostsTool.handler({})).rejects.toThrow('API Error');
});
});
describe('get_post tool', () => {
it('should call getPost with correct ID', async () => {
const mockResponse = { id: 1, title: { rendered: 'Test Post' } };
mockWordPressService.posts.getPost.mockResolvedValue(mockResponse);
const getPostTool = tools.find(tool => tool.name === 'get_post');
const result = await getPostTool.handler({ id: 1 });
expect(mockWordPressService.posts.getPost).toHaveBeenCalledWith(1);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('create_post tool', () => {
it('should call createPost with correct data', async () => {
const postData = {
title: 'New Post',
content: 'Post content',
status: 'publish'
};
const mockResponse = { id: 2, title: { rendered: 'New Post' } };
mockWordPressService.posts.createPost.mockResolvedValue(mockResponse);
const createPostTool = tools.find(tool => tool.name === 'create_post');
const result = await createPostTool.handler(postData);
expect(mockWordPressService.posts.createPost).toHaveBeenCalledWith(postData);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('update_post tool', () => {
it('should call updatePost with correct parameters', async () => {
const updateData = {
title: 'Updated Post',
content: 'Updated content'
};
const mockResponse = { id: 1, title: { rendered: 'Updated Post' } };
mockWordPressService.posts.updatePost.mockResolvedValue(mockResponse);
const updatePostTool = tools.find(tool => tool.name === 'update_post');
const result = await updatePostTool.handler({ id: 1, ...updateData });
expect(mockWordPressService.posts.updatePost).toHaveBeenCalledWith(1, updateData);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('delete_post tool', () => {
it('should call deletePost with correct parameters', async () => {
const mockResponse = { deleted: true };
mockWordPressService.posts.deletePost.mockResolvedValue(mockResponse);
const deletePostTool = tools.find(tool => tool.name === 'delete_post');
const result = await deletePostTool.handler({ id: 1, force: true });
expect(mockWordPressService.posts.deletePost).toHaveBeenCalledWith(1, true);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('get_pages tool', () => {
it('should call listPages with correct parameters', async () => {
const mockResponse = [{ id: 1, title: { rendered: 'Test Page' } }];
mockWordPressService.pages.listPages.mockResolvedValue(mockResponse);
const getPagesTool = tools.find(tool => tool.name === 'get_pages');
const result = await getPagesTool.handler({
per_page: 10,
status: 'publish'
});
expect(mockWordPressService.pages.listPages).toHaveBeenCalledWith({
per_page: 10,
status: 'publish'
});
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('get_page tool', () => {
it('should call getPage with correct ID', async () => {
const mockResponse = { id: 1, title: { rendered: 'Test Page' } };
mockWordPressService.pages.getPage.mockResolvedValue(mockResponse);
const getPageTool = tools.find(tool => tool.name === 'get_page');
const result = await getPageTool.handler({ id: 1 });
expect(mockWordPressService.pages.getPage).toHaveBeenCalledWith(1);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('create_page tool', () => {
it('should call createPage with correct data', async () => {
const pageData = {
title: 'New Page',
content: 'Page content',
status: 'publish'
};
const mockResponse = { id: 2, title: { rendered: 'New Page' } };
mockWordPressService.pages.createPage.mockResolvedValue(mockResponse);
const createPageTool = tools.find(tool => tool.name === 'create_page');
const result = await createPageTool.handler(pageData);
expect(mockWordPressService.pages.createPage).toHaveBeenCalledWith(pageData);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('update_page tool', () => {
it('should call updatePage with correct parameters', async () => {
const updateData = {
title: 'Updated Page',
content: 'Updated content'
};
const mockResponse = { id: 1, title: { rendered: 'Updated Page' } };
mockWordPressService.pages.updatePage.mockResolvedValue(mockResponse);
const updatePageTool = tools.find(tool => tool.name === 'update_page');
const result = await updatePageTool.handler({ id: 1, ...updateData });
expect(mockWordPressService.pages.updatePage).toHaveBeenCalledWith(1, updateData);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('delete_page tool', () => {
it('should call deletePage with correct parameters', async () => {
const mockResponse = { deleted: true };
mockWordPressService.pages.deletePage.mockResolvedValue(mockResponse);
const deletePageTool = tools.find(tool => tool.name === 'delete_page');
const result = await deletePageTool.handler({ id: 1, force: false });
expect(mockWordPressService.pages.deletePage).toHaveBeenCalledWith(1, false);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('get_media tool', () => {
it('should call listMedia with correct parameters', async () => {
const mockResponse = [{ id: 1, title: { rendered: 'Test Image' } }];
mockWordPressService.media.listMedia.mockResolvedValue(mockResponse);
const getMediaTool = tools.find(tool => tool.name === 'get_media');
const result = await getMediaTool.handler({
per_page: 10,
media_type: 'image'
});
expect(mockWordPressService.media.listMedia).toHaveBeenCalledWith({
per_page: 10,
media_type: 'image'
});
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('upload_media tool', () => {
it('should call uploadMedia with correct parameters', async () => {
const mediaData = {
file: 'test-file.jpg',
title: 'Test Image',
alt_text: 'Test alt text'
};
const mockResponse = { id: 1, title: { rendered: 'Test Image' } };
mockWordPressService.media.uploadMedia.mockResolvedValue(mockResponse);
const uploadMediaTool = tools.find(tool => tool.name === 'upload_media');
const result = await uploadMediaTool.handler(mediaData);
expect(mockWordPressService.media.uploadMedia).toHaveBeenCalledWith(mediaData);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('get_users tool', () => {
it('should call listUsers with correct parameters', async () => {
const mockResponse = [{ id: 1, name: 'Test User' }];
mockWordPressService.users.listUsers.mockResolvedValue(mockResponse);
const getUsersTool = tools.find(tool => tool.name === 'get_users');
const result = await getUsersTool.handler({
per_page: 10,
roles: 'administrator'
});
expect(mockWordPressService.users.listUsers).toHaveBeenCalledWith({
per_page: 10,
roles: 'administrator'
});
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('get_user tool', () => {
it('should call getUser with correct ID', async () => {
const mockResponse = { id: 1, name: 'Test User' };
mockWordPressService.users.getUser.mockResolvedValue(mockResponse);
const getUserTool = tools.find(tool => tool.name === 'get_user');
const result = await getUserTool.handler({ id: 1 });
expect(mockWordPressService.users.getUser).toHaveBeenCalledWith(1);
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('get_categories tool', () => {
it('should call getCategories with correct parameters', async () => {
const mockResponse = [{ id: 1, name: 'Test Category' }];
mockWordPressService.taxonomies.getCategories.mockResolvedValue(mockResponse);
const getCategoriesTool = tools.find(tool => tool.name === 'get_categories');
const result = await getCategoriesTool.handler({
per_page: 10,
hide_empty: false
});
expect(mockWordPressService.taxonomies.getCategories).toHaveBeenCalledWith({
per_page: 10,
hide_empty: false
});
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('get_tags tool', () => {
it('should call getTags with correct parameters', async () => {
const mockResponse = [{ id: 1, name: 'Test Tag' }];
mockWordPressService.taxonomies.getTags.mockResolvedValue(mockResponse);
const getTagsTool = tools.find(tool => tool.name === 'get_tags');
const result = await getTagsTool.handler({
per_page: 10,
hide_empty: false
});
expect(mockWordPressService.taxonomies.getTags).toHaveBeenCalledWith({
per_page: 10,
hide_empty: false
});
expect(result).toEqual({
content: [{
type: 'text',
text: JSON.stringify(mockResponse, null, 2)
}]
});
});
});
describe('tool registration', () => {
it('should register all expected WordPress tools', () => {
const expectedTools = [
'get_posts',
'get_post',
'create_post',
'update_post',
'delete_post',
'get_pages',
'get_page',
'create_page',
'update_page',
'delete_page',
'get_media',
'upload_media',
'get_users',
'get_user',
'get_categories',
'get_tags'
];
const toolNames = tools.map(tool => tool.name);
expectedTools.forEach(expectedTool => {
expect(toolNames).toContain(expectedTool);
});
expect(tools).toHaveLength(16);
});
it('should have correct input schemas for all tools', () => {
tools.forEach(tool => {
expect(tool.inputSchema).toBeDefined();
expect(tool.inputSchema.type).toBe('object');
expect(tool.handler).toBeInstanceOf(Function);
});
});
});
});