316 lines
12 KiB
Swift
316 lines
12 KiB
Swift
//
|
|
// JellyfinClientLibraryTests.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 Testing
|
|
|
|
@testable import LuminateServices
|
|
|
|
/// Covers the DTO-to-domain mapping that every browse surface depends on.
|
|
@Suite struct JellyfinClientLibraryTests {
|
|
/// A movie DTO exercising the wrapped enum, image tag, and user data payloads.
|
|
private var movieResult: Components.Schemas.BaseItemDtoQueryResult {
|
|
Components.Schemas.BaseItemDtoQueryResult(
|
|
items: [
|
|
Components.Schemas.BaseItemDto(
|
|
name: "Arrival",
|
|
id: "i1",
|
|
overview: "Linguist meets heptapods.",
|
|
productionYear: 2016,
|
|
_type: .init(value1: .movie),
|
|
userData: .init(value1: Components.Schemas.UserItemDataDto(isFavorite: true, played: false)),
|
|
imageTags: .init(additionalProperties: ["Primary": "abc", "NotAnImageType": "zzz"]),
|
|
backdropImageTags: ["bd1"]
|
|
)
|
|
],
|
|
totalRecordCount: 1,
|
|
startIndex: 0
|
|
)
|
|
}
|
|
|
|
/// 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("Maps an item, dropping image tags the client cannot name")
|
|
func mapsItems() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getItemsOutput = .ok(.init(body: .json(movieResult)))
|
|
let client = makeClient(api)
|
|
|
|
let page = try await client.items()
|
|
|
|
#expect(page.totalRecordCount == 1)
|
|
let item = try #require(page.items.first)
|
|
#expect(item.id == "i1")
|
|
#expect(item.name == "Arrival")
|
|
#expect(item.kind == .movie)
|
|
#expect(item.productionYear == 2016)
|
|
#expect(item.imageTags == [.primary: "abc"])
|
|
#expect(item.backdropImageTags == ["bd1"])
|
|
#expect(item.userData?.isFavorite == true)
|
|
#expect(item.userData?.played == false)
|
|
}
|
|
|
|
@Test("Every JSON profile the server may negotiate decodes identically")
|
|
func jsonProfilesCollapse() async throws {
|
|
var camelCase = MockJellyfinAPI()
|
|
camelCase.getItemsOutput = .ok(.init(body: .applicationJsonProfile_Quot_camelcase_quot_(movieResult)))
|
|
var pascalCase = MockJellyfinAPI()
|
|
pascalCase.getItemsOutput = .ok(.init(body: .applicationJsonProfile_Quot_pascalcase_quot_(movieResult)))
|
|
|
|
let fromCamelCase = try await makeClient(camelCase).items()
|
|
let fromPascalCase = try await makeClient(pascalCase).items()
|
|
|
|
#expect(fromCamelCase == fromPascalCase)
|
|
#expect(fromCamelCase.items.first?.kind == .movie)
|
|
}
|
|
|
|
@Test("Maps libraries and drops unsupported collection types")
|
|
func mapsLibraries() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getUserViewsOutput = .ok(
|
|
.init(
|
|
body: .json(
|
|
Components.Schemas.BaseItemDtoQueryResult(
|
|
items: [
|
|
Components.Schemas.BaseItemDto(
|
|
name: "Shows",
|
|
id: "l1",
|
|
childCount: 12,
|
|
collectionType: .init(value1: .tvshows),
|
|
imageTags: .init(additionalProperties: ["Primary": "tag1"])
|
|
),
|
|
Components.Schemas.BaseItemDto(
|
|
name: "Music",
|
|
id: "l2",
|
|
collectionType: .init(value1: .music)
|
|
),
|
|
]
|
|
)
|
|
)
|
|
)
|
|
)
|
|
let client = makeClient(api)
|
|
|
|
let libraries = try await client.libraries()
|
|
|
|
#expect(libraries.count == 2)
|
|
#expect(libraries[0].collectionType == .tvShows)
|
|
#expect(libraries[0].primaryImageTag == "tag1")
|
|
#expect(libraries[0].childCount == 12)
|
|
#expect(libraries[1].collectionType == nil)
|
|
}
|
|
|
|
@Test("A blank search term is rejected before any request is sent")
|
|
func blankSearchTermRejected() async {
|
|
let client = makeClient(MockJellyfinAPI())
|
|
|
|
await #expect(throws: JellyfinClientError.invalidArgument(name: "term")) {
|
|
_ = try await client.searchHints(term: " ")
|
|
}
|
|
}
|
|
|
|
@Test("An empty series identifier is rejected before any request is sent")
|
|
func emptySeriesIdentifierRejected() async {
|
|
let client = makeClient(MockJellyfinAPI())
|
|
|
|
await #expect(throws: JellyfinClientError.invalidArgument(name: "seriesID")) {
|
|
_ = try await client.seasons(seriesID: "")
|
|
}
|
|
}
|
|
|
|
@Test("A fully populated query maps every field onto the generated request")
|
|
func fullyPopulatedQueryMapsEveryField() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getItemsOutput = .ok(.init(body: .json(movieResult)))
|
|
let client = makeClient(api)
|
|
|
|
let query = JellyfinMediaQuery(
|
|
userID: "u1",
|
|
parentID: "p1",
|
|
includeItemKinds: [.movie],
|
|
searchTerm: "arrival",
|
|
sortBy: [.sortName],
|
|
sortOrder: [.ascending],
|
|
fields: [.overview],
|
|
startIndex: 20,
|
|
limit: 40,
|
|
recursive: true,
|
|
isFavorite: true,
|
|
isPlayed: false,
|
|
genres: ["Drama"],
|
|
years: [2020],
|
|
nameStartsWith: "A"
|
|
)
|
|
|
|
_ = try await client.items(query)
|
|
|
|
let input = try #require(api.inputs.last("GetItems", as: Operations.GetItems.Input.self))
|
|
#expect(input.query.userId == "u1")
|
|
#expect(input.query.parentId == "p1")
|
|
#expect(input.query.searchTerm == "arrival")
|
|
#expect(input.query.recursive == true)
|
|
#expect(input.query.isFavorite == true)
|
|
#expect(input.query.isPlayed == false)
|
|
#expect(input.query.genres == ["Drama"])
|
|
#expect(input.query.years == [2020])
|
|
#expect(input.query.nameStartsWith == "A")
|
|
#expect(input.query.startIndex == 20)
|
|
#expect(input.query.limit == 40)
|
|
#expect(input.query.includeItemTypes == [.movie])
|
|
#expect(input.query.sortBy == [.sortName])
|
|
#expect(input.query.sortOrder == [.ascending])
|
|
#expect(input.query.fields == [.overview])
|
|
}
|
|
|
|
@Test("Fetching one item maps the DTO and forwards the item id and user id")
|
|
func fetchesOneItem() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getItemOutput = .ok(.init(body: .json(Components.Schemas.BaseItemDto(name: "Arrival", id: "i1"))))
|
|
let client = makeClient(api)
|
|
|
|
let item = try await client.item(id: "i1", userID: "u9")
|
|
|
|
#expect(item.id == "i1")
|
|
#expect(item.name == "Arrival")
|
|
let input = try #require(api.inputs.last("GetItem", as: Operations.GetItem.Input.self))
|
|
#expect(input.path.itemId == "i1")
|
|
#expect(input.query.userId == "u9")
|
|
}
|
|
|
|
@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: "id")) {
|
|
_ = try await client.item(id: "", userID: nil)
|
|
}
|
|
}
|
|
|
|
@Test("resumeItems forwards paging and defaults the item-kind filter to movies and episodes")
|
|
func resumeItemsDefaultsItemKinds() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getResumeItemsOutput = .ok(.init(body: .json(movieResult)))
|
|
let client = makeClient(api)
|
|
|
|
_ = try await client.resumeItems(JellyfinListOptions(startIndex: 5, limit: 10))
|
|
|
|
let input = try #require(api.inputs.last("GetResumeItems", as: Operations.GetResumeItems.Input.self))
|
|
#expect(input.query.startIndex == 5)
|
|
#expect(input.query.limit == 10)
|
|
#expect(input.query.includeItemTypes == [.movie, .episode])
|
|
}
|
|
|
|
@Test("nextUp records an explicit series id, or omits it for every series")
|
|
func nextUpRecordsSeriesID() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getNextUpOutput = .ok(.init(body: .json(movieResult)))
|
|
let client = makeClient(api)
|
|
|
|
_ = try await client.nextUp(seriesID: "s1", options: JellyfinListOptions())
|
|
let withSeries = try #require(api.inputs.last("GetNextUp", as: Operations.GetNextUp.Input.self))
|
|
#expect(withSeries.query.seriesId == "s1")
|
|
|
|
_ = try await client.nextUp(seriesID: nil, options: JellyfinListOptions())
|
|
let withoutSeries = try #require(api.inputs.last("GetNextUp", as: Operations.GetNextUp.Input.self))
|
|
#expect(withoutSeries.query.seriesId == nil)
|
|
}
|
|
|
|
@Test("latestMedia maps a plain item list and forwards its options")
|
|
func latestMediaMapsPlainList() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getLatestMediaOutput = .ok(
|
|
.init(
|
|
body: .json([
|
|
Components.Schemas.BaseItemDto(name: "Arrival", id: "i1"),
|
|
Components.Schemas.BaseItemDto(name: "Dune", id: "i2"),
|
|
])
|
|
)
|
|
)
|
|
let client = makeClient(api)
|
|
|
|
let items = try await client.latestMedia(JellyfinListOptions(parentID: "lib1", limit: 2))
|
|
|
|
#expect(items.map(\.id) == ["i1", "i2"])
|
|
#expect(items.map(\.name) == ["Arrival", "Dune"])
|
|
let input = try #require(api.inputs.last("GetLatestMedia", as: Operations.GetLatestMedia.Input.self))
|
|
#expect(input.query.parentId == "lib1")
|
|
#expect(input.query.limit == 2)
|
|
}
|
|
|
|
@Test("episodes forwards season and season-id filters")
|
|
func episodesForwardsSeasonFilters() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getEpisodesOutput = .ok(.init(body: .json(movieResult)))
|
|
let client = makeClient(api)
|
|
|
|
_ = try await client.episodes(
|
|
seriesID: "series1",
|
|
options: JellyfinListOptions(seasonID: "season-1", season: 2)
|
|
)
|
|
|
|
let input = try #require(api.inputs.last("GetEpisodes", as: Operations.GetEpisodes.Input.self))
|
|
#expect(input.path.seriesId == "series1")
|
|
#expect(input.query.season == 2)
|
|
#expect(input.query.seasonId == "season-1")
|
|
}
|
|
|
|
@Test("searchHints maps hints and forwards paging")
|
|
func searchHintsMapsAndForwardsPaging() async throws {
|
|
var api = MockJellyfinAPI()
|
|
api.getSearchHintsOutput = .ok(
|
|
.init(
|
|
body: .json(
|
|
Components.Schemas.SearchHintResult(
|
|
searchHints: [
|
|
Components.Schemas.SearchHint(id: "h1", name: "Arrival", matchedTerm: "arrival"),
|
|
Components.Schemas.SearchHint(id: "h2", name: "Arrival 2", matchedTerm: "arrival"),
|
|
]
|
|
)
|
|
)
|
|
)
|
|
)
|
|
let client = makeClient(api)
|
|
|
|
let hints = try await client.searchHints(
|
|
term: "arrival",
|
|
options: JellyfinListOptions(startIndex: 10, limit: 5)
|
|
)
|
|
|
|
#expect(hints.map(\.id) == ["h1", "h2"])
|
|
#expect(hints.map(\.name) == ["Arrival", "Arrival 2"])
|
|
#expect(hints.map(\.matchedTerm) == ["arrival", "arrival"])
|
|
let input = try #require(api.inputs.last("GetSearchHints", as: Operations.GetSearchHints.Input.self))
|
|
#expect(input.query.startIndex == 10)
|
|
#expect(input.query.limit == 5)
|
|
#expect(input.query.searchTerm == "arrival")
|
|
}
|
|
}
|