geesawra.industries/hydraulophone
An unofficial YouTube Music app for iPhone, iPad, and Mac.
0
0
import HydraulophoneCore
import XCTest
@testable import Hydraulophone
@MainActor
final class HomeViewModelTests: XCTestCase {
func testOfflineFixtureLoadsDeterministicShelvesAndMergesOnePage() async {
let viewModel = HomeViewModel(service: OfflineFixtureMusicService())
await viewModel.load(audience: .anonymous, sessionID: 1)
XCTAssertEqual(viewModel.sections.map(\.id), ["fixture-discover", "fixture-albums"])
XCTAssertEqual(
viewModel.sections[0].items.map(\.id),
[
"song-fixture-song-1",
"playlist-fixture-playlist",
])
XCTAssertTrue(viewModel.hasMore)
await viewModel.loadMore()
XCTAssertFalse(viewModel.hasMore)
XCTAssertEqual(viewModel.sections.map(\.id), ["fixture-discover", "fixture-albums"])
XCTAssertEqual(
viewModel.sections[0].items.map(\.id),
[
"song-fixture-song-1",
"playlist-fixture-playlist",
"song-fixture-song-2",
])
}
func testReloadFailureKeepsExistingHomeContent() async {
let firstPage = HomePage(
sections: [
HomeSection(
id: "stable-shelf",
title: "Stable shelf",
items: [.song(testSong(id: "first"))]
)
]
)
let service = HomeServiceStub(responses: [.success(firstPage), .failure(.offline)])
let viewModel = HomeViewModel(service: service)
await viewModel.load(audience: .anonymous, sessionID: 3)
await viewModel.load(
audience: .anonymous,
sessionID: 3,
filter: HomeFilter(title: "Different filter", params: "fixture-filter"),
policy: .reload
)
XCTAssertEqual(viewModel.sections.map(\.id), ["stable-shelf"])
XCTAssertEqual(viewModel.sections[0].items.map(\.id), ["song-first"])
XCTAssertNotNil(viewModel.errorMessage)
XCTAssertNil(
viewModel.selectedFilter, "Failed filter changes must not relabel retained content")
}
func testSessionTransitionClearsImmediatelyAndNewSessionReloads() async {
let firstPage = HomePage(
sections: [
HomeSection(
id: "first-account",
title: "First account",
items: [.song(testSong(id: "first-account-song"))]
)
]
)
let secondPage = HomePage(
sections: [
HomeSection(
id: "second-account",
title: "Second account",
items: [.song(testSong(id: "second-account-song"))]
)
]
)
let service = HomeServiceStub(responses: [.success(firstPage), .success(secondPage)])
let viewModel = HomeViewModel(service: service)
await viewModel.load(audience: .authenticated, sessionID: 10)
await viewModel.load(
audience: .anonymous,
sessionID: 11,
isSessionTransitioning: true
)
XCTAssertTrue(viewModel.sections.isEmpty)
XCTAssertNil(viewModel.errorMessage)
await viewModel.load(audience: .anonymous, sessionID: 11)
XCTAssertEqual(viewModel.sections.map(\.id), ["second-account"])
}
func testRepeatedContinuationStopsPagination() async {
let repeatedToken = PaginationToken(rawValue: "same-token")
let firstPage = HomePage(
sections: [
HomeSection(
id: "shelf",
title: "Shelf",
items: [.song(testSong(id: "one"))]
)
],
next: repeatedToken
)
let secondPage = HomePage(
sections: [
HomeSection(
id: "shelf",
title: "Shelf",
items: [.song(testSong(id: "two"))]
)
],
next: repeatedToken
)
let service = HomeServiceStub(responses: [.success(firstPage), .success(secondPage)])
let viewModel = HomeViewModel(service: service)
await viewModel.load(audience: .anonymous, sessionID: 1)
await viewModel.loadMore()
XCTAssertFalse(viewModel.hasMore)
XCTAssertEqual(viewModel.sections[0].items.map(\.id), ["song-one", "song-two"])
}
func testHomeLoadsOneContinuationPageAtATimeAndMergesShelves() async {
let pageOne = HomePage(
sections: [HomeSection(id: "shelf", title: "Shelf", items: [.song(testSong(id: "one"))])],
next: PaginationToken(rawValue: "opaque-one")
)
let pageTwo = HomePage(
sections: [HomeSection(id: "shelf", title: "Shelf", items: [.song(testSong(id: "two"))])],
next: PaginationToken(rawValue: "opaque-two")
)
let pageThree = HomePage(
sections: [HomeSection(id: "shelf", title: "Shelf", items: [.song(testSong(id: "three"))])]
)
let service = HomeServiceStub(responses: [
.success(pageOne), .success(pageTwo), .success(pageThree),
])
let viewModel = HomeViewModel(service: service)
await viewModel.load(audience: .authenticated, sessionID: 7)
XCTAssertEqual(viewModel.sections[0].items.map(\.id), ["song-one"])
XCTAssertTrue(viewModel.hasMore)
await viewModel.loadMore()
XCTAssertEqual(viewModel.sections[0].items.map(\.id), ["song-one", "song-two"])
XCTAssertTrue(viewModel.hasMore)
let afterSecondPage = await service.requestedContinuations
XCTAssertEqual(afterSecondPage, [nil, "opaque-one"])
await viewModel.loadMore()
XCTAssertEqual(viewModel.sections[0].items.map(\.id), ["song-one", "song-two", "song-three"])
XCTAssertFalse(viewModel.hasMore)
let allRequests = await service.requestedContinuations
XCTAssertEqual(allRequests, [nil, "opaque-one", "opaque-two"])
}
func testFailedContinuationWaitsForExplicitRetry() async {
let initial = HomePage(
sections: [HomeSection(id: "shelf", title: "Shelf", items: [.song(testSong(id: "one"))])],
next: PaginationToken(rawValue: "retry-token")
)
let recovered = HomePage(
sections: [HomeSection(id: "shelf", title: "Shelf", items: [.song(testSong(id: "two"))])]
)
let service = HomeServiceStub(responses: [
.success(initial), .failure(.offline), .success(recovered),
])
let viewModel = HomeViewModel(service: service)
await viewModel.load(audience: .authenticated, sessionID: 8)
await viewModel.loadMore()
XCTAssertNotNil(viewModel.loadMoreErrorMessage)
XCTAssertTrue(viewModel.hasMore)
let failedRequests = await service.requestedContinuations
XCTAssertEqual(failedRequests.count, 2)
await viewModel.retryLoadMore()
XCTAssertNil(viewModel.loadMoreErrorMessage)
XCTAssertFalse(viewModel.hasMore)
XCTAssertEqual(viewModel.sections[0].items.map(\.id), ["song-one", "song-two"])
let allRequests = await service.requestedContinuations
XCTAssertEqual(allRequests.count, 3)
}
private func testSong(id: String) -> Song {
Song(id: id, title: id, artist: "Fixture Artist")
}
}
private enum HomeStubError: Error, LocalizedError, Sendable {
case offline
var errorDescription: String? {
"The offline Home fixture failed."
}
}
private actor HomeServiceStub: PaginatedCatalogMusicService {
private var responses: [Result<HomePage, HomeStubError>]
private(set) var requestedContinuations: [String?] = []
init(responses: [Result<HomePage, HomeStubError>]) {
self.responses = responses
}
func search(query: String) async throws -> [SearchResult] {
[]
}
func playlists() async throws -> [Playlist] {
[]
}
func tracks(in playlist: Playlist) async throws -> [Song] {
[]
}
func stream(for song: Song) async throws -> URL {
throw HomeStubError.offline
}
func searchPage(
query: String,
continuation: PaginationToken?,
policy: CatalogLoadPolicy
) async throws -> Page<SearchResult> {
Page(items: [])
}
func playlistPage(
continuation: PaginationToken?,
policy: CatalogLoadPolicy
) async throws -> Page<Playlist> {
Page(items: [])
}
func trackPage(
in playlist: Playlist,
continuation: PaginationToken?,
policy: CatalogLoadPolicy
) async throws -> Page<Song> {
Page(items: [])
}
func albumTrackPage(
in album: Album,
continuation: PaginationToken?,
policy: CatalogLoadPolicy
) async throws -> Page<Song> {
Page(items: [])
}
func homePage(
audience: HomeAudience,
filter: HomeFilter?,
continuation: PaginationToken?,
policy: CatalogLoadPolicy
) async throws -> HomePage {
requestedContinuations.append(continuation?.rawValue)
guard !responses.isEmpty else {
throw HomeStubError.offline
}
return try responses.removeFirst().get()
}
}