|  | @@ -1,200 +1,79 @@
 | 
	
		
			
				|  |  | -using System;
 | 
	
		
			
				|  |  |  using System.Collections.Generic;
 | 
	
		
			
				|  |  | -using System.Text.RegularExpressions;
 | 
	
		
			
				|  |  |  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.Controller.Providers;
 | 
	
		
			
				|  |  |  using MediaBrowser.Model.Entities;
 | 
	
		
			
				|  |  |  using MediaBrowser.Model.Globalization;
 | 
	
		
			
				|  |  |  using MediaBrowser.Providers.MediaInfo;
 | 
	
		
			
				|  |  |  using Moq;
 | 
	
		
			
				|  |  |  using Xunit;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -namespace Jellyfin.Providers.Tests.MediaInfo
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -    public class SubtitleResolverTests
 | 
	
		
			
				|  |  | -    {
 | 
	
		
			
				|  |  | -        private const string VideoDirectoryPath = "Test Data/Video";
 | 
	
		
			
				|  |  | -        private const string MetadataDirectoryPath = "Test Data/Metadata";
 | 
	
		
			
				|  |  | -        private readonly SubtitleResolver _subtitleResolver;
 | 
	
		
			
				|  |  | +namespace Jellyfin.Providers.Tests.MediaInfo;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        public SubtitleResolverTests()
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | -            var englishCultureDto = new CultureDto("English", "English", "en", new[] { "eng" });
 | 
	
		
			
				|  |  | -            var frenchCultureDto = new CultureDto("French", "French", "fr", new[] { "fre", "fra" });
 | 
	
		
			
				|  |  | +public class SubtitleResolverTests
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    private readonly SubtitleResolver _subtitleResolver;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            var localizationManager = new Mock<ILocalizationManager>(MockBehavior.Loose);
 | 
	
		
			
				|  |  | -            localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"en.*", RegexOptions.IgnoreCase)))
 | 
	
		
			
				|  |  | -                .Returns(englishCultureDto);
 | 
	
		
			
				|  |  | -            localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"fr.*", RegexOptions.IgnoreCase)))
 | 
	
		
			
				|  |  | -                .Returns(frenchCultureDto);
 | 
	
		
			
				|  |  | +    public SubtitleResolverTests()
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        // prep BaseItem and Video for calls made that expect managers
 | 
	
		
			
				|  |  | +        Video.LiveTvManager = Mock.Of<ILiveTvManager>();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
 | 
	
		
			
				|  |  | -            mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny<MediaInfoRequest>(), It.IsAny<CancellationToken>()))
 | 
	
		
			
				|  |  | -                .Returns<MediaInfoRequest, CancellationToken>((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo
 | 
	
		
			
				|  |  | -                {
 | 
	
		
			
				|  |  | -                    MediaStreams = new List<MediaStream>
 | 
	
		
			
				|  |  | -                    {
 | 
	
		
			
				|  |  | -                        new()
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                }));
 | 
	
		
			
				|  |  | +        var applicationPaths = new Mock<IServerApplicationPaths>().Object;
 | 
	
		
			
				|  |  | +        var serverConfig = new Mock<IServerConfigurationManager>();
 | 
	
		
			
				|  |  | +        serverConfig.Setup(c => c.ApplicationPaths)
 | 
	
		
			
				|  |  | +            .Returns(applicationPaths);
 | 
	
		
			
				|  |  | +        BaseItem.ConfigurationManager = serverConfig.Object;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            _subtitleResolver = new SubtitleResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions());
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | +        // build resolver to test with
 | 
	
		
			
				|  |  | +        var localizationManager = Mock.Of<ILocalizationManager>();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        [Fact]
 | 
	
		
			
				|  |  | -        public async void AddExternalStreamsAsync_GivenMixedFilenames_ReturnsValidSubtitles()
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | -            var startIndex = 0;
 | 
	
		
			
				|  |  | -            var index = startIndex;
 | 
	
		
			
				|  |  | -            var files = new[]
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/MyVideo.en.srt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/MyVideo.en.forced.default.sub",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.mp3",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.png",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.srt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.txt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.vtt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.ass",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.sub",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.ssa",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.smi",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.sami",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.mks",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.en.srt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.default.en.srt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.default.forced.en.srt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.en.default.forced.srt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.en.With Additional Garbage.sub",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.With Additional Garbage.English.sub",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/My.Video.With.Additional.Garbage.en.srt",
 | 
	
		
			
				|  |  | -                VideoDirectoryPath + "/Some.Other.Video.srt"
 | 
	
		
			
				|  |  | -            };
 | 
	
		
			
				|  |  | -            var metadataFiles = new[]
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | -                MetadataDirectoryPath + "/My.Video.en.srt"
 | 
	
		
			
				|  |  | -            };
 | 
	
		
			
				|  |  | -            var expectedResult = new[]
 | 
	
		
			
				|  |  | +        var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
 | 
	
		
			
				|  |  | +        mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny<MediaInfoRequest>(), It.IsAny<CancellationToken>()))
 | 
	
		
			
				|  |  | +            .Returns<MediaInfoRequest, CancellationToken>((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/MyVideo.en.srt", "srt", "eng", null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/MyVideo.en.forced.default.sub", "sub", "eng", null, index++, isDefault: true, isForced: true),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.srt", "srt", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.vtt", "vtt", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.ass", "ass", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.sub", "sub", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.ssa", "ssa", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.smi", "smi", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.sami", "sami", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.mks", "mks", null, null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.en.srt", "srt", "eng", null, index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.default.en.srt", "srt", "eng", null, index++, isDefault: true),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.default.forced.en.srt", "srt", "eng", null, index++, isForced: true, isDefault: true),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.en.default.forced.srt", "srt", "eng", null, index++, isForced: true, isDefault: true),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.en.With Additional Garbage.sub", "sub", "eng", "With Additional Garbage", index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.With Additional Garbage.English.sub", "sub", "eng", "With Additional Garbage", index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(VideoDirectoryPath + "/My.Video.With.Additional.Garbage.en.srt", "srt", "eng", "With.Additional.Garbage", index++),
 | 
	
		
			
				|  |  | -                CreateMediaStream(MetadataDirectoryPath + "/My.Video.en.srt", "srt", "eng", null, index)
 | 
	
		
			
				|  |  | -            };
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            var video = new Mock<Video>();
 | 
	
		
			
				|  |  | -            video.CallBase = true;
 | 
	
		
			
				|  |  | -            video.Setup(moq => moq.Path).Returns(VideoDirectoryPath + "/My.Video.mkv");
 | 
	
		
			
				|  |  | -            video.Setup(moq => moq.GetInternalMetadataPath()).Returns(MetadataDirectoryPath);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
 | 
	
		
			
				|  |  | -            directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
 | 
	
		
			
				|  |  | -                .Returns(files);
 | 
	
		
			
				|  |  | -            directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Metadata"), It.IsAny<bool>(), It.IsAny<bool>()))
 | 
	
		
			
				|  |  | -                .Returns(metadataFiles);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            var streams = await _subtitleResolver.GetExternalStreamsAsync(video.Object, startIndex, directoryService.Object, false, CancellationToken.None);
 | 
	
		
			
				|  |  | +                MediaStreams = new List<MediaStream>
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    new()
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            Assert.Equal(expectedResult.Length, streams.Count);
 | 
	
		
			
				|  |  | -            for (var i = 0; i < expectedResult.Length; i++)
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | -                var expected = expectedResult[i];
 | 
	
		
			
				|  |  | -                var actual = streams[i];
 | 
	
		
			
				|  |  | +        _subtitleResolver = new SubtitleResolver(localizationManager, mediaEncoder.Object, new NamingOptions());
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.Index, actual.Index);
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.Type, actual.Type);
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.IsExternal, actual.IsExternal);
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.Path, actual.Path);
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.IsDefault, actual.IsDefault);
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.IsForced, actual.IsForced);
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.Language, actual.Language);
 | 
	
		
			
				|  |  | -                Assert.Equal(expected.Title, actual.Title);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | +    [Theory]
 | 
	
		
			
				|  |  | +    [InlineData("My.Video.srt", false, true)]
 | 
	
		
			
				|  |  | +    [InlineData("My.Video.mp3", false, false)]
 | 
	
		
			
				|  |  | +    [InlineData("My.Video.srt", true, true)]
 | 
	
		
			
				|  |  | +    [InlineData("My.Video.mp3", true, false)]
 | 
	
		
			
				|  |  | +    public async void GetExternalStreams_MixedFilenames_PicksSubtitles(string file, bool metadataDirectory, bool matches)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        [Theory]
 | 
	
		
			
				|  |  | -        [InlineData("MyVideo.en.srt", "srt", "eng", null, false, false)]
 | 
	
		
			
				|  |  | -        [InlineData("MyVideo.en.forced.default.srt", "srt", "eng", null, true, true)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.srt", "srt", null, null, false, false)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.foreign.srt", "srt", null, null, true, false)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.default.srt", "srt", null, null, false, true)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.forced.default.srt", "srt", null, null, true, true)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.en.srt", "srt", "eng", null, false, false)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.fr.en.srt", "srt", "eng", "fr", false, false)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.en.fr.srt", "srt", "fre", "en", false, false)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.default.en.srt", "srt", "eng", null, false, true)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.default.forced.en.srt", "srt", "eng", null, true, true)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.en.default.forced.srt", "srt", "eng", null, true, true)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.Track Label.srt", "srt", null, "Track Label", false, false)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.Track.Label.srt", "srt", null, "Track.Label", false, false)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.Track Label.en.default.forced.srt", "srt", "eng", "Track Label", true, true)]
 | 
	
		
			
				|  |  | -        [InlineData("My.Video.en.default.forced.Track Label.srt", "srt", "eng", "Track Label", true, true)]
 | 
	
		
			
				|  |  | -        public async void AddExternalStreamsAsync_GivenSingleFile_ReturnsExpectedSubtitle(string file, string codec, string? language, string? title, bool isForced, bool isDefault)
 | 
	
		
			
				|  |  | +        var video = new Movie
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
 | 
	
		
			
				|  |  | +            Path = MediaInfoResolverTests.VideoDirectoryPath + "/My.Video.mkv"
 | 
	
		
			
				|  |  | +        };
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            var video = new Mock<Video>();
 | 
	
		
			
				|  |  | -            video.CallBase = true;
 | 
	
		
			
				|  |  | -            video.Setup(moq => moq.Path).Returns(VideoDirectoryPath + "/My.Video.mkv");
 | 
	
		
			
				|  |  | -            video.Setup(moq => moq.GetInternalMetadataPath()).Returns(MetadataDirectoryPath);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
 | 
	
		
			
				|  |  | -            directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
 | 
	
		
			
				|  |  | -                .Returns(new[] { VideoDirectoryPath + "/" + file });
 | 
	
		
			
				|  |  | -            directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Metadata"), It.IsAny<bool>(), It.IsAny<bool>()))
 | 
	
		
			
				|  |  | -                .Returns(Array.Empty<string>());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            var streams = await _subtitleResolver.GetExternalStreamsAsync(video.Object, 0, directoryService.Object, false, CancellationToken.None);
 | 
	
		
			
				|  |  | +        var directoryService = MediaInfoResolverTests.GetDirectoryServiceForExternalFile(file, metadataDirectory);
 | 
	
		
			
				|  |  | +        var streams = await _subtitleResolver.GetExternalStreamsAsync(video, 0, directoryService, false, CancellationToken.None);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        if (matches)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  |              Assert.Single(streams);
 | 
	
		
			
				|  |  |              var actual = streams[0];
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            var expected = CreateMediaStream(VideoDirectoryPath + "/" + file, codec, language, title, 0, isForced, isDefault);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.Index, actual.Index);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.Type, actual.Type);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.IsExternal, actual.IsExternal);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.Path, actual.Path);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.IsDefault, actual.IsDefault);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.IsForced, actual.IsForced);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.Language, actual.Language);
 | 
	
		
			
				|  |  | -            Assert.Equal(expected.Title, actual.Title);
 | 
	
		
			
				|  |  | +            Assert.Equal(MediaStreamType.Subtitle, actual.Type);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        private static MediaStream CreateMediaStream(string path, string codec, string? language, string? title, int index, bool isForced = false, bool isDefault = false)
 | 
	
		
			
				|  |  | +        else
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            return new()
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | -                Index = index,
 | 
	
		
			
				|  |  | -                Codec = codec,
 | 
	
		
			
				|  |  | -                Type = MediaStreamType.Subtitle,
 | 
	
		
			
				|  |  | -                IsExternal = true,
 | 
	
		
			
				|  |  | -                Path = path,
 | 
	
		
			
				|  |  | -                IsDefault = isDefault,
 | 
	
		
			
				|  |  | -                IsForced = isForced,
 | 
	
		
			
				|  |  | -                Language = language,
 | 
	
		
			
				|  |  | -                Title = title
 | 
	
		
			
				|  |  | -            };
 | 
	
		
			
				|  |  | +            Assert.Empty(streams);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |