|
| 1 | +import { describe, it, expect, afterEach, vi } from "vitest"; |
| 2 | +import { cleanup, screen, waitFor } from "@testing-library/react"; |
| 3 | +import { ServerStatusClient, type StatusCapabilitiesConfig } from "../server-status-client"; |
| 4 | +import { renderWithLibProviders } from "../../../../test/render-with-providers"; |
| 5 | + |
| 6 | +const HEIC_CONFIG: StatusCapabilitiesConfig = { |
| 7 | + endpoint: "/api/admin/capabilities", |
| 8 | + items: [ |
| 9 | + { |
| 10 | + id: "heicAvailable", |
| 11 | + label: "HEIC image decoding", |
| 12 | + availableText: "Available", |
| 13 | + unavailableText: "Not available", |
| 14 | + hint: "HEIC uploads will be rejected with 415 — check Dockerfile", |
| 15 | + }, |
| 16 | + ], |
| 17 | +}; |
| 18 | + |
| 19 | +function jsonResponse(body: unknown): Response { |
| 20 | + return { ok: true, json: () => Promise.resolve(body) } as unknown as Response; |
| 21 | +} |
| 22 | + |
| 23 | +/** Route fetch responses by URL; an unmapped URL rejects (surfacing test gaps). */ |
| 24 | +function stubFetch(handlers: Record<string, () => Promise<Response>>) { |
| 25 | + vi.stubGlobal( |
| 26 | + "fetch", |
| 27 | + vi.fn((input: RequestInfo | URL) => { |
| 28 | + const url = typeof input === "string" ? input : input.toString(); |
| 29 | + const handler = handlers[url]; |
| 30 | + if (!handler) { |
| 31 | + return Promise.reject(new Error(`unexpected fetch: ${url}`)); |
| 32 | + } |
| 33 | + return handler(); |
| 34 | + }), |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + cleanup(); |
| 40 | + vi.unstubAllGlobals(); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("ServerStatusClient", () => { |
| 44 | + it("renders only the backend health row when no capabilities config is given", async () => { |
| 45 | + stubFetch({ |
| 46 | + "/api/health": () => Promise.resolve(jsonResponse({ status: "UP" })), |
| 47 | + }); |
| 48 | + |
| 49 | + renderWithLibProviders(<ServerStatusClient />); |
| 50 | + |
| 51 | + await waitFor(() => expect(screen.getByText("System Status")).toBeInTheDocument()); |
| 52 | + expect(screen.queryByText("HEIC image decoding")).not.toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("renders a green capability row from the fetched capabilities map", async () => { |
| 56 | + stubFetch({ |
| 57 | + "/api/health": () => Promise.resolve(jsonResponse({ status: "UP" })), |
| 58 | + "/api/admin/capabilities": () => Promise.resolve(jsonResponse({ heicAvailable: true })), |
| 59 | + }); |
| 60 | + |
| 61 | + renderWithLibProviders(<ServerStatusClient capabilities={HEIC_CONFIG} />); |
| 62 | + |
| 63 | + await waitFor(() => expect(screen.getByText("HEIC image decoding")).toBeInTheDocument()); |
| 64 | + expect(screen.getByText("Available")).toBeInTheDocument(); |
| 65 | + // Backend health row is still present. |
| 66 | + expect(screen.getByText("System Status")).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("renders the capability as available=false when the endpoint reports it unavailable", async () => { |
| 70 | + stubFetch({ |
| 71 | + "/api/health": () => Promise.resolve(jsonResponse({ status: "UP" })), |
| 72 | + "/api/admin/capabilities": () => Promise.resolve(jsonResponse({ heicAvailable: false })), |
| 73 | + }); |
| 74 | + |
| 75 | + renderWithLibProviders(<ServerStatusClient capabilities={HEIC_CONFIG} />); |
| 76 | + |
| 77 | + await waitFor(() => expect(screen.getByText("Not available")).toBeInTheDocument()); |
| 78 | + }); |
| 79 | + |
| 80 | + it("fails safe to unavailable when the capabilities fetch rejects", async () => { |
| 81 | + stubFetch({ |
| 82 | + "/api/health": () => Promise.resolve(jsonResponse({ status: "UP" })), |
| 83 | + "/api/admin/capabilities": () => Promise.reject(new Error("network down")), |
| 84 | + }); |
| 85 | + |
| 86 | + renderWithLibProviders(<ServerStatusClient capabilities={HEIC_CONFIG} />); |
| 87 | + |
| 88 | + await waitFor(() => expect(screen.getByText("Not available")).toBeInTheDocument()); |
| 89 | + expect(screen.queryByText("Available")).not.toBeInTheDocument(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("fails safe to unavailable when the capabilities endpoint returns a non-ok response", async () => { |
| 93 | + stubFetch({ |
| 94 | + "/api/health": () => Promise.resolve(jsonResponse({ status: "UP" })), |
| 95 | + "/api/admin/capabilities": () => |
| 96 | + Promise.resolve({ ok: false, json: () => Promise.resolve(null) } as unknown as Response), |
| 97 | + }); |
| 98 | + |
| 99 | + renderWithLibProviders(<ServerStatusClient capabilities={HEIC_CONFIG} />); |
| 100 | + |
| 101 | + await waitFor(() => expect(screen.getByText("Not available")).toBeInTheDocument()); |
| 102 | + }); |
| 103 | +}); |
0 commit comments