// // JellyfinClientErrorMappingTests.swift // // Copyright 2026 Brendan Szymanski // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // SPDX-License-Identifier: GPL-3.0-or-later // import Foundation import LuminateAPI import LuminateCore import OpenAPIRuntime import Testing @testable import LuminateServices /// Covers the translation from generated HTTP outcomes to ``JellyfinClientError``. /// /// Every operation hand-writes its status switch, so this pins the shape those switches share. @Suite struct JellyfinClientErrorMappingTests { /// Builds a client wired to a double. /// /// - Parameter api: The stubbed API. /// - Returns: A client that routes every call to `api`. private func makeClient(_ api: MockJellyfinAPI) -> JellyfinClient { JellyfinClient( configuration: JellyfinClientConfiguration(serverURL: URL(string: "http://localhost")!), api: api ) } @Test("An unconfigured client rejects operations before network access") func unconfiguredClientThrows() async { let client = JellyfinClient( configuration: JellyfinClientConfiguration(serverURL: nil) ) #expect(await client.serverURL == nil) await #expect(throws: JellyfinClientError.notConfigured) { _ = try await client.items() } } @Test("A 401 maps to unauthorized") func unauthorizedMaps() async { var api = MockJellyfinAPI() api.getItemsOutput = .unauthorized(.init()) let client = makeClient(api) await #expect(throws: JellyfinClientError.unauthorized) { _ = try await client.items() } } @Test("A 403 maps to forbidden") func forbiddenMaps() async { var api = MockJellyfinAPI() api.getItemsOutput = .forbidden(.init()) let client = makeClient(api) await #expect(throws: JellyfinClientError.forbidden) { _ = try await client.items() } } @Test("An undocumented status is reported with its operation and code") func undocumentedMaps() async { var api = MockJellyfinAPI() api.getItemsOutput = .undocumented(statusCode: 418, .init()) let client = makeClient(api) await #expect(throws: JellyfinClientError.unexpectedStatus(operation: "GetItems", statusCode: 418)) { _ = try await client.items() } } @Test("A starting server surfaces its Retry-After hint") func serviceUnavailableCarriesRetryAfter() async { var api = MockJellyfinAPI() api.getPublicSystemInfoOutput = .serviceUnavailable( .init(headers: .init(retryAfter: 5), body: .html(HTTPBody("starting"))) ) let client = makeClient(api) await #expect(throws: JellyfinClientError.serviceUnavailable(retryAfterSeconds: 5)) { _ = try await client.publicServerInfo() } } }