import { describe, it, expect, beforeAll } from '@jest/globals';
import { EmailService, SMSService, PushService } from '@shieldai/shared-notifications';

describe('Notification Performance Benchmarks', () => {
  let emailService: EmailService;
  let smsService: SMSService;
  let pushService: PushService;

  beforeAll(() => {
    emailService = EmailService.getInstance();
    smsService = SMSService.getInstance();
    pushService = PushService.getInstance();
  });

  describe('Rate Limit Checks', () => {
    it('should check email rate limit within 1ms', async () => {
      const iterations = 1000;
      const startTime = performance.now();

      for (let i = 0; i < iterations; i++) {
        emailService.getRateLimitStatus();
      }

      const endTime = performance.now();
      const avgTime = (endTime - startTime) / iterations;

      expect(avgTime).toBeLessThan(1);
    });

    it('should check SMS rate limit within 1ms', async () => {
      const iterations = 1000;
      const startTime = performance.now();

      for (let i = 0; i < iterations; i++) {
        smsService.getRateLimitStatus();
      }

      const endTime = performance.now();
      const avgTime = (endTime - startTime) / iterations;

      expect(avgTime).toBeLessThan(1);
    });

    it('should check push rate limit within 1ms', async () => {
      const iterations = 1000;
      const startTime = performance.now();

      for (let i = 0; i < iterations; i++) {
        pushService.getRateLimitStatus();
      }

      const endTime = performance.now();
      const avgTime = (endTime - startTime) / iterations;

      expect(avgTime).toBeLessThan(1);
    });
  });

  describe('Concurrency', () => {
    it('should handle 100 concurrent rate limit checks', async () => {
      const promises = Array.from({ length: 100 }, () =>
        emailService.getRateLimitStatus()
      );

      const startTime = performance.now();
      const results = await Promise.all(promises);
      const endTime = performance.now();

      expect(results).toHaveLength(100);
      expect(endTime - startTime).toBeLessThan(50);
    });
  });
});
