luminate/Tests/LuminateServicesTests/JellyfinClientImageTests.swift

89 lines
3.1 KiB
Swift

//
// JellyfinClientImageTests.swift
//
// Copyright 2026 Brendan Szymanski <hello@bscubed.dev>
//
// 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 <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
import Foundation
import LuminateAPI
import LuminateCore
import OpenAPIRuntime
import Testing
@testable import LuminateServices
/// Covers artwork download, which is the one operation returning raw bytes rather than JSON.
@Suite struct JellyfinClientImageTests {
/// 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("Collects the streamed image body into bytes")
func collectsImageBytes() async throws {
var api = MockJellyfinAPI()
api.getItemImageOutput = .ok(.init(body: .image_Ast_(HTTPBody([0, 1, 2, 255] as [UInt8]))))
let client = makeClient(api)
let data = try await client.image(itemID: "i1", type: .primary)
#expect(data == Data([0, 1, 2, 255]))
}
@Test("An index selects the indexed-image operation")
func indexedImageUsesIndexedOperation() async throws {
var api = MockJellyfinAPI()
api.getItemImageByIndexOutput = .ok(.init(body: .image_Ast_(HTTPBody([9, 9] as [UInt8]))))
let client = makeClient(api)
let data = try await client.image(
itemID: "i1",
type: .backdrop,
index: 2,
request: JellyfinImageRequest(fillWidth: 300)
)
#expect(data == Data([9, 9]))
}
@Test("A missing image maps to notFound")
func missingImageMapsToNotFound() async {
var api = MockJellyfinAPI()
api.getItemImageOutput = .notFound(.init(body: .json(Components.Schemas.ProblemDetails())))
let client = makeClient(api)
await #expect(throws: JellyfinClientError.notFound) {
_ = try await client.image(itemID: "i1", type: .logo)
}
}
@Test("An empty item identifier is rejected before any request is sent")
func emptyItemIdentifierRejected() async {
let client = makeClient(MockJellyfinAPI())
await #expect(throws: JellyfinClientError.invalidArgument(name: "itemID")) {
_ = try await client.image(itemID: "", type: .primary)
}
}
}