Download packages/app/src/utils/server-protocol.test.ts from SaylorTwift/opencode: direct link, hf CLI and curl.
- Browser
- Download file 1.82 kB
-
https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/app/src/utils/server-protocol.test.ts
- Command line
-
hf download hf://SaylorTwift/opencode/packages/app/src/utils/server-protocol.test.ts
-
curl -L -o server-protocol.test.ts https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/app/src/utils/server-protocol.test.ts
1.82 kB
| import { describe, expect, test } from "bun:test" | |
| import { detectServerProtocol } from "./server-protocol" | |
| const server = { url: "http://localhost:4096" } | |
| const json = (value: unknown, status = 200) => | |
| new Response(JSON.stringify(value), { status, headers: { "content-type": "application/json" } }) | |
| const mockFetch = (run: (input: string | URL | Request) => Promise<Response>) => | |
| Object.assign(run, { preconnect: globalThis.fetch.preconnect }) | |
| describe("detectServerProtocol", () => { | |
| test("prefers the legacy health endpoint when both API generations exist", async () => { | |
| const fetcher = mockFetch((input) => { | |
| const path = new URL(input instanceof Request ? input.url : input).pathname | |
| if (path === "/global/health") return Promise.resolve(json({ healthy: true, version: "1.18.4" })) | |
| return Promise.resolve(json({ healthy: true, version: "2.0.0", pid: 123 })) | |
| }) | |
| expect(await detectServerProtocol(server, fetcher)).toBe("v1") | |
| }) | |
| test("recognizes V2 health by its process identifier", async () => { | |
| const fetcher = mockFetch((input) => { | |
| const path = new URL(input instanceof Request ? input.url : input).pathname | |
| if (path === "/global/health") return Promise.resolve(json({}, 404)) | |
| return Promise.resolve(json({ healthy: true, version: "2.0.0", pid: 123 })) | |
| }) | |
| expect(await detectServerProtocol(server, fetcher)).toBe("v2") | |
| }) | |
| test("recognizes the transitional V1 API health response", async () => { | |
| const fetcher = mockFetch((input) => { | |
| const path = new URL(input instanceof Request ? input.url : input).pathname | |
| if (path === "/global/health") return Promise.resolve(json({}, 404)) | |
| return Promise.resolve(json({ healthy: true })) | |
| }) | |
| expect(await detectServerProtocol(server, fetcher)).toBe("v1") | |
| }) | |
| }) | |