aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Library/MovieResolverTests.cs
blob: 3aac3cdc43cd8db724db1bfc5f89cc1c9326c284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System.Collections.Generic;
using Emby.Naming.Common;
using Emby.Naming.Video;
using Emby.Server.Implementations.Library.Resolvers.Movies;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;

namespace Jellyfin.Server.Implementations.Tests.Library;

public class MovieResolverTests
{
    private static readonly NamingOptions _namingOptions = new();
    private static readonly VideoListResolver _videoListResolver = new(_namingOptions);

    [Fact]
    public void Resolve_GivenLocalAlternateVersion_ResolvesToVideo()
    {
        var movieResolver = new MovieResolver(Mock.Of<IImageProcessor>(), Mock.Of<ILogger<MovieResolver>>(), _namingOptions, Mock.Of<IDirectoryService>(), _videoListResolver);
        var itemResolveArgs = new ItemResolveArgs(
            Mock.Of<IServerApplicationPaths>(),
            null)
        {
            Parent = null,
            FileInfo = new FileSystemMetadata
            {
                FullName = "/movies/Black Panther (2018)/Black Panther (2018) - 1080p 3D.mk3d"
            }
        };

        Assert.NotNull(movieResolver.Resolve(itemResolveArgs));
    }

    [Fact]
    public void ResolveMultiple_GivenTvShowsCollection_CreatesEpisodeItems()
    {
        // For a tvshows collection, the multi-version grouping must still produce
        // Episode BaseItems (not generic Video) so downstream metadata fetching
        // and series-aware logic apply.
        var movieResolver = new MovieResolver(Mock.Of<IImageProcessor>(), Mock.Of<ILogger<MovieResolver>>(), _namingOptions, Mock.Of<IDirectoryService>(), _videoListResolver);

        var parent = new Folder { Path = "/TV/Show/Season 1" };
        var files = new List<FileSystemMetadata>
        {
            new() { FullName = "/TV/Show/Season 1/Show - S01E01 - 1080p.mkv", Name = "Show - S01E01 - 1080p.mkv", IsDirectory = false },
            new() { FullName = "/TV/Show/Season 1/Show - S01E01 - 720p.mkv", Name = "Show - S01E01 - 720p.mkv", IsDirectory = false },
            new() { FullName = "/TV/Show/Season 1/Show - S01E02.mkv", Name = "Show - S01E02.mkv", IsDirectory = false }
        };

        var result = movieResolver.ResolveMultiple(parent, files, CollectionType.tvshows, Mock.Of<IDirectoryService>());

        Assert.NotNull(result);
        Assert.Equal(2, result.Items.Count);
        Assert.All(result.Items, item => Assert.IsType<Episode>(item));

        // The S01E01 item should have one alternate version
        var s01e01 = result.Items.Find(i => i.Path.Contains("S01E01", System.StringComparison.Ordinal));
        Assert.NotNull(s01e01);
        Assert.Single(((Video)s01e01).LocalAlternateVersions);
    }

    [Fact]
    public void ResolveMultiple_GivenMoviesCollection_CreatesMovieItems()
    {
        // For a movies collection, the multi-version grouping must produce Movie
        // BaseItems (not generic Video) so downstream movie-specific logic applies.
        var movieResolver = new MovieResolver(Mock.Of<IImageProcessor>(), Mock.Of<ILogger<MovieResolver>>(), _namingOptions, Mock.Of<IDirectoryService>(), _videoListResolver);

        var parent = new Folder { Path = "/movies/Inception (2010)" };
        var files = new List<FileSystemMetadata>
        {
            new() { FullName = "/movies/Inception (2010)/Inception (2010) - 1080p.mkv", Name = "Inception (2010) - 1080p.mkv", IsDirectory = false },
            new() { FullName = "/movies/Inception (2010)/Inception (2010) - 720p.mkv", Name = "Inception (2010) - 720p.mkv", IsDirectory = false }
        };

        var result = movieResolver.ResolveMultiple(parent, files, CollectionType.movies, Mock.Of<IDirectoryService>());

        Assert.NotNull(result);
        Assert.Single(result.Items);
        Assert.All(result.Items, item => Assert.IsType<Movie>(item));
        Assert.Single(((Video)result.Items[0]).LocalAlternateVersions);
    }

