AudioBookListResolverTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Linq;
  3. using Emby.Naming.AudioBook;
  4. using Emby.Naming.Common;
  5. using MediaBrowser.Model.IO;
  6. using Xunit;
  7. namespace Jellyfin.Naming.Tests.AudioBook
  8. {
  9. public class AudioBookListResolverTests
  10. {
  11. private readonly NamingOptions _namingOptions = new NamingOptions();
  12. [Fact]
  13. public void TestStackAndExtras()
  14. {
  15. // No stacking here because there is no part/disc/etc
  16. var files = new[]
  17. {
  18. "Harry Potter and the Deathly Hallows/Part 1.mp3",
  19. "Harry Potter and the Deathly Hallows/Part 2.mp3",
  20. "Harry Potter and the Deathly Hallows/Extra.mp3",
  21. "Batman/Chapter 1.mp3",
  22. "Batman/Chapter 2.mp3",
  23. "Batman/Chapter 3.mp3",
  24. "Badman/audiobook.mp3",
  25. "Badman/extra.mp3",
  26. "Superman (2020)/Part 1.mp3",
  27. "Superman (2020)/extra.mp3",
  28. "Ready Player One (2020)/audiobook.mp3",
  29. "Ready Player One (2020)/extra.mp3",
  30. ".mp3"
  31. };
  32. var resolver = GetResolver();
  33. var result = resolver.Resolve(files.Select(i => new FileSystemMetadata
  34. {
  35. IsDirectory = false,
  36. FullName = i
  37. })).ToList();
  38. Assert.Equal(5, result.Count);
  39. Assert.Equal(2, result[0].Files.Count);
  40. Assert.Single(result[0].Extras);
  41. Assert.Equal("Harry Potter and the Deathly Hallows", result[0].Name);
  42. Assert.Equal(3, result[1].Files.Count);
  43. Assert.Empty(result[1].Extras);
  44. Assert.Equal("Batman", result[1].Name);
  45. Assert.Single(result[2].Files);
  46. Assert.Single(result[2].Extras);
  47. Assert.Equal("Badman", result[2].Name);
  48. Assert.Single(result[3].Files);
  49. Assert.Single(result[3].Extras);
  50. Assert.Equal("Superman", result[3].Name);
  51. Assert.Single(result[4].Files);
  52. Assert.Single(result[4].Extras);
  53. Assert.Equal("Ready Player One", result[4].Name);
  54. }
  55. [Fact]
  56. public void TestAlternativeVersions()
  57. {
  58. var files = new[]
  59. {
  60. "Harry Potter and the Deathly Hallows/Chapter 1.ogg",
  61. "Harry Potter and the Deathly Hallows/Chapter 1.mp3",
  62. "Deadpool.mp3",
  63. "Deadpool [HQ].mp3",
  64. "Superman/audiobook.mp3",
  65. "Superman/Superman.mp3",
  66. "Superman/Superman [HQ].mp3",
  67. "Superman/extra.mp3",
  68. "Batman/ Chapter 1 .mp3",
  69. "Batman/Chapter 1[loss-less].mp3"
  70. };
  71. var resolver = GetResolver();
  72. var result = resolver.Resolve(files.Select(i => new FileSystemMetadata
  73. {
  74. IsDirectory = false,
  75. FullName = i
  76. })).ToList();
  77. Assert.Equal(5, result.Count);
  78. // HP - Same name so we don't care which file is alternative
  79. Assert.Single(result[0].AlternateVersions);
  80. // DP
  81. Assert.Empty(result[1].AlternateVersions);
  82. // DP HQ (directory missing so we do not group deadpools together)
  83. Assert.Empty(result[2].AlternateVersions);
  84. // Superman
  85. // Priority:
  86. // 1. Name
  87. // 2. audiobook
  88. // 3. Names with modifiers
  89. Assert.Equal(2, result[3].AlternateVersions.Count);
  90. var paths = result[3].AlternateVersions.Select(x => x.Path).ToList();
  91. Assert.Contains("Superman/audiobook.mp3", paths);
  92. Assert.Contains("Superman/Superman [HQ].mp3", paths);
  93. // Batman
  94. Assert.Single(result[4].AlternateVersions);
  95. }
  96. [Fact]
  97. public void TestNameYearExtraction()
  98. {
  99. var data = new[]
  100. {
  101. new NameYearPath
  102. {
  103. Name = "Harry Potter and the Deathly Hallows",
  104. Path = "Harry Potter and the Deathly Hallows (2007)/Chapter 1.ogg",
  105. Year = 2007
  106. },
  107. new NameYearPath
  108. {
  109. Name = "Batman",
  110. Path = "Batman (2020).ogg",
  111. Year = 2020
  112. },
  113. new NameYearPath
  114. {
  115. Name = "Batman",
  116. Path = "Batman( 2021 ).mp3",
  117. Year = 2021
  118. },
  119. new NameYearPath
  120. {
  121. Name = "Batman(*2021*)",
  122. Path = "Batman(*2021*).mp3",
  123. Year = null
  124. },
  125. new NameYearPath
  126. {
  127. Name = "Batman",
  128. Path = "Batman.mp3",
  129. Year = null
  130. },
  131. new NameYearPath
  132. {
  133. Name = "+ Batman .",
  134. Path = " + Batman . .mp3",
  135. Year = null
  136. },
  137. new NameYearPath
  138. {
  139. Name = " ",
  140. Path = " .mp3",
  141. Year = null
  142. }
  143. };
  144. var resolver = GetResolver();
  145. var result = resolver.Resolve(data.Select(i => new FileSystemMetadata
  146. {
  147. IsDirectory = false,
  148. FullName = i.Path
  149. })).ToList();
  150. Assert.Equal(data.Length, result.Count);
  151. for (int i = 0; i < data.Length; i++)
  152. {
  153. Assert.Equal(data[i].Name, result[i].Name);
  154. Assert.Equal(data[i].Year, result[i].Year);
  155. }
  156. }
  157. [Fact]
  158. public void TestWithMetadata()
  159. {
  160. var files = new[]
  161. {
  162. "Harry Potter and the Deathly Hallows/Chapter 1.ogg",
  163. "Harry Potter and the Deathly Hallows/Harry Potter and the Deathly Hallows.nfo"
  164. };
  165. var resolver = GetResolver();
  166. var result = resolver.Resolve(files.Select(i => new FileSystemMetadata
  167. {
  168. IsDirectory = false,
  169. FullName = i
  170. }));
  171. Assert.Single(result);
  172. }
  173. [Fact]
  174. public void TestWithExtra()
  175. {
  176. var files = new[]
  177. {
  178. "Harry Potter and the Deathly Hallows/Chapter 1.mp3",
  179. "Harry Potter and the Deathly Hallows/Harry Potter and the Deathly Hallows trailer.mp3"
  180. };
  181. var resolver = GetResolver();
  182. var result = resolver.Resolve(files.Select(i => new FileSystemMetadata
  183. {
  184. IsDirectory = false,
  185. FullName = i
  186. })).ToList();
  187. Assert.Single(result);
  188. }
  189. [Fact]
  190. public void TestWithoutFolder()
  191. {
  192. var files = new[]
  193. {
  194. "Harry Potter and the Deathly Hallows trailer.mp3"
  195. };
  196. var resolver = GetResolver();
  197. var result = resolver.Resolve(files.Select(i => new FileSystemMetadata
  198. {
  199. IsDirectory = false,
  200. FullName = i
  201. })).ToList();
  202. Assert.Single(result);
  203. }
  204. [Fact]
  205. public void TestEmpty()
  206. {
  207. var files = Array.Empty<string>();
  208. var resolver = GetResolver();
  209. var result = resolver.Resolve(files.Select(i => new FileSystemMetadata
  210. {
  211. IsDirectory = false,
  212. FullName = i
  213. })).ToList();
  214. Assert.Empty(result);
  215. }
  216. private AudioBookListResolver GetResolver()
  217. {
  218. return new AudioBookListResolver(_namingOptions);
  219. }
  220. internal struct NameYearPath
  221. {
  222. public string Name;
  223. public string Path;
  224. public int? Year;
  225. }
  226. }
  227. }