using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Emby.Naming.Common; using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.IO; using MediaBrowser.Providers.MediaInfo; using Microsoft.Extensions.Logging; using Moq; using Xunit; namespace Jellyfin.Providers.Tests.MediaInfo; public class AudioResolverTests { private readonly AudioResolver _audioResolver; public AudioResolverTests() { // prep BaseItem and Video for calls made that expect managers Video.RecordingsManager = Mock.Of(); var applicationPaths = new Mock().Object; var serverConfig = new Mock(); serverConfig.Setup(c => c.ApplicationPaths) .Returns(applicationPaths); BaseItem.ConfigurationManager = serverConfig.Object; // build resolver to test with var localizationManager = Mock.Of(); var mediaEncoder = new Mock(MockBehavior.Strict); mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny(), It.IsAny())) .Returns((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo { MediaStreams = new List { new() { Type = MediaStreamType.Audio } } })); var fileSystem = new Mock(MockBehavior.Strict); fileSystem.Setup(fs => fs.DirectoryExists(It.IsRegex(MediaInfoResolverTests.VideoDirectoryRegex))) .Returns(true); fileSystem.Setup(fs => fs.DirectoryExists(It.IsRegex(MediaInfoResolverTests.MetadataDirectoryRegex))) .Returns(true); _audioResolver = new AudioResolver(Mock.Of>(), localizationManager, mediaEncoder.Object, fileSystem.Object, new NamingOptions()); } [Theory] [InlineData("My.Video.srt", false, false)] [InlineData("My.Video.mp3", false, true)] [InlineData("My.Video.srt", true, false)] [InlineData("My.Video.mp3", true, true)] public async Task GetExternalStreams_MixedFilenames_PicksAudio(string file, bool metadataDirectory, bool matches) { BaseItem.MediaSourceManager = Mock.Of(); var video = new Movie { Path = MediaInfoResolverTests.VideoDirectoryPath + "/My.Video.mkv" }; var directoryService = MediaInfoResolverTests.GetDirectoryServiceForExternalFile(file, metadataDirectory); var streams = await _audioResolver.GetExternalStreamsAsync(video, 0, directoryService, false, CancellationToken.None); if (matches) { Assert.Single(streams); var actual = streams[0]; Assert.Equal(MediaStreamType.Audio, actual.Type); } else { Assert.Empty(streams); } } }