geesawra.industries/hydraulophone
An unofficial YouTube Music app for iPhone, iPad, and Mac.
0
0
import Foundation
import Testing
@testable import HydraulophoneCore
struct LyricsDecoratorTests {
private let song = Song(id: "lyrics-song", title: "Song", artist: "Artist")
@Test
func cacheDecoratorPassesThroughLyricsWithoutRetainingThem() async throws {
let first = SongLyrics(lines: [LyricsLine(id: 0, text: "First")])
let second = SongLyrics(lines: [LyricsLine(id: 0, text: "Second")])
let upstream = MutableLyricsMusicService(lyrics: first)
let cached = CachedMusicService(base: upstream)
#expect(try await cached.lyrics(for: song) == first)
await upstream.setLyrics(second)
#expect(try await cached.lyrics(for: song) == second)
#expect(await upstream.callCount == 2)
}
@Test
func cacheDecoratorRejectsOldLyricsAfterSessionChange() async throws {
let upstream = ControlledLyricsMusicService()
let cached = CachedMusicService(base: upstream)
let old = Task { try await cached.lyrics(for: song) }
await waitUntil { await upstream.hasRequest }
await cached.setSession(nil)
await upstream.release(with: SongLyrics(lines: [LyricsLine(id: 0, text: "Old")]))
await #expect(throws: CachedMusicServiceError.staleCatalogRequest) {
try await old.value
}
}
@Test
func offlineGateRejectsAndCancelsLyricsRequests() async throws {
let upstream = ControlledLyricsMusicService()
let offline = OfflineMusicService(upstream: upstream)
let request = Task { try await offline.lyrics(for: song) }
await waitUntil { await upstream.hasRequest }
await offline.setOfflineOnly(true)
await upstream.release(with: SongLyrics(lines: [LyricsLine(id: 0, text: "Late")]))
await #expect(throws: CancellationError.self) {
try await request.value
}
await #expect(throws: OfflineModeError.self) {
try await offline.lyrics(for: song)
}
}
private func waitUntil(_ condition: @escaping () async -> Bool) async {
for _ in 0..<10_000 {
if await condition() { return }
await Task.yield()
}
Issue.record("Timed out waiting for lyrics request")
}
}
private actor MutableLyricsMusicService: MusicService, LyricsProviding {
private var currentLyrics: SongLyrics?
private(set) var callCount = 0
init(lyrics: SongLyrics?) {
currentLyrics = lyrics
}
func setLyrics(_ lyrics: SongLyrics?) {
currentLyrics = lyrics
}
func lyrics(for song: Song) async throws -> SongLyrics? {
callCount += 1
return currentLyrics
}
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 CancellationError() }
}
private actor ControlledLyricsMusicService: MusicService, LyricsProviding,
SessionManagingMusicService
{
private var pending: CheckedContinuation<SongLyrics?, Error>?
var hasRequest: Bool { pending != nil }
func lyrics(for song: Song) async throws -> SongLyrics? {
try await withCheckedThrowingContinuation { continuation in
pending = continuation
}
}
func release(with lyrics: SongLyrics?) {
pending?.resume(returning: lyrics)
pending = nil
}
func setSession(_ session: UserSession?) async {}
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 CancellationError() }
}