import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { createMockRequest } from '../tests/utils/createMockRequest';

describe('rateLimit utils', () => {
  beforeEach(() => {
    vi.resetModules();
    vi.unstubAllEnvs();
  });

  afterEach(() => {
    vi.unstubAllEnvs();
  });

  describe('shouldSkipRateLimit', () => {
    it('should return false (do not skip) when NODE_ENV is "production"', async () => {
      vi.stubEnv('NODE_ENV', 'production');
      const { shouldSkipRateLimit } = await import('./rateLimit');

      const req = createMockRequest({ headers: {} });
      expect(shouldSkipRateLimit(req)).toBe(false);
    });

    it('should return true (skip) when NODE_ENV is "development"', async () => {
      vi.stubEnv('NODE_ENV', 'development');
      const { shouldSkipRateLimit } = await import('./rateLimit');

      const req = createMockRequest({ headers: {} });
      expect(shouldSkipRateLimit(req)).toBe(true);
    });

    it('should return true (skip) when NODE_ENV is "staging"', async () => {
      vi.stubEnv('NODE_ENV', 'staging');
      const { shouldSkipRateLimit } = await import('./rateLimit');

      const req = createMockRequest({ headers: {} });
      expect(shouldSkipRateLimit(req)).toBe(true);
    });

    it('should return true (skip) when NODE_ENV is "test" and header is missing', async () => {
      vi.stubEnv('NODE_ENV', 'test');
      const { shouldSkipRateLimit } = await import('./rateLimit');

      const req = createMockRequest({ headers: {} });
      expect(shouldSkipRateLimit(req)).toBe(true);
    });

    it('should return false (do not skip) when NODE_ENV is "test" and header is "true"', async () => {
      vi.stubEnv('NODE_ENV', 'test');
      const { shouldSkipRateLimit } = await import('./rateLimit');

      const req = createMockRequest({
        headers: { 'x-test-rate-limit-enable': 'true' },
      });
      expect(shouldSkipRateLimit(req)).toBe(false);
    });

    it('should return true (skip) when NODE_ENV is "test" and header is "false"', async () => {
      vi.stubEnv('NODE_ENV', 'test');
      const { shouldSkipRateLimit } = await import('./rateLimit');

      const req = createMockRequest({
        headers: { 'x-test-rate-limit-enable': 'false' },
      });
      expect(shouldSkipRateLimit(req)).toBe(true);
    });

    it('should return false (do not skip) when NODE_ENV is "development" and header is "true"', async () => {
      vi.stubEnv('NODE_ENV', 'development');
      const { shouldSkipRateLimit } = await import('./rateLimit');

      const req = createMockRequest({
        headers: { 'x-test-rate-limit-enable': 'true' },
      });
      expect(shouldSkipRateLimit(req)).toBe(false);
    });
  });
});
