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

geesawra.industries/hydraulophone

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

0
0
main
131 lines · 3.8 KB Raw History
import AVFoundation
import Foundation
import HydraulophoneCore
import XCTest

@testable import Hydraulophone

final class PlaybackStreamRefreshTests: XCTestCase {
  func testRefreshDispatchUsesDedicatedStreamRefreshOperation() async throws {
    let service = RefreshRecordingMusicService()
    let song = Song(id: "song", title: "Song", artist: "Artist")

    let initialStream = try await PlaybackStreamResolver.resolve(
      service: service,
      song: song,
      refreshing: false
    )
    let refreshedStream = try await PlaybackStreamResolver.resolve(
      service: service,
      song: song,
      refreshing: true
    )

    XCTAssertEqual(initialStream.url, URL(fileURLWithPath: "/tmp/initial.m4a"))
    XCTAssertNil(initialStream.userAgent)
    XCTAssertEqual(refreshedStream.url, URL(fileURLWithPath: "/tmp/refreshed.m4a"))
    XCTAssertNil(refreshedStream.userAgent)
    let operations = await service.operations()
    XCTAssertEqual(operations, ["stream", "refresh"])
  }

  func testDescriptorResolverKeepsUserAgentAcrossRefreshBoundary() async throws {
    let service = DescriptorRecordingMusicService()
    let song = Song(id: "song", title: "Song", artist: "Artist")

    let initialStream = try await PlaybackStreamResolver.resolve(
      service: service,
      song: song,
      refreshing: false
    )
    let refreshedStream = try await PlaybackStreamResolver.resolve(
      service: service,
      song: song,
      refreshing: true
    )

    XCTAssertEqual(initialStream.userAgent, "HydraulophonePlayback/1")
    XCTAssertEqual(refreshedStream.userAgent, "HydraulophonePlayback/1")
    XCTAssertEqual(initialStream.authoritativeDuration, 301.5)
    XCTAssertEqual(refreshedStream.authoritativeDuration, 301.5)
    var resourceLoader: StreamingResourceLoader?
    let asset = PlaybackAssetFactory.makeAsset(
      for: refreshedStream,
      retaining: &resourceLoader
    )
    XCTAssertEqual(asset.url, refreshedStream.url)
    XCTAssertEqual(
      PlaybackAssetFactory.options(for: refreshedStream)[AVURLAssetHTTPUserAgentKey] as? String,
      "HydraulophonePlayback/1"
    )
    resourceLoader?.cancelAll()
    let operations = await service.operations()
    XCTAssertEqual(operations, ["descriptor-false", "descriptor-true"])
  }
}

private actor RefreshRecordingMusicService: StreamRefreshingMusicService {
  private var recordedOperations: [String] = []

  func operations() -> [String] {
    recordedOperations
  }

  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 {
    recordedOperations.append("stream")
    return URL(fileURLWithPath: "/tmp/initial.m4a")
  }

  func refreshStream(for song: Song) async throws -> URL {
    recordedOperations.append("refresh")
    return URL(fileURLWithPath: "/tmp/refreshed.m4a")
  }
}

private actor DescriptorRecordingMusicService: MusicService, AudioStreamResolving {
  private var recordedOperations: [String] = []

  func operations() -> [String] {
    recordedOperations
  }

  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 {
    URL(fileURLWithPath: "/tmp/legacy.m4a")
  }

  func resolveAudioStream(
    for song: Song,
    refreshing: Bool
  ) async throws -> ResolvedAudioStream {
    recordedOperations.append("descriptor-\(refreshing)")
    return ResolvedAudioStream(
      url: URL(fileURLWithPath: refreshing ? "/tmp/refreshed.m4a" : "/tmp/initial.m4a"),
      userAgent: "HydraulophonePlayback/1",
      authoritativeDuration: 301.5
    )
  }
}