Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

backendReadiness.test.ts

83 lines2.4 KB

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 reports a successful session endpoint", 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 });
24​
25 it("retries after a readiness request stalls past the per-request timeout", async () => {
26 const fetchImpl = vi
27 .fn<typeof fetch>()
28 .mockImplementationOnce(
29 (_input, init) =>
30 new Promise((_resolve, reject) => {
31 init?.signal?.addEventListener(
32 "abort",
33 () => {
34 reject(new Error("request timed out"));
35 },
36 { once: true },
37 );
38 }) as ReturnType<typeof fetch>,
39 )
40 .mockResolvedValueOnce(new Response(null, { status: 200 }));
41​
42 await waitForHttpReady("http://127.0.0.1:3773", {
43 fetchImpl,
44 timeoutMs: 100,
45 intervalMs: 0,
46 requestTimeoutMs: 1,
47 });
48​
49 expect(fetchImpl).toHaveBeenCalledTimes(2);
50 });
51​
52 it("aborts an in-flight readiness wait", async () => {
53 const controller = new AbortController();
54 const fetchImpl = vi.fn<typeof fetch>().mockImplementation(
55 () =>
56 new Promise((_resolve, reject) => {
57 controller.signal.addEventListener(
58 "abort",
59 () => {
60 reject(new BackendReadinessAbortedError());
61 },
62 { once: true },
63 );
64 }) as ReturnType<typeof fetch>,
65 );
66​
67 const waitPromise = waitForHttpReady("http://127.0.0.1:3773", {
68 fetchImpl,
69 timeoutMs: 1_000,
70 intervalMs: 0,
71 signal: controller.signal,
72 });
73​
74 controller.abort();
75​
76 await expect(waitPromise).rejects.toBeInstanceOf(BackendReadinessAbortedError);
77 });
78​
79 it("recognizes aborted readiness errors", () => {
80 expect(isBackendReadinessAborted(new BackendReadinessAbortedError())).toBe(true);
81 expect(isBackendReadinessAborted(new Error("nope"))).toBe(false);
82 });
83});