Open History to see the last commit
1import { describe, expect, it, vi } from "vitest";
2
3import {
4 BackendReadinessAbortedError,
5 isBackendReadinessAborted,
6 waitForHttpReady,
7} from "./backendReadiness";
8
9describe("waitForHttpReady", () => {
10 it("returns once the backend serves the requested readiness path", async () => {
11 const fetchImpl = vi
12 .fn<typeof fetch>()
13 .mockResolvedValueOnce(new Response(null, { status: 503 }))
14 .mockResolvedValueOnce(new Response(null, { status: 200 }));
15
16 await waitForHttpReady("http://127.0.0.1:3773", {
17 fetchImpl,
18 timeoutMs: 1_000,
19 intervalMs: 0,
20 });
21
22 expect(fetchImpl).toHaveBeenCalledTimes(2);
23 expect(fetchImpl).toHaveBeenNthCalledWith(
24 1,
25 "http://127.0.0.1:3773/",
26 expect.objectContaining({ redirect: "manual" }),
27 );
28 });
29
30 it("retries after a readiness request stalls past the per-request timeout", async () => {
31 const fetchImpl = vi
32 .fn<typeof fetch>()
33 .mockImplementationOnce(
34 (_input, init) =>
35 new Promise((_resolve, reject) => {
36 init?.signal?.addEventListener(
37 "abort",
38 () => {
39 reject(new Error("request timed out"));
40 },
41 { once: true },
42 );
43 }) as ReturnType<typeof fetch>,
44 )
45 .mockResolvedValueOnce(new Response(null, { status: 200 }));
46
47 await waitForHttpReady("http://127.0.0.1:3773", {
48 fetchImpl,
49 timeoutMs: 100,
50 intervalMs: 0,
51 requestTimeoutMs: 1,
52 });
53
54 expect(fetchImpl).toHaveBeenCalledTimes(2);
55 });
56
57 it("aborts an in-flight readiness wait", async () => {
58 const controller = new AbortController();
59 const fetchImpl = vi.fn<typeof fetch>().mockImplementation(
60 () =>
61 new Promise((_resolve, reject) => {
62 controller.signal.addEventListener(
63 "abort",
64 () => {
65 reject(new BackendReadinessAbortedError());
66 },
67 { once: true },
68 );
69 }) as ReturnType<typeof fetch>,
70 );
71
72 const waitPromise = waitForHttpReady("http://127.0.0.1:3773", {
73 fetchImpl,
74 timeoutMs: 1_000,
75 intervalMs: 0,
76 signal: controller.signal,
77 });
78
79 controller.abort();
80
81 await expect(waitPromise).rejects.toBeInstanceOf(BackendReadinessAbortedError);
82 });
83
84 it("recognizes aborted readiness errors", () => {
85 expect(isBackendReadinessAborted(new BackendReadinessAbortedError())).toBe(true);
86 expect(isBackendReadinessAborted(new Error("nope"))).toBe(false);
87 });
88
89 it("supports custom readiness predicates", async () => {
90 const fetchImpl = vi
91 .fn<typeof fetch>()
92 .mockResolvedValueOnce(new Response(null, { status: 200 }))
93 .mockResolvedValueOnce(new Response(null, { status: 204 }));
94
95 await waitForHttpReady("http://127.0.0.1:3773", {
96 fetchImpl,
97 timeoutMs: 1_000,
98 intervalMs: 0,
99 path: "/api/healthz",
100 isReady: (response) => response.status === 204,
101 });
102
103 expect(fetchImpl).toHaveBeenNthCalledWith(
104 1,
105 "http://127.0.0.1:3773/api/healthz",
106 expect.objectContaining({ redirect: "manual" }),
107 );
108 expect(fetchImpl).toHaveBeenNthCalledWith(
109 2,
110 "http://127.0.0.1:3773/api/healthz",
111 expect.objectContaining({ redirect: "manual" }),
112 );
113 });
114});