import request from 'supertest'; import app from '../../main'; describe('API Endpoints Integration Tests', () => { const validApiKey = 'test-api-key'; const invalidApiKey = 'invalid-api-key'; describe('Health Check', () => { it('should return health status', async () => { const response = await request(app).get('/health').expect(200); expect(response.body).toHaveProperty('status', 'healthy'); }); }); describe('Authentication', () => { it('should validate API key successfully', async () => { const response = await request(app) .post('/v2/auth/validate') .set('x-api-key', validApiKey) .send({ api_key: validApiKey }) .expect(200); expect(response.body).toHaveProperty('user_id'); }); it('should reject invalid API key', async () => { const response = await request(app) .post('/v2/auth/validate') .set('x-api-key', invalidApiKey) .send({ api_key: invalidApiKey }) .expect(401); expect(response.body).toHaveProperty('error', 'invalid_api_key'); }); it('should require API key in body', async () => { const response = await request(app) .post('/v2/auth/validate') .set('x-api-key', validApiKey) .send({}) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); }); }); describe('Conversations', () => { it('should list conversations', async () => { const response = await request(app) .get('/v2/conversations') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('conversations'); expect(response.body).toHaveProperty('pagination'); }); it('should create a conversation', async () => { const conversationData = { replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation', callback_url: 'https://example.com/callback', metadata: { test: 'data' } }; const response = await request(app) .post('/v2/conversations') .set('x-api-key', validApiKey) .send(conversationData) .expect(201); expect(response.body).toHaveProperty('conversation_id'); expect(response.body.replica_id).toBe(conversationData.replica_id); expect(response.body.persona_id).toBe(conversationData.persona_id); expect(response.body.status).toBe('active'); expect(response.body.created_at).toBeDefined(); }); it('should require replica_id when creating conversation', async () => { const response = await request(app) .post('/v2/conversations') .set('x-api-key', validApiKey) .send({ persona_id: 'test-persona', conversation_name: 'Test Conversation' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'replica_id is required'); }); it('should require persona_id when creating conversation', async () => { const response = await request(app) .post('/v2/conversations') .set('x-api-key', validApiKey) .send({ replica_id: 'test-replica', conversation_name: 'Test Conversation' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'persona_id is required'); }); it('should get conversation by ID', async () => { const createResponse = await request(app) .post('/v2/conversations') .set('x-api-key', validApiKey) .send({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation' }) .expect(201); const conversationId = createResponse.body.conversation_id; const response = await request(app) .get(`/v2/conversations/${conversationId}`) .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('conversation_id', conversationId); expect(response.body).toHaveProperty('replica_id'); expect(response.body).toHaveProperty('persona_id'); }); it('should return 404 for non-existent conversation', async () => { const response = await request(app) .get('/v2/conversations/non-existent-id') .set('x-api-key', validApiKey) .expect(404); expect(response.body).toHaveProperty('error', 'conversation_not_found'); }); it('should end a conversation', async () => { const createResponse = await request(app) .post('/v2/conversations') .set('x-api-key', validApiKey) .send({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation' }) .expect(201); const conversationId = createResponse.body.conversation_id; const response = await request(app) .post(`/v2/conversations/${conversationId}/end`) .set('x-api-key', validApiKey) .send({ _reason: 'User ended conversation' }) .expect(200); expect(response.body).toHaveProperty('conversation_id', conversationId); expect(response.body.status).toBe('ended'); expect(response.body.ended_at).toBeDefined(); }); it('should delete a conversation', async () => { const createResponse = await request(app) .post('/v2/conversations') .set('x-api-key', validApiKey) .send({ replica_id: 'test-replica', persona_id: 'test-persona', conversation_name: 'Test Conversation' }) .expect(201); const conversationId = createResponse.body.conversation_id; await request(app) .delete(`/v2/conversations/${conversationId}`) .set('x-api-key', validApiKey) .expect(200); // Verify it's deleted await request(app) .get(`/v2/conversations/${conversationId}`) .set('x-api-key', validApiKey) .expect(404); }); }); describe('Personas', () => { it('should list personas', async () => { const response = await request(app) .get('/v2/personas') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('personas'); expect(response.body).toHaveProperty('pagination'); }); it('should create a persona', async () => { const personaData = { name: 'Test Persona', description: 'A test persona', instructions: 'Test instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }; const response = await request(app) .post('/v2/personas') .set('x-api-key', validApiKey) .send(personaData) .expect(201); expect(response.body).toHaveProperty('persona_id'); expect(response.body.name).toBe(personaData.name); expect(response.body.description).toBe(personaData.description); expect(response.body.instructions).toBe(personaData.instructions); expect(response.body.created_at).toBeDefined(); }); it('should require name when creating persona', async () => { const response = await request(app) .post('/v2/personas') .set('x-api-key', validApiKey) .send({ description: 'A test persona', instructions: 'Test instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'name is required'); }); it('should get persona by ID', async () => { const createResponse = await request(app) .post('/v2/personas') .set('x-api-key', validApiKey) .send({ name: 'Test Persona', description: 'A test persona', instructions: 'Test instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }) .expect(201); const personaId = createResponse.body.persona_id; const response = await request(app) .get(`/v2/personas/${personaId}`) .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('persona_id', personaId); expect(response.body).toHaveProperty('name'); expect(response.body).toHaveProperty('description'); }); it('should update a persona', async () => { const createResponse = await request(app) .post('/v2/personas') .set('x-api-key', validApiKey) .send({ name: 'Original Name', description: 'Original Description', instructions: 'Original Instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }) .expect(201); const personaId = createResponse.body.persona_id; const response = await request(app) .put(`/v2/personas/${personaId}`) .set('x-api-key', validApiKey) .send({ name: 'Updated Name', description: 'Updated Description' }) .expect(200); expect(response.body).toHaveProperty('persona_id', personaId); expect(response.body.name).toBe('Updated Name'); expect(response.body.description).toBe('Updated Description'); }); it('should delete a persona', async () => { const createResponse = await request(app) .post('/v2/personas') .set('x-api-key', validApiKey) .send({ name: 'Test Persona', description: 'A test persona', instructions: 'Test instructions', voice_id: 'en-US-Neural2-F', language: 'en-US' }) .expect(201); const personaId = createResponse.body.persona_id; await request(app) .delete(`/v2/personas/${personaId}`) .set('x-api-key', validApiKey) .expect(200); // Verify it's deleted await request(app) .get(`/v2/personas/${personaId}`) .set('x-api-key', validApiKey) .expect(404); }); }); describe('Videos', () => { it('should list videos', async () => { const response = await request(app) .get('/v2/videos') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('videos'); expect(response.body).toHaveProperty('pagination'); }); it('should create a video', async () => { const videoData = { conversation_id: 'test-conversation', title: 'Test Video', description: 'A test video', format: 'mp4', quality: 'high', metadata: { test: 'data' } }; const response = await request(app) .post('/v2/videos') .set('x-api-key', validApiKey) .send(videoData) .expect(201); expect(response.body).toHaveProperty('video_id'); expect(response.body.conversation_id).toBe(videoData.conversation_id); expect(response.body.title).toBe(videoData.title); expect(response.body.status).toBe('processing'); expect(response.body.created_at).toBeDefined(); }); it('should require conversation_id when creating video', async () => { const response = await request(app) .post('/v2/videos') .set('x-api-key', validApiKey) .send({ title: 'Test Video', format: 'mp4', quality: 'high' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'conversation_id is required'); }); it('should get video by ID', async () => { const createResponse = await request(app) .post('/v2/videos') .set('x-api-key', validApiKey) .send({ conversation_id: 'test-conversation', title: 'Test Video', format: 'mp4', quality: 'high' }) .expect(201); const videoId = createResponse.body.video_id; const response = await request(app) .get(`/v2/videos/${videoId}`) .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('video_id', videoId); expect(response.body).toHaveProperty('conversation_id'); expect(response.body).toHaveProperty('title'); }); it('should delete a video', async () => { const createResponse = await request(app) .post('/v2/videos') .set('x-api-key', validApiKey) .send({ conversation_id: 'test-conversation', title: 'Test Video', format: 'mp4', quality: 'high' }) .expect(201); const videoId = createResponse.body.video_id; await request(app) .delete(`/v2/videos/${videoId}`) .set('x-api-key', validApiKey) .expect(200); // Verify it's deleted await request(app) .get(`/v2/videos/${videoId}`) .set('x-api-key', validApiKey) .expect(404); }); }); describe('Speech', () => { it('should list speech items', async () => { const response = await request(app) .get('/v2/speech') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('speech'); expect(response.body).toHaveProperty('pagination'); }); it('should create speech', async () => { 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 response = await request(app) .post('/v2/speech') .set('x-api-key', validApiKey) .send(speechData) .expect(201); expect(response.body).toHaveProperty('speech_id'); expect(response.body.text).toBe(speechData.text); expect(response.body.voice_id).toBe(speechData.voice_id); expect(response.body.status).toBe('processing'); expect(response.body.created_at).toBeDefined(); }); it('should require text when creating speech', async () => { const response = await request(app) .post('/v2/speech') .set('x-api-key', validApiKey) .send({ voice_id: 'en-US-Neural2-F', language: 'en-US', speed: 1.0, pitch: 0 }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'text is required'); }); it('should get speech by ID', async () => { const createResponse = await request(app) .post('/v2/speech') .set('x-api-key', validApiKey) .send({ text: 'Test speech', voice_id: 'en-US-Neural2-F', language: 'en-US', speed: 1.0, pitch: 0 }) .expect(201); const speechId = createResponse.body.speech_id; const response = await request(app) .get(`/v2/speech/${speechId}`) .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('speech_id', speechId); expect(response.body).toHaveProperty('text'); expect(response.body).toHaveProperty('voice_id'); }); it('should delete speech', async () => { const createResponse = await request(app) .post('/v2/speech') .set('x-api-key', validApiKey) .send({ text: 'Test speech', voice_id: 'en-US-Neural2-F', language: 'en-US', speed: 1.0, pitch: 0 }) .expect(201); const speechId = createResponse.body.speech_id; await request(app) .delete(`/v2/speech/${speechId}`) .set('x-api-key', validApiKey) .expect(200); // Verify it's deleted await request(app) .get(`/v2/speech/${speechId}`) .set('x-api-key', validApiKey) .expect(404); }); }); describe('Lipsync', () => { it('should list lipsync items', async () => { const response = await request(app) .get('/v2/lipsync') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('lipsync'); expect(response.body).toHaveProperty('pagination'); }); it('should create lipsync', async () => { 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 response = await request(app) .post('/v2/lipsync') .set('x-api-key', validApiKey) .send(lipsyncData) .expect(201); expect(response.body).toHaveProperty('lipsync_id'); expect(response.body.replica_id).toBe(lipsyncData.replica_id); expect(response.body.audio_url).toBe(lipsyncData.audio_url); expect(response.body.status).toBe('processing'); expect(response.body.created_at).toBeDefined(); }); it('should require replica_id when creating lipsync', async () => { const response = await request(app) .post('/v2/lipsync') .set('x-api-key', validApiKey) .send({ audio_url: 'https://example.com/audio.mp3', title: 'Test Lipsync', quality: 'high' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'replica_id is required'); }); it('should require audio_url when creating lipsync', async () => { const response = await request(app) .post('/v2/lipsync') .set('x-api-key', validApiKey) .send({ replica_id: 'test-replica', title: 'Test Lipsync', quality: 'high' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'audio_url is required'); }); it('should get lipsync by ID', async () => { const createResponse = await request(app) .post('/v2/lipsync') .set('x-api-key', validApiKey) .send({ replica_id: 'test-replica', audio_url: 'https://example.com/audio.mp3', title: 'Test Lipsync', quality: 'high' }) .expect(201); const lipsyncId = createResponse.body.lipsync_id; const response = await request(app) .get(`/v2/lipsync/${lipsyncId}`) .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('lipsync_id', lipsyncId); expect(response.body).toHaveProperty('replica_id'); expect(response.body).toHaveProperty('audio_url'); }); it('should delete lipsync', async () => { const createResponse = await request(app) .post('/v2/lipsync') .set('x-api-key', validApiKey) .send({ replica_id: 'test-replica', audio_url: 'https://example.com/audio.mp3', title: 'Test Lipsync', quality: 'high' }) .expect(201); const lipsyncId = createResponse.body.lipsync_id; await request(app) .delete(`/v2/lipsync/${lipsyncId}`) .set('x-api-key', validApiKey) .expect(200); // Verify it's deleted await request(app) .get(`/v2/lipsync/${lipsyncId}`) .set('x-api-key', validApiKey) .expect(404); }); }); describe('Webhooks', () => { it('should list webhooks', async () => { const response = await request(app) .get('/v2/webhooks') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('webhooks'); expect(response.body).toHaveProperty('pagination'); }); it('should create webhook', async () => { const webhookData = { url: 'https://example.com/webhook', events: ['conversation.created', 'video.completed'], secret: 'webhook-secret', description: 'Test webhook', status: 'active' }; const response = await request(app) .post('/v2/webhooks') .set('x-api-key', validApiKey) .send(webhookData) .expect(201); expect(response.body).toHaveProperty('webhook_id'); expect(response.body.url).toBe(webhookData.url); expect(response.body.events).toEqual(webhookData.events); expect(response.body.secret).toBe(webhookData.secret); expect(response.body.status).toBe('active'); expect(response.body.created_at).toBeDefined(); }); it('should require url when creating webhook', async () => { const response = await request(app) .post('/v2/webhooks') .set('x-api-key', validApiKey) .send({ events: ['conversation.created'], secret: 'webhook-secret' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'url is required'); }); it('should require events when creating webhook', async () => { const response = await request(app) .post('/v2/webhooks') .set('x-api-key', validApiKey) .send({ url: 'https://example.com/webhook', secret: 'webhook-secret' }) .expect(400); expect(response.body).toHaveProperty('error', 'validation_error'); expect(response.body).toHaveProperty('message', 'events array is required and must not be empty'); }); it('should delete webhook', async () => { const createResponse = await request(app) .post('/v2/webhooks') .set('x-api-key', validApiKey) .send({ url: 'https://example.com/webhook', events: ['conversation.created'], secret: 'webhook-secret' }) .expect(201); const webhookId = createResponse.body.webhook_id; await request(app) .delete(`/v2/webhooks/${webhookId}`) .set('x-api-key', validApiKey) .expect(200); // Verify it's deleted await request(app) .get(`/v2/webhooks/${webhookId}`) .set('x-api-key', validApiKey) .expect(404); }); }); describe('Analytics', () => { it('should return usage analytics', async () => { const response = await request(app) .get('/v2/analytics/usage') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('usage'); expect(response.body.usage).toHaveProperty('conversations'); expect(response.body.usage).toHaveProperty('videos'); expect(response.body.usage).toHaveProperty('speech'); expect(response.body.usage).toHaveProperty('lipsync'); }); it('should return conversation analytics', async () => { const response = await request(app) .get('/v2/analytics/conversations') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('conversations'); expect(response.body.conversations).toHaveProperty('total'); expect(response.body.conversations).toHaveProperty('by_status'); expect(response.body.conversations).toHaveProperty('by_replica'); expect(response.body.conversations).toHaveProperty('by_persona'); expect(response.body.conversations).toHaveProperty('completion_rate'); expect(response.body.conversations).toHaveProperty('average_duration_minutes'); }); }); describe('Replicas', () => { it('should list replicas', async () => { const response = await request(app) .get('/v2/replicas') .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('data'); expect(typeof response.body.data).toBe('object'); }); it('should get replica by ID', async () => { // First get a list to find a valid replica ID const listResponse = await request(app) .get('/v2/replicas') .set('x-api-key', validApiKey) .expect(200); const replicaData = listResponse.body.data; const replicaIds = Object.keys(replicaData); if (replicaIds.length > 0) { // Get the actual replica_id from the first replica, not the array index const firstReplica = replicaData[replicaIds[0]]; const replicaId = firstReplica.replica_id; const response = await request(app) .get(`/v2/replicas/${replicaId}`) .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('replica_id', replicaId); expect(response.body).toHaveProperty('replica_name'); expect(response.body).toHaveProperty('status'); } else { // If no replicas exist, create one for testing const createResponse = await request(app) .post('/v2/replicas') .set('x-api-key', validApiKey) .send({ replica_name: 'Test Replica', model_name: 'phoenix-2' }) .expect(201); const replicaId = createResponse.body.replica_id; const response = await request(app) .get(`/v2/replicas/${replicaId}`) .set('x-api-key', validApiKey) .expect(200); expect(response.body).toHaveProperty('replica_id', replicaId); expect(response.body).toHaveProperty('replica_name'); expect(response.body).toHaveProperty('status'); } }); it('should return 404 for non-existent replica', async () => { const response = await request(app) .get('/v2/replicas/non-existent-id') .set('x-api-key', validApiKey) .expect(404); expect(response.body).toHaveProperty('error', 'replica_not_found'); }); }); describe('Error Handling', () => { it('should return 401 for missing API key', async () => { const response = await request(app) .get('/v2/conversations') .expect(401); expect(response.body).toHaveProperty('error', 'missing_api_key'); }); it('should return 401 for invalid API key', async () => { const response = await request(app) .get('/v2/conversations') .set('x-api-key', invalidApiKey) .expect(401); expect(response.body).toHaveProperty('error', 'invalid_api_key'); }); it('should return 404 for non-existent routes', async () => { const response = await request(app) .get('/v2/non-existent') .set('x-api-key', validApiKey) .expect(404); expect(response.body).toHaveProperty('error', 'not_found'); }); }); });