/** * Test file for Tavus cost control system */ import { TavusCostController } from '../shared/utils/api-clients/tavus-cost-controller'; describe('TavusCostController', () => { let costController: TavusCostController; beforeEach(() => { costController = new TavusCostController({ isDevelopmentMode: true, maxConversationDuration: 30, autoEndConversations: true, usageLimit: 5, rateLimitDelay: 1000, enableMockMode: false, lowCreditThreshold: 20 }); costController.activate(); }); afterEach(() => { costController.deactivate(); }); describe('Development Mode Detection', () => { it('should detect development mode correctly', () => { expect(costController.shouldApplyCostControls()).toBe(true); }); it('should not apply cost controls when deactivated', () => { costController.deactivate(); expect(costController.shouldApplyCostControls()).toBe(false); }); }); describe('API Call Tracking', () => { it('should track API calls and respect usage limits', () => { // First 5 calls should succeed for (let i = 0; i < 5; i++) { expect(costController.trackApiCall('create_conversation')).toBe(true); } // 6th call should fail due to usage limit expect(costController.trackApiCall('create_conversation')).toBe(false); }); it('should track different call types with different credit estimates', () => { expect(costController.trackApiCall('create_conversation')).toBe(true); expect(costController.trackApiCall('get_conversation')).toBe(true); expect(costController.trackApiCall('video_stream')).toBe(true); const stats = costController.getUsageStats(); expect(stats.totalApiCalls).toBe(3); expect(stats.estimatedCreditsUsed).toBeGreaterThan(0); }); it('should respect rate limiting', () => { expect(costController.trackApiCall('create_conversation')).toBe(true); // Immediate second call should be rate limited expect(costController.trackApiCall('create_conversation')).toBe(false); }); }); describe('Conversation Monitoring', () => { it('should start and stop conversation monitoring', () => { const conversationId = 'test-conversation-123'; costController.startConversationMonitoring(conversationId); // Check that monitoring is active const stats = costController.getUsageStats(); expect(stats.conversationStartTime).toBeDefined(); costController.stopConversationMonitoring(); // Check that monitoring is stopped const updatedStats = costController.getUsageStats(); expect(updatedStats.conversationStartTime).toBeUndefined(); }); }); describe('Credit Warning System', () => { it('should warn when credits are low', () => { const remainingCredits = 5; const totalCredits = 100; expect(costController.shouldWarnLowCredits(remainingCredits, totalCredits)).toBe(true); const warning = costController.getLowCreditWarning(remainingCredits, totalCredits); expect(warning).toContain('5/100'); expect(warning).toContain('5.0%'); }); it('should not warn when credits are sufficient', () => { const remainingCredits = 50; const totalCredits = 100; expect(costController.shouldWarnLowCredits(remainingCredits, totalCredits)).toBe(false); }); }); describe('Configuration Management', () => { it('should update configuration correctly', () => { costController.updateConfig({ usageLimit: 10, maxConversationDuration: 60 }); const config = costController.getConfig(); expect(config.usageLimit).toBe(10); expect(config.maxConversationDuration).toBe(60); }); it('should reset usage statistics', () => { costController.trackApiCall('create_conversation'); costController.trackApiCall('get_conversation'); const statsBefore = costController.getUsageStats(); expect(statsBefore.totalApiCalls).toBe(2); costController.resetUsageStats(); const statsAfter = costController.getUsageStats(); expect(statsAfter.totalApiCalls).toBe(0); expect(statsAfter.currentSessionCalls).toBe(0); expect(statsAfter.estimatedCreditsUsed).toBe(0); }); }); describe('Mock Mode', () => { it('should enable mock mode when configured', () => { costController.updateConfig({ enableMockMode: true }); expect(costController.isMockModeEnabled()).toBe(true); }); it('should disable mock mode when not in development mode', () => { costController.updateConfig({ isDevelopmentMode: false, enableMockMode: true }); expect(costController.isMockModeEnabled()).toBe(false); }); }); });