UNPKG

@alvinveroy/codecompass

Version:

AI-powered MCP server for codebase navigation and LLM prompt optimization

136 lines (135 loc) 6.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const server_1 = require("../lib/server"); // Mock dependencies // Mock metrics module - REMOVED // const mockMetricsObj = { // counters: {}, // timings: {}, // uptime: 0, // queryRefinements: {}, // toolChains: {}, // feedbackStats: { count: 0, average: 0, min: 0, max: 0 }, // agent: { // runs: 0, // completions: 0, // averageSteps: 0, // toolUsage: {} // } // }; // vi.mock('../lib/metrics', () => ({ // Metrics removed // resetMetrics: vi.fn(), // getMetrics: vi.fn(() => mockMetricsObj), // incrementCounter: vi.fn(), // recordTiming: vi.fn() // })); vitest_1.vi.mock('../lib/state', () => ({ getOrCreateSession: vitest_1.vi.fn(() => ({ id: 'test_session', queries: [], suggestions: [], context: { repoPath: '/test/repo' }, createdAt: Date.now(), lastUpdated: Date.now() })), addQuery: vitest_1.vi.fn(), addSuggestion: vitest_1.vi.fn(), addFeedback: vitest_1.vi.fn(), updateContext: vitest_1.vi.fn(), getRecentQueries: vitest_1.vi.fn(() => []), getRelevantResults: vitest_1.vi.fn(() => []) })); // Import mocked functions after mocking // import { resetMetrics, getMetrics } from '../lib/metrics'; // Metrics removed const state_1 = require("../lib/state"); (0, vitest_1.describe)('Server Tools', () => { (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); }); (0, vitest_1.afterEach)(() => { vitest_1.vi.resetAllMocks(); }); (0, vitest_1.describe)('normalizeToolParams', () => { (0, vitest_1.it)('should handle string input as query', () => { const result = (0, server_1.normalizeToolParams)('test query'); (0, vitest_1.expect)(result).toEqual({ query: 'test query' }); }); (0, vitest_1.it)('should handle JSON string input', () => { const result = (0, server_1.normalizeToolParams)('{"query": "test query", "sessionId": "123"}'); (0, vitest_1.expect)(result).toEqual({ query: 'test query', sessionId: '123' }); }); (0, vitest_1.it)('should handle object input with query property', () => { const result = (0, server_1.normalizeToolParams)({ query: 'test query' }); (0, vitest_1.expect)(result).toEqual({ query: 'test query' }); }); (0, vitest_1.it)('should handle object input with prompt property', () => { const result = (0, server_1.normalizeToolParams)({ prompt: 'test prompt' }); (0, vitest_1.expect)(result).toEqual({ prompt: 'test prompt' }); }); (0, vitest_1.it)('should handle object input with sessionId property', () => { const result = (0, server_1.normalizeToolParams)({ sessionId: '123' }); (0, vitest_1.expect)(result).toEqual({ sessionId: '123' }); }); (0, vitest_1.it)('should handle object input without query/prompt/sessionId properties', () => { const obj = { foo: 'bar', baz: 123 }; const result = (0, server_1.normalizeToolParams)(obj); (0, vitest_1.expect)(result).toEqual(obj); // Expect the object to be returned as-is }); (0, vitest_1.it)('should handle primitive values', () => { const result = (0, server_1.normalizeToolParams)(42); (0, vitest_1.expect)(result).toEqual({ query: '42' }); }); (0, vitest_1.it)('should handle null input', () => { const result = (0, server_1.normalizeToolParams)(null); (0, vitest_1.expect)(result).toEqual({ query: '' }); // Updated to expect empty string for null }); }); (0, vitest_1.describe)('Session Management', () => { (0, vitest_1.it)('should create a new session when sessionId is not provided', () => { (0, state_1.getOrCreateSession)(undefined, '/test/repo'); (0, vitest_1.expect)(state_1.getOrCreateSession).toHaveBeenCalledWith(undefined, '/test/repo'); }); (0, vitest_1.it)('should use existing session when sessionId is provided', () => { (0, state_1.getOrCreateSession)('existing_session', '/test/repo'); (0, vitest_1.expect)(state_1.getOrCreateSession).toHaveBeenCalledWith('existing_session', '/test/repo'); }); (0, vitest_1.it)('should add query to session', () => { (0, state_1.addQuery)('test_session', 'test query', [], 0.8); (0, vitest_1.expect)(state_1.addQuery).toHaveBeenCalledWith('test_session', 'test query', [], 0.8); }); (0, vitest_1.it)('should add suggestion to session', () => { (0, state_1.addSuggestion)('test_session', 'test prompt', 'test suggestion'); (0, vitest_1.expect)(state_1.addSuggestion).toHaveBeenCalledWith('test_session', 'test prompt', 'test suggestion'); }); }); // describe('Metrics Management', () => { // Metrics removed // it('should reset metrics', () => { // resetMetrics(); // expect(resetMetrics).toHaveBeenCalled(); // }); // it('should get metrics', () => { // // Create a manual mock for this specific test // vi.mocked(getMetrics).mockReturnValueOnce({ // counters: { test: 1 }, // timings: { test: { count: 1, totalMs: 100, avgMs: 100, minMs: 100, maxMs: 100 } }, // uptime: 1000, // queryRefinements: {}, // toolChains: {}, // feedbackStats: { count: 0, average: 0, min: 0, max: 0 }, // agent: { // runs: 0, // completions: 0, // averageSteps: 0, // toolUsage: {} // } // }); // const metrics = getMetrics(); // expect(getMetrics).toHaveBeenCalled(); // expect(metrics).toEqual(expect.objectContaining({ // counters: expect.any(Object), // timings: expect.any(Object) // })); // }); // }); });