using ErsatzTV.Application.Channels; using ErsatzTV.Core; using LanguageExt; using ErsatzTV.Core.Errors; using ErsatzTV.Infrastructure.Data; using ErsatzTV.Tests.Support; using Microsoft.EntityFrameworkCore; using NUnit.Framework; using Shouldly; namespace ErsatzTV.Tests.Application.Channels; [TestFixture] public class CreateChannelHandlerTests : ChannelHandlerTestBase { private CreateChannelHandler MakeHandler() => new(Worker, Db.Factory, SearchTargets); [Test] public async Task Should_Create_Channel_When_Valid() { await SeedFFmpegProfile(); Either result = await MakeHandler().Handle(MakeCreate(number: "7", name: "News"), CancellationToken.None); result.IsRight.ShouldBeTrue(); await using TvContext context = Db.CreateContext(); bool exists = await context.Channels.AnyAsync(c => c.Number == "7" && c.Name == "News"); exists.ShouldBeTrue(); } [Test] public async Task Should_Reject_Duplicate_Number_With_422_Error() { await SeedFFmpegProfile(); await SeedChannel(1, "5"); Either result = await MakeHandler().Handle(MakeCreate(number: "5"), CancellationToken.None); BaseError error = LeftOf(result); error.ShouldNotBeOfType(); error.Value.ShouldContain("unique"); } [Test] public async Task Should_Reject_ShowInEpg_When_Disabled() { await SeedFFmpegProfile(); Either result = await MakeHandler().Handle( MakeCreate(number: "8", isEnabled: false, showInEpg: true), CancellationToken.None); BaseError error = LeftOf(result); error.ShouldNotBeOfType(); error.Value.ShouldContain("EPG"); } [Test] public async Task Should_Reject_Invalid_External_Logo_Url() { await SeedFFmpegProfile(); Either result = await MakeHandler().Handle( MakeCreate(number: "9", logoPath: "ftp://example.com/logo.png"), CancellationToken.None); BaseError error = LeftOf(result); error.ShouldNotBeOfType(); error.Value.ShouldContain("logo"); } [Test] public async Task Should_Reject_Empty_Group() { await SeedFFmpegProfile(); Either result = await MakeHandler().Handle(MakeCreate(number: "11", group: " "), CancellationToken.None); BaseError error = LeftOf(result); error.ShouldNotBeOfType(); error.Value.ShouldContain("group"); } [Test] public async Task Should_Reject_Nonexistent_FFmpegProfile() { // intentionally do not seed an FFmpegProfile Either result = await MakeHandler().Handle(MakeCreate(number: "12", ffmpegProfileId: 999), CancellationToken.None); BaseError error = LeftOf(result); error.ShouldNotBeOfType(); error.Value.ShouldContain("FFmpegProfile"); } private static BaseError LeftOf(Either either) => either.Match(Left: e => e, Right: _ => throw new AssertionException("Expected a Left result")); }