import { describe, it, expect, vi, afterEach } from 'vitest';
import { rateLimit, _resetBuckets } from '../src/lib/rate-limit';

afterEach(() => { _resetBuckets(); vi.useRealTimers(); });

describe('rateLimit', () => {
  it('consente fino a max richieste', () => {
    expect(rateLimit('a', 2, 1000)).toBe(true);
    expect(rateLimit('a', 2, 1000)).toBe(true);
    expect(rateLimit('a', 2, 1000)).toBe(false);
  });
  it('chiavi indipendenti', () => {
    expect(rateLimit('a', 1, 1000)).toBe(true);
    expect(rateLimit('b', 1, 1000)).toBe(true);
  });
  it('la finestra scade', () => {
    vi.useFakeTimers();
    expect(rateLimit('a', 1, 1000)).toBe(true);
    expect(rateLimit('a', 1, 1000)).toBe(false);
    vi.advanceTimersByTime(1100);
    expect(rateLimit('a', 1, 1000)).toBe(true);
  });
});
