import assert from 'node:assert/strict';
import test from 'node:test';
import { createIpRateLimiter } from '../src/rateLimits.js';

test('limits requests per IP within the configured window', () => {
  const limiter = createIpRateLimiter({
    windowMs: 60_000,
    max: 2,
    message: 'Too many reads.'
  });

  assert.equal(limiter.check('203.0.113.8').remaining, 1);
  assert.equal(limiter.check('203.0.113.8').remaining, 0);
  assert.equal(limiter.check('203.0.113.9').remaining, 1);

  assert.throws(
    () => limiter.check('203.0.113.8'),
    (error) => error.statusCode === 429 && error.message === 'Too many reads.'
  );
});
