|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +interface GrafanaFetchMock { |
| 4 | + fetcher: typeof fetch; |
| 5 | + urls: string[]; |
| 6 | +} |
| 7 | + |
| 8 | +function createGrafanaFetchMock(handler: (url: URL) => Response): GrafanaFetchMock { |
| 9 | + const urls: string[] = []; |
| 10 | + const fetcher = (async (input: RequestInfo | URL): Promise<Response> => { |
| 11 | + const url = new URL(input instanceof Request ? input.url : String(input)); |
| 12 | + urls.push(url.toString()); |
| 13 | + return handler(url); |
| 14 | + }) as typeof fetch; |
| 15 | + return { fetcher, urls }; |
| 16 | +} |
| 17 | + |
| 18 | +function jsonResponse(value: unknown, status = 200): Response { |
| 19 | + return new Response(JSON.stringify(value), { |
| 20 | + status, |
| 21 | + headers: { "content-type": "application/json" }, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +describe("Grafana App Platform API version discovery", () => { |
| 26 | + it("prefers v1 and caches a successfully discovered version", async () => { |
| 27 | + vi.resetModules(); |
| 28 | + const { grafanaActionHandlers } = await import("./runtime.ts"); |
| 29 | + let discoveryRequests = 0; |
| 30 | + const mock = createGrafanaFetchMock((url) => { |
| 31 | + if (url.pathname === "/apis/folder.grafana.app") { |
| 32 | + discoveryRequests += 1; |
| 33 | + return jsonResponse({ |
| 34 | + versions: [{ version: "v0alpha1" }, { version: "v1beta1" }, { version: "v1" }], |
| 35 | + }); |
| 36 | + } |
| 37 | + return jsonResponse({ items: [], metadata: {} }); |
| 38 | + }); |
| 39 | + const context = { |
| 40 | + baseUrl: "https://grafana-v1.example", |
| 41 | + apiKey: "test-token", |
| 42 | + fetcher: mock.fetcher, |
| 43 | + }; |
| 44 | + |
| 45 | + await grafanaActionHandlers.list_folders({}, context); |
| 46 | + await grafanaActionHandlers.list_folders({}, context); |
| 47 | + |
| 48 | + expect(discoveryRequests).toBe(1); |
| 49 | + expect(mock.urls).toEqual([ |
| 50 | + "https://grafana-v1.example/apis/folder.grafana.app", |
| 51 | + "https://grafana-v1.example/apis/folder.grafana.app/v1/namespaces/default/folders", |
| 52 | + "https://grafana-v1.example/apis/folder.grafana.app/v1/namespaces/default/folders", |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("retries discovery after a failure instead of caching the v1 fallback", async () => { |
| 57 | + vi.resetModules(); |
| 58 | + const { grafanaActionHandlers } = await import("./runtime.ts"); |
| 59 | + let discoveryRequests = 0; |
| 60 | + const mock = createGrafanaFetchMock((url) => { |
| 61 | + if (url.pathname === "/apis/folder.grafana.app") { |
| 62 | + discoveryRequests += 1; |
| 63 | + if (discoveryRequests === 1) { |
| 64 | + return jsonResponse({ message: "temporary failure" }, 500); |
| 65 | + } |
| 66 | + return jsonResponse({ versions: [{ version: "v1beta1" }] }); |
| 67 | + } |
| 68 | + if (url.pathname.includes("/v1beta1/")) { |
| 69 | + return jsonResponse({ items: [], metadata: {} }); |
| 70 | + } |
| 71 | + return jsonResponse({ message: "unsupported version" }, 404); |
| 72 | + }); |
| 73 | + const context = { |
| 74 | + baseUrl: "https://grafana-v1beta1.example", |
| 75 | + apiKey: "test-token", |
| 76 | + fetcher: mock.fetcher, |
| 77 | + }; |
| 78 | + |
| 79 | + await expect(grafanaActionHandlers.list_folders({}, context)).rejects.toThrow("unsupported version"); |
| 80 | + await expect(grafanaActionHandlers.list_folders({}, context)).resolves.toMatchObject({ folders: [] }); |
| 81 | + |
| 82 | + expect(discoveryRequests).toBe(2); |
| 83 | + expect(mock.urls).toEqual([ |
| 84 | + "https://grafana-v1beta1.example/apis/folder.grafana.app", |
| 85 | + "https://grafana-v1beta1.example/apis/folder.grafana.app/v1/namespaces/default/folders", |
| 86 | + "https://grafana-v1beta1.example/apis/folder.grafana.app", |
| 87 | + "https://grafana-v1beta1.example/apis/folder.grafana.app/v1beta1/namespaces/default/folders", |
| 88 | + ]); |
| 89 | + }); |
| 90 | +}); |
0 commit comments