import { afterEach, describe, expect, it, vi } from "vitest"; import { FeatureDataUnavailableError, getMapFeatures } from "../src/api"; afterEach(() => { vi.unstubAllGlobals(); }); describe("getMapFeatures", () => { it("rejects an unavailable feature source instead of treating it as an empty result", async () => { vi.stubGlobal( "fetch", vi.fn(async () => new Response( JSON.stringify({ type: "FeatureCollection", features: [], metadata: { source: "unavailable", warning: "Keine lokale Feature-Datenbank konfiguriert." } }), { status: 200, headers: { "content-type": "application/json" } } ) ) ); await expect( getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["harbours"] }) ).rejects.toEqual( expect.objectContaining({ name: "FeatureDataUnavailableError", message: "Keine lokale Feature-Datenbank konfiguriert." }) ); await expect( getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["locks"] }) ).rejects.toBeInstanceOf(FeatureDataUnavailableError); }); it("keeps usable postgis and legacy feature collections intact", async () => { const responses = [ { type: "FeatureCollection", features: [], metadata: { source: "postgis" } }, { type: "FeatureCollection", features: [] } ]; vi.stubGlobal( "fetch", vi.fn(async () => new Response(JSON.stringify(responses.shift()), { status: 200, headers: { "content-type": "application/json" } }) ) ); await expect( getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["bridges"] }) ).resolves.toMatchObject({ metadata: { source: "postgis" } }); await expect( getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["bridges"] }) ).resolves.toMatchObject({ features: [] }); }); it("uses the API error message for an unavailable source", async () => { vi.stubGlobal( "fetch", vi.fn(async () => new Response( JSON.stringify({ error: "feature_source_unavailable", message: "Keine lokale Karten- und Ereignisdatenbank konfiguriert." }), { status: 503, statusText: "Service Unavailable", headers: { "content-type": "application/json" } } ) ) ); await expect( getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["locks"] }) ).rejects.toThrow( "Keine lokale Karten- und Ereignisdatenbank konfiguriert." ); }); });