import { describe, expect, it } from 'vitest';

import { RateLimiter } from '../packages/connectors/pacifica/src/rateLimiter';

describe('RateLimiter', () => {
  it('limits the number of requests per interval', async () => {
    const limiter = new RateLimiter({ capacity: 2, refillAmount: 2, refillIntervalMs: 50 });

    const start = Date.now();
    await limiter.acquire();
    await limiter.acquire();
    const p = limiter.acquire();
    const elapsed = Date.now() - start;
    expect(elapsed).toBeLessThan(10);
    await p;
    const totalElapsed = Date.now() - start;
    expect(totalElapsed).toBeGreaterThanOrEqual(50);
  });
});
