Download packages/app/src/utils/server-health.test.ts from SaylorTwift/opencode: direct link, hf CLI and curl.
- Browser
- Download file 6.21 kB
-
https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/app/src/utils/server-health.test.ts
- Command line
-
hf download hf://SaylorTwift/opencode/packages/app/src/utils/server-health.test.ts
-
curl -L -o server-health.test.ts https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/app/src/utils/server-health.test.ts
6.21 kB
| import { describe, expect, test } from "bun:test" | |
| import type { ServerConnection } from "@/context/server" | |
| import { checkServerHealth } from "./server-health" | |
| const server: ServerConnection.HttpBase = { | |
| url: "http://localhost:4096", | |
| } | |
| function abortFromInput(input: RequestInfo | URL, init?: RequestInit) { | |
| if (init?.signal) return init.signal | |
| if (input instanceof Request) return input.signal | |
| return undefined | |
| } | |
| describe("checkServerHealth", () => { | |
| test("returns healthy response with version", async () => { | |
| let request: URL | undefined | |
| const fetch = (async (input: RequestInfo | URL) => { | |
| request = input instanceof URL ? input : new URL(input instanceof Request ? input.url : input) | |
| return new Response(JSON.stringify({ healthy: true, version: "1.2.3" }), { | |
| status: 200, | |
| headers: { "content-type": "application/json" }, | |
| }) | |
| }) as unknown as typeof globalThis.fetch | |
| const result = await checkServerHealth(server, fetch) | |
| expect(result).toEqual({ healthy: true, version: "1.2.3" }) | |
| expect(request?.pathname).toBe("/api/health") | |
| }) | |
| test("falls back to the V1 health endpoint", async () => { | |
| const paths: string[] = [] | |
| const fetch = (async (input: RequestInfo | URL) => { | |
| const url = input instanceof URL ? input : new URL(input instanceof Request ? input.url : input) | |
| paths.push(url.pathname) | |
| if (url.pathname === "/api/health") return new Response(undefined, { status: 404 }) | |
| return Response.json({ healthy: true, version: "1.18.4" }) | |
| }) as unknown as typeof globalThis.fetch | |
| expect(await checkServerHealth(server, fetch)).toEqual({ healthy: true, version: "1.18.4" }) | |
| expect(paths).toEqual(["/api/health", "/global/health"]) | |
| }) | |
| test("falls back when the current health response is malformed", async () => { | |
| const paths: string[] = [] | |
| const fetch = (async (input: RequestInfo | URL) => { | |
| const url = input instanceof URL ? input : new URL(input instanceof Request ? input.url : input) | |
| paths.push(url.pathname) | |
| if (url.pathname === "/api/health") return Response.json({}) | |
| return Response.json({ healthy: true, version: "1.18.4" }) | |
| }) as unknown as typeof globalThis.fetch | |
| expect(await checkServerHealth(server, fetch)).toEqual({ healthy: true, version: "1.18.4" }) | |
| expect(paths).toEqual(["/api/health", "/global/health"]) | |
| }) | |
| test("allows slow servers thirty seconds by default", async () => { | |
| const timeout = Object.getOwnPropertyDescriptor(AbortSignal, "timeout") | |
| let timeoutMs = 0 | |
| Object.defineProperty(AbortSignal, "timeout", { | |
| configurable: true, | |
| value: (ms: number) => { | |
| timeoutMs = ms | |
| return new AbortController().signal | |
| }, | |
| }) | |
| const fetch = (async () => | |
| new Response(JSON.stringify({ healthy: true, version: "1.2.3" }), { | |
| status: 200, | |
| headers: { "content-type": "application/json" }, | |
| })) as unknown as typeof globalThis.fetch | |
| await checkServerHealth(server, fetch).finally(() => { | |
| if (timeout) Object.defineProperty(AbortSignal, "timeout", timeout) | |
| if (!timeout) Reflect.deleteProperty(AbortSignal, "timeout") | |
| }) | |
| expect(timeoutMs).toBe(30_000) | |
| }) | |
| test("returns unhealthy when request fails", async () => { | |
| const fetch = (async () => { | |
| throw new Error("network") | |
| }) as unknown as typeof globalThis.fetch | |
| const result = await checkServerHealth(server, fetch) | |
| expect(result).toEqual({ healthy: false }) | |
| }) | |
| test("uses timeout fallback when AbortSignal.timeout is unavailable", async () => { | |
| const timeout = Object.getOwnPropertyDescriptor(AbortSignal, "timeout") | |
| Object.defineProperty(AbortSignal, "timeout", { | |
| configurable: true, | |
| value: undefined, | |
| }) | |
| let aborted = false | |
| const fetch = ((input: RequestInfo | URL, init?: RequestInit) => | |
| new Promise<Response>((_resolve, reject) => { | |
| const signal = abortFromInput(input, init) | |
| signal?.addEventListener( | |
| "abort", | |
| () => { | |
| aborted = true | |
| reject(new DOMException("Aborted", "AbortError")) | |
| }, | |
| { once: true }, | |
| ) | |
| })) as unknown as typeof globalThis.fetch | |
| const result = await checkServerHealth(server, fetch, { | |
| timeoutMs: 10, | |
| }).finally(() => { | |
| if (timeout) Object.defineProperty(AbortSignal, "timeout", timeout) | |
| if (!timeout) Reflect.deleteProperty(AbortSignal, "timeout") | |
| }) | |
| expect(aborted).toBe(true) | |
| expect(result).toEqual({ healthy: false }) | |
| }) | |
| test("uses provided abort signal", async () => { | |
| let signal: AbortSignal | undefined | |
| const fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { | |
| signal = abortFromInput(input, init) | |
| return new Response(JSON.stringify({ healthy: true, version: "1.2.3" }), { | |
| status: 200, | |
| headers: { "content-type": "application/json" }, | |
| }) | |
| }) as unknown as typeof globalThis.fetch | |
| const abort = new AbortController() | |
| await checkServerHealth(server, fetch, { | |
| signal: abort.signal, | |
| }) | |
| expect(signal).toBe(abort.signal) | |
| }) | |
| test("retries transient failures and eventually succeeds", async () => { | |
| let count = 0 | |
| const fetch = (async () => { | |
| count += 1 | |
| if (count < 3) throw new TypeError("network") | |
| return new Response(JSON.stringify({ healthy: true, version: "1.2.3" }), { | |
| status: 200, | |
| headers: { "content-type": "application/json" }, | |
| }) | |
| }) as unknown as typeof globalThis.fetch | |
| const result = await checkServerHealth(server, fetch, { | |
| retryCount: 2, | |
| retryDelayMs: 1, | |
| }) | |
| expect(count).toBe(3) | |
| expect(result).toEqual({ healthy: true, version: "1.2.3" }) | |
| }) | |
| test("returns unhealthy when retries are exhausted", async () => { | |
| let count = 0 | |
| const fetch = (async () => { | |
| count += 1 | |
| throw new TypeError("network") | |
| }) as unknown as typeof globalThis.fetch | |
| const result = await checkServerHealth(server, fetch, { | |
| retryCount: 2, | |
| retryDelayMs: 1, | |
| }) | |
| expect(count).toBe(6) | |
| expect(result).toEqual({ healthy: false }) | |
| }) | |
| }) | |