using System.Threading.Channels; using ErsatzTV.Application; using ErsatzTV.Application.Artworks; using ErsatzTV.Application.Channels; using ErsatzTV.Core.Domain; using ErsatzTV.Core.Interfaces.Images; using ErsatzTV.Core.Interfaces.Search; using ErsatzTV.Infrastructure.Data; using NSubstitute; using NUnit.Framework; using DomainChannel = ErsatzTV.Core.Domain.Channel; namespace ErsatzTV.Tests.Support; public abstract class ChannelHandlerTestBase { protected InMemoryTvContext Db = null!; protected ChannelWriter Worker = null!; protected ISearchTargets SearchTargets = null!; protected IRemoteLogoCacher RemoteLogoCacher = null!; [SetUp] public async Task BaseSetUp() { Db = await InMemoryTvContext.CreateAsync(); Worker = System.Threading.Channels.Channel.CreateUnbounded().Writer; SearchTargets = Substitute.For(); RemoteLogoCacher = Substitute.For(); } [TearDown] public async Task BaseTearDown() => await Db.DisposeAsync(); protected async Task SeedFFmpegProfile(int id = 1) { await using TvContext context = Db.CreateContext(); context.FFmpegProfiles.Add(new FFmpegProfile { Id = id, Name = $"profile-{id}" }); await context.SaveChangesAsync(); } protected async Task SeedChannel( int id, string number, string name = "Test", int ffmpegProfileId = 1, string group = "ErsatzTV") { await using TvContext context = Db.CreateContext(); var channel = new DomainChannel(Guid.NewGuid()) { Id = id, Number = number, Name = name, Group = group, Categories = string.Empty, FFmpegProfileId = ffmpegProfileId, StreamSelector = string.Empty, PreferredAudioLanguageCode = string.Empty, PreferredAudioTitle = string.Empty, PreferredSubtitleLanguageCode = string.Empty, MusicVideoCreditsTemplate = string.Empty, StreamingMode = StreamingMode.TransportStreamHybrid, PlayoutSource = ChannelPlayoutSource.Generated, PlayoutMode = ChannelPlayoutMode.Continuous }; context.Channels.Add(channel); await context.SaveChangesAsync(); return channel; } protected async Task SeedPlayout(int id, int channelId) { await using TvContext context = Db.CreateContext(); context.Playouts.Add( new Playout { Id = id, ChannelId = channelId, ScheduleKind = PlayoutScheduleKind.Classic }); await context.SaveChangesAsync(); } protected static CreateChannel MakeCreate( string number = "5", int ffmpegProfileId = 1, bool isEnabled = true, bool showInEpg = false, string logoPath = "", string name = "Test", string group = "ErsatzTV", StreamingMode streamingMode = StreamingMode.TransportStreamHybrid) => new( name, number, group, string.Empty, ffmpegProfileId, null, new ArtworkContentTypeModel(logoPath, string.Empty), ChannelStreamSelectorMode.Default, string.Empty, string.Empty, string.Empty, ChannelPlayoutSource.Generated, ChannelPlayoutMode.Continuous, null, null, streamingMode, null, null, string.Empty, ChannelSubtitleMode.None, ChannelMusicVideoCreditsMode.None, string.Empty, ChannelSongVideoMode.Default, ChannelTranscodeMode.OnDemand, ChannelIdleBehavior.StopOnDisconnect, isEnabled, showInEpg); protected static UpdateChannel MakeUpdate( int channelId, string number = "5", int ffmpegProfileId = 1, bool isEnabled = true, bool showInEpg = false, string logoPath = "", string name = "Test", string group = "ErsatzTV", List graphicsElementIds = null) => new( channelId, name, number, group, string.Empty, ffmpegProfileId, null, new ArtworkContentTypeModel(logoPath, string.Empty), ChannelStreamSelectorMode.Default, string.Empty, string.Empty, string.Empty, ChannelPlayoutSource.Generated, ChannelPlayoutMode.Continuous, null, null, StreamingMode.TransportStreamHybrid, null, null, string.Empty, ChannelSubtitleMode.None, ChannelMusicVideoCreditsMode.None, string.Empty, ChannelSongVideoMode.Default, ChannelTranscodeMode.OnDemand, ChannelIdleBehavior.StopOnDisconnect, isEnabled, showInEpg, graphicsElementIds ?? []); }