import { MockDataService } from '../../services/mock-data-service'; import { IdGenerator as _IdGenerator } from '../../utils/id-generator'; describe('MockDataService', () => { beforeEach(() => { // Re-initialize with default data for each test void MockDataService.initialize(); }); describe('Authentication', () => { it('should validate valid API keys', () => { const result = MockDataService.validateApiKey('test-api-key'); expect(result).toBe(true); }); it('should reject invalid API keys', () => { const result = MockDataService.validateApiKey('invalid-key'); expect(result).toBe(false); }); it('should return user info for valid API key', () => { const userInfo = MockDataService.getUserInfo('test-api-key'); expect(userInfo).toHaveProperty('user_id'); expect(userInfo).toHaveProperty('email'); expect(userInfo).toHaveProperty('plan'); expect(userInfo).toHaveProperty('credits_remaining'); expect(userInfo).toHaveProperty('permissions'); }); }); describe('Conversations', () => { it('should create a conversation', () => { const conversationData = { replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation', callback_url: 'https://example.com/callback', metadata: { test: 'data' } }; const conversation = MockDataService.createConversation(conversationData); expect(conversation).toHaveProperty('conversation_id'); expect(conversation.replica_id).toBe(conversationData.replica_id); expect(conversation.persona_id).toBe(conversationData.persona_id); expect(conversation.status).toBe('active'); expect(conversation.created_at).toBeDefined(); }); it('should get a conversation by ID', () => { const conversationData = { replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation' }; const created = MockDataService.createConversation(conversationData); const retrieved = MockDataService.getConversation(created.conversation_id); expect(retrieved).toEqual(created); }); it('should return null for non-existent conversation', () => { const result = MockDataService.getConversation('non-existent-id'); expect(result).toBeNull(); }); it('should list all conversations', () => { // Get initial count const initialCount = MockDataService.getConversations().length; const conv1 = MockDataService.createConversation({ replica_id: 'replica-1', persona_id: 'persona-1', conversation_name: 'Conversation 1' }); const conv2 = MockDataService.createConversation({ replica_id: 'replica-2', persona_id: 'persona-2', conversation_name: 'Conversation 2' }); const conversations = MockDataService.getConversations(); expect(conversations).toHaveLength(initialCount + 2); expect(conversations).toContain(conv1); expect(conversations).toContain(conv2); }); it('should end a conversation', () => { const conversation = MockDataService.createConversation({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation' }); const endedConversation = MockDataService.endConversation(conversation.conversation_id, 'User ended'); expect(endedConversation?.status).toBe('ended'); expect(endedConversation?.ended_at).toBeDefined(); expect(endedConversation?.duration_seconds).toBeGreaterThanOrEqual(0); }); it('should delete a conversation', () => { const conversation = MockDataService.createConversation({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation' }); const deleted = MockDataService.deleteConversation(conversation.conversation_id); expect(deleted).toBe(true); const retrieved = MockDataService.getConversation(conversation.conversation_id); expect(retrieved).toBeNull(); }); }); describe('Personas', () => { it('should create a persona', () => { const persona = MockDataService.createPersona({ name: 'Test Persona', description: 'A test persona', instructions: 'Test instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }); expect(persona).toHaveProperty('persona_id'); expect(persona.name).toBe('Test Persona'); expect(persona.description).toBe('A test persona'); expect(persona.instructions).toBe('Test instructions'); expect(persona.created_at).toBeDefined(); }); it('should get a persona by ID', () => { const created = MockDataService.createPersona({ name: 'Test Persona', description: 'Description', instructions: 'Instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }); const retrieved = MockDataService.getPersona(created.persona_id); expect(retrieved).toEqual(created); }); it('should list all personas', () => { // Get initial count const initialCount = MockDataService.getPersonas().length; const persona1 = MockDataService.createPersona({ name: 'Persona 1', description: 'Desc 1', instructions: 'Instructions 1', voice_id: 'en-US-Neural2-F', language: 'en-US' }); const persona2 = MockDataService.createPersona({ name: 'Persona 2', description: 'Desc 2', instructions: 'Instructions 2', voice_id: 'en-US-Neural2-M', language: 'en-US' }); const personas = MockDataService.getPersonas(); expect(personas).toHaveLength(initialCount + 2); expect(personas).toContain(persona1); expect(personas).toContain(persona2); }); it('should update a persona', () => { const persona = MockDataService.createPersona({ name: 'Original Name', description: 'Original Description', instructions: 'Original Instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }); const updated = MockDataService.updatePersona(persona.persona_id, { name: 'Updated Name', description: 'Updated Description' }); expect(updated?.name).toBe('Updated Name'); expect(updated?.description).toBe('Updated Description'); expect(updated?.instructions).toBe('Original Instructions'); // Should remain unchanged }); it('should delete a persona', () => { const persona = MockDataService.createPersona({ name: 'Test Persona', description: 'Description', instructions: 'Instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }); const deleted = MockDataService.deletePersona(persona.persona_id); expect(deleted).toBe(true); const retrieved = MockDataService.getPersona(persona.persona_id); expect(retrieved).toBeNull(); }); }); describe('Videos', () => { it('should create a video', () => { const videoData = { conversation_id: 'test-conversation', title: 'Test Video', description: 'A test video', format: 'mp4', quality: 'high', metadata: { test: 'data' } }; const video = MockDataService.createVideo(videoData); expect(video).toHaveProperty('video_id'); expect(video.conversation_id).toBe(videoData.conversation_id); expect(video.title).toBe(videoData.title); expect(video.status).toBe('processing'); expect(video.progress).toBe(0); expect(video.estimated_completion).toBeDefined(); }); it('should get a video by ID', () => { const created = MockDataService.createVideo({ conversation_id: 'test-conversation', title: 'Test Video', format: 'mp4', quality: 'high' }); const retrieved = MockDataService.getVideo(created.video_id); expect(retrieved).toEqual(created); }); it('should list all videos', () => { // Get initial count const initialCount = MockDataService.getVideos().length; const video1 = MockDataService.createVideo({ conversation_id: 'conv-1', title: 'Video 1', format: 'mp4', quality: 'high' }); const video2 = MockDataService.createVideo({ conversation_id: 'conv-2', title: 'Video 2', format: 'mp4', quality: 'medium' }); const videos = MockDataService.getVideos(); expect(videos).toHaveLength(initialCount + 2); expect(videos).toContain(video1); expect(videos).toContain(video2); }); it('should delete a video', () => { const video = MockDataService.createVideo({ conversation_id: 'test-conversation', title: 'Test Video', format: 'mp4', quality: 'high' }); const deleted = MockDataService.deleteVideo(video.video_id); expect(deleted).toBe(true); const retrieved = MockDataService.getVideo(video.video_id); expect(retrieved).toBeNull(); }); }); describe('Speech', () => { it('should create speech', () => { const speechData = { text: 'Hello, this is a test speech', voice_id: 'en-US-Neural2-F', language: 'en-US', speed: 1.0, pitch: 0, metadata: { test: 'data' } }; const speech = MockDataService.createSpeech(speechData); expect(speech).toHaveProperty('speech_id'); expect(speech.text).toBe(speechData.text); expect(speech.voice_id).toBe(speechData.voice_id); expect(speech.status).toBe('processing'); expect(speech.progress).toBe(0); }); it('should get speech by ID', () => { const created = MockDataService.createSpeech({ text: 'Test speech', voice_id: 'en-US-Neural2-F', language: 'en-US', speed: 1.0, pitch: 0 }); const retrieved = MockDataService.getSpeechById(created.speech_id); expect(retrieved).toEqual(created); }); it('should list all speech items', () => { // Get initial count const initialCount = MockDataService.getSpeech().length; const speech1 = MockDataService.createSpeech({ text: 'Speech 1', voice_id: 'en-US-Neural2-F', language: 'en-US', speed: 1.0, pitch: 0 }); const speech2 = MockDataService.createSpeech({ text: 'Speech 2', voice_id: 'en-US-Neural2-M', language: 'en-US', speed: 1.2, pitch: 0 }); const speeches = MockDataService.getSpeech(); expect(speeches).toHaveLength(initialCount + 2); expect(speeches).toContain(speech1); expect(speeches).toContain(speech2); }); it('should delete speech', () => { const speech = MockDataService.createSpeech({ text: 'Test speech', voice_id: 'en-US-Neural2-F', language: 'en-US', speed: 1.0, pitch: 0 }); const deleted = MockDataService.deleteSpeech(speech.speech_id); expect(deleted).toBe(true); const retrieved = MockDataService.getSpeechById(speech.speech_id); expect(retrieved).toBeNull(); }); }); describe('Lipsync', () => { it('should create lipsync', () => { const lipsyncData = { replica_id: 'test-replica', audio_url: 'https://example.com/audio.mp3', title: 'Test Lipsync', description: 'A test lipsync', quality: 'high', metadata: { test: 'data' } }; const lipsync = MockDataService.createLipsync(lipsyncData); expect(lipsync).toHaveProperty('lipsync_id'); expect(lipsync.replica_id).toBe(lipsyncData.replica_id); expect(lipsync.audio_url).toBe(lipsyncData.audio_url); expect(lipsync.status).toBe('processing'); expect(lipsync.progress).toBe(0); }); it('should get lipsync by ID', () => { const created = MockDataService.createLipsync({ replica_id: 'test-replica', audio_url: 'https://example.com/audio.mp3', title: 'Test Lipsync', quality: 'high' }); const retrieved = MockDataService.getLipsync(created.lipsync_id); expect(retrieved).toEqual(created); }); it('should list all lipsyncs', () => { // Get initial count const initialCount = MockDataService.getLipsyncs().length; const lipsync1 = MockDataService.createLipsync({ replica_id: 'replica-1', audio_url: 'https://example.com/audio1.mp3', title: 'Lipsync 1', quality: 'high' }); const lipsync2 = MockDataService.createLipsync({ replica_id: 'replica-2', audio_url: 'https://example.com/audio2.mp3', title: 'Lipsync 2', quality: 'medium' }); const lipsyncs = MockDataService.getLipsyncs(); expect(lipsyncs).toHaveLength(initialCount + 2); expect(lipsyncs).toContain(lipsync1); expect(lipsyncs).toContain(lipsync2); }); it('should delete lipsync', () => { const lipsync = MockDataService.createLipsync({ replica_id: 'test-replica', audio_url: 'https://example.com/audio.mp3', title: 'Test Lipsync', quality: 'high' }); const deleted = MockDataService.deleteLipsync(lipsync.lipsync_id); expect(deleted).toBe(true); const retrieved = MockDataService.getLipsync(lipsync.lipsync_id); expect(retrieved).toBeNull(); }); }); describe('Webhooks', () => { it('should create webhook', () => { const webhookData = { url: 'https://example.com/webhook', events: ['conversation.created', 'video.completed'], secret: 'webhook-secret', description: 'Test webhook', status: 'active' }; const webhook = MockDataService.createWebhook(webhookData); expect(webhook).toHaveProperty('webhook_id'); expect(webhook.url).toBe(webhookData.url); expect(webhook.events).toEqual(webhookData.events); expect(webhook.secret).toBe(webhookData.secret); expect(webhook.status).toBe('active'); }); it('should list all webhooks', () => { // Get initial count const initialCount = MockDataService.getWebhooks().length; const webhook1 = MockDataService.createWebhook({ url: 'https://example.com/webhook1', events: ['conversation.created'], secret: 'secret1' }); const webhook2 = MockDataService.createWebhook({ url: 'https://example.com/webhook2', events: ['video.completed'], secret: 'secret2' }); const webhooks = MockDataService.getWebhooks(); expect(webhooks).toHaveLength(initialCount + 2); expect(webhooks).toContain(webhook1); expect(webhooks).toContain(webhook2); }); it('should delete webhook', () => { const webhook = MockDataService.createWebhook({ url: 'https://example.com/webhook', events: ['conversation.created'], secret: 'webhook-secret' }); const deleted = MockDataService.deleteWebhook(webhook.webhook_id); expect(deleted).toBe(true); // Verify the specific webhook was deleted const retrieved = MockDataService.getWebhooks().find(w => w.webhook_id === webhook.webhook_id); expect(retrieved).toBeUndefined(); }); }); describe('Analytics', () => { it('should return usage analytics', () => { // Create some test data MockDataService.createConversation({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation' }); MockDataService.createVideo({ conversation_id: 'test-conversation', title: 'Test Video', format: 'mp4', quality: 'high' }); const analytics = MockDataService.getUsageAnalytics(); expect(analytics).toHaveProperty('usage'); expect(analytics.usage).toHaveProperty('conversations'); expect(analytics.usage).toHaveProperty('videos'); expect(analytics.usage).toHaveProperty('speech'); expect(analytics.usage).toHaveProperty('lipsync'); }); it('should return conversation analytics', () => { // Create test conversations MockDataService.createConversation({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Active Conversation' }); const endedConversation = MockDataService.createConversation({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Ended Conversation' }); MockDataService.endConversation(endedConversation.conversation_id); const analytics = MockDataService.getConversationAnalytics(); expect(analytics).toHaveProperty('conversations'); expect(analytics.conversations).toHaveProperty('total'); expect(analytics.conversations).toHaveProperty('by_status'); expect(analytics.conversations).toHaveProperty('by_replica'); expect(analytics.conversations).toHaveProperty('by_persona'); expect(analytics.conversations).toHaveProperty('completion_rate'); expect(analytics.conversations).toHaveProperty('average_duration_minutes'); }); }); describe('Initialization', () => { it('should initialize with default data', () => { void MockDataService.initialize(); // Check that default data was created expect(MockDataService.getPersonas().length).toBeGreaterThan(0); expect(MockDataService.getReplicas().length).toBeGreaterThan(0); expect(MockDataService.getConversations().length).toBeGreaterThan(0); }); }); });