using ErsatzTV.Application.Channels; using ErsatzTV.Core; using ErsatzTV.Core.Domain; using ErsatzTV.Infrastructure.Data; using ErsatzTV.Tests.Support; using LanguageExt; using Microsoft.EntityFrameworkCore; using NUnit.Framework; using Shouldly; namespace ErsatzTV.Tests.Application.Channels; [TestFixture] public class UpdateChannelGraphicsElementsTests : ChannelHandlerTestBase { private UpdateChannelHandler MakeHandler() => new(Worker, Db.Factory, SearchTargets, RemoteLogoCacher); private async Task<(int ElementAId, int ElementBId)> SeedGraphicsElements() { await using TvContext context = Db.CreateContext(); var elementA = new GraphicsElement { Path = "element-a.yml" }; var elementB = new GraphicsElement { Path = "element-b.yml" }; context.GraphicsElements.AddRange(elementA, elementB); await context.SaveChangesAsync(); return (elementA.Id, elementB.Id); } [Test] public async Task Should_Reconcile_GraphicsElement_Join_Add_Then_Remove() { await SeedFFmpegProfile(); Channel channel = await SeedChannel(1, "5"); (int elementAId, int elementBId) = await SeedGraphicsElements(); Either addResult = await MakeHandler().Handle( MakeUpdate(channel.Id, number: "5", graphicsElementIds: [elementAId, elementBId]), CancellationToken.None); addResult.IsRight.ShouldBeTrue(); await using (TvContext context = Db.CreateContext()) { Channel reloaded = await context.Channels.Include(c => c.ChannelGraphicsElements) .SingleAsync(c => c.Id == channel.Id); reloaded.ChannelGraphicsElements.Select(x => x.GraphicsElementId) .OrderBy(id => id) .ShouldBe(new[] { elementAId, elementBId }.OrderBy(id => id)); } Either removeResult = await MakeHandler().Handle( MakeUpdate(channel.Id, number: "5", graphicsElementIds: [elementAId]), CancellationToken.None); removeResult.IsRight.ShouldBeTrue(); await using (TvContext context = Db.CreateContext()) { Channel reloaded = await context.Channels.Include(c => c.ChannelGraphicsElements) .SingleAsync(c => c.Id == channel.Id); reloaded.ChannelGraphicsElements.Select(x => x.GraphicsElementId).ShouldBe(new[] { elementAId }); } Either clearResult = await MakeHandler().Handle( MakeUpdate(channel.Id, number: "5", graphicsElementIds: []), CancellationToken.None); clearResult.IsRight.ShouldBeTrue(); await using (TvContext context = Db.CreateContext()) { Channel reloaded = await context.Channels.Include(c => c.ChannelGraphicsElements) .SingleAsync(c => c.Id == channel.Id); reloaded.ChannelGraphicsElements.ShouldBeEmpty(); } } }