    [Fact]
    public void AllExtrasTypesFolderNames_ContainsSampleSingularAndPlural()
    {
        Assert.True(_namingOptions.AllExtrasTypesFolderNames.ContainsKey("sample"));
        Assert.True(_namingOptions.AllExtrasTypesFolderNames.ContainsKey("Sample"));
        Assert.True(_namingOptions.AllExtrasTypesFolderNames.ContainsKey("samples"));
    }

    [Theory]
    [InlineData("Sample")]
    [InlineData("sample")]
    [InlineData("SAMPLE")]
    [InlineData("samples")]
    public void ResolvePath_MovieFolderWithSampleSubfolder_ResolvesToMovie(string sampleDirName)
    {
        var libraryManager = new Mock<ILibraryManager>();
        libraryManager.Setup(m => m.GetLibraryOptions(It.IsAny<BaseItem>())).Returns(new LibraryOptions());
        libraryManager.Setup(m => m.IgnoreFile(It.IsAny<FileSystemMetadata>(), It.IsAny<BaseItem>())).Returns(false);

        var resolver = new MovieResolver(Mock.Of<IImageProcessor>(), Mock.Of<ILogger<MovieResolver>>(), _namingOptions, Mock.Of<IDirectoryService>(), _videoListResolver);
        var args = new ItemResolveArgs(
            Mock.Of<IServerApplicationPaths>(),
            libraryManager.Object)
        {
            Parent = new Folder(),
            CollectionType = CollectionType.movies,
            FileInfo = new FileSystemMetadata
            {
                FullName = "/media/Outer Colony (2026)",
                IsDirectory = true
            },
            FileSystemChildren = new[]
            {
                new FileSystemMetadata
                {
                    FullName = "/media/Outer Colony (2026)/Outer Colony (2026).mkv",
                    Name = "Outer Colony (2026).mkv"
                },
                new FileSystemMetadata
                {
                    FullName = "/media/Outer Colony (2026)/" + sampleDirName,
                    Name = sampleDirName,
                    IsDirectory = true
                }
            }
        };

        Assert.IsType<Movie>(resolver.ResolvePath(args));
    }

    [Fact]
    public void ResolvePath_MovieFolderWithRealSubfolder_DoesNotResolveToSingleMovie()
    {
        var libraryManager = new Mock<ILibraryManager>();
        libraryManager.Setup(m => m.GetLibraryOptions(It.IsAny<BaseItem>())).Returns(new LibraryOptions());
        libraryManager.Setup(m => m.IgnoreFile(It.IsAny<FileSystemMetadata>(), It.IsAny<BaseItem>())).Returns(false);

        var resolver = new MovieResolver(Mock.Of<IImageProcessor>(), Mock.Of<ILogger<MovieResolver>>(), _namingOptions, Mock.Of<IDirectoryService>(), _videoListResolver);
        var args = new ItemResolveArgs(
            Mock.Of<IServerApplicationPaths>(),
            libraryManager.Object)
        {
            Parent = new Folder(),
            CollectionType = CollectionType.movies,
            FileInfo = new FileSystemMetadata
            {
                FullName = "/media/Outer Colony (2026)",
                IsDirectory = true
            },
            FileSystemChildren = new[]
            {
                new FileSystemMetadata
                {
                    FullName = "/media/Outer Colony (2026)/Outer Colony (2026).mkv",
                    Name = "Outer Colony (2026).mkv"
                },
                new FileSystemMetadata
                {
                    FullName = "/media/Outer Colony (2026)/Feature",
                    Name = "Feature",
                    IsDirectory = true
                }
            }
        };

        Assert.Null(resolver.ResolvePath(args));
    }
}