geesawra.industries/hydraulophone
An unofficial YouTube Music app for iPhone, iPad, and Mac.
0
0
#if DEBUG
import Foundation
import HydraulophoneCore
/// Opt-in authenticated diagnostic. It makes one search and at most two
/// lyrics requests, without playback, account mutation, or content logging.
@MainActor
enum LyricsLiveSmoke {
private enum Failure: String, Error {
case session
case capability
case search
case noSongs = "no_songs"
case lyrics
case unavailable
case cueBoundary = "cue_boundary"
}
static func run(model: AppModel) async {
print("lyrics_smoke outcome=start")
do {
guard try await waitForSession(model: model) else {
print("lyrics_smoke outcome=skipped reason=authentication_or_offline")
return
}
let referenceProbe = ProcessInfo.processInfo.arguments.contains(
"-HydraulophoneLyricsReferenceSmoke")
guard let catalogLyrics = model.musicService as? any LyricsProviding else {
throw Failure.capability
}
let lyricsService: any LyricsProviding =
referenceProbe ? BetterLyricsProvider() : catalogLyrics
let songs: [Song]
if referenceProbe {
// The public example from Better Lyrics' documentation. This mode
// calls only that provider and never submits this synthetic ID.
songs = [
Song(
id: "lyrics-reference-only", title: "E85", artist: "Don Toliver",
album: "OCTANE", durationText: "2:34")
]
} else if let currentSong = model.playback.currentSong {
songs = [currentSong]
} else {
let results: [SearchResult]
do {
results = try await model.musicService.search(query: "Daft Punk Get Lucky")
} catch is CancellationError {
throw CancellationError()
} catch {
throw Failure.search
}
songs = results.compactMap { result -> Song? in
if case .song(let song) = result { return song }
return nil
}
}
guard !songs.isEmpty else { throw Failure.noSongs }
var requestCount = 0
var foundLyrics: SongLyrics?
var requestFailed = false
for song in songs.prefix(2) {
try Task.checkCancellation()
requestCount += 1
do {
if let lyrics = try await lyricsService.lyrics(for: song), !lyrics.lines.isEmpty {
foundLyrics = lyrics
break
}
} catch is CancellationError {
throw CancellationError()
} catch {
requestFailed = true
reportFailure(error)
}
}
guard let lyrics = foundLyrics else {
throw requestFailed ? Failure.lyrics : Failure.unavailable
}
guard verifyCueBoundaries() else { throw Failure.cueBoundary }
print(
"lyrics_smoke outcome=success candidate_songs=\(songs.count) lyrics_requests=\(requestCount) line_count=\(lyrics.lines.count) word_timed_lines=\(lyrics.lines.filter { !$0.words.isEmpty }.count) synchronized=\(lyrics.isSynchronized) cue_check=true"
)
} catch is CancellationError {
print("lyrics_smoke outcome=cancelled")
} catch let failure as Failure {
print("lyrics_smoke outcome=failure stage=\(failure.rawValue)")
} catch {
print("lyrics_smoke outcome=failure stage=unknown")
}
}
private static func reportFailure(_ error: Error) {
switch error {
case InnerTubeError.authenticationRequired:
print("lyrics_smoke request_failure=authentication")
case InnerTubeError.httpStatus(let status):
print("lyrics_smoke request_failure=innertube_http status=\(status)")
case LRCLibLyricsError.httpStatus(let status):
print("lyrics_smoke request_failure=lrclib_http status=\(status)")
case LRCLibLyricsError.rateLimited:
print("lyrics_smoke request_failure=lrclib_rate_limit")
case LRCLibLyricsError.invalidResponse:
print("lyrics_smoke request_failure=lrclib_response")
case let error as URLError:
print("lyrics_smoke request_failure=transport code=\(error.code.rawValue)")
default:
print("lyrics_smoke request_failure=other")
}
}
private static func waitForSession(model: AppModel) async throws -> Bool {
let deadline = Date().addingTimeInterval(20)
while model.isRestoringSession || model.isSessionTransitioning {
try Task.checkCancellation()
guard Date() < deadline else { throw Failure.session }
try await Task.sleep(for: .milliseconds(100))
}
return model.session?.isAuthenticated == true && !model.isOfflineOnly
}
private static func verifyCueBoundaries() -> Bool {
let cues = SongLyrics(lines: [
LyricsLine(id: 1, text: "Synthetic first cue", startTime: 0, endTime: 5),
LyricsLine(id: 2, text: "Synthetic second cue", startTime: 5, endTime: 10),
])
return cues.activeLineIndex(at: 0) == 0
&& cues.activeLineIndex(at: 4.999) == 0
&& cues.activeLineIndex(at: 5) == 1
&& cues.activeLineIndex(at: 10) == nil
}
}
#endif