| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { makeProviderReplayGate } from "./replayGate.ts"; |
| 4 | |
| 5 | describe("makeProviderReplayGate", () => { |
| 6 | it("holds a gated emit until its label is released", async () => { |
| 7 | const gate = makeProviderReplayGate(["approval"]); |
| 8 | let emitted = false; |
| 9 | const emit = gate.beforeEmit("approval").then(() => { |
| 10 | emitted = true; |
| 11 | }); |
| 12 | |
| 13 | expect(await gate.waitForReached("approval")).toBe(true); |
| 14 | expect(emitted).toBe(false); |
| 15 | expect(gate.release("approval")).toBe(true); |
| 16 | await emit; |
| 17 | expect(emitted).toBe(true); |
| 18 | expect(gate.release("approval")).toBe(false); |
| 19 | }); |
| 20 | |
| 21 | it("passes ungated labels through and stops waiting when the emit is aborted", async () => { |
| 22 | const gate = makeProviderReplayGate(["approval"]); |
| 23 | await gate.beforeEmit(undefined); |
| 24 | await gate.beforeEmit("other"); |
| 25 | |
| 26 | const controller = new AbortController(); |
| 27 | const emit = gate.beforeEmit("approval", controller.signal); |
| 28 | controller.abort(); |
| 29 | await emit; |
| 30 | expect(gate.hasReached("approval")).toBe(true); |
| 31 | }); |
| 32 | |
| 33 | it("rejects duplicate labels", () => { |
| 34 | expect(() => makeProviderReplayGate(["approval", "approval"])).toThrow( |
| 35 | "Duplicate provider replay gate label approval.", |
| 36 | ); |
| 37 | }); |
| 38 | }); |