diff --git a/apps/api-manager/src/routes/(protected)/metrics/+page.svelte b/apps/api-manager/src/routes/(protected)/metrics/+page.svelte index 75660346..a4cd0be9 100644 --- a/apps/api-manager/src/routes/(protected)/metrics/+page.svelte +++ b/apps/api-manager/src/routes/(protected)/metrics/+page.svelte @@ -204,9 +204,10 @@ // Transport: an explicit ?transport= param wins; otherwise default to // gRPC when the 'OBP API (gRPC)' health check reported healthy at load. // Decided before submitQuery so the URL it writes carries the right value. + // The URL is left to submitQuery: replaceState can't run before the router has started. const urlTransport = urlParams.get("transport"); if (urlTransport === "grpc" || (urlTransport !== "rest" && data.grpcAvailable)) { - setTransport("grpc"); + setTransport("grpc", { updateUrl: false }); } else { // REST with polling; user can switch to gRPC. startAutoRefresh(); @@ -500,13 +501,15 @@ }; } - function setTransport(next: "rest" | "grpc") { + function setTransport(next: "rest" | "grpc", { updateUrl = true } = {}) { if (next === transport) return; transport = next; - // Keep the choice in the page URL so reloads and shared links preserve it. - const pageUrl = new URL(window.location.href); - pageUrl.searchParams.set("transport", next); - replaceState(pageUrl, {}); + if (updateUrl) { + // Keep the choice in the page URL so reloads and shared links preserve it. + const pageUrl = new URL(window.location.href); + pageUrl.searchParams.set("transport", next); + replaceState(pageUrl, {}); + } if (next === "grpc") { stopAutoRefresh(); connectStream(); diff --git a/apps/api-manager/src/routes/(protected)/metrics/page.svelte.test.ts b/apps/api-manager/src/routes/(protected)/metrics/page.svelte.test.ts new file mode 100644 index 00000000..4489f60c --- /dev/null +++ b/apps/api-manager/src/routes/(protected)/metrics/page.svelte.test.ts @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2025-2026 TESOBE GmbH + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +// The metrics page picks its transport on first load. When gRPC is the default it must open the stream +// without calling replaceState, which SvelteKit can't run before its router has started. +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { render, fireEvent } from '@testing-library/svelte'; +import { readable } from 'svelte/store'; + +const navigation = vi.hoisted(() => ({ goto: vi.fn(), replaceState: vi.fn(), invalidate: vi.fn() })); +vi.mock('$app/navigation', () => navigation); +vi.mock('$app/stores', () => ({ + page: readable({ data: { externalLinks: {} }, url: new URL('http://localhost/metrics') }) +})); +vi.mock('$lib/utils/toastService', () => ({ toast: { info: vi.fn(), error: vi.fn() } })); +vi.mock('$env/dynamic/public', () => ({ env: {} })); +vi.mock('$lib/config', () => ({ + configHelpers: { getObpConnectionInfo: () => ({ host: 'obp.test', port: 443, displayName: 'obp.test:443' }) } +})); + +import Page from './+page.svelte'; + +const streams: string[] = []; +class FakeEventSource { + onopen: (() => void) | null = null; + onmessage: ((event: MessageEvent) => void) | null = null; + onerror: (() => void) | null = null; + constructor(url: string) { + streams.push(url); + } + addEventListener() {} + close() {} +} + +function pageData(overrides: Record = {}) { + return { + metrics: { metrics: [] }, + lastUpdated: new Date().toISOString(), + hasApiAccess: true, + error: null, + externalLinks: {}, + metricsRecordingCheck: null, + grpcAvailable: true, + ...overrides + }; +} + +describe('metrics page transport on first load', () => { + beforeEach(() => { + streams.length = 0; + vi.stubGlobal('EventSource', FakeEventSource); + window.history.replaceState({}, '', '/metrics'); + }); + + it('opens the gRPC stream without replaceState when gRPC is available', async () => { + render(Page, { props: { data: pageData() } }); + await vi.waitFor(() => expect(streams).toHaveLength(1)); + expect(streams[0]).toMatch(/^\/backend\/metrics\/stream\?/); + expect(navigation.replaceState).not.toHaveBeenCalled(); + expect(navigation.goto).toHaveBeenCalledWith(expect.stringContaining('transport=grpc'), expect.anything()); + }); + + it('honours ?transport=grpc without replaceState', async () => { + window.history.replaceState({}, '', '/metrics?transport=grpc'); + render(Page, { props: { data: pageData({ grpcAvailable: false }) } }); + await vi.waitFor(() => expect(streams).toHaveLength(1)); + expect(navigation.replaceState).not.toHaveBeenCalled(); + }); + + it('stays on REST without a stream when gRPC is not available', async () => { + render(Page, { props: { data: pageData({ grpcAvailable: false }) } }); + await vi.waitFor(() => expect(navigation.goto).toHaveBeenCalled()); + expect(navigation.goto).toHaveBeenCalledWith(expect.stringContaining('transport=rest'), expect.anything()); + expect(streams).toHaveLength(0); + }); + + it('keeps the choice in the URL when the user switches transport', async () => { + const { getByTestId } = render(Page, { props: { data: pageData() } }); + await vi.waitFor(() => expect(streams).toHaveLength(1)); + await fireEvent.click(getByTestId('transport-rest')); + expect(navigation.replaceState).toHaveBeenCalledTimes(1); + expect(String(navigation.replaceState.mock.calls[0][0])).toContain('transport=rest'); + }); +}); diff --git a/apps/api-manager/vite.config.ts b/apps/api-manager/vite.config.ts index 959bf99e..5cca907f 100644 --- a/apps/api-manager/vite.config.ts +++ b/apps/api-manager/vite.config.ts @@ -41,7 +41,7 @@ export default defineConfig({ clearMocks: true, include: ['src/**/*.svelte.{test,spec}.{js,ts}', 'test/**/*.svelte.{test,spec}.{js,ts}'], exclude: ['src/lib/server/**'], - setupFiles: ['./vitest-setup-client.ts', './test/setup.ts'] + setupFiles: ['./vitest-setup-client.ts'] } }, { diff --git a/apps/api-manager/vitest-setup-client.ts b/apps/api-manager/vitest-setup-client.ts new file mode 100644 index 00000000..7b5fba8f --- /dev/null +++ b/apps/api-manager/vitest-setup-client.ts @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2025-2026 TESOBE GmbH + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import '@testing-library/jest-dom/vitest';