Files
Musify/backend/Tests/Musify.Application.Tests/Tracks/TrackCreationWithoutPictureTests.cs
T
Admin ebfd41b160 Add track publication, album types, optional track pictures and batch upload URLs
Tracks
- Track.IsPublished (migration marks existing tracks as published). New
  tracks are created unpublished.
- PUT /tracks/{id}/published and PUT /tracks/published change the flag;
  publishing requires completed audio processing (the batch endpoint
  reports skipped tracks).
- An unpublished track is only visible to its owner. A WherePublished /
  VisibleTo filter is applied to search, genres, listening history, last
  listened, likes, mixes, album and playlist contents and counts, recently
  listened albums, public user tracks, track details and stream tickets.
- isPublished is part of the track responses, GET /tracks/users/{userId}
  accepts a published filter and status-counts reports published and
  unpublished totals.
- CreateTrackRequest.pictureIntentId is optional: without it the track
  uses the album cover or the preset picture and no picture processing
  runs. CreateTrackResourcesEvent carries nullable picture fields, so
  queues must be drained before deploying.
- POST /tracks/upload-urls accepts an audios list and one optional picture
  shared by all of them, and returns an intent per file. The upload intent
  limits are raised so a batch fits.

Albums
- AlbumType (Album, EP, Single) on create, update and responses.
- CreateAlbumRequest.trackIds creates the album and links the tracks in a
  single save.
2026-09-25 01:37:55 +02:00

