server-protocol.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { describe, expect, test } from "bun:test"
  2. import { detectServerProtocol } from "./server-protocol"
  3. const server = { url: "http://localhost:4096" }
  4. const json = (value: unknown, status = 200) =>
  5. new Response(JSON.stringify(value), { status, headers: { "content-type": "application/json" } })
  6. const mockFetch = (run: (input: string | URL | Request) => Promise<Response>) =>
  7. Object.assign(run, { preconnect: globalThis.fetch.preconnect })
  8. describe("detectServerProtocol", () => {
  9. test("prefers the legacy health endpoint when both API generations exist", async () => {
  10. const fetcher = mockFetch((input) => {
  11. const path = new URL(input instanceof Request ? input.url : input).pathname
  12. if (path === "/global/health") return Promise.resolve(json({ healthy: true, version: "1.18.4" }))
  13. return Promise.resolve(json({ healthy: true, version: "2.0.0", pid: 123 }))
  14. })
  15. expect(await detectServerProtocol(server, fetcher)).toBe("v1")
  16. })
  17. test("recognizes V2 health by its process identifier", async () => {
  18. const fetcher = mockFetch((input) => {
  19. const path = new URL(input instanceof Request ? input.url : input).pathname
  20. if (path === "/global/health") return Promise.resolve(json({}, 404))
  21. return Promise.resolve(json({ healthy: true, version: "2.0.0", pid: 123 }))
  22. })
  23. expect(await detectServerProtocol(server, fetcher)).toBe("v2")
  24. })
  25. test("recognizes the transitional V1 API health response", async () => {
  26. const fetcher = mockFetch((input) => {
  27. const path = new URL(input instanceof Request ? input.url : input).pathname
  28. if (path === "/global/health") return Promise.resolve(json({}, 404))
  29. return Promise.resolve(json({ healthy: true }))
  30. })
  31. expect(await detectServerProtocol(server, fetcher)).toBe("v1")
  32. })
  33. })