Menu
popagent
publicLatest change 6edc7e928c57dab23c0973ed3f7ee70b3ebb72d5 - Initial popagent baseline by AkurAI Build
import { describe, expect, test } from "bun:test";
import { singleFlight } from "./single-flight";
describe("singleFlight", () => {
test("shares one in-flight operation across rapid calls", async () => {
let calls = 0;
let resolve!: (value: string) => void;
const operation = singleFlight(() => {
calls++;
return new Promise<string>((done) => { resolve = done; });
});
const first = operation();
const second = operation();
expect(calls).toBe(1);
expect(second).toBe(first);
resolve("created");
expect(await first).toBe("created");
const third = operation();
expect(calls).toBe(2);
resolve("created-again");
expect(await third).toBe("created-again");
expect(calls).toBe(2);
});
test("allows retry after failure", async () => {
let calls = 0;
const operation = singleFlight(async () => {
if (++calls === 1) throw new Error("temporary");
return "created";
});
await expect(operation()).rejects.toThrow("temporary");
await expect(operation()).resolves.toBe("created");
});
});