~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/hydraulophone

An unofficial YouTube Music app for iPhone, iPad, and Mac.

0
0
main
86 lines · 4.0 KB Raw History
#if DEBUG
  import Foundation
  import HydraulophoneCore
  import OSLog

  /// Opt-in, read-only simulator diagnostic: search, linked artist and album.
  /// No playback, account mutation, raw response or identifying log output.
  @MainActor
  enum TrackNavigationSmoke {
    private enum Failure: Error { case metadata, artist, album }

    static func run(model: AppModel) async {
      let logger = Logger(subsystem: "com.hydraulophone", category: "smoke")
      var consoleOutcome = "failure"
      defer { print("smoke outcome=\(consoleOutcome) mode=track_actions") }
      do {
        let deadline = Date().addingTimeInterval(20)
        while model.isRestoringSession, Date() < deadline {
          try await Task.sleep(for: .milliseconds(100))
        }
        guard !model.isRestoringSession, model.session?.isAuthenticated == true,
          !model.isOfflineOnly
        else {
          consoleOutcome = "skipped"
          logger.notice("smoke outcome=skipped mode=track_actions")
          return
        }
        let results = try await model.musicService.search(query: "Protest The Hero Fishhook")
        let songs = results.compactMap { result -> Song? in
          if case .song(let song) = result { return song }
          return nil
        }
        logger.notice(
          "track_actions songs=\(songs.count) artist_links=\(songs.filter { $0.artistReferences?.isEmpty == false }.count) album_links=\(songs.filter { $0.albumReference != nil }.count) album_names=\(songs.filter { $0.album != nil }.count)"
        )
        guard
          let song = songs.first(where: { $0.artistReferences?.isEmpty == false }),
          let artist = song.artistReferences?.first
        else { throw Failure.metadata }
        guard let browsing = model.musicService as? any ArtistBrowsingMusicService else {
          throw Failure.artist
        }
        let page = try await browsing.artistPage(artist, continuation: nil, policy: .reload)
        let artistArtwork = page.header?.thumbnailURL ?? artist.thumbnailURL
        guard !page.sections.isEmpty, artistArtwork != nil else { throw Failure.artist }
        // Video search results can omit an album. Follow the artist page's
        // actual album endpoint instead of inferring a relationship from names.
        let albums = page.sections.flatMap(\.items).compactMap { item -> Album? in
          if case .album(let album) = item { return album }
          return nil
        }
        guard let album = song.albumReference ?? albums.first else { throw Failure.album }
        let collection = CollectionTracksViewModel(album: album, service: model.musicService)
        await collection.load()
        guard collection.errorMessage == nil, !collection.songs.isEmpty,
          collection.displayAlbum?.thumbnailURL != nil,
          collection.songs.allSatisfy({ $0.albumReference?.id == album.id })
        else { throw Failure.album }
        logger.notice(
          "track_actions metadata=true artist_sections=\(page.sections.count) album_tracks=\(collection.songs.count)"
        )
        logger.notice(
          "track_actions artist_artwork=true album_artwork=true album_header_artwork=\(collection.header?.thumbnailURL != nil)"
        )
        print(
          "track_actions artist_artwork=true album_artwork=true album_header_artwork=\(collection.header?.thumbnailURL != nil)"
        )
        consoleOutcome = "success"
        logger.notice("smoke outcome=success mode=track_actions")
      } catch is CancellationError {
        consoleOutcome = "cancelled"
        logger.notice("smoke outcome=cancelled mode=track_actions")
      } catch let failure as Failure {
        let stage: String
        switch failure {
        case .metadata: stage = "metadata"
        case .artist: stage = "artist"
        case .album: stage = "album"
        }
        logger.error("smoke outcome=failure mode=track_actions stage=\(stage, privacy: .public)")
      } catch {
        logger.error("smoke outcome=failure mode=track_actions code=\((error as NSError).code)")
      }
    }
  }
#endif