153 lines
6.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using Musify.Application.Configuration;
using Musify.Application.Contracts;
using Musify.Application.Events;
using Musify.Application.Pictures;
using Musify.Application.Services;
using Musify.Application.Tests.TestSupport;
using Musify.Application.Tracks;
using Musify.Domain.Entities;
using Musify.Domain.ValueObjects;
using NSubstitute;
using Xunit;
namespace Musify.Application.Tests.Tracks
{
public sealed class TrackCreationWithoutPictureTests : HandlerTestBase
{
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private readonly IEventBus _eventBus = Substitute.For<IEventBus>();
private readonly IStorageService _storageService = Substitute.For<IStorageService>();
private CreateTrackCommandHandler CreateHandler()
{
_storageService
.HeadObjectAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(new ObjectMetaData("audio/mpeg", 1024));
return new CreateTrackCommandHandler(
Database,
_eventBus,
new UploadIntentValidator(Database, _storageService, TestConfigurations.UploadIntent()),
NoOpLogger<CreateTrackCommandHandler>(),
TestConfigurations.Track());
}
[Fact]
public async Task CreateTrack_WithoutPicture_UsesPresetsAndSkipsPictureProcessing()
{
var user = TestEntities.User();
var audio = TestEntities.UploadIntent(user.Id, UploadIntentPurpose.TrackAudio, objectName: "song.mp3");
await SeedAsync(user, audio);
var routes = TestConfigurations.Track().Routes;
var result = await CreateHandler().Handle(new CreateTrackCommand(user.Id, "No cover", null, audio.Id, [Genre.Pop]), Ct);
Assert.False(result.IsError);
Assert.False(result.Value.IsPublished);
var stored = await Database.Tracks.AsNoTracking().SingleAsync(Ct);
Assert.Null(stored.Pictures.OriginalName);
Assert.Equal(routes.PresetSmallPicture, stored.Pictures.SmallName);
Assert.Equal(routes.PresetMediumPicture, stored.Pictures.MediumName);
Assert.Equal(routes.PresetLargePicture, stored.Pictures.LargeName);
Assert.Equal(ProcessingStatus.Completed, stored.Pictures.ProcessingStatus);
Assert.False(stored.IsPublished);
await _eventBus.Received(1).PublishAsync(
Arg.Is<CreateTrackResourcesEvent>(message =>
message.PictureIntentId == null
&& message.PictureSourceKey == null
&& message.PictureDestinationKey == null
&& message.AudioIntentId == audio.Id),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task CreateTrack_WithPicture_StaysUnpublishedAndKeepsPictureFlow()
{
var user = TestEntities.User();
var picture = TestEntities.UploadIntent(user.Id, UploadIntentPurpose.TrackPicture, objectName: "cover.webp");
var audio = TestEntities.UploadIntent(user.Id, UploadIntentPurpose.TrackAudio, objectName: "song.mp3");
await SeedAsync(user, picture, audio);
var result = await CreateHandler().Handle(new CreateTrackCommand(user.Id, "Cover", picture.Id, audio.Id, [Genre.Pop]), Ct);
Assert.False(result.IsError);
Assert.False(result.Value.IsPublished);
var stored = await Database.Tracks.AsNoTracking().SingleAsync(Ct);
Assert.Equal("cover.webp", stored.Pictures.OriginalName);
Assert.Equal(ProcessingStatus.Pending, stored.Pictures.ProcessingStatus);
await _eventBus.Received(1).PublishAsync(
Arg.Is<CreateTrackResourcesEvent>(message => message.PictureIntentId == picture.Id && message.PictureSourceKey != null),
Arg.Any<CancellationToken>());
}
private GetCoverQueryHandler CreateCoverHandler() => new(
Database,
TestConfigurations.Storage("covers-bucket"),
TestConfigurations.Track(),
TestConfigurations.Album(),
TestConfigurations.PlayList());
[Fact]
public async Task Cover_TrackWithoutPictureInAnAlbum_UsesTheAlbumCover()
{
var owner = TestEntities.User();
var track = TestEntities.Track(owner, pictures: new TrackPictures
{
SmallName = "PresetSmallPicture.webp",
MediumName = "PresetMediumPicture.webp",
LargeName = "PresetLargePicture.webp",
ProcessingStatus = ProcessingStatus.Completed
});
var album = TestEntities.Album(owner.Id, pictures: new EntityPictures
{
OriginalName = "album-original.webp",
SmallName = "album-small.webp",
MediumName = "album-medium.webp",
LargeName = "album-large.webp"
});
await SeedAsync(owner, track, album, new AlbumHasTrack { AlbumId = album.Id, TrackId = track.Id, TrackNumber = 1 });
var result = await CreateCoverHandler().Handle(new GetCoverQuery(CoverOwner.Track, track.Id, PictureSize.Small), Ct);
Assert.False(result.IsError);
Assert.EndsWith("album-small.webp", result.Value.Key);
}
[Fact]
public async Task Cover_TrackWithoutPictureAndAlbum_FallsBackToThePreset()
{
var owner = TestEntities.User();
var track = TestEntities.Track(owner, pictures: new TrackPictures
{
SmallName = "PresetSmallPicture.webp",
MediumName = "PresetMediumPicture.webp",
LargeName = "PresetLargePicture.webp",
ProcessingStatus = ProcessingStatus.Completed
});
await SeedAsync(owner, track);
var result = await CreateCoverHandler().Handle(new GetCoverQuery(CoverOwner.Track, track.Id, PictureSize.Small), Ct);
Assert.False(result.IsError);
Assert.EndsWith("PresetSmallPicture.webp", result.Value.Key);
}
[Fact]
public async Task Cover_TrackWithOwnPicture_IgnoresTheAlbumCover()
{
var owner = TestEntities.User();
var track = TestEntities.Track(owner);
var album = TestEntities.Album(owner.Id);
await SeedAsync(owner, track, album, new AlbumHasTrack { AlbumId = album.Id, TrackId = track.Id, TrackNumber = 1 });
var result = await CreateCoverHandler().Handle(new GetCoverQuery(CoverOwner.Track, track.Id, PictureSize.Small), Ct);
Assert.EndsWith("small.webp", result.Value.Key);
Assert.DoesNotContain("album", result.Value.Key, StringComparison.OrdinalIgnoreCase);
}
}
}