AudioBookListResolverTests.cs 8.0 KB

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