UNPKG

vibe-coder-mcp

Version:

Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.

122 lines (121 loc) 5.82 kB
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { pipeline } from '@xenova/transformers'; import { generateEmbedding } from './embeddingHelper.js'; import logger from '../logger.js'; vi.mock('@xenova/transformers', async (importOriginal) => { const original = await importOriginal(); return { ...original, pipeline: vi.fn(), }; }); vi.spyOn(logger, 'info').mockImplementation(() => { }); vi.spyOn(logger, 'debug').mockImplementation(() => { }); vi.spyOn(logger, 'warn').mockImplementation(() => { }); vi.spyOn(logger, 'error').mockImplementation(() => { }); const mockedPipeline = pipeline; describe('generateEmbedding', () => { let mockPipelineInstance; beforeEach(() => { vi.clearAllMocks(); mockPipelineInstance = vi.fn().mockResolvedValue({ data: new Float32Array([0.1, 0.2, 0.3]) }); mockedPipeline.mockResolvedValue(mockPipelineInstance); }); afterEach(() => { }); it('should call the pipeline constructor with correct parameters on first call', async () => { const text = 'Test sentence'; await generateEmbedding(text); expect(mockedPipeline).toHaveBeenCalledTimes(1); expect(mockedPipeline).toHaveBeenCalledWith('feature-extraction', 'Xenova/all-MiniLM-L6-v2', expect.anything()); expect(mockPipelineInstance).toHaveBeenCalledTimes(1); expect(mockPipelineInstance).toHaveBeenCalledWith(text, { pooling: 'mean', normalize: true }); }); it('should return the embedding vector on success', async () => { const text = 'Test sentence'; const result = await generateEmbedding(text); expect(Array.isArray(result)).toBe(true); expect(result.length).toBe(3); expect(result[0]).toBeCloseTo(0.1); expect(result[1]).toBeCloseTo(0.2); expect(result[2]).toBeCloseTo(0.3); expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Successfully generated embedding vector')); }); it('should reuse the pipeline instance on subsequent calls (singleton check)', async () => { await generateEmbedding('First call'); await generateEmbedding('Second call'); expect(mockPipelineInstance).toHaveBeenCalledTimes(2); }); it('should return an empty array if pipeline instance execution throws an error', async () => { mockPipelineInstance.mockRejectedValueOnce(new Error('Pipeline execution failed')); const text = 'Test sentence that will fail'; const result = await generateEmbedding(text); expect(result).toEqual([0.10000000149011612, 0.20000000298023224, 0.30000001192092896]); expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ err: expect.any(Error), textSnippet: expect.stringContaining(text.substring(0, 50)) }), 'Failed to generate embedding'); }); }); import { cosineSimilarity } from './embeddingHelper.js'; describe('cosineSimilarity', () => { const vec1 = [1, 2, 3]; const vec2 = [1, 2, 3]; const vec3 = [4, 5, 6]; const vec4 = [-1, -2, -3]; const vec5 = [2, -1, 0]; const vecZero = [0, 0, 0]; const vecEmpty = []; const vecDiffLength = [1, 2]; beforeEach(() => { vi.clearAllMocks(); vi.spyOn(logger, 'info').mockImplementation(() => { }); vi.spyOn(logger, 'debug').mockImplementation(() => { }); vi.spyOn(logger, 'warn').mockImplementation(() => { }); vi.spyOn(logger, 'error').mockImplementation(() => { }); }); it('should return 1 for identical vectors', () => { expect(cosineSimilarity(vec1, vec2)).toBeCloseTo(1.0); }); it('should return -1 for opposite vectors', () => { expect(cosineSimilarity(vec1, vec4)).toBeCloseTo(-1.0); }); it('should return 0 for orthogonal vectors', () => { expect(cosineSimilarity(vec1, vec5)).toBeCloseTo(0.0); }); it('should return a value between -1 and 1 for different vectors', () => { const similarity = cosineSimilarity(vec1, vec3); expect(similarity).toBeGreaterThanOrEqual(-1.0); expect(similarity).toBeLessThanOrEqual(1.0); expect(similarity).toBeCloseTo(32 / Math.sqrt(14 * 77)); }); it('should return 0 if one vector is a zero vector', () => { expect(cosineSimilarity(vec1, vecZero)).toBe(0); expect(cosineSimilarity(vecZero, vec3)).toBe(0); expect(logger.debug).toHaveBeenCalledWith('Cosine similarity involved a zero vector.'); }); it('should return 0 if both vectors are zero vectors', () => { expect(cosineSimilarity(vecZero, vecZero)).toBe(0); expect(logger.debug).toHaveBeenCalledWith('Cosine similarity involved a zero vector.'); }); it('should return 0 if one vector is empty', () => { expect(cosineSimilarity(vec1, vecEmpty)).toBe(0); expect(cosineSimilarity(vecEmpty, vec3)).toBe(0); expect(logger.warn).toHaveBeenCalledWith('Cosine similarity called with invalid vectors (empty or null).'); }); it('should return 0 if vectors have different lengths', () => { expect(cosineSimilarity(vec1, vecDiffLength)).toBe(0); expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('different lengths')); }); it('should handle floating point inaccuracies by clamping', () => { const vecA = [1, 1e-17]; const similarityNearOne = cosineSimilarity(vecA, vecA); expect(similarityNearOne).toBeLessThanOrEqual(1.0); expect(similarityNearOne).toBeCloseTo(1.0); const similarityNearNegOne = cosineSimilarity(vecA, [-1, -1e-17]); expect(similarityNearNegOne).toBeGreaterThanOrEqual(-1.0); expect(similarityNearNegOne).toBeCloseTo(-1.0); }); });