import { describe, expect, it } from 'vitest' import { limiter } from './client' describe('limiter', () => { it('runs at most n calls at once, all of them, failures included', async () => { const limit = limiter(2) let running = 0, peak = 0 const call = (n: number) => limit(async () => { peak = Math.max(peak, ++running) await new Promise(r => setTimeout(r, 5)) running-- if (n === 2) throw new Error('503') return n }) const out = await Promise.allSettled([1, 2, 3, 4, 5].map(call)) expect(peak).toBe(2) expect(out.map(r => (r.status === 'fulfilled' ? r.value : 'x'))).toEqual([1, 'x', 3, 4, 5]) }) })