UNPKG

@translated/lara-mcp

Version:

Lara API official MCP server

91 lines (90 loc) 3.72 kB
import { describe, it, expect, beforeEach } from 'vitest'; import { addTranslation, addTranslationSchema } from '../../mcp/tools/add_translation.js'; import { getMockTranslator, setupTranslatorMock } from '../utils/mocks.js'; // Setup mocks setupTranslatorMock(); describe('addTranslationSchema', () => { it('should validate valid input with required fields', () => { const validInput = { id: ['mem_xyz123'], source: 'en-US', target: 'it-IT', sentence: 'Hello world', translation: 'Ciao mondo' }; expect(() => addTranslationSchema.parse(validInput)).not.toThrow(); }); it('should validate input with all optional fields', () => { const validInput = { id: ['mem_xyz123'], source: 'en-US', target: 'it-IT', sentence: 'Hello world', translation: 'Ciao mondo', tuid: 'translation-123', sentence_before: 'How are you?', sentence_after: 'How is the weather?' }; expect(() => addTranslationSchema.parse(validInput)).not.toThrow(); }); it('should reject input with missing required fields', () => { const invalidInput = { id: ['mem_xyz123'], source: 'en-US', // Missing target sentence: 'Hello world', translation: 'Ciao mondo' }; expect(() => addTranslationSchema.parse(invalidInput)).toThrow(); }); }); describe('addTranslation', () => { let mockTranslator; beforeEach(() => { mockTranslator = getMockTranslator(); }); it('should call lara.memories.addTranslation with required parameters', async () => { const mockResult = { success: true }; mockTranslator.memories.addTranslation.mockResolvedValue(mockResult); const args = { id: ['mem_xyz123'], source: 'en-US', target: 'it-IT', sentence: 'Hello world', translation: 'Ciao mondo' }; const result = await addTranslation(args, mockTranslator); expect(mockTranslator.memories.addTranslation).toHaveBeenCalledWith(args.id, args.source, args.target, args.sentence, args.translation); expect(result).toEqual(mockResult); }); it('should include tuid when provided', async () => { const mockResult = { success: true }; mockTranslator.memories.addTranslation.mockResolvedValue(mockResult); const args = { id: ['mem_xyz123'], source: 'en-US', target: 'it-IT', sentence: 'Hello world', translation: 'Ciao mondo', tuid: 'translation-123', sentence_before: 'How are you?', sentence_after: 'How is the weather?' }; const result = await addTranslation(args, mockTranslator); expect(mockTranslator.memories.addTranslation).toHaveBeenCalledWith(args.id, args.source, args.target, args.sentence, args.translation, args.tuid, args.sentence_before, args.sentence_after); expect(result).toEqual(mockResult); }); it('should throw error if only one of sentence_before or sentence_after is provided', async () => { const args = { id: ['mem_xyz123'], source: 'en-US', target: 'it-IT', sentence: 'Hello world', translation: 'Ciao mondo', tuid: 'translation-123', sentence_before: 'How are you?' // Missing sentence_after }; await expect(addTranslation(args, mockTranslator)).rejects.toThrow('Please provide both sentence_before and sentence_after'); }); });