mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/mihomo-party-org/clash-party.git
synced 2026-09-20 08:03:39 +08:00
29 lines
937 B
JavaScript
29 lines
937 B
JavaScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { createRateLimiter } from './ratelimit.mjs'
|
|
|
|
test('allows up to max hits then blocks within the window', () => {
|
|
let clock = 0
|
|
const rl = createRateLimiter({ max: 3, windowMs: 1000, now: () => clock })
|
|
assert.equal(rl.hit('ip'), true)
|
|
assert.equal(rl.hit('ip'), true)
|
|
assert.equal(rl.hit('ip'), true)
|
|
assert.equal(rl.hit('ip'), false) // 4th in window
|
|
})
|
|
|
|
test('resets after the window elapses', () => {
|
|
let clock = 0
|
|
const rl = createRateLimiter({ max: 1, windowMs: 1000, now: () => clock })
|
|
assert.equal(rl.hit('ip'), true)
|
|
assert.equal(rl.hit('ip'), false)
|
|
clock += 1001
|
|
assert.equal(rl.hit('ip'), true)
|
|
})
|
|
|
|
test('tracks keys independently', () => {
|
|
const rl = createRateLimiter({ max: 1, windowMs: 1000, now: () => 0 })
|
|
assert.equal(rl.hit('a'), true)
|
|
assert.equal(rl.hit('b'), true)
|
|
assert.equal(rl.hit('a'), false)
|
|
})
|