/** * Tests for TavusClient Video Avatar Generation and Streaming * * Tests video avatar generation, streaming sessions, and persona management * functionality of the Tavus.io API client. */ import { TavusClient, TavusConfig } from '../shared/utils/api-clients/tavus-client'; // Mock fetch for testing const mockFetch = jest.fn(); global.fetch = mockFetch; describe('TavusClient Video Avatar Generation', () => { let tavusClient: TavusClient; let mockConfig: TavusConfig; beforeEach(() => { // Reset mocks mockFetch.mockClear(); // Create mock config mockConfig = { apiKey: 'test-api-key-123', debug: true, }; // Create client instance tavusClient = new TavusClient(mockConfig); }); describe('Video Session Management', () => { beforeEach(async () => { // Mock successful initialization mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ replicas: [] }), }); await tavusClient.initialize(); }); it('should create video session successfully', async () => { const mockSession = { sessionId: 'sess_123', replicaId: 'r1', personaId: 'p1', webRtcRoomUrl: 'https://room.tavus.io/sess_123', status: 'pending', startedAt: '2024-01-01T00:00:00Z', }; mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => mockSession, }); const session = await tavusClient.createVideoSession('r1', 'p1', 'premium'); expect(session).toEqual(mockSession); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/sessions', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, body: JSON.stringify({ replica_id: 'r1', persona_id: 'p1', pipeline_mode: 'premium', enable_streaming: true, }), }) ); }); it('should get session details successfully', async () => { const mockSession = { sessionId: 'sess_123', replicaId: 'r1', personaId: 'p1', webRtcRoomUrl: 'https://room.tavus.io/sess_123', status: 'active', startedAt: '2024-01-01T00:00:00Z', transcriptUrl: 'https://tavusapi.com/transcript/sess_123', recordingUrl: 'https://tavusapi.com/recording/sess_123', }; mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => mockSession, }); const session = await tavusClient.getSession('sess_123'); expect(session).toEqual(mockSession); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/sessions/sess_123', expect.objectContaining({ method: 'GET', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, }) ); }); it('should end session successfully', async () => { mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ success: true }), }); await tavusClient.endSession('sess_123'); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/sessions/sess_123', expect.objectContaining({ method: 'DELETE', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, }) ); }); }); describe('Avatar Generation', () => { beforeEach(async () => { // Mock successful initialization mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ replicas: [] }), }); await tavusClient.initialize(); }); }); describe('Video Streaming', () => { beforeEach(async () => { // Mock successful initialization mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ replicas: [] }), }); await tavusClient.initialize(); }); it('should start video stream successfully', async () => { const mockStreamSession = { sessionId: 'sess_123', roomUrl: 'https://room.tavus.io/sess_123', accessToken: 'token_123', isActive: true, startTime: '2024-01-01T00:00:00Z', videoQuality: 'high', audioQuality: 'high', pipelineMode: 'premium', }; mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => mockStreamSession, }); const streamSession = await tavusClient.startVideoStream('sess_123', 'high'); expect(streamSession).toEqual(mockStreamSession); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/sessions/sess_123/stream', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, body: JSON.stringify({ video_quality: 'high', enable_webrtc: true, }), }) ); }); it('should stop video stream successfully', async () => { mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ success: true }), }); await tavusClient.stopVideoStream('sess_123'); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/sessions/sess_123/stream', expect.objectContaining({ method: 'DELETE', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, }) ); }); }); describe('Persona Management', () => { beforeEach(async () => { // Mock successful initialization mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ replicas: [] }), }); await tavusClient.initialize(); }); it('should get personas successfully', async () => { const mockPersonas = [ { personaId: 'p1', name: 'Customer Support', description: 'Helpful customer support agent', promptTemplate: 'You are a helpful customer support agent.', language: 'en', voiceEngine: 'Tavus', pipelineMode: 'basic', }, { personaId: 'p2', name: 'Sales Assistant', description: 'Professional sales assistant', promptTemplate: 'You are a professional sales assistant.', language: 'en', voiceEngine: 'ElevenLabs', pipelineMode: 'premium', }, ]; mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ personas: mockPersonas }), }); const personas = await tavusClient.getPersonas(); expect(personas).toEqual(mockPersonas); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/personas', expect.objectContaining({ method: 'GET', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, }) ); }); it('should create persona successfully', async () => { const mockPersona = { personaId: 'p3', name: 'Technical Expert', description: 'Technical expert for complex questions', promptTemplate: 'You are a technical expert.', language: 'en', voiceEngine: 'PlayHT', pipelineMode: 'enterprise', }; mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => mockPersona, }); const persona = await tavusClient.createPersona({ name: 'Technical Expert', description: 'Technical expert for complex questions', promptTemplate: 'You are a technical expert.', language: 'en', voiceEngine: 'PlayHT', pipelineMode: 'enterprise', }); expect(persona).toEqual(mockPersona); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/personas', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, body: JSON.stringify({ name: 'Technical Expert', description: 'Technical expert for complex questions', prompt_template: 'You are a technical expert.', language: 'en', voice_engine: 'PlayHT', pipeline_mode: 'enterprise', }), }) ); }); }); describe('Session Transcripts and Recordings', () => { beforeEach(async () => { // Mock successful initialization mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ replicas: [] }), }); await tavusClient.initialize(); }); it('should get session transcript successfully', async () => { const mockTranscript = 'Hello, how can I help you today? I am here to assist you with any questions you may have.'; mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ transcript: mockTranscript }), }); const transcript = await tavusClient.getSessionTranscript('sess_123'); expect(transcript).toBe(mockTranscript); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/sessions/sess_123/transcript', expect.objectContaining({ method: 'GET', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, }) ); }); it('should get session recording URL successfully', async () => { const mockRecordingUrl = 'https://tavusapi.com/recordings/sess_123.mp4'; mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ recording_url: mockRecordingUrl }), }); const recordingUrl = await tavusClient.getSessionRecording('sess_123'); expect(recordingUrl).toBe(mockRecordingUrl); expect(mockFetch).toHaveBeenCalledWith( 'https://tavusapi.com/v2/sessions/sess_123/recording', expect.objectContaining({ method: 'GET', headers: { 'Content-Type': 'application/json', 'x-api-key': 'test-api-key-123', }, }) ); }); }); describe('Error Handling', () => { beforeEach(async () => { // Mock successful initialization mockFetch.mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ replicas: [] }), }); await tavusClient.initialize(); }); it('should throw error if not initialized for video operations', async () => { const uninitializedClient = new TavusClient(mockConfig); await expect(uninitializedClient.createVideoSession('r1', 'p1')).rejects.toThrow( 'TavusClient must be initialized before creating video sessions' ); await expect(uninitializedClient.generateAvatar({ replicaId: 'r1', personaId: 'p1', script: 'test', })).rejects.toThrow( 'TavusClient must be initialized before generating avatars' ); await expect(uninitializedClient.startVideoStream('sess_123')).rejects.toThrow( 'TavusClient must be initialized before starting video streams' ); }); it('should handle network errors gracefully', async () => { mockFetch.mockRejectedValueOnce(new Error('Network error')); await expect(tavusClient.createVideoSession('r1', 'p1')).rejects.toThrow('Network error'); }); }); });