- CreateAutoTunedChannelsRequest.ToCommand(): guard null Channels (was NREing on a request body that omits "channels", causing HTTP 500). - CreateAutoTunedChannelsHandler.CreateOne: when CreateChannelFromLineup returns Left (Skipped/Failed), roll back the just-created SmartCollection via DeleteSmartCollection so retries don't fail on SmartCollection-name uniqueness. Best-effort; the delete result does not change the outcome. - PreviewAutoTuneChannelsHandler: filter out proposals whose generated name exceeds the 50-char Channel.Name limit before number allocation, so numbers aren't wasted on proposals that can never be created. - docs/superpowers/specs/2026-07-16-auto-tuning-design.md: fix field-name drift in JSON examples (proposedNumber -> number, error -> reason) to match the actual AutoTuneProposal/AutoTuneChannelOutcome DTOs. Refs #69
122 lines
4.8 KiB
C#
122 lines
4.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using ErsatzTV.Application.Channels;
|
|
using ErsatzTV.Application.MediaCollections;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Api.Channels;
|
|
using ErsatzTV.Core.Domain;
|
|
using LanguageExt;
|
|
using MediatR;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Shouldly;
|
|
|
|
namespace ErsatzTV.Tests.Application.Channels;
|
|
|
|
[TestFixture]
|
|
public class CreateAutoTunedChannelsHandlerTests
|
|
{
|
|
private ISender _mediator = null!;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_mediator = Substitute.For<ISender>();
|
|
// Smart collection creation always succeeds, echoing an incrementing id.
|
|
_mediator.Send(Arg.Any<CreateSmartCollection>(), Arg.Any<CancellationToken>())
|
|
.Returns(ci =>
|
|
{
|
|
var cmd = ci.Arg<CreateSmartCollection>();
|
|
return (Either<BaseError, SmartCollectionViewModel>)
|
|
new SmartCollectionViewModel(7, cmd.Name, cmd.Query);
|
|
});
|
|
_mediator.Send(Arg.Any<DeleteSmartCollection>(), Arg.Any<CancellationToken>())
|
|
.Returns((Either<BaseError, LanguageExt.Unit>)LanguageExt.Unit.Default);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Creates_Selected_Channels_And_Reports_Counts()
|
|
{
|
|
_mediator.Send(Arg.Any<CreateChannelFromLineup>(), Arg.Any<CancellationToken>())
|
|
.Returns((Either<BaseError, CreateChannelFromLineupResponseModel>)
|
|
new CreateChannelFromLineupResponseModel(88, null, 1, 2));
|
|
|
|
var result = await Handle(new CreateAutoTunedChannels(
|
|
TemplateId: 3, Group: "Auto-Tuned",
|
|
new List<AutoTuneChannelSelection>
|
|
{
|
|
new(AutoTuneAxis.TvShow, "The Office", "The Office", "500")
|
|
}));
|
|
|
|
result.CreatedCount.ShouldBe(1);
|
|
result.Results[0].Status.ShouldBe(AutoTuneOutcomeStatus.Created);
|
|
result.Results[0].ChannelId.ShouldBe(88);
|
|
|
|
// Smart collection built with the server-generated query.
|
|
await _mediator.Received().Send(
|
|
Arg.Is<CreateSmartCollection>(c => c.Query == "type:episode AND show_title:\"The Office\""),
|
|
Arg.Any<CancellationToken>());
|
|
|
|
// Channel created referencing the smart collection id, number, and SeasonEpisode order.
|
|
await _mediator.Received().Send(
|
|
Arg.Is<CreateChannelFromLineup>(c =>
|
|
c.Number == "500" &&
|
|
c.TemplateId == 3 &&
|
|
c.Advanced.PlaybackOrder == PlaybackOrder.SeasonEpisode &&
|
|
c.Lineup.Count == 1 &&
|
|
c.Lineup[0].CollectionType == CollectionType.SmartCollection &&
|
|
c.Lineup[0].SmartCollectionId == 7),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task Number_Collision_Is_Skipped_Not_Failed()
|
|
{
|
|
_mediator.Send(Arg.Any<CreateChannelFromLineup>(), Arg.Any<CancellationToken>())
|
|
.Returns((Either<BaseError, CreateChannelFromLineupResponseModel>)
|
|
BaseError.New("Channel number must be unique"));
|
|
|
|
var result = await Handle(new CreateAutoTunedChannels(3, "Auto-Tuned",
|
|
new List<AutoTuneChannelSelection> { new(AutoTuneAxis.TvGenre, "Comedy", "Comedy", "500") }));
|
|
|
|
result.SkippedCount.ShouldBe(1);
|
|
result.Results[0].Status.ShouldBe(AutoTuneOutcomeStatus.Skipped);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Number_Collision_Rolls_Back_Orphaned_SmartCollection()
|
|
{
|
|
_mediator.Send(Arg.Any<CreateChannelFromLineup>(), Arg.Any<CancellationToken>())
|
|
.Returns((Either<BaseError, CreateChannelFromLineupResponseModel>)
|
|
BaseError.New("Channel number must be unique"));
|
|
|
|
var result = await Handle(new CreateAutoTunedChannels(3, "Auto-Tuned",
|
|
new List<AutoTuneChannelSelection> { new(AutoTuneAxis.TvGenre, "Comedy", "Comedy", "500") }));
|
|
|
|
result.Results[0].Status.ShouldBe(AutoTuneOutcomeStatus.Skipped);
|
|
|
|
await _mediator.Received().Send(
|
|
Arg.Is<DeleteSmartCollection>(d => d.SmartCollectionId == 7),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task Other_Errors_Are_Failed()
|
|
{
|
|
_mediator.Send(Arg.Any<CreateChannelFromLineup>(), Arg.Any<CancellationToken>())
|
|
.Returns((Either<BaseError, CreateChannelFromLineupResponseModel>)
|
|
BaseError.New("FFmpegProfile 9 does not exist."));
|
|
|
|
var result = await Handle(new CreateAutoTunedChannels(3, "Auto-Tuned",
|
|
new List<AutoTuneChannelSelection> { new(AutoTuneAxis.TvGenre, "Comedy", "Comedy", "500") }));
|
|
|
|
result.FailedCount.ShouldBe(1);
|
|
result.Results[0].Status.ShouldBe(AutoTuneOutcomeStatus.Failed);
|
|
}
|
|
|
|
private Task<AutoTuneResult> Handle(CreateAutoTunedChannels request) =>
|
|
new CreateAutoTunedChannelsHandler(_mediator).Handle(request, CancellationToken.None);
|
|
}
|