Split TrackApplicationResponse into a Local/External hierarchy

Same treatment as AlbumApplicationResponse, but Track is the cleaner
case: OwnerUserId (local-only) and ExternalId (external-only) are
both genuinely exclusive to one side, so the base carries neither and
is abstract — every real value is one of the two derived types.

    TrackApplicationResponse (abstract, shared fields + FromEntity dispatch)
      LocalTrackApplicationResponse    (+ OwnerUserId)
      ExternalTrackApplicationResponse (+ ExternalId)

Hit a real gotcha along the way: the response already had an explicit
Source property, which collides with the "source" discriminator STJ's
polymorphism support injects ("contains property 'source' that
conflicts with an existing metadata property name" — only surfaced at
runtime in the Api integration tests, not at build time). Fixed by
dropping the now-redundant explicit Source field entirely; the
discriminator already serializes the identical "Local"/"YouTube"
value under the same name, so the wire contract is unchanged. Updated
the two call sites that read .Source directly (TrackSearchItemResponse
and a playlist track filter that needed flatMap instead of
filter().map() to keep the narrowing across the chain).
This commit is contained in:
2026-09-10 19:19:59 +02:00
parent 7ae6c0a7a2
commit 10766bbb47
9 changed files with 144 additions and 32 deletions
@@ -1,7 +1,6 @@
using Musify.Application.Tests.TestSupport;
using Musify.Application.Tracks.Responses;
using Musify.Domain.Entities;
using Musify.Domain.ValueObjects;
using Xunit;
namespace Musify.Application.Tests.Shared
@@ -14,10 +13,9 @@ namespace Musify.Application.Tests.Shared
var owner = TestEntities.User(1, "The Owner");
var track = TestEntities.LocalTrack(owner);
var response = TrackApplicationResponse.FromEntity(track, listensCount: 3);
var response = Assert.IsType<LocalTrackApplicationResponse>(TrackApplicationResponse.FromEntity(track, listensCount: 3));
Assert.Equal("The Owner", response.Artist);
Assert.Equal(TrackSource.Local, response.Source);
Assert.Equal(owner.Id, response.OwnerUserId);
Assert.Equal(3, response.ListensCount);
}
@@ -34,7 +32,7 @@ namespace Musify.Application.Tests.Shared
new TrackArtist { TrackId = track.Id, ArtistId = first.Id, Position = 0, Artist = first, Track = track }
];
var response = TrackApplicationResponse.FromEntity(track, listensCount: 0);
var response = Assert.IsType<ExternalTrackApplicationResponse>(TrackApplicationResponse.FromEntity(track, listensCount: 0));
Assert.Equal("First Artist, Second Artist", response.Artist);
Assert.Equal(track.ExternalId, response.ExternalId);
@@ -3,6 +3,7 @@ using Musify.Application.Contracts;
using Musify.Application.Services;
using Musify.Application.Tests.TestSupport;
using Musify.Application.Tracks;
using Musify.Application.Tracks.Responses;
using Musify.Domain.ValueObjects;
using NSubstitute;
using Xunit;
@@ -47,7 +48,8 @@ namespace Musify.Application.Tests.Tracks
Assert.False(result.IsError);
Assert.Equal("My Song", result.Value.Title);
Assert.Equal(user.Id, result.Value.OwnerUserId);
var localResponse = Assert.IsType<LocalTrackApplicationResponse>(result.Value);
Assert.Equal(user.Id, localResponse.OwnerUserId);
await eventBus.Received(1).PublishAsync(Arg.Any<Musify.Application.Events.CreateTrackResourcesEvent>(), Arg.Any<CancellationToken>());
var stored = await Database.LocalTracks.FindAsync([result.Value.Id], TestContext.Current.CancellationToken);
@@ -1,6 +1,7 @@
using ErrorOr;
using Musify.Application.Tests.TestSupport;
using Musify.Application.Tracks;
using Musify.Application.Tracks.Responses;
using Xunit;
namespace Musify.Application.Tests.Tracks
@@ -21,7 +22,8 @@ namespace Musify.Application.Tests.Tracks
Assert.False(result.IsError);
Assert.Equal("My Song", result.Value.Title);
Assert.Equal(owner.Name, result.Value.Artist);
Assert.Equal(owner.Id, result.Value.OwnerUserId);
var localResponse = Assert.IsType<LocalTrackApplicationResponse>(result.Value);
Assert.Equal(owner.Id, localResponse.OwnerUserId);
}
[Fact]