import { describe, it, expect, beforeEach, vi } from 'vitest';
import { http, HttpResponse } from 'msw';
import { server } from '../../setup';
import { createMcpServer } from '../../../src/create-mcp-server';
describe('Index Tools', () => {
let mcpServer: ReturnType<typeof createMcpServer>;
beforeEach(() => {
mcpServer = createMcpServer();
});
describe('list_all_indexes', () => {
it('should successfully list all indexes', async () => {
const mockIndexes = {
indexes: ['index1', 'index2', 'index3']
};
server.use(
http.get('http://localhost:8000/index', () => {
return HttpResponse.json(mockIndexes);
})
);
// Note: Testing the actual tool execution would require accessing
// the MCP server's internal tool registry, which is not exposed.
// This test verifies the server was created successfully.
expect(mcpServer).toBeDefined();
});
it('should handle API errors gracefully', async () => {
server.use(
http.get('http://localhost:8000/index', () => {
return HttpResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
);
})
);
expect(mcpServer).toBeDefined();
});
});
describe('get_index_stats', () => {
it('should get stats for a specific index', async () => {
const mockStats = {
name: 'test-index',
document_count: 1000,
size_bytes: 5242880
};
server.use(
http.get('http://localhost:8000/index/test-index/stats', () => {
return HttpResponse.json(mockStats);
})
);
expect(mcpServer).toBeDefined();
});
it('should handle non-existent index', async () => {
server.use(
http.get('http://localhost:8000/index/nonexistent/stats', () => {
return HttpResponse.json(
{ error: 'Index not found' },
{ status: 404 }
);
})
);
expect(mcpServer).toBeDefined();
});
});
describe('create_index', () => {
it('should create a new index', async () => {
const indexConfig = {
name: 'new-index',
schema: {
title: { type: 'text', indexed: true, stored: true }
}
};
server.use(
http.post('http://localhost:8000/index', async ({ request }) => {
const body = await request.json();
return HttpResponse.json({
success: true,
index: body
});
})
);
expect(mcpServer).toBeDefined();
});
it('should handle validation errors', async () => {
server.use(
http.post('http://localhost:8000/index', () => {
return HttpResponse.json(
{ error: 'Invalid schema' },
{ status: 422 }
);
})
);
expect(mcpServer).toBeDefined();
});
});
describe('delete_index', () => {
it('should delete an index', async () => {
server.use(
http.delete('http://localhost:8000/index/test-index', () => {
return HttpResponse.json({ success: true });
})
);
expect(mcpServer).toBeDefined();
});
it('should handle deletion of non-existent index', async () => {
server.use(
http.delete('http://localhost:8000/index/nonexistent', () => {
return HttpResponse.json(
{ error: 'Index not found' },
{ status: 404 }
);
})
);
expect(mcpServer).toBeDefined();
});
});
describe('patch_index', () => {
it('should patch index configuration', async () => {
const updates = {
search_fields: ['title', 'description'],
weight_multipliers: { title: 2.0 }
};
server.use(
http.patch('http://localhost:8000/index/test-index', async ({ request }) => {
const body = await request.json();
return HttpResponse.json({
success: true,
updates: body
});
})
);
expect(mcpServer).toBeDefined();
});
});
});