Generalize YouTube-backed endpoints behind a source parameter

Removes the last YouTube-specific routes (/youtube/*) and merges them
into the general track/album/playlist endpoints, keyed by a `source`
string validated against TrackSource. This mirrors the earlier track
search merge for albums and playlist-track additions:

- GetAlbums now merges local albums with live YouTube Music search
  results, paginated via a youtubeContinuationToken, deduped against
  already-materialized ExternalAlbum rows.
- New GET /albums/external/{source}/{externalId} and
  GET /tracks/external/{source}/{externalId}/stream replace the
  YouTube-only equivalents.
- POST /playlists/{id}/tracks replaces the separate
  .../youtube-tracks endpoint, accepting an optional Source/ExternalId
  pair alongside TrackId.
- AlbumSearchItemResponse/AlbumsSearchResponse follow the same
  discriminated-union + trailing-HasNextPage-field pattern already
  used for track search, so HasNextPage stays a required field in the
  generated OpenAPI schema.
- Frontend routes renamed accordingly (/albums/youtube/[albumId] ->
  /albums/external/[source]/[externalId], etc.) with a single
  /api/thumbnail proxy replacing /api/youtube/thumbnail.

Musify.Api.Endpoints.YouTubeEndpoints and the standalone
SearchYouTubeAlbums query are removed as a result.
This commit is contained in:
2026-09-10 15:03:36 +02:00
parent c5b2b71eea
commit 29a7fed6a5
29 changed files with 353 additions and 254 deletions
@@ -1,15 +1,22 @@
using ErrorOr;
using Musify.Application.Albums;
using Musify.Application.Contracts;
using Musify.Application.Tests.TestSupport;
using Musify.Domain.ValueObjects;
using NSubstitute;
using Xunit;
namespace Musify.Application.Tests.Albums
{
public sealed class GetAlbumsQueryHandlerTests : HandlerTestBase
{
private GetAlbumsQueryHandler CreateHandler() => new(Database);
private readonly IYouTubeMusicService youTubeMusicService = Substitute.For<IYouTubeMusicService>();
private GetAlbumsQueryHandler CreateHandler() =>
new(Database, youTubeMusicService, NoOpLogger<GetAlbumsQueryHandler>());
[Fact]
public async Task Handle_NoTitleFilter_ReturnsAllAlbumsPaged()
public async Task Handle_NoTitleFilter_ReturnsAllAlbumsPaged_AndDoesNotCallYouTube()
{
var owner = TestEntities.User();
await SeedAsync(owner, TestEntities.UserAlbum(owner.Id, "Alpha"), TestEntities.UserAlbum(owner.Id, "Beta"));
@@ -18,19 +25,68 @@ namespace Musify.Application.Tests.Albums
Assert.False(result.IsError);
Assert.Equal(2, result.Value.TotalItemCount);
Assert.All(result.Value.Items, item => Assert.NotNull(item.Album));
await youTubeMusicService.DidNotReceiveWithAnyArgs()
.SearchAlbumsAsync(default!, default!, TestContext.Current.CancellationToken);
}
[Fact]
public async Task Handle_TitleFilter_ReturnsOnlyMatchingAlbums()
public async Task Handle_TitleFilter_ReturnsMatchingLocalAlbums_FollowedByYouTubeResults()
{
var owner = TestEntities.User();
await SeedAsync(owner, TestEntities.UserAlbum(owner.Id, "Rock Classics"), TestEntities.UserAlbum(owner.Id, "Jazz Favorites"));
var album = new YouTubeAlbumResult("album-1", "Rock Classics Reissue", "Various Artists", "thumb.jpg", 2020, false, false, []);
youTubeMusicService.SearchAlbumsAsync("rock", string.Empty, Arg.Any<CancellationToken>())
.Returns(new YouTubeAlbumSearchResult([album], "next-token"));
var result = await CreateHandler().Handle(new GetAlbumsQuery(Title: "rock", PageNumber: 1, PageSize: 10), TestContext.Current.CancellationToken);
Assert.False(result.IsError);
var album = Assert.Single(result.Value.Items);
Assert.Equal("Rock Classics", album.Title);
Assert.Equal(2, result.Value.Items.Count);
Assert.NotNull(result.Value.Items[0].Album);
Assert.Equal("Rock Classics", result.Value.Items[0].Album!.Title);
Assert.Null(result.Value.Items[1].Album);
Assert.NotNull(result.Value.Items[1].YouTubeAlbum);
Assert.Equal("album-1", result.Value.Items[1].YouTubeAlbum!.AlbumId);
Assert.Equal("next-token", result.Value.NextYoutubeContinuationToken);
Assert.False(result.Value.YoutubeUnavailable);
}
[Fact]
public async Task Handle_TitleFilter_SkipsYouTubeResultAlreadyMaterializedLocally()
{
var externalAlbum = TestEntities.ExternalAlbum(externalId: "album-1", title: "Rock Classics", source: TrackSource.YouTube);
await SeedAsync(externalAlbum);
var album = new YouTubeAlbumResult("album-1", "Rock Classics", "Various Artists", "thumb.jpg", 2020, false, false, []);
youTubeMusicService.SearchAlbumsAsync("rock", string.Empty, Arg.Any<CancellationToken>())
.Returns(new YouTubeAlbumSearchResult([album], string.Empty));
var result = await CreateHandler().Handle(new GetAlbumsQuery(Title: "rock", PageNumber: 1, PageSize: 10), TestContext.Current.CancellationToken);
Assert.False(result.IsError);
var item = Assert.Single(result.Value.Items);
Assert.NotNull(item.Album);
Assert.Null(item.YouTubeAlbum);
}
[Fact]
public async Task Handle_TitleFilter_YouTubeUnavailable_StillReturnsLocalResults()
{
var owner = TestEntities.User();
await SeedAsync(owner, TestEntities.UserAlbum(owner.Id, "Rock Classics"));
youTubeMusicService.SearchAlbumsAsync("rock", string.Empty, Arg.Any<CancellationToken>())
.Returns(Error.Forbidden(description: "The YouTube Music source is disabled."));
var result = await CreateHandler().Handle(new GetAlbumsQuery(Title: "rock", PageNumber: 1, PageSize: 10), TestContext.Current.CancellationToken);
Assert.False(result.IsError);
var item = Assert.Single(result.Value.Items);
Assert.NotNull(item.Album);
Assert.True(result.Value.YoutubeUnavailable);
Assert.Null(result.Value.NextYoutubeContinuationToken);
}
[Fact]