import axios from 'axios'; describe('Fake Tavus Server E2E Tests', () => { const validApiKey = 'test-api-key'; const invalidApiKey = 'invalid-key'; describe('Health Check', () => { describe('GET /health', () => { it('should return health status', async () => { const res = await axios.get(`/health`); expect(res.status).toBe(200); expect(res.data).toHaveProperty('status', 'healthy'); expect(res.data).toHaveProperty('timestamp'); expect(res.data).toHaveProperty('environment'); expect(res.data).toHaveProperty('version'); // Validate timestamp is a valid ISO string expect(new Date(res.data.timestamp)).toBeInstanceOf(Date); expect(res.data.status).toBe('healthy'); expect(typeof res.data.version).toBe('string'); }); }); }); describe('Authentication', () => { describe('POST /v2/auth/validate', () => { it('should validate valid API key', async () => { const res = await axios.post(`/v2/auth/validate`, {}, { headers: { 'x-api-key': validApiKey, 'Content-Type': 'application/json' } }); expect(res.status).toBe(200); expect(res.data).toHaveProperty('user_id'); expect(res.data).toHaveProperty('plan'); expect(res.data).toHaveProperty('credits_remaining'); expect(res.data).toHaveProperty('api_key_valid', true); // Validate response data types and values expect(typeof res.data.user_id).toBe('string'); expect(res.data.user_id).toMatch(/^user_[a-z0-9]+$/); expect(['free', 'pro', 'enterprise']).toContain(res.data.plan); expect(typeof res.data.credits_remaining).toBe('number'); expect(res.data.credits_remaining).toBeGreaterThanOrEqual(0); }); it('should reject invalid API key', async () => { try { await axios.post(`/v2/auth/validate`, {}, { headers: { 'x-api-key': invalidApiKey, 'Content-Type': 'application/json' } }); fail('Should have thrown an error'); } catch (error: unknown) { const axiosError = error as { response?: { status?: number; data?: { error?: unknown; message?: unknown; status_code?: number } } }; expect(axiosError.response?.status).toBe(401); expect(axiosError.response?.data?.error).toBeDefined(); expect(axiosError.response?.data?.message).toBeDefined(); expect(axiosError.response?.data?.status_code).toBe(401); } }); it('should reject missing API key', async () => { try { await axios.post(`/v2/auth/validate`, {}, { headers: { 'Content-Type': 'application/json' } }); fail('Should have thrown an error'); } catch (error: unknown) { const axiosError = error as { response?: { status?: number; data?: { error?: unknown } } }; expect(axiosError.response?.status).toBe(401); expect(axiosError.response?.data?.error).toBeDefined(); } }); it('should handle malformed request body', async () => { try { await axios.post(`/v2/auth/validate`, 'invalid-json', { headers: { 'x-api-key': validApiKey, 'Content-Type': 'application/json' } }); fail('Should have thrown an error'); } catch (error: unknown) { const axiosError = error as { response?: { status?: number } }; expect([400, 422, 500]).toContain(axiosError.response?.status); } }); }); }); describe('Conversations API', () => { describe('GET /v2/conversations', () => { it('should return conversations list with valid API key', async () => { const res = await axios.get(`/v2/conversations`, { headers: { 'x-api-key': validApiKey } }); expect(res.status).toBe(200); expect(res.data).toHaveProperty('conversations'); expect(Array.isArray(res.data.conversations)).toBe(true); // Validate conversation structure if any exist if (res.data.conversations.length > 0) { const conversation = res.data.conversations[0]; expect(conversation).toHaveProperty('conversation_id'); expect(conversation).toHaveProperty('persona_id'); expect(conversation).toHaveProperty('status'); expect(conversation).toHaveProperty('created_at'); expect(conversation).toHaveProperty('updated_at'); // Validate data types expect(typeof conversation.conversation_id).toBe('string'); expect(typeof conversation.persona_id).toBe('string'); expect(['active', 'ended']).toContain(conversation.status); expect(new Date(conversation.created_at)).toBeInstanceOf(Date); expect(new Date(conversation.updated_at)).toBeInstanceOf(Date); } }); it('should reject invalid API key', async () => { try { await axios.get(`/v2/conversations`, { headers: { 'x-api-key': invalidApiKey } }); fail('Should have thrown an error'); } catch (error: unknown) { const axiosError = error as { response?: { status?: number; data?: { error?: unknown } } }; expect(axiosError.response?.status).toBe(401); expect(axiosError.response?.data?.error).toBeDefined(); } }); it('should handle pagination parameters', async () => { const res = await axios.get(`/v2/conversations?limit=5&offset=0`, { headers: { 'x-api-key': validApiKey } }); expect(res.status).toBe(200); expect(res.data).toHaveProperty('conversations'); expect(res.data.conversations.length).toBeLessThanOrEqual(5); }); }); describe('POST /v2/conversations', () => { it('should create conversation with valid data', async () => { const conversationData = { replica_id: 'rmeojzs1lhx', persona_id: 'pmeojzs1l', conversation_name: 'Test Conversation' }; const res = await axios.post(`/v2/conversations`, conversationData, { headers: { 'x-api-key': validApiKey, 'Content-Type': 'application/json' } }); expect(res.status).toBe(201); expect(res.data).toHaveProperty('conversation_id'); expect(res.data).toHaveProperty('persona_id', conversationData.persona_id); expect(res.data).toHaveProperty('conversation_name', conversationData.conversation_name); expect(res.data).toHaveProperty('status', 'active'); expect(res.data).toHaveProperty('created_at'); expect(res.data).toHaveProperty('updated_at'); }); it('should reject missing required fields', async () => { try { await axios.post(`/v2/conversations`, {}, { headers: { 'x-api-key': validApiKey, 'Content-Type': 'application/json' } }); fail('Should have thrown an error'); } catch (error: unknown) { const axiosError = error as { response?: { status?: number; data?: { error?: unknown } } }; expect(axiosError.response?.status).toBe(400); expect(axiosError.response?.data?.error).toBeDefined(); } }); }); }); });