aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Library/SeasonResolverTests.cs
blob: 133a3f7d471cc7f83984b07f6927b11b28fd6598 (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
using Emby.Naming.Common;
using Emby.Server.Implementations.Library.Resolvers.TV;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;

namespace Jellyfin.Server.Implementations.Tests.Library
{
    public class SeasonResolverTests
    {
        private static readonly NamingOptions _namingOptions = new();
        private readonly SeasonResolver _resolver;

        public SeasonResolverTests()
        {
            var localizationMock = new Mock<ILocalizationManager>();
            localizationMock
                .Setup(l => l.GetLocalizedString(It.IsAny<string>()))
                .Returns("Season {0}");

            _resolver = new SeasonResolver(
                _namingOptions,
                localizationMock.Object,
                Mock.Of<ILogger<SeasonResolver>>());
        }

        [Theory]
        [InlineData("/media/Show/Season 01 [tvdbid=12345]", MetadataProvider.Tvdb, "12345")]
        [InlineData("/media/Show/Season 01 [tvdbid-12345]", MetadataProvider.Tvdb, "12345")]
        [InlineData("/media/Show/Season 01 (tvdbid=12345)", MetadataProvider.Tvdb, "12345")]
        [InlineData("/media/Show/Season 02 [tvmazeid=67890]", MetadataProvider.TvMaze, "67890")]
        [InlineData("/media/Show/Season 02 [tvmazeid-67890]", MetadataProvider.TvMaze, "67890")]
        [InlineData("/media/Show/Season 03 [tmdbid=99999]", MetadataProvider.Tmdb, "99999")]
        [InlineData("/media/Show/Season 03 [tmdbid-99999]", MetadataProvider.Tmdb, "99999")]
        public void Resolve_SeasonFolderWithProviderId_SetsProviderId(string path, MetadataProvider provider, string expectedId)
        {
            var series = new Series { Path = "/media/Show" };

            var args = new MediaBrowser.Controller.Library.ItemResolveArgs(
                Mock.Of<IServerApplicationPaths>(),
                null)
            {
                Parent = series,
                LibraryOptions = new LibraryOptions(),
                FileInfo = new FileSystemMetadata
                {
                    FullName = path,
                    IsDirectory = true
                }
            };

            var season = _resolver.Resolve(args);

            Assert.NotNull(season);
            Assert.True(season.TryGetProviderId(provider, out var actualId));
            Assert.Equal(expectedId, actualId);
        }

        [Fact]
        public void Resolve_SeasonFolderWithMultipleProviderIds_SetsAll()
        {
            var series = new Series { Path = "/media/Show" };

            var args = new MediaBrowser.Controller.Library.ItemResolveArgs(
                Mock.Of<IServerApplicationPaths>(),
                null)
            {
                Parent = series,
                LibraryOptions = new LibraryOptions(),
                FileInfo = new FileSystemMetadata
                {
                    FullName = "/media/Show/Season 01 [tvdbid=12345][tmdbid=99999]",
                    IsDirectory = true
                }
            };

            var season = _resolver.Resolve(args);

            Assert.NotNull(season);
            Assert.True(season.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId));
            Assert.Equal("12345", tvdbId);
            Assert.True(season.TryGetProviderId(MetadataProvider.Tmdb, out var tmdbId));
            Assert.Equal("99999", tmdbId);
        }

        [Fact]
        public void Resolve_SeasonFolderWithSeriesProviderIdInParentPath_DoesNotInheritSeriesId()
        {
            // Series folder has tvdbid=11111, season folder has tvdbid=22222.
            // The season should only pick up its own ID, not the series-level one.
            var series = new Series { Path = "/media/Show [tvdbid=11111]" };

            var args = new MediaBrowser.Controller.Library.ItemResolveArgs(
                Mock.Of<IServerApplicationPaths>(),
                null)
            {
                Parent = series,
                LibraryOptions = new LibraryOptions(),
                FileInfo = new FileSystemMetadata
                {
                    FullName = "/media/Show [tvdbid=11111]/Season 01 [tvdbid=22222]",
                    IsDirectory = true
                }
            };

            var season = _resolver.Resolve(args);

            Assert.NotNull(season);
            Assert.True(season.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId));
            Assert.Equal("22222", tvdbId);
        }

        [Fact]
        public void Resolve_SeasonFolderWithNoProviderId_HasNoProviderIds()
        {
            var series = new Series { Path = "/media/Show" };

            var args = new MediaBrowser.Controller.Library.ItemResolveArgs(
                Mock.Of<IServerApplicationPaths>(),
                null)
            {
                Parent = series,
                LibraryOptions = new LibraryOptions(),
                FileInfo = new FileSystemMetadata
                {
                    FullName = "/media/Show/Season 01",
                    IsDirectory = true
                }
            };

            var season = _resolver.Resolve(args);

            Assert.NotNull(season);
            Assert.False(season.TryGetProviderId(MetadataProvider.Tvdb, out _));
            Assert.False(season.TryGetProviderId(MetadataProvider.TvMaze, out _));
            Assert.False(season.TryGetProviderId(MetadataProvider.Tmdb, out _));
        }
    }
}