// The e2e suite's request wrapper (e2e/retryConnect.ts): a connect that never came about is tried // again, as every TCP stack would; any other failure, or an answer, goes straight to the test. import { describe, expect, it, vi } from "vitest"; import { retryConnect } from "../../e2e/retryConnect"; const noConnection = () => new Error("apiRequestContext.get: connect ETIMEDOUT 127.0.0.1:8080"); describe("retryConnect", () => { it("tries a connect that timed out again, and hands back the answer that came", async () => { vi.useFakeTimers(); const get = vi .fn() .mockRejectedValueOnce(noConnection()) .mockRejectedValueOnce(noConnection()) .mockResolvedValue("ok"); const api = retryConnect({ get }); const answer = api.get("/healthz"); await vi.runAllTimersAsync(); await expect(answer).resolves.toBe("ok"); expect(get).toHaveBeenCalledTimes(3); expect(get).toHaveBeenLastCalledWith("/healthz"); vi.useRealTimers(); }); it("passes any other failure straight on, untried", async () => { const post = vi.fn().mockRejectedValue(new Error("read ECONNRESET")); await expect(retryConnect({ post }).post("/auth/password-login")).rejects.toThrow("ECONNRESET"); expect(post).toHaveBeenCalledTimes(1); }); it("gives up after its last wait, with the connect error", async () => { vi.useFakeTimers(); const get = vi.fn().mockRejectedValue(noConnection()); const answer = retryConnect({ get }).get("/api/me"); const settled = expect(answer).rejects.toThrow("ETIMEDOUT"); await vi.runAllTimersAsync(); await settled; expect(get).toHaveBeenCalledTimes(6); // the first try and five more vi.useRealTimers(); }); });