luminate/Tests/LuminateServicesTests/JellyfinClientUserStateTests.swift

146 lines
5.7 KiB
Swift

//
// JellyfinClientUserStateTests.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 watched-state and favourite mutations, and their itemID validation.
@Suite struct JellyfinClientUserStateTests {
/// 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("Marking an item played returns the server's recomputed state and forwards the user and date")
func markPlayedSucceeds() async throws {
var api = MockJellyfinAPI()
api.markPlayedItemOutput = .ok(
.init(body: .json(Components.Schemas.UserItemDataDto(isFavorite: true, played: true)))
)
let client = makeClient(api)
let datePlayed = Date(timeIntervalSince1970: 1_700_000_000)
let userData = try await client.markPlayed(itemID: "i1", userID: "u1", datePlayed: datePlayed)
#expect(userData.isFavorite == true)
#expect(userData.played == true)
let input = try #require(api.inputs.last("MarkPlayedItem", as: Operations.MarkPlayedItem.Input.self))
#expect(input.query.userId == "u1")
#expect(input.query.datePlayed == datePlayed)
}
@Test("An empty item identifier rejects markPlayed before any request is sent")
func markPlayedEmptyItemIDRejected() async {
let api = MockJellyfinAPI()
let client = makeClient(api)
await #expect(throws: JellyfinClientError.invalidArgument(name: "itemID")) {
_ = try await client.markPlayed(itemID: "", userID: nil, datePlayed: nil)
}
#expect(api.inputs.last("MarkPlayedItem", as: Operations.MarkPlayedItem.Input.self) == nil)
}
@Test("Marking an item unplayed returns the server's recomputed state")
func markUnplayedSucceeds() async throws {
var api = MockJellyfinAPI()
api.markUnplayedItemOutput = .ok(
.init(body: .json(Components.Schemas.UserItemDataDto(isFavorite: false, played: false)))
)
let client = makeClient(api)
let userData = try await client.markUnplayed(itemID: "i1", userID: nil)
#expect(userData.isFavorite == false)
#expect(userData.played == false)
}
@Test("An empty item identifier rejects markUnplayed before any request is sent")
func markUnplayedEmptyItemIDRejected() async {
let api = MockJellyfinAPI()
let client = makeClient(api)
await #expect(throws: JellyfinClientError.invalidArgument(name: "itemID")) {
_ = try await client.markUnplayed(itemID: "", userID: nil)
}
#expect(api.inputs.last("MarkUnplayedItem", as: Operations.MarkUnplayedItem.Input.self) == nil)
}
@Test("Marking an item a favourite returns the server's recomputed state")
func markFavoriteSucceeds() async throws {
var api = MockJellyfinAPI()
api.markFavoriteItemOutput = .ok(
.init(body: .json(Components.Schemas.UserItemDataDto(isFavorite: true, played: false)))
)
let client = makeClient(api)
let userData = try await client.markFavorite(itemID: "i1", userID: nil)
#expect(userData.isFavorite == true)
#expect(userData.played == false)
}
@Test("An empty item identifier rejects markFavorite before any request is sent")
func markFavoriteEmptyItemIDRejected() async {
let api = MockJellyfinAPI()
let client = makeClient(api)
await #expect(throws: JellyfinClientError.invalidArgument(name: "itemID")) {
_ = try await client.markFavorite(itemID: "", userID: nil)
}
#expect(api.inputs.last("MarkFavoriteItem", as: Operations.MarkFavoriteItem.Input.self) == nil)
}
@Test("Unmarking an item a favourite returns the server's recomputed state")
func unmarkFavoriteSucceeds() async throws {
var api = MockJellyfinAPI()
api.unmarkFavoriteItemOutput = .ok(
.init(body: .json(Components.Schemas.UserItemDataDto(isFavorite: false, played: true)))
)
let client = makeClient(api)
let userData = try await client.unmarkFavorite(itemID: "i1", userID: nil)
#expect(userData.isFavorite == false)
#expect(userData.played == true)
}
@Test("An empty item identifier rejects unmarkFavorite before any request is sent")
func unmarkFavoriteEmptyItemIDRejected() async {
let api = MockJellyfinAPI()
let client = makeClient(api)
await #expect(throws: JellyfinClientError.invalidArgument(name: "itemID")) {
_ = try await client.unmarkFavorite(itemID: "", userID: nil)
}
#expect(api.inputs.last("UnmarkFavoriteItem", as: Operations.UnmarkFavoriteItem.Input.self) == nil)
}
}