FindExtrasTests.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using AutoFixture;
  5. using AutoFixture.AutoMoq;
  6. using Emby.Naming.Common;
  7. using Emby.Server.Implementations.Library.Resolvers;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Entities.Movies;
  11. using MediaBrowser.Controller.Entities.TV;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.Resolvers;
  14. using MediaBrowser.Controller.Sorting;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.IO;
  17. using Moq;
  18. using Xunit;
  19. namespace Jellyfin.Server.Implementations.Tests.Library.LibraryManager;
  20. public class FindExtrasTests
  21. {
  22. private readonly Emby.Server.Implementations.Library.LibraryManager _libraryManager;
  23. public FindExtrasTests()
  24. {
  25. var fixture = new Fixture().Customize(new AutoMoqCustomization());
  26. fixture.Register(() => new NamingOptions());
  27. var configMock = fixture.Freeze<Mock<IServerConfigurationManager>>();
  28. configMock.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns("/data");
  29. var fileSystemMock = fixture.Freeze<Mock<IFileSystem>>();
  30. fileSystemMock.Setup(f => f.GetFileInfo(It.IsAny<string>())).Returns<string>(path => new FileSystemMetadata { FullName = path });
  31. _libraryManager = fixture.Build<Emby.Server.Implementations.Library.LibraryManager>().Do(s => s.AddParts(
  32. fixture.Create<IEnumerable<IResolverIgnoreRule>>(),
  33. new List<IItemResolver> { new GenericVideoResolver<Video>(fixture.Create<NamingOptions>()) },
  34. fixture.Create<IEnumerable<IIntroProvider>>(),
  35. fixture.Create<IEnumerable<IBaseItemComparer>>(),
  36. fixture.Create<IEnumerable<ILibraryPostScanTask>>()))
  37. .Create();
  38. // This is pretty terrible but unavoidable
  39. BaseItem.FileSystem ??= fixture.Create<IFileSystem>();
  40. BaseItem.MediaSourceManager ??= fixture.Create<IMediaSourceManager>();
  41. }
  42. [Fact]
  43. public void FindExtras_SeparateMovieFolder_FindsCorrectExtras()
  44. {
  45. var owner = new Movie { Name = "Up", Path = "/movies/Up/Up.mkv" };
  46. var paths = new List<string>
  47. {
  48. "/movies/Up/Up.mkv",
  49. "/movies/Up/Up - trailer.mkv",
  50. "/movies/Up/Up - sample.mkv",
  51. "/movies/Up/Up something else.mkv"
  52. };
  53. var files = paths.Select(p => new FileSystemMetadata
  54. {
  55. FullName = p,
  56. IsDirectory = false
  57. }).ToList();
  58. var extras = _libraryManager.FindExtras(owner, files).OrderBy(e => e.ExtraType).ToList();
  59. Assert.Equal(2, extras.Count);
  60. Assert.Equal(ExtraType.Trailer, extras[0].ExtraType);
  61. Assert.Equal(ExtraType.Sample, extras[1].ExtraType);
  62. }
  63. [Fact]
  64. public void FindExtras_SeparateMovieFolderWithMixedExtras_FindsCorrectExtras()
  65. {
  66. var owner = new Movie { Name = "Up", Path = "/movies/Up/Up.mkv" };
  67. var paths = new List<string>
  68. {
  69. "/movies/Up/Up.mkv",
  70. "/movies/Up/Up - trailer.mkv",
  71. "/movies/Up/trailers/some trailer.mkv",
  72. "/movies/Up/behind the scenes/the making of Up.mkv",
  73. "/movies/Up/behind the scenes.mkv",
  74. "/movies/Up/Up - sample.mkv",
  75. "/movies/Up/Up something else.mkv"
  76. };
  77. var files = paths.Select(p => new FileSystemMetadata
  78. {
  79. FullName = p,
  80. IsDirectory = false
  81. }).ToList();
  82. var extras = _libraryManager.FindExtras(owner, files).OrderBy(e => e.ExtraType).ToList();
  83. Assert.Equal(4, extras.Count);
  84. Assert.Equal(ExtraType.Trailer, extras[0].ExtraType);
  85. Assert.Equal(ExtraType.Trailer, extras[1].ExtraType);
  86. Assert.Equal(ExtraType.BehindTheScenes, extras[2].ExtraType);
  87. Assert.Equal(ExtraType.Sample, extras[3].ExtraType);
  88. }
  89. [Fact]
  90. public void FindExtras_SeparateMovieFolderWithMixedExtras_FindsOnlyExtrasInMovieFolder()
  91. {
  92. var owner = new Movie { Name = "Up", Path = "/movies/Up/Up.mkv" };
  93. var paths = new List<string>
  94. {
  95. "/movies/Up/Up.mkv",
  96. "/movies/Up/trailer.mkv",
  97. "/movies/Another Movie/trailer.mkv"
  98. };
  99. var files = paths.Select(p => new FileSystemMetadata
  100. {
  101. FullName = p,
  102. IsDirectory = false
  103. }).ToList();
  104. var extras = _libraryManager.FindExtras(owner, files).OrderBy(e => e.ExtraType).ToList();
  105. Assert.Single(extras);
  106. Assert.Equal(ExtraType.Trailer, extras[0].ExtraType);
  107. Assert.Equal("trailer", extras[0].FileNameWithoutExtension);
  108. Assert.Equal("/movies/Up/trailer.mkv", extras[0].Path);
  109. }
  110. [Fact]
  111. public void FindExtras_SeparateMovieFolderWithParts_FindsCorrectExtras()
  112. {
  113. var owner = new Movie { Name = "Up", Path = "/movies/Up/Up - part1.mkv" };
  114. var paths = new List<string>
  115. {
  116. "/movies/Up/Up - part1.mkv",
  117. "/movies/Up/Up - part2.mkv",
  118. "/movies/Up/trailer.mkv",
  119. "/movies/Another Movie/trailer.mkv"
  120. };
  121. var files = paths.Select(p => new FileSystemMetadata
  122. {
  123. FullName = p,
  124. IsDirectory = false
  125. }).ToList();
  126. var extras = _libraryManager.FindExtras(owner, files).OrderBy(e => e.ExtraType).ToList();
  127. Assert.Single(extras);
  128. Assert.Equal(ExtraType.Trailer, extras[0].ExtraType);
  129. Assert.Equal("trailer", extras[0].FileNameWithoutExtension);
  130. Assert.Equal("/movies/Up/trailer.mkv", extras[0].Path);
  131. }
  132. [Fact]
  133. public void FindExtras_SeriesWithTrailers_FindsCorrectExtras()
  134. {
  135. var owner = new Series { Name = "Dexter", Path = "/series/Dexter" };
  136. var paths = new List<string>
  137. {
  138. "/series/Dexter/Season 1/S01E01.mkv",
  139. "/series/Dexter/trailer.mkv",
  140. "/series/Dexter/trailers/trailer2.mkv",
  141. };
  142. var files = paths.Select(p => new FileSystemMetadata
  143. {
  144. FullName = p,
  145. IsDirectory = string.IsNullOrEmpty(Path.GetExtension(p))
  146. }).ToList();
  147. var extras = _libraryManager.FindExtras(owner, files).OrderBy(e => e.ExtraType).ToList();
  148. Assert.Equal(2, extras.Count);
  149. Assert.Equal(ExtraType.Trailer, extras[0].ExtraType);
  150. Assert.Equal("trailer", extras[0].FileNameWithoutExtension);
  151. Assert.Equal("/series/Dexter/trailer.mkv", extras[0].Path);
  152. Assert.Equal("/series/Dexter/trailers/trailer2.mkv", extras[1].Path);
  153. }
  154. }