Combine track search with live YouTube Music results

GET /tracks now merges the local catalog page with a live YouTube Music
search (deduplicated against tracks already provisioned locally) whenever a
name filter is given, appending the YouTube hits after the local matches and
carrying a continuation token for further pages. Each item is tagged with its
source via the new TrackSearchItemResponse/TracksSearchResponse shapes,
wrapping the existing TrackApplicationResponse or a raw YouTubeSongResult so
neither type had to change.

GET /youtube/search is removed along with its now-unused handler; the
web-player's explore page, its /api/tracks proxy, and the YouTube filler
helper are updated to call the combined endpoint instead of hitting the two
separately.
This commit is contained in:
2026-09-10 09:55:58 +02:00
parent c418844178
commit 92a06a3c15
15 changed files with 266 additions and 240 deletions
@@ -1,15 +1,22 @@
using ErrorOr;
using Musify.Application.Contracts;
using Musify.Application.Tests.TestSupport;
using Musify.Application.Tracks;
using Musify.Domain.ValueObjects;
using NSubstitute;
using Xunit;
namespace Musify.Application.Tests.Tracks
{
public sealed class GetTracksQueryHandlerTests : HandlerTestBase
{
private GetTracksQueryHandler CreateHandler() => new(Database);
private readonly IYouTubeMusicService youTubeMusicService = Substitute.For<IYouTubeMusicService>();
private GetTracksQueryHandler CreateHandler() =>
new(Database, youTubeMusicService, NoOpLogger<GetTracksQueryHandler>());
[Fact]
public async Task Handle_NoNameFilter_ReturnsEveryTrackAUserOwns()
public async Task Handle_NoNameFilter_ReturnsEveryTrackAUserOwns_AndDoesNotCallYouTube()
{
var owner = TestEntities.User();
var trackA = TestEntities.LocalTrack(owner, "Alpha");
@@ -23,10 +30,14 @@ namespace Musify.Application.Tests.Tracks
Assert.False(result.IsError);
Assert.Equal(2, result.Value.TotalItemCount);
Assert.Equal(2, result.Value.Items.Count);
Assert.All(result.Value.Items, item => Assert.NotNull(item.Track));
await youTubeMusicService.DidNotReceiveWithAnyArgs()
.SearchSongsAsync(default!, default!, TestContext.Current.CancellationToken);
}
[Fact]
public async Task Handle_NameFilter_ReturnsOnlyMatchingTracks()
public async Task Handle_NameFilter_ReturnsMatchingLocalTracks_FollowedByYouTubeResults()
{
var owner = TestEntities.User();
var match = TestEntities.LocalTrack(owner, "Bohemian Rhapsody");
@@ -36,11 +47,65 @@ namespace Musify.Application.Tests.Tracks
new Musify.Domain.Entities.UserHasTrack { UserId = owner.Id, TrackId = match.Id },
new Musify.Domain.Entities.UserHasTrack { UserId = owner.Id, TrackId = other.Id });
var song = new YouTubeSongResult("video-1", "Bohemian Rhapsody (Live)", "Queen", "A Night at the Opera",
354, "thumb.jpg", false, []);
youTubeMusicService.SearchSongsAsync("rhapsody", string.Empty, Arg.Any<CancellationToken>())
.Returns(new YouTubeSearchResult([song], "next-token"));
var result = await CreateHandler().Handle(new GetTracksQuery(Name: "rhapsody", PageNumber: 1, PageSize: 10), TestContext.Current.CancellationToken);
Assert.False(result.IsError);
var track = Assert.Single(result.Value.Items);
Assert.Equal("Bohemian Rhapsody", track.Title);
Assert.Equal(2, result.Value.Items.Count);
Assert.NotNull(result.Value.Items[0].Track);
Assert.Equal("Bohemian Rhapsody", result.Value.Items[0].Track!.Title);
Assert.Null(result.Value.Items[1].Track);
Assert.NotNull(result.Value.Items[1].YouTubeSong);
Assert.Equal("video-1", result.Value.Items[1].YouTubeSong!.VideoId);
Assert.Equal("next-token", result.Value.NextYoutubeContinuationToken);
Assert.False(result.Value.YoutubeUnavailable);
}
[Fact]
public async Task Handle_NameFilter_SkipsYouTubeResultAlreadyProvisionedLocally()
{
var owner = TestEntities.User();
var provisioned = TestEntities.ExternalTrack(externalId: "video-1", title: "Bohemian Rhapsody", source: TrackSource.YouTube);
await SeedAsync(
owner, provisioned,
new Musify.Domain.Entities.UserHasTrack { UserId = owner.Id, TrackId = provisioned.Id });
var song = new YouTubeSongResult("video-1", "Bohemian Rhapsody", "Queen", "A Night at the Opera",
354, "thumb.jpg", false, []);
youTubeMusicService.SearchSongsAsync("rhapsody", string.Empty, Arg.Any<CancellationToken>())
.Returns(new YouTubeSearchResult([song], string.Empty));
var result = await CreateHandler().Handle(new GetTracksQuery(Name: "rhapsody", PageNumber: 1, PageSize: 10), TestContext.Current.CancellationToken);
Assert.False(result.IsError);
var item = Assert.Single(result.Value.Items);
Assert.NotNull(item.Track);
Assert.Null(item.YouTubeSong);
}
[Fact]
public async Task Handle_NameFilter_YouTubeUnavailable_StillReturnsLocalResults()
{
var owner = TestEntities.User();
var match = TestEntities.LocalTrack(owner, "Bohemian Rhapsody");
await SeedAsync(
owner, match,
new Musify.Domain.Entities.UserHasTrack { UserId = owner.Id, TrackId = match.Id });
youTubeMusicService.SearchSongsAsync("rhapsody", string.Empty, Arg.Any<CancellationToken>())
.Returns(Error.Forbidden(description: "The YouTube Music source is disabled."));
var result = await CreateHandler().Handle(new GetTracksQuery(Name: "rhapsody", PageNumber: 1, PageSize: 10), TestContext.Current.CancellationToken);
Assert.False(result.IsError);
var item = Assert.Single(result.Value.Items);
Assert.NotNull(item.Track);
Assert.True(result.Value.YoutubeUnavailable);
Assert.Null(result.Value.NextYoutubeContinuationToken);
}
}
}