/* @vitest-environment node */ import { describe, it, expect } from "vitest"; import { ApiClientError } from "@/lib/frontend/apiClient"; import { mapSearchError } from "./errorMapping.js"; describe("lib/frontend/search/errorMapping", () => { it("returns null for missing error", () => { expect(mapSearchError(null)).toBe(null); expect(mapSearchError(undefined)).toBe(null); }); it("maps unauthenticated", () => { const err = new ApiClientError({ status: 401, code: "AUTH_UNAUTHENTICATED", message: "Unauthorized", }); const mapped = mapSearchError(err); expect(mapped).toMatchObject({ kind: "unauthenticated" }); }); it("maps forbidden", () => { const err = new ApiClientError({ status: 403, code: "AUTH_FORBIDDEN_BRANCH", message: "Forbidden", }); const mapped = mapSearchError(err); expect(mapped).toMatchObject({ kind: "forbidden" }); }); it("maps known validation errors to friendly German copy", () => { const err = new ApiClientError({ status: 400, code: "VALIDATION_SEARCH_MISSING_FILTER", message: "At least one of q or date range must be provided", }); const mapped = mapSearchError(err); expect(mapped).toEqual({ kind: "validation", title: "Kein Suchbegriff", description: "Bitte geben Sie einen Suchbegriff ein.", }); }); it("maps unknown validation errors to a generic validation message", () => { const err = new ApiClientError({ status: 400, code: "VALIDATION_SOMETHING_NEW", message: "nope", }); const mapped = mapSearchError(err); expect(mapped?.kind).toBe("validation"); expect(mapped?.title).toBe("Ungültige Eingabe"); }); it("maps network errors", () => { const err = new ApiClientError({ status: 0, code: "CLIENT_NETWORK_ERROR", message: "Network error", }); const mapped = mapSearchError(err); expect(mapped).toMatchObject({ kind: "generic", title: "Netzwerkfehler" }); }); it("maps unknown errors to generic", () => { const mapped = mapSearchError(new Error("boom")); expect(mapped).toMatchObject({ kind: "generic" }); }); it("maps VALIDATION_SEARCH_RANGE to a specific German message", () => { const err = new ApiClientError({ status: 400, code: "VALIDATION_SEARCH_RANGE", message: "Invalid date range", }); const mapped = mapSearchError(err); expect(mapped).toMatchObject({ kind: "validation", title: "Ungültiger Zeitraum", }); expect(mapped.description).toMatch(/Startdatum/i); }); });