🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { describe, expect, it } from 'vitest';
import { mapWithConcurrency } from '../concurrency';
describe('mapWithConcurrency', () => {
it('keeps results in input order regardless of completion order', async () => {
const out = await mapWithConcurrency([30, 10, 20, 0], 2, async (ms, i) => {
await new Promise((r) => setTimeout(r, ms));
return `${i}:${ms}`;
});
expect(out).toEqual(['0:30', '1:10', '2:20', '3:0']);
});
it('never exceeds the ceiling', async () => {
let inFlight = 0;
let peak = 0;
await mapWithConcurrency(
Array.from({ length: 20 }, (_, i) => i),
4,
async () => {
inFlight += 1;
peak = Math.max(peak, inFlight);
await new Promise((r) => setTimeout(r, 5));
inFlight -= 1;
return null;
},
);
expect(peak).toBeLessThanOrEqual(4);
expect(peak).toBeGreaterThan(1);
});
it('processes every item exactly once', async () => {
const seen: number[] = [];
await mapWithConcurrency(
Array.from({ length: 50 }, (_, i) => i),
7,
async (n) => {
await new Promise((r) => setTimeout(r, 1));
seen.push(n);
return n;
},
);
expect(seen.sort((a, b) => a - b)).toEqual(Array.from({ length: 50 }, (_, i) => i));
});
it('handles an empty list and a limit larger than the list', async () => {
expect(await mapWithConcurrency([], 5, async () => 1)).toEqual([]);
expect(await mapWithConcurrency([1, 2], 99, async (n) => n * 2)).toEqual([2, 4]);
});
});