aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Jellyfin.Api.Tests/Helpers/HlsHelpersTests.cs111
-rw-r--r--tests/Jellyfin.Api.Tests/ModelBinders/CommaDelimitedCollectionModelBinderTests.cs108
-rw-r--r--tests/Jellyfin.Controller.Tests/MediaEncoding/HlsPrerollTests.cs89
-rw-r--r--tests/Jellyfin.LiveTv.Tests/Guide/GuideManagerTests.cs95
-rw-r--r--tests/Jellyfin.LiveTv.Tests/Listings/ListingsManagerTests.cs44
-rw-r--r--tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectLineupTests.cs130
-rw-r--r--tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectTests.cs915
-rw-r--r--tests/Jellyfin.LiveTv.Tests/Recordings/RecordingsMetadataManagerTests.cs49
-rw-r--r--tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleCacheMetadataTests.cs128
-rw-r--r--tests/Jellyfin.Model.Tests/Dlna/StreamBuilderTests.cs33
-rw-r--r--tests/Jellyfin.Naming.Tests/ExternalFiles/ExternalPathParserTests.cs19
-rw-r--r--tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs40
-rw-r--r--tests/Jellyfin.Providers.Tests/Tmdb/TmdbUtilsTests.cs16
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryAncestorFilterTests.cs174
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryStreamFilterTests.cs18
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/ItemPersistenceOwnedRowTests.cs56
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/PeopleRepositoryParentFilterTests.cs131
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs72
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/RecordedCommand.cs8
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Item/SqliteDbTestFixture.cs4
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/DotIgnoreIgnoreRuleTest.cs101
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs93
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManagerConvertImageTests.cs50
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/MediaSourceManagerTests.cs25
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs15
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/QueryFiltersTests.cs534
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/SqlSearchProviderTests.cs56
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs75
-rw-r--r--tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs248
-rw-r--r--tests/Jellyfin.Server.Integration.Tests/Controllers/FilterControllerTests.cs35
-rw-r--r--tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs55
-rw-r--r--tests/Jellyfin.Server.Tests/Migrations/FixLibrarySubtitleDownloadLanguagesTests.cs85
-rw-r--r--tests/Jellyfin.Server.Tests/Migrations/MigrateRatingLevelsTests.cs311
33 files changed, 3787 insertions, 136 deletions
diff --git a/tests/Jellyfin.Api.Tests/Helpers/HlsHelpersTests.cs b/tests/Jellyfin.Api.Tests/Helpers/HlsHelpersTests.cs
new file mode 100644
index 0000000000..e25a1a853a
--- /dev/null
+++ b/tests/Jellyfin.Api.Tests/Helpers/HlsHelpersTests.cs
@@ -0,0 +1,111 @@
+using System;
+using System.ComponentModel;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Threading.Tasks;
+using Jellyfin.Api.Helpers;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.MediaEncoding;
+using MediaBrowser.Controller.Streaming;
+using MediaBrowser.Model.Dto;
+using Microsoft.Extensions.Logging.Abstractions;
+using Microsoft.Win32.SafeHandles;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.Api.Tests.Helpers;
+
+public sealed class HlsHelpersTests : IDisposable
+{
+ private const string PlaylistText = "#EXTM3U\n#EXTINF:3.0,Café\nplaylist0.ts\n#EXTINF:3.0,\nplaylist1.ts\n";
+ private readonly DirectoryInfo _directory = Directory.CreateTempSubdirectory();
+
+ private string PlaylistPath => Path.Combine(_directory.FullName, "playlist.m3u8");
+
+ [Fact]
+ public void GetLivePlaylistText_PreservesText()
+ {
+ File.WriteAllText(PlaylistPath, PlaylistText);
+ using var state = CreateState("ts");
+
+ Assert.Equal(PlaylistText, HlsHelpers.GetLivePlaylistText(PlaylistPath, state));
+ }
+
+ [Fact]
+ public void GetLivePlaylistText_ReadsWhileReplacementHandleIsOpen_Windows()
+ {
+ Assert.SkipUnless(OperatingSystem.IsWindows(), "Requires Windows file sharing semantics and CreateFileW.");
+ File.WriteAllText(PlaylistPath, "#EXTM3U\n");
+ var temporaryPath = PlaylistPath + ".tmp";
+ File.WriteAllText(temporaryPath, PlaylistText);
+ using var replacementHandle = OpenDeleteHandle(temporaryPath);
+ File.Move(temporaryPath, PlaylistPath, true);
+ using var state = CreateState("ts");
+
+ Assert.Equal(PlaylistText, HlsHelpers.GetLivePlaylistText(PlaylistPath, state));
+ }
+
+ [Fact]
+ public void GetLivePlaylistText_DoesNotShareWithInPlaceWriter_Windows()
+ {
+ Assert.SkipUnless(OperatingSystem.IsWindows(), "Requires Windows file sharing semantics.");
+ File.WriteAllText(PlaylistPath, PlaylistText);
+ using var writer = new FileStream(PlaylistPath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete);
+ using var state = CreateState("ts");
+
+ Assert.Throws<IOException>(() => HlsHelpers.GetLivePlaylistText(PlaylistPath, state));
+ }
+
+ [Fact]
+ public async Task WaitForMinimumSegmentCount_ReadsWhileReplacementHandleIsOpen_Windows()
+ {
+ Assert.SkipUnless(OperatingSystem.IsWindows(), "Requires Windows file sharing semantics and CreateFileW.");
+ await File.WriteAllTextAsync(PlaylistPath, "#EXTM3U\n", TestContext.Current.CancellationToken);
+ var temporaryPath = PlaylistPath + ".tmp";
+ await File.WriteAllTextAsync(temporaryPath, PlaylistText, TestContext.Current.CancellationToken);
+ using var replacementHandle = OpenDeleteHandle(temporaryPath);
+ File.Move(temporaryPath, PlaylistPath, true);
+ using var cancellation = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken);
+ cancellation.CancelAfter(TimeSpan.FromSeconds(10));
+
+ // The DELETE-access handle stays open for the entire wait; no overlap timing is required.
+ await HlsHelpers.WaitForMinimumSegmentCount(PlaylistPath, 2, NullLogger.Instance, cancellation.Token);
+ Assert.False(cancellation.IsCancellationRequested);
+ }
+
+ public void Dispose()
+ {
+ _directory.Delete(true);
+ }
+
+ private static StreamState CreateState(string container)
+ {
+ return new StreamState(Mock.Of<IMediaSourceManager>(), TranscodingJobType.Hls, Mock.Of<ITranscodeManager>())
+ {
+ Request = new StreamingRequestDto { SegmentContainer = container },
+ MediaSource = new MediaSourceInfo()
+ };
+ }
+
+ private static SafeFileHandle OpenDeleteHandle(string path)
+ {
+ // DELETE access without FILE_FLAG_DELETE_ON_CLOSE models FFmpeg's retained rename handle.
+ const uint DeleteAccess = 0x00010000;
+ const uint OpenExisting = 3;
+ var handle = CreateFile(path, DeleteAccess, FileShare.ReadWrite | FileShare.Delete, IntPtr.Zero, OpenExisting, 0, IntPtr.Zero);
+ Assert.False(handle.IsInvalid, new Win32Exception(Marshal.GetLastPInvokeError()).Message);
+ return handle;
+ }
+
+ [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
+ [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
+ private static extern SafeFileHandle CreateFile(
+ string fileName,
+ uint desiredAccess,
+ FileShare shareMode,
+ IntPtr securityAttributes,
+ uint creationDisposition,
+ uint flagsAndAttributes,
+ IntPtr templateFile);
+}
diff --git a/tests/Jellyfin.Api.Tests/ModelBinders/CommaDelimitedCollectionModelBinderTests.cs b/tests/Jellyfin.Api.Tests/ModelBinders/CommaDelimitedCollectionModelBinderTests.cs
index e6b9acfe19..6514523cf4 100644
--- a/tests/Jellyfin.Api.Tests/ModelBinders/CommaDelimitedCollectionModelBinderTests.cs
+++ b/tests/Jellyfin.Api.Tests/ModelBinders/CommaDelimitedCollectionModelBinderTests.cs
@@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Jellyfin.Api.ModelBinders;
+using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
+using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Primitives;
using Moq;
@@ -226,5 +228,111 @@ namespace Jellyfin.Api.Tests.ModelBinders
Assert.NotNull(listResult);
Assert.Single(listResult);
}
+
+ [Fact]
+ public async Task BindModelAsync_ItemFieldsQuery_SkipsLegacyValues()
+ {
+ var bindingContextMock = CreateBindingContext(
+ "fields",
+ typeof(ItemFields[]),
+ new StringValues("PremiereDate,Overview,UserData,Genres,RunTimeTicks"));
+ var modelBinder = new CommaDelimitedCollectionModelBinder(new NullLogger<CommaDelimitedCollectionModelBinder>());
+
+ await modelBinder.BindModelAsync(bindingContextMock.Object);
+
+ Assert.Equal(
+ new[] { ItemFields.Overview, ItemFields.Genres },
+ (ItemFields[]?)bindingContextMock.Object.Result.Model);
+ }
+
+ [Fact]
+ public async Task BindModelAsync_ItemFieldsQuery_ReturnsEmptyForOnlyLegacyValues()
+ {
+ var bindingContextMock = CreateBindingContext(
+ "fields",
+ typeof(ItemFields[]),
+ new StringValues("PremiereDate,UserData,RunTimeTicks"));
+ var modelBinder = new CommaDelimitedCollectionModelBinder(new NullLogger<CommaDelimitedCollectionModelBinder>());
+
+ await modelBinder.BindModelAsync(bindingContextMock.Object);
+
+ Assert.Empty((ItemFields[]?)bindingContextMock.Object.Result.Model ?? Array.Empty<ItemFields>());
+ }
+
+ [Fact]
+ public async Task BindModelAsync_ItemFieldsQuery_BindsCaseInsensitiveValues()
+ {
+ var bindingContextMock = CreateBindingContext("fields", typeof(ItemFields[]), new StringValues("overview,GENRES"));
+ var modelBinder = new CommaDelimitedCollectionModelBinder(new NullLogger<CommaDelimitedCollectionModelBinder>());
+
+ await modelBinder.BindModelAsync(bindingContextMock.Object);
+
+ Assert.Equal(
+ new[] { ItemFields.Overview, ItemFields.Genres },
+ (ItemFields[]?)bindingContextMock.Object.Result.Model);
+ }
+
+ [Fact]
+ public async Task BindModelAsync_ItemFieldsQuery_BindsRepeatedQueryParameters()
+ {
+ var bindingContextMock = CreateBindingContext(
+ "fields",
+ typeof(ItemFields[]),
+ new StringValues(new[] { "Overview", "Genres" }));
+ var modelBinder = new CommaDelimitedCollectionModelBinder(new NullLogger<CommaDelimitedCollectionModelBinder>());
+
+ await modelBinder.BindModelAsync(bindingContextMock.Object);
+
+ Assert.Equal(
+ new[] { ItemFields.Overview, ItemFields.Genres },
+ (ItemFields[]?)bindingContextMock.Object.Result.Model);
+ }
+
+ [Fact]
+ public async Task BindModelAsync_EnumArrayQuery_PreservesNumericValues()
+ {
+ var bindingContextMock = CreateBindingContext("test", typeof(TestType[]), new StringValues("0,1"));
+ var modelBinder = new CommaDelimitedCollectionModelBinder(new NullLogger<CommaDelimitedCollectionModelBinder>());
+
+ await modelBinder.BindModelAsync(bindingContextMock.Object);
+
+ Assert.Equal(new[] { TestType.How, TestType.Much }, (TestType[]?)bindingContextMock.Object.Result.Model);
+ }
+
+ [Fact]
+ public async Task BindModelAsync_InvalidEnumValues_DoesNotLogExceptions()
+ {
+ var bindingContextMock = CreateBindingContext(
+ "fields",
+ typeof(ItemFields[]),
+ new StringValues("PremiereDate,UserData,RunTimeTicks"));
+ var loggerMock = new Mock<ILogger<CommaDelimitedCollectionModelBinder>>();
+ var modelBinder = new CommaDelimitedCollectionModelBinder(loggerMock.Object);
+
+ await modelBinder.BindModelAsync(bindingContextMock.Object);
+
+ loggerMock.Verify(
+ logger => logger.Log(
+ It.IsAny<LogLevel>(),
+ It.IsAny<EventId>(),
+ It.IsAny<It.IsAnyType>(),
+ It.IsAny<Exception?>(),
+ It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
+ Times.Never);
+ }
+
+ private static Mock<ModelBindingContext> CreateBindingContext(string modelName, Type modelType, StringValues values)
+ {
+ var valueProvider = new QueryStringValueProvider(
+ new BindingSource(string.Empty, string.Empty, false, false),
+ new QueryCollection(new Dictionary<string, StringValues> { { modelName, values } }),
+ CultureInfo.InvariantCulture);
+ var bindingContextMock = new Mock<ModelBindingContext>();
+ bindingContextMock.Setup(b => b.ValueProvider).Returns(valueProvider);
+ bindingContextMock.Setup(b => b.ModelName).Returns(modelName);
+ bindingContextMock.Setup(b => b.ModelType).Returns(modelType);
+ bindingContextMock.SetupProperty(b => b.Result);
+ return bindingContextMock;
+ }
}
}
diff --git a/tests/Jellyfin.Controller.Tests/MediaEncoding/HlsPrerollTests.cs b/tests/Jellyfin.Controller.Tests/MediaEncoding/HlsPrerollTests.cs
new file mode 100644
index 0000000000..10453a29ff
--- /dev/null
+++ b/tests/Jellyfin.Controller.Tests/MediaEncoding/HlsPrerollTests.cs
@@ -0,0 +1,89 @@
+using System;
+using System.Globalization;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Controller.MediaEncoding;
+using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Entities;
+using Microsoft.Extensions.Configuration;
+using Moq;
+using Xunit;
+using IConfigurationManager = MediaBrowser.Common.Configuration.IConfigurationManager;
+
+namespace Jellyfin.Controller.Tests.MediaEncoding;
+
+public class HlsPrerollTests
+{
+ [Theory]
+ [InlineData(TranscodingJobType.Hls, "copy", "aac", "mp4", "mkv", true)]
+ [InlineData(TranscodingJobType.Hls, "copy", "aac", "ts", "mkv", true)]
+ [InlineData(TranscodingJobType.Hls, "copy", "opus", "mp4", "mkv", true)]
+ [InlineData(TranscodingJobType.Hls, "copy", "mp3", "ts", "mkv", true)]
+ [InlineData(TranscodingJobType.Hls, "copy", "copy", "mp4", "mkv", false)]
+ [InlineData(TranscodingJobType.Hls, "libx264", "copy", "mp4", "mkv", false)]
+ [InlineData(TranscodingJobType.Hls, "libx264", "aac", "mp4", "mkv", false)]
+ [InlineData(TranscodingJobType.Progressive, "copy", "aac", "mp4", "mkv", false)]
+ [InlineData(TranscodingJobType.Hls, "copy", "aac", "mp4", "wtv", false)]
+ public void GetFastSeekCommandLineParameter_RetainsAudioPreroll(
+ TranscodingJobType type,
+ string video,
+ string audio,
+ string container,
+ string input,
+ bool retainsPreroll)
+ {
+ var state = CreateState(type);
+ state.OutputVideoCodec = video;
+ state.OutputAudioCodec = audio;
+ state.InputContainer = input;
+ state.BaseRequest.StartTimeTicks = 312_810_000L;
+ var args = CreateHelper().GetFastSeekCommandLineParameter(state, new EncodingOptions(), container);
+ Assert.Equal(retainsPreroll, args.Contains("-noaccurate_seek", StringComparison.Ordinal));
+ Assert.StartsWith(type == TranscodingJobType.Hls && video == "copy" ? "-ss 00:00:31.781" : "-ss 00:00:31.281", args, StringComparison.Ordinal);
+ }
+
+ [Theory]
+ [InlineData(false, "aac", 312_810_000L)]
+ [InlineData(true, "", 312_810_000L)]
+ [InlineData(true, "aac", 0L)]
+ public void GetFastSeekCommandLineParameter_WithoutAudioOrSeek_OmitsNoAccurateSeek(bool hasAudio, string audioCodec, long seekTicks)
+ {
+ var state = CreateState();
+ if (!hasAudio)
+ {
+ state.AudioStream = null;
+ }
+
+ state.OutputAudioCodec = audioCodec;
+ state.BaseRequest.StartTimeTicks = seekTicks;
+ var args = CreateHelper().GetFastSeekCommandLineParameter(state, new EncodingOptions(), "mp4");
+ Assert.DoesNotContain("noaccurate_seek", args, StringComparison.Ordinal);
+ }
+
+ private static EncodingHelper CreateHelper()
+ {
+ var encoder = new Mock<IMediaEncoder>();
+ encoder.Setup(e => e.GetTimeParameter(It.IsAny<long>()))
+ .Returns((long ticks) => TimeSpan.FromTicks(ticks).ToString(@"hh\:mm\:ss\.fff", CultureInfo.InvariantCulture));
+ encoder.SetupGet(e => e.EncoderVersion).Returns(new Version(8, 1, 3));
+ return new EncodingHelper(
+ Mock.Of<IApplicationPaths>(),
+ encoder.Object,
+ Mock.Of<ISubtitleEncoder>(),
+ Mock.Of<IConfiguration>(),
+ Mock.Of<IConfigurationManager>(),
+ Mock.Of<IPathManager>());
+ }
+
+ private static EncodingJobInfo CreateState(TranscodingJobType type = TranscodingJobType.Hls) => new(type)
+ {
+ IsVideoRequest = true,
+ OutputVideoCodec = "copy",
+ OutputAudioCodec = "aac",
+ InputContainer = "mkv",
+ RunTimeTicks = TimeSpan.FromSeconds(45).Ticks,
+ VideoStream = new MediaStream { Codec = "h264", Type = MediaStreamType.Video, NalLengthSize = "4" },
+ AudioStream = new MediaStream { Codec = "flac", Type = MediaStreamType.Audio, Channels = 1 },
+ BaseRequest = new BaseEncodingJobOptions()
+ };
+}
diff --git a/tests/Jellyfin.LiveTv.Tests/Guide/GuideManagerTests.cs b/tests/Jellyfin.LiveTv.Tests/Guide/GuideManagerTests.cs
new file mode 100644
index 0000000000..a09768df86
--- /dev/null
+++ b/tests/Jellyfin.LiveTv.Tests/Guide/GuideManagerTests.cs
@@ -0,0 +1,95 @@
+using System;
+using System.Net;
+using System.Threading.Tasks;
+using Jellyfin.LiveTv.Guide;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.LiveTv;
+using MediaBrowser.Controller.Persistence;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.IO;
+using Microsoft.Extensions.Logging.Abstractions;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.LiveTv.Tests.Guide;
+
+public sealed class GuideManagerTests : IDisposable
+{
+ private const string ImageUrl = "https://json.schedulesdirect.org/20141201/image/assets/p1_b.jpg?token=abc";
+ private const string ImageNotFoundBody = "{\"response\":\"IMAGE_NOT_FOUND\",\"code\":5000,\"message\":\"test\"}";
+
+ private readonly ILibraryManager? _previousLibraryManager = BaseItem.LibraryManager;
+ private readonly Mock<ILibraryManager> _libraryManager = new();
+ private readonly Mock<ISchedulesDirectService> _schedulesDirect = new();
+
+ public GuideManagerTests()
+ {
+ // UpdateToRepositoryAsync goes through the static.
+ BaseItem.LibraryManager = _libraryManager.Object;
+ }
+
+ public void Dispose()
+ {
+ BaseItem.LibraryManager = _previousLibraryManager!;
+ }
+
+ [Fact]
+ public async Task PreCacheImages_ImageNotFound_ReportsTheBodyAndRemovesTheImage()
+ {
+ var program = CreateProgram();
+ _schedulesDirect.Setup(s => s.CanDownloadImage(ImageUrl)).Returns(true);
+ _schedulesDirect.Setup(s => s.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ImageNotFoundBody))
+ .Returns(ImageDownloadFailureAction.RemoveImage);
+
+ // ConvertImageToLocal wraps the rejection, which is the only place the SD error body survives.
+ _libraryManager.Setup(l => l.ConvertImageToLocal(program, It.IsAny<ItemImageInfo>(), 0, false))
+ .ThrowsAsync(new InvalidOperationException(
+ "test",
+ new HttpResponseBodyException("test", HttpStatusCode.NotFound, ImageNotFoundBody)));
+
+ await CreateGuideManager().PreCacheImages([program], DateTime.UtcNow.AddDays(GuideManager.MaxCacheDays));
+
+ _schedulesDirect.Verify(s => s.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ImageNotFoundBody), Times.Once);
+ Assert.Empty(program.ImageInfos);
+ }
+
+ [Fact]
+ public async Task PreCacheImages_ImageBlocked_RemovesItWithoutDownloading()
+ {
+ // A blocked url left on the program would be fetched from SD when a client displays it.
+ var program = CreateProgram();
+ _schedulesDirect.Setup(s => s.CanDownloadImage(ImageUrl)).Returns(false);
+
+ await CreateGuideManager().PreCacheImages([program], DateTime.UtcNow.AddDays(GuideManager.MaxCacheDays));
+
+ _libraryManager.Verify(
+ l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), It.IsAny<int>(), It.IsAny<bool>()),
+ Times.Never);
+ Assert.Empty(program.ImageInfos);
+ }
+
+ private static LiveTvProgram CreateProgram()
+ => new()
+ {
+ Id = Guid.NewGuid(),
+ Name = "Test",
+ EndDate = DateTime.UtcNow.AddHours(1),
+ ImageInfos = [new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }]
+ };
+
+ private GuideManager CreateGuideManager()
+ => new(
+ NullLogger<GuideManager>.Instance,
+ Mock.Of<IConfigurationManager>(),
+ Mock.Of<IFileSystem>(),
+ Mock.Of<IItemRepository>(),
+ _libraryManager.Object,
+ Mock.Of<ILiveTvManager>(),
+ Mock.Of<ITunerHostManager>(),
+ Mock.Of<IRecordingsManager>(),
+ _schedulesDirect.Object,
+ null!);
+}
diff --git a/tests/Jellyfin.LiveTv.Tests/Listings/ListingsManagerTests.cs b/tests/Jellyfin.LiveTv.Tests/Listings/ListingsManagerTests.cs
index 40934d9c6b..ebeacf9115 100644
--- a/tests/Jellyfin.LiveTv.Tests/Listings/ListingsManagerTests.cs
+++ b/tests/Jellyfin.LiveTv.Tests/Listings/ListingsManagerTests.cs
@@ -1,4 +1,8 @@
using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
using Jellyfin.LiveTv.Configuration;
using Jellyfin.LiveTv.Listings;
using MediaBrowser.Common.Configuration;
@@ -47,4 +51,44 @@ public class ListingsManagerTests
_config.GetLiveTvConfiguration().ListingProviders,
p => p.Id.Equals(id, StringComparison.Ordinal));
}
+
+ [Fact]
+ public async Task GetProgramsAsync_FirstProviderCannotListChannels_FallsBackToTheNext()
+ {
+ // Arrange
+ var failing = new Mock<IListingsProvider>();
+ failing.SetupGet(x => x.Type).Returns("failing");
+ failing.Setup(x => x.GetChannels(It.IsAny<ListingsProviderInfo>(), It.IsAny<CancellationToken>()))
+ .ThrowsAsync(new InvalidOperationException("offline"));
+
+ var working = new Mock<IListingsProvider>();
+ working.SetupGet(x => x.Type).Returns("working");
+ working.Setup(x => x.GetChannels(It.IsAny<ListingsProviderInfo>(), It.IsAny<CancellationToken>()))
+ .ReturnsAsync(new List<ChannelInfo> { new() { Id = "5", Number = "5" } });
+ working.Setup(x => x.GetProgramsAsync(It.IsAny<ListingsProviderInfo>(), "5", It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
+ .ReturnsAsync(new[] { new ProgramInfo { Id = "p1" } });
+
+ Mock.Get(_config)
+ .Setup(x => x.GetConfiguration(It.IsAny<string>()))
+ .Returns(new LiveTvOptions
+ {
+ ListingProviders =
+ [
+ new ListingsProviderInfo { Id = "a", Type = "failing" },
+ new ListingsProviderInfo { Id = "b", Type = "working" }
+ ]
+ });
+
+ var manager = new ListingsManager(_logger, _config, _taskManager, _tunerHostManager, [failing.Object, working.Object]);
+
+ // Act
+ var programs = await manager.GetProgramsAsync(
+ new ChannelInfo { Id = "5", Number = "5", TunerHostId = "tuner" },
+ DateTime.UtcNow,
+ DateTime.UtcNow.AddDays(1),
+ CancellationToken.None);
+
+ // Assert
+ Assert.Single(programs);
+ }
}
diff --git a/tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectLineupTests.cs b/tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectLineupTests.cs
deleted file mode 100644
index 04c7b05c3d..0000000000
--- a/tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectLineupTests.cs
+++ /dev/null
@@ -1,130 +0,0 @@
-using System;
-using System.IO;
-using System.Net;
-using System.Net.Http;
-using System.Threading;
-using System.Threading.Tasks;
-using MediaBrowser.Common.Configuration;
-using MediaBrowser.Model.LiveTv;
-using Microsoft.Extensions.Logging.Abstractions;
-using Moq;
-using Moq.Protected;
-using Xunit;
-using SchedulesDirectProvider = Jellyfin.LiveTv.Listings.SchedulesDirect;
-
-namespace Jellyfin.LiveTv.Tests.Listings;
-
-public class SchedulesDirectLineupTests
-{
- private const string InvalidUserResponse = "{\"response\":\"INVALID_USER\",\"code\":4003,\"message\":\"Invalid user.\",\"serverID\":\"AWS-SD-web.1\"}";
-
- private static readonly ListingsProviderInfo _info = new() { Username = "user", Password = "password" };
-
- [Fact]
- public async Task GetLineups_ValidCredentials_ReturnsLineups()
- {
- var tokenResponse = await CreateSuccessfulLogin();
- using var provider = CreateProvider(tokenResponse, await GetHeadendsResponse());
-
- var lineups = await provider.GetLineups(_info, "USA", "90210");
-
- Assert.NotEmpty(lineups);
- Assert.Contains(lineups, i => string.Equals(i.Id, "USA-OTA-90210", StringComparison.Ordinal));
- Assert.Contains(lineups, i => string.Equals(i.Name, "Antenna", StringComparison.Ordinal));
- }
-
- [Fact]
- public async Task GetLineups_LoginFails_Throws()
- {
- using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
-
- // An empty lineup list is indistinguishable from "no lineups for this location", so a
- // failed login has to surface as an error instead.
- await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
- }
-
- [Fact]
- public async Task Validate_LoginFails_Throws()
- {
- using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
-
- await Assert.ThrowsAnyAsync<Exception>(() => provider.Validate(_info, true, false));
- }
-
- [Fact]
- public async Task Validate_AfterAccountError_RecoversWithoutRestart()
- {
- var login = CreateFailedLogin();
- using var provider = CreateProvider(login, await GetHeadendsResponse());
-
- await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
-
- // The account error disables Schedules Direct; saving the provider again is the user
- // correcting their credentials, and that has to recover without a server restart.
- login.Status = HttpStatusCode.OK;
- login.Body = await GetTokenResponse();
-
- await provider.Validate(_info, true, false);
-
- Assert.NotEmpty(await provider.GetLineups(_info, "USA", "90210"));
- }
-
- private static async Task<Response> CreateSuccessfulLogin()
- => new() { Status = HttpStatusCode.OK, Body = await GetTokenResponse() };
-
- private static Response CreateFailedLogin()
- => new() { Status = HttpStatusCode.BadRequest, Body = InvalidUserResponse };
-
- private static Task<string> GetTokenResponse()
- => File.ReadAllTextAsync("Test Data/SchedulesDirect/token_live_response.json", TestContext.Current.CancellationToken);
-
- private static Task<string> GetHeadendsResponse()
- => File.ReadAllTextAsync("Test Data/SchedulesDirect/headends_response.json", TestContext.Current.CancellationToken);
-
- private static SchedulesDirectProvider CreateProvider(Response login, string headendsResponse)
- {
- var messageHandler = new Mock<HttpMessageHandler>();
- messageHandler.Protected()
- .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
- .Returns<HttpRequestMessage, CancellationToken>((m, _) =>
- {
- var path = m.RequestUri!.AbsolutePath;
- if (path.EndsWith("/token", StringComparison.Ordinal))
- {
- return Task.FromResult(new HttpResponseMessage(login.Status)
- {
- Content = new StringContent(login.Body)
- });
- }
-
- if (path.EndsWith("/headends", StringComparison.Ordinal))
- {
- return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
- {
- Content = new StringContent(headendsResponse)
- });
- }
-
- return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound));
- });
-
- var httpClientFactory = new Mock<IHttpClientFactory>();
- httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
- .Returns(() => new HttpClient(messageHandler.Object));
-
- var appPaths = new Mock<IApplicationPaths>();
- appPaths.SetupGet(x => x.CachePath).Returns(Path.GetTempPath());
-
- return new SchedulesDirectProvider(
- NullLogger<SchedulesDirectProvider>.Instance,
- httpClientFactory.Object,
- appPaths.Object);
- }
-
- private sealed class Response
- {
- public HttpStatusCode Status { get; set; }
-
- public string Body { get; set; } = string.Empty;
- }
-}
diff --git a/tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectTests.cs b/tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectTests.cs
new file mode 100644
index 0000000000..51d855cf8a
--- /dev/null
+++ b/tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectTests.cs
@@ -0,0 +1,915 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Text.Json;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.LiveTv;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.LiveTv;
+using Microsoft.Extensions.Logging.Abstractions;
+using Moq;
+using Moq.Protected;
+using Xunit;
+using SchedulesDirectProvider = Jellyfin.LiveTv.Listings.SchedulesDirect;
+
+namespace Jellyfin.LiveTv.Tests.Listings;
+
+public class SchedulesDirectTests : IDisposable
+{
+ private const string InvalidUserResponse = "{\"response\":\"INVALID_USER\",\"code\":4003,\"message\":\"Invalid user.\",\"serverID\":\"AWS-SD-web.1\"}";
+ private const string ImageUrl = "https://json.schedulesdirect.org/20141201/image/assets/p1_b.jpg?token=abc";
+
+ private static readonly ListingsProviderInfo _info = new() { Username = "user", Password = "password" };
+
+ private readonly Mock<ILibraryManager> _libraryManager = new();
+
+ // The daily limit files live under CachePath, so every provider needs its own directory or
+ // one test leaves a limit behind for the next.
+ private readonly string _cachePath = Path.Combine(Path.GetTempPath(), "jf-sd-" + Guid.NewGuid().ToString("N"));
+
+ [Fact]
+ public async Task GetLineups_ValidCredentials_ReturnsLineups()
+ {
+ var tokenResponse = await CreateSuccessfulLogin();
+ using var provider = CreateProvider(tokenResponse, await GetHeadendsResponse());
+
+ var lineups = await provider.GetLineups(_info, "USA", "90210");
+
+ Assert.NotEmpty(lineups);
+ Assert.Contains(lineups, i => string.Equals(i.Id, "USA-OTA-90210", StringComparison.Ordinal));
+ Assert.Contains(lineups, i => string.Equals(i.Name, "Antenna", StringComparison.Ordinal));
+ }
+
+ [Fact]
+ public async Task GetLineups_LoginFails_Throws()
+ {
+ using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
+
+ // An empty lineup list is indistinguishable from "no lineups for this location", so a
+ // failed login has to surface as an error instead.
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
+ }
+
+ [Fact]
+ public async Task Validate_LoginFails_Throws()
+ {
+ using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
+
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.Validate(_info, true, false));
+ }
+
+ [Fact]
+ public async Task Validate_AfterAccountError_RecoversWithoutRestart()
+ {
+ var login = CreateFailedLogin();
+ using var provider = CreateProvider(login, await GetHeadendsResponse());
+
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
+
+ // The account error disables Schedules Direct; saving the provider again is the user
+ // correcting their credentials, and that has to recover without a server restart.
+ login.Status = HttpStatusCode.OK;
+ login.Body = await GetTokenResponse();
+
+ await provider.Validate(_info, true, false);
+
+ Assert.NotEmpty(await provider.GetLineups(_info, "USA", "90210"));
+ }
+
+ [Fact]
+ public async Task GetLineups_TokenRejectedWithHttp200_DisablesService()
+ {
+ // The account lockout this guards against arrived as HTTP 200 carrying code 4005.
+ var login = new Response
+ {
+ Status = HttpStatusCode.OK,
+ Body = "{\"response\":\"JSON_ACCOUNT_ACCESS_DISABLED\",\"code\":4005,\"serverID\":\"20141201.web\",\"message\":\"Access to account via JSON has been disabled.\"}"
+ };
+ using var provider = CreateProvider(login, await GetHeadendsResponse());
+
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
+
+ // The account error disables Schedules Direct: a retry must not reach the network again.
+ login.Body = await GetTokenResponse();
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
+ }
+
+ [Theory]
+ [InlineData(5002, "MAX_IMAGE_DOWNLOADS")]
+ [InlineData(5003, "MAX_IMAGE_DOWNLOADS_TRIAL")]
+ public async Task ReportImageDownloadFailure_ImageLimitCode_BlocksOnFirstReport(int code, string response)
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(code, response));
+
+ Assert.Equal(ImageDownloadFailureAction.None, action);
+ Assert.False(provider.CanDownloadImage(ImageUrl));
+ }
+
+ [Fact]
+ public async Task ReportImageDownloadFailure_ImageNotFound_DropsUrlWithoutBlockingTheDay()
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND"));
+
+ // The spec requires that url is never requested again, but the day is not blocked and
+ // every other image stays downloadable.
+ Assert.Equal(ImageDownloadFailureAction.RemoveImage, action);
+ Assert.False(provider.CanDownloadImage(ImageUrl));
+ Assert.True(provider.CanDownloadImage("https://json.schedulesdirect.org/20141201/image/assets/p2_b.jpg?token=abc"));
+ }
+
+ [Fact]
+ public async Task ReportImageDownloadFailure_InvalidUriLimit_BlocksAndDropsUrl()
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5004, "MAX_IMAGE_INVALID_URI_ERRORS"));
+
+ Assert.Equal(ImageDownloadFailureAction.RemoveImage, action);
+ Assert.False(provider.CanDownloadImage(ImageUrl));
+ }
+
+ [Theory]
+ [InlineData(1004, "TOKEN_MISSING")]
+ [InlineData(4006, "TOKEN_EXPIRED")]
+ public async Task ReportImageDownloadFailure_TokenCode_DoesNotBlockTheDay(int code, string response)
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, ErrorBody(code, response));
+
+ Assert.Equal(ImageDownloadFailureAction.RemoveImage, action);
+ Assert.True(provider.CanDownloadImage(ImageUrl));
+ }
+
+ [Fact]
+ public async Task ReportImageDownloadFailure_UnreadableBody_FallsBackToFailureStreak()
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ for (var i = 0; i < 9; i++)
+ {
+ provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, "<html>gateway error</html>");
+ Assert.True(provider.CanDownloadImage(ImageUrl));
+ }
+
+ provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, "<html>gateway error</html>");
+
+ Assert.False(provider.CanDownloadImage(ImageUrl));
+ }
+
+ [Fact]
+ public async Task ReportImageDownloadSuccess_ResetsUnexplainedFailureStreak()
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ for (var i = 0; i < 9; i++)
+ {
+ provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, null);
+ }
+
+ provider.ReportImageDownloadSuccess(ImageUrl);
+ provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, null);
+
+ Assert.True(provider.CanDownloadImage(ImageUrl));
+ }
+
+ [Fact]
+ public async Task ReportImageDownloadFailure_ForeignUrl_IsIgnored()
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ var action = provider.ReportImageDownloadFailure("https://example.com/image.jpg", HttpStatusCode.NotFound, ErrorBody(5002, "MAX_IMAGE_DOWNLOADS"));
+
+ Assert.Equal(ImageDownloadFailureAction.None, action);
+ Assert.True(provider.CanDownloadImage(ImageUrl));
+ Assert.True(provider.CanDownloadImage("https://example.com/image.jpg"));
+ }
+
+ [Fact]
+ public async Task ConvertImageToLocal_Blocked_DoesNotDownload()
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+ provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5002, "MAX_IMAGE_DOWNLOADS"));
+
+ var image = await provider.ConvertImageToLocal(new LiveTvProgram(), new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }, 0);
+
+ Assert.Null(image);
+ _libraryManager.Verify(
+ l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), It.IsAny<int>(), It.IsAny<bool>()),
+ Times.Never);
+ }
+
+ [Fact]
+ public async Task ConvertImageToLocal_ImageNotFound_StopsOfferingTheUri()
+ {
+ using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
+
+ // LibraryManager wraps the rejection, which is the only place the SD error body survives.
+ _libraryManager.Setup(l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), 0, true))
+ .ThrowsAsync(new InvalidOperationException(
+ "test",
+ new HttpResponseBodyException("test", HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND"))));
+
+ await Assert.ThrowsAsync<InvalidOperationException>(
+ () => provider.ConvertImageToLocal(new LiveTvProgram(), new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }, 0));
+
+ Assert.False(provider.CanDownloadImage(ImageUrl));
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_UnchangedMd5_ServesSecondRefreshFromCache()
+ {
+ var server = new FakeServer();
+ using var provider = CreateProvider(server);
+
+ var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+ Assert.NotEmpty(first);
+
+ var schedulesAfterFirst = server.Counts["/schedules"];
+ var programsAfterFirst = server.Counts.GetValueOrDefault("/programs");
+ Assert.True(schedulesAfterFirst > 0);
+ Assert.True(programsAfterFirst > 0);
+
+ var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ // The md5 is unchanged, so only the hash check should have gone to the server.
+ Assert.Equal(first.Count(), second.Count());
+ Assert.Equal(schedulesAfterFirst, server.Counts["/schedules"]);
+ Assert.Equal(programsAfterFirst, server.Counts.GetValueOrDefault("/programs"));
+ Assert.Equal(2, server.Counts["/schedules/md5"]);
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_ChangedMd5_DownloadsTheScheduleAgain()
+ {
+ var server = new FakeServer();
+ using var provider = CreateProvider(server);
+
+ await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+ var afterFirst = server.Counts["/schedules"];
+
+ server.Md5Salt = "changed";
+ await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.True(server.Counts["/schedules"] > afterFirst);
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_SystemOffline_ThrowsInsteadOfReportingNoPrograms()
+ {
+ var server = new FakeServer { SystemStatus = "Offline" };
+ using var provider = CreateProvider(server);
+
+ // An empty list means "this channel has no programs", and the guide refresh acts on that
+ // by deleting the ones it already has. A failure has to surface as one.
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+
+ Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_OfflineCodeWithHttp200_ThrowsInsteadOfReportingNoPrograms()
+ {
+ // A 200 carrying an error code used to deserialize into an empty status that read as online.
+ var server = new FakeServer { StatusCode = 3000 };
+ using var provider = CreateProvider(server);
+
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+
+ Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_ImageLimitHit_StillReturnsPrograms()
+ {
+ // The image limit must not be mistaken for a fetch failure: the listings are still good,
+ // only the artwork is unavailable.
+ var server = new FakeServer { HasImageArtwork = true };
+ using var provider = CreateProvider(server);
+
+ provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5002, "MAX_IMAGE_DOWNLOADS"));
+ Assert.False(provider.CanDownloadImage(ImageUrl));
+
+ var programs = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.NotEmpty(programs);
+ Assert.All(programs, p => Assert.Null(p.ImageUrl));
+ Assert.Equal(0, server.Counts.GetValueOrDefault("/metadata/programs"));
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_Md5SaysDateIsOutOfRange_DoesNotRequestTheSchedule()
+ {
+ // 7020 is what the hash check exists to catch: asking /schedules for the day anyway just
+ // earns the same error, which is why the spec points at the md5 endpoint for the range.
+ var server = new FakeServer { Md5ErrorCode = 7020 };
+ using var provider = CreateProvider(server);
+
+ var programs = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.Empty(programs);
+ Assert.Equal(1, server.Counts["/schedules/md5"]);
+ Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_ScheduleQueued_HonoursRetryTime()
+ {
+ // The queued response is a bare object rather than the usual array, and it carries the
+ // earliest time the schedule may be asked for again.
+ var server = new FakeServer { ScheduleRetryTime = DateTime.UtcNow.AddMinutes(10) };
+ using var provider = CreateProvider(server);
+
+ // A queued schedule is a failure, not an empty channel: reporting no programs would have
+ // the guide refresh delete the ones this channel already has.
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+ var afterFirst = server.Counts["/schedules"];
+
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+
+ Assert.Equal(afterFirst, server.Counts["/schedules"]);
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_ScheduleQueuedRetryTimePassed_AsksAgain()
+ {
+ var server = new FakeServer { ScheduleRetryTime = DateTime.UtcNow.AddSeconds(-1) };
+ using var provider = CreateProvider(server);
+
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+ var afterFirst = server.Counts["/schedules"];
+
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+
+ Assert.True(server.Counts["/schedules"] > afterFirst);
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_CacheHit_StillRefreshesArtwork()
+ {
+ // Artwork uris are ephemeral and their changes are not reflected in the schedule md5, so
+ // the spec forbids storing them; the index has to be requested again on a cache hit.
+ var server = new FakeServer { HasImageArtwork = true };
+ using var provider = CreateProvider(server);
+
+ var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+ Assert.All(first, p => Assert.Contains("p1_b.jpg", p.ImageUrl, StringComparison.Ordinal));
+
+ var metadataAfterFirst = server.Counts["/metadata/programs"];
+
+ var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.Equal(1, server.Counts["/schedules"]);
+ Assert.True(server.Counts["/metadata/programs"] > metadataAfterFirst);
+ Assert.All(second, p => Assert.Contains("p1_b.jpg", p.ImageUrl, StringComparison.Ordinal));
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_AfterImageNotFound_StopsOfferingTheUri()
+ {
+ var server = new FakeServer { HasImageArtwork = true };
+ using var provider = CreateProvider(server);
+
+ var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+ var imageUrl = first.First().ImageUrl;
+ Assert.NotNull(imageUrl);
+
+ Assert.Equal(
+ ImageDownloadFailureAction.RemoveImage,
+ provider.ReportImageDownloadFailure(imageUrl, HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND")));
+
+ // Handing the same dead uri back every refresh is what walks an account into
+ // MAX_IMAGE_INVALID_URI_ERRORS, so it must not survive the rejection.
+ var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.All(second, p => Assert.Null(p.ImageUrl));
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_OneDayQueuedNextToCachedDays_ServesThePreviousCopy()
+ {
+ var server = new FakeServer();
+ using var provider = CreateProvider(server);
+
+ var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ // One day changed and is now queued; the others still match the cache. Leaving the queued
+ // day out would have the guide refresh delete its programs.
+ server.ChangedDate = server.RequestedDates[0];
+ server.ScheduleRetryTime = DateTime.UtcNow.AddMinutes(10);
+ var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.Equal(2, server.Counts["/schedules"]);
+ Assert.Equal(first.Select(p => p.Id).Order(), second.Select(p => p.Id).Order());
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_ProgramQueued_DoesNotCacheTheIncompleteDay()
+ {
+ var server = new FakeServer { ProgramErrorCode = 6001 };
+ using var provider = CreateProvider(server);
+
+ // With no previous copy to stand in, a queued program fails the channel rather than
+ // dropping its airings, which the guide refresh would delete.
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+
+ // The program finishing generation need not change the day's md5, so the day must be
+ // downloaded again rather than served from the cache without it.
+ server.ProgramErrorCode = 0;
+ var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.NotEmpty(second);
+ Assert.Equal(2, server.Counts["/programs"]);
+ }
+
+ [Theory]
+ [InlineData(null)]
+ [InlineData("EP000000020001")]
+ public async Task GetProgramsAsync_ProgramQueuedAfterGoodRefresh_KeepsItsAirings(string? queuedProgramId)
+ {
+ var server = new FakeServer { SecondProgram = true };
+ using var provider = CreateProvider(server);
+
+ var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+ Assert.Contains(first, p => p.Id!.Contains("EP000000020001", StringComparison.Ordinal));
+
+ // The day changed and one (or every) program on it is still being generated.
+ server.Md5Salt = "changed";
+ server.ProgramErrorCode = 6001;
+ server.QueuedProgramId = queuedProgramId;
+ var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.Equal(first.Select(p => p.Id).Order(), second.Select(p => p.Id).Order());
+ }
+
+ [Theory]
+ [InlineData(HttpStatusCode.OK, 3000, "SERVICE_OFFLINE")]
+ [InlineData(HttpStatusCode.Forbidden, 4009, "TOO_MANY_LOGINS")]
+ public async Task GetProgramsAsync_StopCodeOnMd5_DoesNotRequestSchedules(HttpStatusCode status, int code, string response)
+ {
+ var server = new FakeServer { Md5RejectionBody = ErrorBody(code, response), Md5RejectionStatus = status };
+ using var provider = CreateProvider(server);
+
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+
+ Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
+ }
+
+ [Theory]
+ [InlineData(1004, "TOKEN_MISSING")]
+ [InlineData(1010, "INVALID_TOKEN")]
+ public async Task GetProgramsAsync_TokenRejected_LogsInAgain(int code, string response)
+ {
+ var server = new FakeServer { Md5RejectionBody = ErrorBody(code, response) };
+ using var provider = CreateProvider(server);
+
+ var programs = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+
+ Assert.NotEmpty(programs);
+ Assert.Equal(2, server.Counts["/token"]);
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_ServiceOffline_DoesNotLogInAgainDuringTheOfflineHour()
+ {
+ var server = new FakeServer { StatusCode = 3000 };
+ using var provider = CreateProvider(server);
+
+ for (var i = 0; i < 3; i++)
+ {
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
+ }
+
+ // MAX_LOGIN_ATTEMPTS counts logins, so each channel of a refresh must not log in again.
+ Assert.Equal(1, server.Counts["/token"]);
+ }
+
+ [Fact]
+ public async Task GetProgramsAsync_AfterImageNotFoundAndRestart_StopsOfferingTheUri()
+ {
+ var server = new FakeServer { HasImageArtwork = true };
+
+ using (var provider = CreateProvider(server))
+ {
+ var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+ provider.ReportImageDownloadFailure(first.First().ImageUrl!, HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND"));
+ }
+
+ // A second provider over the same cache directory stands in for a restart.
+ using (var provider = CreateProvider(server))
+ {
+ var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
+ Assert.All(second, p => Assert.Null(p.ImageUrl));
+ }
+ }
+
+ [Theory]
+ [InlineData(HttpStatusCode.Forbidden, 4009, "TOO_MANY_LOGINS")]
+ [InlineData(HttpStatusCode.OK, 4009, "TOO_MANY_LOGINS")]
+ [InlineData(HttpStatusCode.Forbidden, 4010, "TOO_MANY_UNIQUE_IPS")]
+ public async Task GetLineups_AccountLimitReached_StopsLoggingIn(HttpStatusCode status, int code, string response)
+ {
+ var login = new Response
+ {
+ Status = status,
+ Body = ErrorBody(code, response)
+ };
+ using var provider = CreateProvider(login, await GetHeadendsResponse());
+
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
+
+ // These codes count logins, so continuing to ask for a token is what earned them: a
+ // retry must not go back to /token even once the credentials would work.
+ login.Status = HttpStatusCode.OK;
+ login.Body = await GetTokenResponse();
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
+ }
+
+ [Fact]
+ public async Task GetChannels_SystemOffline_ThrowsInsteadOfReportingNoChannels()
+ {
+ var server = new FakeServer { SystemStatus = "Offline" };
+ using var provider = CreateProvider(server);
+
+ // No channels means no channel is mapped, which the guide refresh reads as "no programs".
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.GetChannels(new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" }, TestContext.Current.CancellationToken));
+ }
+
+ [Fact]
+ public async Task GetChannels_LoginBackedOff_ThrowsInsteadOfReportingNoChannels()
+ {
+ var login = new Response { Status = HttpStatusCode.Forbidden, Body = ErrorBody(4009, "TOO_MANY_LOGINS") };
+ using var provider = CreateProvider(login, await GetHeadendsResponse());
+ var info = new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" };
+
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetChannels(info, TestContext.Current.CancellationToken));
+
+ // The second call never reaches /token, so this is the null-token path.
+ await Assert.ThrowsAnyAsync<Exception>(() => provider.GetChannels(info, TestContext.Current.CancellationToken));
+ }
+
+ [Fact]
+ public async Task GetChannels_UnchangedModifiedDate_ServesTheLineupFromCache()
+ {
+ var server = new FakeServer();
+ using var provider = CreateProvider(server);
+ var info = new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" };
+
+ var first = await provider.GetChannels(info, TestContext.Current.CancellationToken);
+ Assert.NotEmpty(first);
+ Assert.Equal(1, server.Counts["/lineups/USA-OTA-90210"]);
+
+ var second = await provider.GetChannels(info, TestContext.Current.CancellationToken);
+
+ Assert.Equal(first.Count, second.Count);
+ Assert.Equal(1, server.Counts["/lineups/USA-OTA-90210"]);
+ }
+
+ [Fact]
+ public async Task GetChannels_NewerModifiedDate_DownloadsTheLineupAgain()
+ {
+ var server = new FakeServer();
+ var info = new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" };
+
+ using (var provider = CreateProvider(server))
+ {
+ await provider.GetChannels(info, TestContext.Current.CancellationToken);
+ }
+
+ server.LineupModified = server.LineupModified.AddDays(1);
+
+ // A second provider over the same cache directory stands in for a restart, which is also
+ // what makes the status re-read rather than coming from its short-lived cache.
+ using (var provider = CreateProvider(server))
+ {
+ await provider.GetChannels(info, TestContext.Current.CancellationToken);
+ }
+
+ Assert.Equal(2, server.Counts["/lineups/USA-OTA-90210"]);
+ }
+
+ [Fact]
+ public async Task Validate_LineupCannotBeAdded_Throws()
+ {
+ var server = new FakeServer { LineupPutStatus = HttpStatusCode.BadRequest };
+ using var provider = CreateProvider(server);
+
+ // Silently reporting success leaves the user with a provider that has no lineup.
+ await Assert.ThrowsAnyAsync<Exception>(
+ () => provider.Validate(new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" }, true, true));
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing && Directory.Exists(_cachePath))
+ {
+ Directory.Delete(_cachePath, true);
+ }
+ }
+
+ // Schedules Direct answers an exhausted image quota with HTTP 200 and a body like this, which
+ // ProviderManager surfaces as a synthetic 404 because it is not an image content type.
+ private static string ErrorBody(int code, string response)
+ => string.Format(
+ CultureInfo.InvariantCulture,
+ "{{\"response\":\"{0}\",\"code\":{1},\"serverID\":\"20141201.debug\",\"message\":\"test\",\"datetime\":\"2026-09-14T00:00:00Z\"}}",
+ response,
+ code);
+
+ private static async Task<Response> CreateSuccessfulLogin()
+ => new() { Status = HttpStatusCode.OK, Body = await GetTokenResponse() };
+
+ private static Response CreateFailedLogin()
+ => new() { Status = HttpStatusCode.BadRequest, Body = InvalidUserResponse };
+
+ private static Task<string> GetTokenResponse()
+ => File.ReadAllTextAsync("Test Data/SchedulesDirect/token_live_response.json", TestContext.Current.CancellationToken);
+
+ private static Task<string> GetHeadendsResponse()
+ => File.ReadAllTextAsync("Test Data/SchedulesDirect/headends_response.json", TestContext.Current.CancellationToken);
+
+ private SchedulesDirectProvider CreateProvider(Response login, string headendsResponse)
+ {
+ var messageHandler = new Mock<HttpMessageHandler>();
+ messageHandler.Protected()
+ .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
+ .Returns<HttpRequestMessage, CancellationToken>((m, _) =>
+ {
+ var path = m.RequestUri!.AbsolutePath;
+ if (path.EndsWith("/token", StringComparison.Ordinal))
+ {
+ return Task.FromResult(new HttpResponseMessage(login.Status)
+ {
+ Content = new StringContent(login.Body)
+ });
+ }
+
+ if (path.EndsWith("/headends", StringComparison.Ordinal))
+ {
+ return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
+ {
+ Content = new StringContent(headendsResponse)
+ });
+ }
+
+ return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound));
+ });
+
+ var httpClientFactory = new Mock<IHttpClientFactory>();
+ httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
+ .Returns(() => new HttpClient(messageHandler.Object));
+
+ var appPaths = new Mock<IApplicationPaths>();
+ appPaths.SetupGet(x => x.CachePath).Returns(_cachePath);
+
+ return new SchedulesDirectProvider(
+ NullLogger<SchedulesDirectProvider>.Instance,
+ httpClientFactory.Object,
+ appPaths.Object,
+ _libraryManager.Object);
+ }
+
+ private SchedulesDirectProvider CreateProvider(FakeServer server)
+ {
+ var messageHandler = new Mock<HttpMessageHandler>();
+ messageHandler.Protected()
+ .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
+ .Returns<HttpRequestMessage, CancellationToken>((m, ct) => server.Respond(m, ct));
+
+ var httpClientFactory = new Mock<IHttpClientFactory>();
+ httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
+ .Returns(() => new HttpClient(messageHandler.Object));
+
+ var appPaths = new Mock<IApplicationPaths>();
+ appPaths.SetupGet(x => x.CachePath).Returns(_cachePath);
+
+ return new SchedulesDirectProvider(
+ NullLogger<SchedulesDirectProvider>.Instance,
+ httpClientFactory.Object,
+ appPaths.Object,
+ _libraryManager.Object);
+ }
+
+ /// <summary>
+ /// A minimal stand-in for the Schedules Direct API that counts what was asked of it.
+ /// </summary>
+ private sealed class FakeServer
+ {
+ public Dictionary<string, int> Counts { get; } = new(StringComparer.Ordinal);
+
+ public string SystemStatus { get; set; } = "Online";
+
+ public int StatusCode { get; set; }
+
+ public string Md5Salt { get; set; } = "base";
+
+ public HttpStatusCode LineupPutStatus { get; set; } = HttpStatusCode.OK;
+
+ /// <summary>
+ /// Gets or sets a per-day error code for the hash endpoint, as SCHEDULE_RANGE_EXCEEDED is
+ /// reported. Zero means every day is available.
+ /// </summary>
+ public int Md5ErrorCode { get; set; }
+
+ /// <summary>
+ /// Gets or sets the retryTime of a SCHEDULE_QUEUED response, which the spec documents as a
+ /// bare object rather than the usual array.
+ /// </summary>
+ public DateTime? ScheduleRetryTime { get; set; }
+
+ public bool HasImageArtwork { get; set; }
+
+ /// <summary>
+ /// Gets or sets a date whose schedule md5 differs from the one first reported.
+ /// </summary>
+ public string? ChangedDate { get; set; }
+
+ public List<string> RequestedDates { get; private set; } = [];
+
+ /// <summary>
+ /// Gets or sets a per-program error code for the programs endpoint, such as PROGRAMID_QUEUED.
+ /// </summary>
+ public int ProgramErrorCode { get; set; }
+
+ /// <summary>
+ /// Gets or sets a body the hash endpoint rejects the next request's token with, as HTTP 403.
+ /// </summary>
+ public string? Md5RejectionBody { get; set; }
+
+ public HttpStatusCode Md5RejectionStatus { get; set; } = HttpStatusCode.Forbidden;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether each day airs a second program.
+ /// </summary>
+ public bool SecondProgram { get; set; }
+
+ /// <summary>
+ /// Gets or sets the only program <see cref="ProgramErrorCode"/> applies to; null means all of them.
+ /// </summary>
+ public string? QueuedProgramId { get; set; }
+
+ public string ImageUri { get; set; } = "assets/p1_b.jpg";
+
+ public DateTime LineupModified { get; set; } = new(2026, 9, 14, 0, 0, 0, DateTimeKind.Utc);
+
+ public async Task<HttpResponseMessage> Respond(HttpRequestMessage message, CancellationToken cancellationToken)
+ {
+ var path = message.RequestUri!.AbsolutePath.Replace("/20141201", string.Empty, StringComparison.Ordinal).TrimEnd('/');
+ Counts[path] = Counts.GetValueOrDefault(path) + 1;
+
+ string body;
+ switch (path)
+ {
+ case "/token":
+ body = "{\"code\":0,\"message\":\"OK\",\"serverID\":\"test\",\"token\":\"f3fca79989cafe7dead71beefedc812b\"}";
+ break;
+
+ case "/status":
+ body = "{\"code\":" + StatusCode.ToString(CultureInfo.InvariantCulture) + ",\"account\":{\"maxLineups\":4},\"lineups\":[{\"lineup\":\"USA-OTA-90210\",\"modified\":\""
+ + LineupModified.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)
+ + "\"}],\"systemStatus\":[{\"status\":\"" + SystemStatus + "\",\"message\":\"test\"}]}";
+ break;
+
+ case "/schedules/md5":
+ if (Md5RejectionBody is not null)
+ {
+ var rejection = Md5RejectionBody;
+ Md5RejectionBody = null;
+ return new HttpResponseMessage(Md5RejectionStatus) { Content = new StringContent(rejection) };
+ }
+
+ RequestedDates = await ReadDates(message, cancellationToken).ConfigureAwait(false);
+ body = BuildMd5Response(RequestedDates);
+ break;
+
+ case "/schedules":
+ body = BuildSchedulesResponse(await ReadDates(message, cancellationToken).ConfigureAwait(false));
+ break;
+
+ case "/programs":
+ var ids = JsonSerializer.Deserialize<List<string>>(await message.Content!.ReadAsStringAsync(cancellationToken).ConfigureAwait(false))!;
+ body = "[" + string.Join(',', ids.Select(BuildProgramEntry)) + "]";
+ break;
+
+ case "/metadata/programs":
+ body = HasImageArtwork
+ ? "[{\"programID\":\"EP000000010001\",\"data\":[{\"width\":\"120\",\"height\":\"180\",\"uri\":\""
+ + ImageUri + "\",\"aspect\":\"2x3\",\"category\":\"Iconic\",\"text\":\"yes\",\"tier\":\"Series\"}]}]"
+ : "[]";
+ break;
+
+ case "/lineups/USA-OTA-90210":
+ if (message.Method == HttpMethod.Get)
+ {
+ body = "{\"map\":[{\"stationID\":\"20454\",\"channel\":\"5\"}],\"stations\":[{\"stationID\":\"20454\",\"name\":\"Test\",\"callsign\":\"TST\"}]}";
+ break;
+ }
+
+ return new HttpResponseMessage(LineupPutStatus)
+ {
+ Content = new StringContent(LineupPutStatus == HttpStatusCode.OK
+ ? "{\"code\":0,\"message\":\"OK\"}"
+ : "{\"response\":\"MAX_LINEUPS\",\"code\":4101,\"message\":\"Exceeded number of lineups.\"}")
+ };
+
+ case "/lineups":
+ body = "{\"code\":0,\"lineups\":[]}";
+ break;
+
+ default:
+ return new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("{}") };
+ }
+
+ return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(body) };
+ }
+
+ private static async Task<List<string>> ReadDates(HttpRequestMessage message, CancellationToken cancellationToken)
+ {
+ var requestBody = await message.Content!.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
+ using var doc = JsonDocument.Parse(requestBody);
+
+ var dates = new List<string>();
+ foreach (var station in doc.RootElement.EnumerateArray())
+ {
+ foreach (var date in station.GetProperty("date").EnumerateArray())
+ {
+ dates.Add(date.GetString()!);
+ }
+ }
+
+ return dates;
+ }
+
+ private string BuildMd5Response(List<string> dates)
+ {
+ var days = dates.ConvertAll(d => Md5ErrorCode == 0
+ ? "\"" + d + "\":{\"code\":0,\"message\":\"OK\",\"lastModified\":\"2026-09-14T00:00:00Z\",\"md5\":\"" + GetMd5(d) + "\"}"
+ : "\"" + d + "\":{\"code\":" + Md5ErrorCode.ToString(CultureInfo.InvariantCulture) + ",\"message\":\"Date requested not within range.\"}");
+
+ return "{\"20454\":{" + string.Join(',', days) + "}}";
+ }
+
+ private string GetMd5(string date)
+ => (string.Equals(date, ChangedDate, StringComparison.Ordinal) ? "changed" : Md5Salt) + date;
+
+ private string BuildProgramEntry(string id)
+ => ProgramErrorCode != 0 && (QueuedProgramId is null || string.Equals(id, QueuedProgramId, StringComparison.Ordinal))
+ ? "{\"programID\":\"" + id + "\",\"code\":" + ProgramErrorCode.ToString(CultureInfo.InvariantCulture) + ",\"message\":\"test\"}"
+ : "{\"programID\":\"" + id + "\",\"titles\":[{\"title120\":\"Test\"}],\"hasImageArtwork\":"
+ + (HasImageArtwork ? "true" : "false") + "}";
+
+ private string BuildSchedulesResponse(List<string> dates)
+ {
+ if (ScheduleRetryTime is { } retryTime)
+ {
+ return "{\"response\":\"SCHEDULE_QUEUED\",\"code\":7100,\"serverID\":\"test\",\"message\":\"Queued.\",\"stationID\":\"20454\",\"retryTime\":\""
+ + retryTime.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture) + "\"}";
+ }
+
+ var days = dates.ConvertAll(d =>
+ "{\"stationID\":\"20454\",\"programs\":[{\"programID\":\"EP000000010001\",\"airDateTime\":\"" + d
+ + "T12:00:00Z\",\"duration\":1800,\"md5\":\"x\"}"
+ + (SecondProgram ? ",{\"programID\":\"EP000000020001\",\"airDateTime\":\"" + d + "T14:00:00Z\",\"duration\":1800,\"md5\":\"y\"}" : string.Empty)
+ + "],\"metadata\":{\"modified\":\"2026-09-14T00:00:00Z\",\"md5\":\""
+ + GetMd5(d) + "\",\"startDate\":\"" + d + "\"}}");
+
+ return "[" + string.Join(',', days) + "]";
+ }
+ }
+
+ private sealed class Response
+ {
+ public HttpStatusCode Status { get; set; }
+
+ public string Body { get; set; } = string.Empty;
+ }
+}
diff --git a/tests/Jellyfin.LiveTv.Tests/Recordings/RecordingsMetadataManagerTests.cs b/tests/Jellyfin.LiveTv.Tests/Recordings/RecordingsMetadataManagerTests.cs
index 14ce470fb4..193f794f1d 100644
--- a/tests/Jellyfin.LiveTv.Tests/Recordings/RecordingsMetadataManagerTests.cs
+++ b/tests/Jellyfin.LiveTv.Tests/Recordings/RecordingsMetadataManagerTests.cs
@@ -10,6 +10,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Entities;
using MediaBrowser.Model.LiveTv;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
@@ -19,6 +20,8 @@ namespace Jellyfin.LiveTv.Tests.Recordings;
public sealed class RecordingsMetadataManagerTests
{
+ private const string ImageUrl = "https://json.schedulesdirect.org/20141201/image/assets/p1_b.jpg?token=abc";
+
private readonly string _tempDir =
Path.Combine(Path.GetTempPath(), "jellyfin-test-" + Guid.NewGuid());
@@ -43,7 +46,8 @@ public sealed class RecordingsMetadataManagerTests
var manager = new RecordingsMetadataManager(
NullLogger<RecordingsMetadataManager>.Instance,
config.Object,
- libraryManager.Object);
+ libraryManager.Object,
+ Mock.Of<ISchedulesDirectService>());
var timer = new TimerInfo { Name = "Test Recording", ProgramId = null };
@@ -61,4 +65,47 @@ public sealed class RecordingsMetadataManagerTests
Assert.InRange(parsed, beforeUtc, afterUtc);
}
+
+ [Fact]
+ public async Task SaveRecordingMetadata_RemoteImage_GoesThroughSchedulesDirect()
+ {
+ // Schedules Direct counts every image request, so a recording must not fetch one behind its back.
+ var (manager, libraryManager, schedulesDirect, recordingPath) = CreateImageManager();
+
+ await manager.SaveRecordingMetadata(new TimerInfo { Name = "Test Recording", ProgramId = "p1" }, recordingPath, null);
+
+ schedulesDirect.Verify(s => s.ConvertImageToLocal(It.IsAny<BaseItem>(), It.Is<ItemImageInfo>(i => i.Path == ImageUrl), 0), Times.Once);
+ libraryManager.Verify(
+ l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), It.IsAny<int>(), It.IsAny<bool>()),
+ Times.Never);
+ }
+
+ private (RecordingsMetadataManager Manager, Mock<ILibraryManager> LibraryManager, Mock<ISchedulesDirectService> SchedulesDirect, string RecordingPath) CreateImageManager()
+ {
+ Directory.CreateDirectory(_tempDir);
+ var recordingPath = Path.Combine(_tempDir, "test-recording.ts");
+
+ var config = new Mock<IConfigurationManager>();
+ config.Setup(c => c.GetConfiguration("livetv"))
+ .Returns(new LiveTvOptions { SaveRecordingNFO = false, SaveRecordingImages = true });
+
+ var program = new LiveTvProgram
+ {
+ Name = "Test",
+ ImageInfos = [new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }]
+ };
+ var libraryManager = new Mock<ILibraryManager>();
+ libraryManager
+ .Setup(l => l.GetItemList(It.IsAny<InternalItemsQuery>()))
+ .Returns([program]);
+
+ var schedulesDirect = new Mock<ISchedulesDirectService>();
+ var manager = new RecordingsMetadataManager(
+ NullLogger<RecordingsMetadataManager>.Instance,
+ config.Object,
+ libraryManager.Object,
+ schedulesDirect.Object);
+
+ return (manager, libraryManager, schedulesDirect, recordingPath);
+ }
}
diff --git a/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleCacheMetadataTests.cs b/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleCacheMetadataTests.cs
new file mode 100644
index 0000000000..f34a07da43
--- /dev/null
+++ b/tests/Jellyfin.MediaEncoding.Tests/Subtitles/SubtitleCacheMetadataTests.cs
@@ -0,0 +1,128 @@
+using System;
+using System.Globalization;
+using System.IO;
+using AutoFixture;
+using AutoFixture.AutoMoq;
+using MediaBrowser.MediaEncoding.Subtitles;
+using MediaBrowser.Model.Dto;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.IO;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.MediaEncoding.Subtitles.Tests;
+
+public sealed class SubtitleCacheMetadataTests : IDisposable
+{
+ private readonly string _directory = Directory.CreateTempSubdirectory("jellyfin-subtitle-metadata-").FullName;
+ private readonly SubtitleEncoder _encoder;
+ private readonly string _cachePath;
+ private readonly string _sourcePath;
+
+ public SubtitleCacheMetadataTests()
+ {
+ _cachePath = Path.Combine(_directory, "3.srt");
+ _sourcePath = Path.Combine(_directory, "movie.mkv");
+ File.WriteAllText(_cachePath, "cached subtitle");
+ File.WriteAllText(_sourcePath, "source");
+ var fixture = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true });
+ fixture.Freeze<Mock<IFileSystem>>().Setup(x => x.GetFileInfo(It.IsAny<string>())).Returns((string path) =>
+ {
+ var info = new FileInfo(path);
+ return new FileSystemMetadata
+ {
+ Exists = info.Exists,
+ Length = info.Exists ? info.Length : 0,
+ LastWriteTimeUtc = info.LastWriteTimeUtc
+ };
+ });
+ _encoder = fixture.Create<SubtitleEncoder>();
+ }
+
+ [Fact]
+ public void Cache_LogicalIndexReassignedToDifferentTrack_IsRejected()
+ {
+ var source = CreateSource();
+ var french = source.MediaStreams[3];
+ _encoder.WriteCacheMeta(_cachePath, source, french);
+
+ AddSidecar(source);
+ var english = source.MediaStreams[3];
+ Assert.Equal(3, english.Index);
+ Assert.False(_encoder.IsCachedSubtitleFresh(_cachePath, source, english));
+ Assert.True(_encoder.IsCachedSubtitleFresh(_cachePath, source, french));
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void Cache_SourcePathChangesWithSameRevision_IsRejected(bool remote)
+ {
+ var source = CreateSource();
+ if (remote)
+ {
+ source.Path = "https://example.invalid/a.mkv";
+ }
+
+ var subtitle = source.MediaStreams[3];
+ _encoder.WriteCacheMeta(_cachePath, source, subtitle);
+ Assert.True(_encoder.IsCachedSubtitleFresh(_cachePath, source, subtitle));
+ var replacement = Path.Combine(_directory, "other.mkv");
+ File.Copy(_sourcePath, replacement);
+ File.SetLastWriteTimeUtc(replacement, File.GetLastWriteTimeUtc(_sourcePath));
+ source.Path = remote ? "https://example.invalid/b.mkv" : replacement;
+ Assert.False(_encoder.IsCachedSubtitleFresh(_cachePath, source, subtitle));
+ }
+
+ [Fact]
+ public void Cache_SourceRevisionChanges_IsRejected()
+ {
+ var source = CreateSource();
+ var subtitle = source.MediaStreams[3];
+ _encoder.WriteCacheMeta(_cachePath, source, subtitle);
+ File.AppendAllText(_sourcePath, "replacement");
+ Assert.False(_encoder.IsCachedSubtitleFresh(_cachePath, source, subtitle));
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void Cache_LegacyOrMissingMetadata_IsRejected(bool legacy)
+ {
+ var source = CreateSource();
+ if (legacy)
+ {
+ var info = new FileInfo(_sourcePath);
+ File.WriteAllText(_cachePath + ".meta", string.Create(CultureInfo.InvariantCulture, $"{info.Length}:{info.LastWriteTimeUtc.Ticks}"));
+ }
+
+ Assert.False(_encoder.IsCachedSubtitleFresh(_cachePath, source, source.MediaStreams[3]));
+ }
+
+ private MediaSourceInfo CreateSource() => new()
+ {
+ Path = _sourcePath,
+ MediaStreams =
+ [
+ new() { Index = 0, Type = MediaStreamType.Video },
+ new() { Index = 1, Type = MediaStreamType.Audio },
+ new() { Index = 2, Type = MediaStreamType.Subtitle, Language = "eng" },
+ new() { Index = 3, Type = MediaStreamType.Subtitle, Language = "fra" }
+ ]
+ };
+
+ private static void AddSidecar(MediaSourceInfo source)
+ {
+ source.MediaStreams = [new MediaStream { IsExternal = true, Type = MediaStreamType.Subtitle, Path = "sidecar.srt" }, .. source.MediaStreams];
+ for (var i = 0; i < source.MediaStreams.Count; i++)
+ {
+ source.MediaStreams[i].Index = i;
+ }
+ }
+
+ public void Dispose()
+ {
+ _encoder.Dispose();
+ Directory.Delete(_directory, true);
+ }
+}
diff --git a/tests/Jellyfin.Model.Tests/Dlna/StreamBuilderTests.cs b/tests/Jellyfin.Model.Tests/Dlna/StreamBuilderTests.cs
index f5a023686c..f5d40e3fc6 100644
--- a/tests/Jellyfin.Model.Tests/Dlna/StreamBuilderTests.cs
+++ b/tests/Jellyfin.Model.Tests/Dlna/StreamBuilderTests.cs
@@ -372,6 +372,39 @@ namespace Jellyfin.Model.Tests
}
[Theory]
+ // Chrome
+ [InlineData("Chrome", "mp4-h264-ac3-aac-aac-srt-2600k", new TranscodeReason[] { 0, TranscodeReason.AudioCodecNotSupported, TranscodeReason.SecondaryAudioNotSupported, TranscodeReason.SecondaryAudioNotSupported, 0 })]
+ [InlineData("Chrome", "mp4-h264-dts-srt-2600k", new TranscodeReason[] { 0, TranscodeReason.AudioCodecNotSupported, 0 })]
+ [InlineData("Chrome", "mkv-vp9-aac-srt-2600k", new TranscodeReason[] { TranscodeReason.ContainerNotSupported, TranscodeReason.ContainerNotSupported, 0 })]
+ // Firefox
+ [InlineData("Firefox", "mp4-h264-ac3-aac-aac-srt-2600k", new TranscodeReason[] { 0, TranscodeReason.AudioCodecNotSupported, TranscodeReason.SecondaryAudioNotSupported, TranscodeReason.SecondaryAudioNotSupported, 0 })]
+ [InlineData("Firefox", "mp4-h264-dts-srt-2600k", new TranscodeReason[] { 0, TranscodeReason.AudioCodecNotSupported, 0 })]
+ [InlineData("Firefox", "mkv-vp9-aac-srt-2600k", new TranscodeReason[] { TranscodeReason.ContainerNotSupported, TranscodeReason.ContainerNotSupported, 0 })]
+ // Tizen3-stereo
+ [InlineData("Tizen3-stereo", "mp4-h264-ac3-aac-aac-srt-2600k", new TranscodeReason[] { 0, 0, TranscodeReason.SecondaryAudioNotSupported, TranscodeReason.SecondaryAudioNotSupported, 0 })]
+ [InlineData("Tizen3-stereo", "mp4-h264-dts-srt-2600k", new TranscodeReason[] { 0, 0, 0 })]
+ [InlineData("Tizen3-stereo", "mkv-vp9-aac-srt-2600k", new TranscodeReason[] { 0, 0, 0 })]
+ // Tizen4-4K-5.1
+ [InlineData("Tizen4-4K-5.1", "mp4-h264-ac3-aac-aac-srt-2600k", new TranscodeReason[] { 0, 0, TranscodeReason.SecondaryAudioNotSupported, TranscodeReason.SecondaryAudioNotSupported, 0 })]
+ [InlineData("Tizen4-4K-5.1", "mp4-h264-dts-srt-2600k", new TranscodeReason[] { 0, TranscodeReason.AudioCodecNotSupported, 0 })]
+ [InlineData("Tizen4-4K-5.1", "mkv-vp9-aac-srt-2600k", new TranscodeReason[] { 0, 0, 0 })]
+ public async Task CheckCompatibility(string deviceName, string mediaSourceName, TranscodeReason[] directPlayErrors)
+ {
+ var options = await GetMediaOptions(deviceName, mediaSourceName);
+
+ var builder = GetStreamBuilder();
+
+ var streamInfo = builder.GetOptimalVideoStream(options);
+ Assert.NotNull(streamInfo);
+
+ var mediaSource = options.MediaSources.First(source => source.Id == streamInfo.MediaSourceId);
+ Assert.NotNull(mediaSource);
+
+ Assert.Equal(directPlayErrors.Length, mediaSource.MediaStreams.Count);
+ Assert.All(mediaSource.MediaStreams, (stream, i) => Assert.Equal(stream.DirectPlayErrors, directPlayErrors[i]));
+ }
+
+ [Theory]
[InlineData("pgssub", null)]
[InlineData("vobsub", "mks")]
public async Task BuildVideoItemWithSecondaryAudioAndExternalGraphicalSubtitleKeepsVideoCopy(string subtitleCodec, string? subtitleContainer)
diff --git a/tests/Jellyfin.Naming.Tests/ExternalFiles/ExternalPathParserTests.cs b/tests/Jellyfin.Naming.Tests/ExternalFiles/ExternalPathParserTests.cs
index 0b8b1f644b..e06ae02e2a 100644
--- a/tests/Jellyfin.Naming.Tests/ExternalFiles/ExternalPathParserTests.cs
+++ b/tests/Jellyfin.Naming.Tests/ExternalFiles/ExternalPathParserTests.cs
@@ -118,4 +118,23 @@ public class ExternalPathParserTests
Assert.Equal(isForced, actual.IsForced);
Assert.Equal(isHearingImpaired, actual.IsHearingImpaired);
}
+
+ [Theory]
+ // LocalizationManager lists ze before the zho code it shares with Chinese.
+ [InlineData("ze", "Chinese (Bilingual)", new[] { "ze", "zho", "chi" }, "zho")]
+ // A hyphen in the display name does not make a culture a variant.
+ [InlineData("el", "Greek, Modern (1453-)", new[] { "ell", "gre" }, "ell")]
+ [InlineData("pt-br", "pt-br", new[] { "pt-br", "por" }, "pt-br")]
+ public void ParseFile_Culture_StoresItsLanguageCode(string tag, string name, string[] codes, string expected)
+ {
+ var localizationManager = new Mock<ILocalizationManager>();
+ localizationManager.Setup(lm => lm.FindLanguageInfo(tag))
+ .Returns(new CultureDto(name, name, tag, codes));
+ var parser = new ExternalPathParser(new NamingOptions(), localizationManager.Object, DlnaProfileType.Subtitle);
+
+ var actual = parser.ParseFile("My.Video." + tag + ".srt", "." + tag);
+
+ Assert.NotNull(actual);
+ Assert.Equal(expected, actual!.Language);
+ }
}
diff --git a/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs b/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs
index d3b82cefcd..da2e9c53d8 100644
--- a/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs
+++ b/tests/Jellyfin.Providers.Tests/Manager/ProviderManagerTests.cs
@@ -2,10 +2,13 @@ using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
+using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.BaseItemManager;
using MediaBrowser.Controller.Configuration;
@@ -17,12 +20,14 @@ using MediaBrowser.Controller.MediaSegments;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Providers.Manager;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
+using Moq.Protected;
using Xunit;
// Allow Moq to see internal class
@@ -488,6 +493,36 @@ namespace Jellyfin.Providers.Tests.Manager
Assert.Equal(ItemCount - 1, processed.Count);
}
+ [Theory]
+ [InlineData(HttpStatusCode.OK, "application/json", HttpStatusCode.NotFound)]
+ [InlineData(HttpStatusCode.Forbidden, "application/json", HttpStatusCode.Forbidden)]
+ public async Task SaveImage_ProviderRefusesWithBody_KeepsTheBody(HttpStatusCode status, string contentType, HttpStatusCode expectedStatus)
+ {
+ // Schedules Direct reports an exhausted image quota as HTTP 200 with a JSON body, and
+ // only the code in that body tells the guide refresh to stop requesting images.
+ const string Body = "{\"response\":\"MAX_IMAGE_DOWNLOADS\",\"code\":5002}";
+
+ var messageHandler = new Mock<HttpMessageHandler>();
+ messageHandler.Protected()
+ .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
+ .ReturnsAsync(() => new HttpResponseMessage(status)
+ {
+ Content = new StringContent(Body, Encoding.UTF8, contentType)
+ });
+
+ var httpClientFactory = new Mock<IHttpClientFactory>();
+ httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
+ .Returns(() => new HttpClient(messageHandler.Object));
+
+ using var providerManager = GetProviderManager(httpClientFactory: httpClientFactory.Object);
+
+ var ex = await Assert.ThrowsAsync<HttpResponseBodyException>(
+ () => providerManager.SaveImage(new Movie(), "https://json.schedulesdirect.org/20141201/image/a.jpg", ImageType.Primary, null, CancellationToken.None));
+
+ Assert.Equal(expectedStatus, ex.StatusCode);
+ Assert.Equal(Body, ex.ResponseBody);
+ }
+
private static void GetMetadataProviders_CanRefreshMetadata_Tester(
string providerType,
bool expected,
@@ -666,7 +701,8 @@ namespace Jellyfin.Providers.Tests.Manager
ServerConfiguration? serverConfiguration = null,
LibraryOptions? libraryOptions = null,
IBaseItemManager? baseItemManager = null,
- ILibraryManager? libraryManager = null)
+ ILibraryManager? libraryManager = null,
+ IHttpClientFactory? httpClientFactory = null)
{
var serverConfigurationManager = new Mock<IServerConfigurationManager>(MockBehavior.Strict);
serverConfigurationManager.Setup(i => i.Configuration)
@@ -683,7 +719,7 @@ namespace Jellyfin.Providers.Tests.Manager
}
var providerManager = new ProviderManager(
- Mock.Of<IHttpClientFactory>(),
+ httpClientFactory ?? Mock.Of<IHttpClientFactory>(),
Mock.Of<ISubtitleManager>(),
serverConfigurationManager.Object,
Mock.Of<ILibraryMonitor>(),
diff --git a/tests/Jellyfin.Providers.Tests/Tmdb/TmdbUtilsTests.cs b/tests/Jellyfin.Providers.Tests/Tmdb/TmdbUtilsTests.cs
index 6e36c08064..5c976d0592 100644
--- a/tests/Jellyfin.Providers.Tests/Tmdb/TmdbUtilsTests.cs
+++ b/tests/Jellyfin.Providers.Tests/Tmdb/TmdbUtilsTests.cs
@@ -22,6 +22,17 @@ namespace Jellyfin.Providers.Tests.Tmdb
}
[Theory]
+ [InlineData("es-419", null, "es,null,en")]
+ [InlineData("es-419", "MX", "es-MX,null,en")]
+ [InlineData("pt-BR", null, "pt-BR,null,en")]
+ [InlineData("en", null, "en,null")]
+ [InlineData("", null, "null,en")]
+ public static void GetImageLanguagesParam_Valid_Success(string language, string? countryCode, string expected)
+ {
+ Assert.Equal(expected, TmdbUtils.GetImageLanguagesParam(language, countryCode));
+ }
+
+ [Theory]
[InlineData(null, null)]
[InlineData("", "")]
public static void NormalizeLanguage_Invalid_Equal(string? input, string? expected)
@@ -36,6 +47,11 @@ namespace Jellyfin.Providers.Tests.Tmdb
[InlineData("en", "GB", "en-US", "en-GB")]
[InlineData("en", "US", "en-US", "en-US")]
[InlineData("en", null, "en-US", "en-US")]
+ // Regions TMDb does not have match the images it files under the countries they cover.
+ [InlineData("es", "MX", "es-419", "es-419")]
+ [InlineData("es", "AR", "es-419", "es-419")]
+ [InlineData("es", "ES", "es-419", "es-ES")]
+ [InlineData("de", "DE", "de-CH", "de-CH")]
// TMDb files nearly every image under a region, which must not stop a plain request matching.
[InlineData("en", "US", "en", "en")]
[InlineData("pt", "PT", "pt", "pt")]
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryAncestorFilterTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryAncestorFilterTests.cs
new file mode 100644
index 0000000000..d1f19b643f
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryAncestorFilterTests.cs
@@ -0,0 +1,174 @@
+using System;
+using System.Linq;
+using Emby.Server.Implementations.Data;
+using Jellyfin.Data.Enums;
+using Jellyfin.Database.Implementations;
+using Jellyfin.Database.Implementations.Entities;
+using Jellyfin.Database.Implementations.Enums;
+using Jellyfin.Server.Implementations.Item;
+using MediaBrowser.Controller.Entities;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.Item;
+
+/// <summary>
+/// Covers <see cref="InternalItemsQuery.AncestorIds"/>, the filter every recursive query under a parent runs on.
+/// </summary>
+public sealed class BaseItemRepositoryAncestorFilterTests : SqliteDbTestFixture
+{
+ private const string FolderType = "MediaBrowser.Controller.Entities.Folder";
+ private const string SeriesType = "MediaBrowser.Controller.Entities.TV.Series";
+ private const string SeasonType = "MediaBrowser.Controller.Entities.TV.Season";
+ private const string EpisodeType = "MediaBrowser.Controller.Entities.TV.Episode";
+
+ private const string SeasonKey = "series-key-001";
+
+ private readonly QueryPlanRecorder _recorder;
+ private readonly BaseItemRepository _repository;
+
+ private readonly Guid _library = Guid.NewGuid();
+ private readonly Guid _series = Guid.NewGuid();
+ private readonly Guid _season = Guid.NewGuid();
+ private readonly Guid _episode = Guid.NewGuid();
+ private readonly Guid _otherSeries = Guid.NewGuid();
+ private readonly Guid _otherEpisode = Guid.NewGuid();
+ private readonly Guid _emptyFolder = Guid.NewGuid();
+
+ public BaseItemRepositoryAncestorFilterTests()
+ : this(new QueryPlanRecorder())
+ {
+ }
+
+ private BaseItemRepositoryAncestorFilterTests(QueryPlanRecorder recorder)
+ : base(recorder)
+ {
+ _recorder = recorder;
+ using (var context = CreateDbContext())
+ {
+ Seed(context);
+ }
+
+ _repository = CreateBaseItemRepository(new ItemTypeLookup());
+ }
+
+ [Fact]
+ public void AncestorIds_SingleAncestor_ReturnsOnlyItsDescendants()
+ {
+ var ids = _repository.GetItemIdsList(new InternalItemsQuery { AncestorIds = [_series] });
+
+ Assert.Equal(new[] { _season, _episode }.Order(), ids.Order());
+ }
+
+ [Fact]
+ public void AncestorIds_OverlappingAncestors_ReturnsUnionWithoutDuplicates()
+ {
+ var ids = _repository.GetItemIdsList(new InternalItemsQuery
+ {
+ AncestorIds = [_library, _series, _otherSeries],
+ IncludeItemTypes = [BaseItemKind.Episode]
+ });
+
+ Assert.Equal(new[] { _episode, _otherEpisode }.Order(), ids.Order());
+ }
+
+ [Fact]
+ public void AncestorIds_ParentWithoutDescendants_ReturnsNothing()
+ {
+ var ids = _repository.GetItemIdsList(new InternalItemsQuery { AncestorIds = [_emptyFolder] });
+
+ Assert.Empty(ids);
+ }
+
+ [Fact]
+ public void AncestorWithPresentationUniqueKey_ReturnsDescendantsOfTheKeyedFolder()
+ {
+ var ids = _repository.GetItemIdsList(new InternalItemsQuery
+ {
+ AncestorWithPresentationUniqueKey = SeasonKey,
+ IncludeItemTypes = [BaseItemKind.Episode]
+ });
+
+ Assert.Equal([_episode], ids);
+ }
+
+ [Fact]
+ public void AncestorIds_SeeksDescendantsByParent()
+ {
+ _recorder.Commands.Clear();
+
+ _repository.GetItemList(new InternalItemsQuery
+ {
+ AncestorIds = [_emptyFolder],
+ OrderBy = [(ItemSortBy.IsFolder, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending)],
+ Limit = 1
+ });
+
+ var query = Assert.Single(_recorder.Commands, c => c.Sql.Contains("\"AncestorIds\"", StringComparison.Ordinal));
+ AssertDrivenByDescendants(Explain(query));
+ }
+
+ [Fact]
+ public void AncestorWithPresentationUniqueKey_SeeksDescendantsByParent()
+ {
+ _recorder.Commands.Clear();
+
+ _repository.GetItemList(new InternalItemsQuery
+ {
+ AncestorWithPresentationUniqueKey = SeasonKey,
+ OrderBy = [(ItemSortBy.IsFolder, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending)],
+ Limit = 1
+ });
+
+ var query = Assert.Single(_recorder.Commands, c => c.Sql.Contains("\"AncestorIds\"", StringComparison.Ordinal));
+ AssertDrivenByDescendants(Explain(query));
+ }
+
+ // The issue's plan scanned BaseItems and probed AncestorIds per row; the fix looks BaseItems up by the descendant ids.
+ private static void AssertDrivenByDescendants(string[] plan)
+ {
+ Assert.Contains(plan, line => line.Contains("COVERING INDEX IX_AncestorIds_ParentItemId_ItemId (ParentItemId=?)", StringComparison.Ordinal));
+ Assert.Contains(plan, line => line.StartsWith("SEARCH b USING INDEX sqlite_autoindex_BaseItems_1 (Id=?)", StringComparison.Ordinal));
+ Assert.DoesNotContain(plan, line => line.StartsWith("SCAN b ", StringComparison.Ordinal) || line == "SCAN b");
+ }
+
+ private void Seed(JellyfinDbContext context)
+ {
+ context.BaseItems.Add(new BaseItemEntity { Id = _library, Type = FolderType, Name = "Shows", IsFolder = true });
+ context.BaseItems.Add(new BaseItemEntity { Id = _series, Type = SeriesType, Name = "Series", IsFolder = true });
+ context.BaseItems.Add(new BaseItemEntity { Id = _season, Type = SeasonType, Name = "Season 1", IsFolder = true, PresentationUniqueKey = SeasonKey });
+ context.BaseItems.Add(new BaseItemEntity { Id = _episode, Type = EpisodeType, Name = "Episode 1" });
+ context.BaseItems.Add(new BaseItemEntity { Id = _otherSeries, Type = SeriesType, Name = "Other series", IsFolder = true });
+ context.BaseItems.Add(new BaseItemEntity { Id = _otherEpisode, Type = EpisodeType, Name = "Other episode" });
+ context.BaseItems.Add(new BaseItemEntity { Id = _emptyFolder, Type = FolderType, Name = "Empty", IsFolder = true });
+
+ // AncestorIds is a closure: production writes one row per ancestor, not just the parent.
+ AddAncestors(context, _series, _library);
+ AddAncestors(context, _season, _series, _library);
+ AddAncestors(context, _episode, _season, _series, _library);
+ AddAncestors(context, _otherSeries, _library);
+ AddAncestors(context, _otherEpisode, _otherSeries, _library);
+ AddAncestors(context, _emptyFolder, _library);
+
+ context.SaveChanges();
+ }
+
+ private static void AddAncestors(JellyfinDbContext context, Guid itemId, params Guid[] ancestorIds)
+ {
+ foreach (var ancestorId in ancestorIds)
+ {
+ context.AncestorIds.Add(new AncestorId
+ {
+ ItemId = itemId,
+ ParentItemId = ancestorId,
+ Item = null!,
+ ParentItem = null!
+ });
+ }
+ }
+
+ private string[] Explain(RecordedCommand query)
+ {
+ using var context = CreateDbContext();
+ return QueryPlanRecorder.Explain(context, query);
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryStreamFilterTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryStreamFilterTests.cs
index 4e8d84850b..002ccb98ea 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryStreamFilterTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryStreamFilterTests.cs
@@ -5,6 +5,8 @@ using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Server.Implementations.Item;
using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.Globalization;
+using Moq;
using Xunit;
using LinkedChildType = Jellyfin.Database.Implementations.Entities.LinkedChildType;
@@ -183,6 +185,22 @@ public sealed class BaseItemRepositoryStreamFilterTests : SqliteDbTestFixture
}
[Fact]
+ public void HasNoAudioTrackWithLanguage_VariantExcludesAnItemWithItsBaseCode()
+ {
+ // ffprobe tags a fr-CA track as the base code, so a fr-ca setting has to see it.
+ var localizationManager = new Mock<ILocalizationManager>();
+ localizationManager
+ .Setup(m => m.FindLanguageInfo("fr-ca"))
+ .Returns(new CultureDto("fr-ca", "French (Canada)", "fr-ca", ["frc"]));
+ localizationManager
+ .Setup(m => m.FindLanguageInfo("fr"))
+ .Returns(new CultureDto("French", "French", "fr", ["fra", "fre"]));
+ var repository = CreateBaseItemRepository(new ItemTypeLookup(), localizationManager.Object);
+
+ Assert.DoesNotContain(_versionedMovie, repository.GetItemIdsList(new InternalItemsQuery { HasNoAudioTrackWithLanguage = "fr-ca" }));
+ }
+
+ [Fact]
public void HasChapterImages_MatchesAnItemWhoseAlternateVersionCarriesThem()
{
Assert.Contains(_versionedMovie, _repository.GetItemIdsList(new InternalItemsQuery { HasChapterImages = true }));
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/ItemPersistenceOwnedRowTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/ItemPersistenceOwnedRowTests.cs
index fea6ccb7a1..d2eb53c393 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Item/ItemPersistenceOwnedRowTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/ItemPersistenceOwnedRowTests.cs
@@ -12,6 +12,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
+using MediaBrowser.Controller.Playlists;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging.Abstractions;
@@ -114,6 +115,61 @@ public sealed class ItemPersistenceOwnedRowTests : SqliteDbTestFixture
}
[Fact]
+ public void SaveItems_BatchLargerThanSqliteVariableLimit_Saves()
+ {
+ // SQLite caps bound variables at 32766; batch-wide lookups must not bind one per item.
+ const int Count = 33_000;
+ var movies = Enumerable.Range(0, Count)
+ .Select(i =>
+ {
+ var movie = CreateMovie(Guid.NewGuid());
+ movie.Genres = [$"Genre {i}"];
+ return movie;
+ })
+ .ToList();
+
+ _service.SaveItems(movies, CancellationToken.None);
+ _service.SaveItems(movies, CancellationToken.None);
+
+ using var ctx = CreateDbContext();
+ Assert.Equal(Count, ctx.BaseItems.Count(e => e.Type == "MediaBrowser.Controller.Entities.Movies.Movie"));
+ Assert.Equal(Count, ctx.ItemValuesMap.Count());
+ }
+
+ [Fact]
+ public void SaveItems_PlaylistLinkedByPathBeyondSqliteVariableLimit_Saves()
+ {
+ var appHost = new Mock<IServerApplicationHost>();
+ appHost.Setup(h => h.ReverseVirtualPath(It.IsAny<string>())).Returns((string p) => p);
+ var service = new ItemPersistenceService(CreateDbContextFactory(), appHost.Object, NullLogger<ItemPersistenceService>.Instance);
+
+ const int Count = 33_000;
+ var movies = Enumerable.Range(0, Count)
+ .Select(i =>
+ {
+ var movie = CreateMovie(Guid.NewGuid());
+ movie.Path = $"/movies/{i}.mkv";
+ return movie;
+ })
+ .ToList();
+ service.SaveItems(movies, CancellationToken.None);
+
+ // Legacy links carry only a path, so the save resolves every one of them in a single lookup.
+#pragma warning disable CS0618 // Type or member is obsolete
+ var playlist = new Playlist
+ {
+ Id = Guid.NewGuid(),
+ Name = "Everything",
+ LinkedChildren = movies.Select(m => new LinkedChild { Path = m.Path, Type = MediaBrowser.Controller.Entities.LinkedChildType.Manual }).ToArray()
+ };
+#pragma warning restore CS0618
+ service.SaveItems([playlist], CancellationToken.None);
+
+ using var ctx = CreateDbContext();
+ Assert.Equal(Count, ctx.LinkedChildren.Count(e => e.ParentId.Equals(playlist.Id)));
+ }
+
+ [Fact]
public async Task ReattachUserData_DetachedRowsFromDifferentEras_CollapsesToMostRecentPlay()
{
var movie = CreateMovie(Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"));
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/PeopleRepositoryParentFilterTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/PeopleRepositoryParentFilterTests.cs
new file mode 100644
index 0000000000..074f37e5c6
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/PeopleRepositoryParentFilterTests.cs
@@ -0,0 +1,131 @@
+using System;
+using System.Linq;
+using Emby.Server.Implementations.Data;
+using Jellyfin.Data.Enums;
+using Jellyfin.Database.Implementations;
+using Jellyfin.Database.Implementations.Entities;
+using Jellyfin.Server.Implementations.Item;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Persistence;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.Item;
+
+/// <summary>
+/// Covers <see cref="InternalPeopleQuery.ParentId"/>, which limits /Persons to the people credited below a folder.
+/// </summary>
+public sealed class PeopleRepositoryParentFilterTests : SqliteDbTestFixture
+{
+ private readonly QueryPlanRecorder _recorder;
+ private readonly PeopleRepository _people;
+
+ private readonly Guid _library = Guid.NewGuid();
+ private readonly Guid _series = Guid.NewGuid();
+ private readonly Guid _episode = Guid.NewGuid();
+ private readonly Guid _secondEpisode = Guid.NewGuid();
+ private readonly Guid _otherSeries = Guid.NewGuid();
+ private readonly Guid _otherEpisode = Guid.NewGuid();
+ private readonly Guid _emptyFolder = Guid.NewGuid();
+
+ public PeopleRepositoryParentFilterTests()
+ : this(new QueryPlanRecorder())
+ {
+ }
+
+ private PeopleRepositoryParentFilterTests(QueryPlanRecorder recorder)
+ : base(recorder)
+ {
+ _recorder = recorder;
+ var lookup = new ItemTypeLookup();
+ using (var context = CreateDbContext())
+ {
+ var folder = lookup.BaseItemKindNames[BaseItemKind.Folder];
+ var series = lookup.BaseItemKindNames[BaseItemKind.Series];
+ var episode = lookup.BaseItemKindNames[BaseItemKind.Episode];
+ context.BaseItems.Add(new BaseItemEntity { Id = _library, Type = folder, Name = "Shows", IsFolder = true });
+ context.BaseItems.Add(new BaseItemEntity { Id = _series, Type = series, Name = "Series", IsFolder = true });
+ context.BaseItems.Add(new BaseItemEntity { Id = _episode, Type = episode, Name = "Episode 1" });
+ context.BaseItems.Add(new BaseItemEntity { Id = _secondEpisode, Type = episode, Name = "Episode 2" });
+ context.BaseItems.Add(new BaseItemEntity { Id = _otherSeries, Type = series, Name = "Other series", IsFolder = true });
+ context.BaseItems.Add(new BaseItemEntity { Id = _otherEpisode, Type = episode, Name = "Other episode" });
+ context.BaseItems.Add(new BaseItemEntity { Id = _emptyFolder, Type = folder, Name = "Empty", IsFolder = true });
+
+ AddAncestors(context, _series, _library);
+ AddAncestors(context, _episode, _series, _library);
+ AddAncestors(context, _secondEpisode, _series, _library);
+ AddAncestors(context, _otherSeries, _library);
+ AddAncestors(context, _otherEpisode, _otherSeries, _library);
+ AddAncestors(context, _emptyFolder, _library);
+ context.SaveChanges();
+ }
+
+ _people = new PeopleRepository(CreateDbContextFactory(), lookup, Mock.Of<IItemQueryHelpers>());
+ _people.UpdatePeople(_episode, [Actor("Lead"), Actor("Guest")]);
+ _people.UpdatePeople(_secondEpisode, [Actor("Lead")]);
+ _people.UpdatePeople(_otherEpisode, [Actor("Stranger")]);
+ }
+
+ [Fact]
+ public void ParentId_ReturnsEachPersonCreditedBelowTheParentOnce()
+ {
+ var result = _people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _series });
+
+ Assert.Equal(["Guest", "Lead"], result.Items.Select(p => p.Name).Distinct().Order());
+ Assert.Equal(2, result.TotalRecordCount);
+ }
+
+ [Fact]
+ public void ParentId_SpanningSeveralFolders_ReturnsTheirUnion()
+ {
+ var result = _people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _library });
+
+ Assert.Equal(["Guest", "Lead", "Stranger"], result.Items.Select(p => p.Name).Distinct().Order());
+ Assert.Equal(3, result.TotalRecordCount);
+ }
+
+ [Fact]
+ public void ParentId_WithoutCreditsBelowIt_ReturnsNobody()
+ {
+ var result = _people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _emptyFolder });
+
+ Assert.Empty(result.Items);
+ Assert.Equal(0, result.TotalRecordCount);
+ }
+
+ [Fact]
+ public void ParentId_SeeksCreditsByDescendant()
+ {
+ _recorder.Commands.Clear();
+
+ _people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _series, Limit = 1 });
+
+ var query = Assert.Single(_recorder.Commands, c => c.Sql.Contains("\"AncestorIds\"", StringComparison.Ordinal) && c.Sql.Contains("ORDER BY", StringComparison.Ordinal));
+ var plan = Explain(query);
+ Assert.Contains(plan, line => line.Contains("COVERING INDEX IX_AncestorIds_ParentItemId_ItemId (ParentItemId=?)", StringComparison.Ordinal));
+ Assert.Contains(plan, line => line.StartsWith("SEARCH p USING INDEX sqlite_autoindex_Peoples_1 (Id=?)", StringComparison.Ordinal));
+ Assert.DoesNotContain(plan, line => line.StartsWith("SCAN p ", StringComparison.Ordinal) || line == "SCAN p");
+ }
+
+ private static PersonInfo Actor(string name) => new() { Name = name, Type = PersonKind.Actor };
+
+ private static void AddAncestors(JellyfinDbContext context, Guid itemId, params Guid[] ancestorIds)
+ {
+ foreach (var ancestorId in ancestorIds)
+ {
+ context.AncestorIds.Add(new AncestorId
+ {
+ ItemId = itemId,
+ ParentItemId = ancestorId,
+ Item = null!,
+ ParentItem = null!
+ });
+ }
+ }
+
+ private string[] Explain(RecordedCommand query)
+ {
+ using var context = CreateDbContext();
+ return QueryPlanRecorder.Explain(context, query);
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs
new file mode 100644
index 0000000000..a356932a78
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/QueryPlanRecorder.cs
@@ -0,0 +1,72 @@
+using System.Collections.Generic;
+using System.Data.Common;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using Jellyfin.Database.Implementations;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Diagnostics;
+
+namespace Jellyfin.Server.Implementations.Tests.Item;
+
+/// <summary>
+/// Records the reader commands EF Core sends, so a test can ask SQLite how it would run one.
+/// </summary>
+internal sealed class QueryPlanRecorder : DbCommandInterceptor
+{
+ /// <summary>
+ /// Gets the recorded commands, oldest first.
+ /// </summary>
+ public List<RecordedCommand> Commands { get; } = [];
+
+ /// <summary>
+ /// Returns the <c>EXPLAIN QUERY PLAN</c> detail lines for a recorded command.
+ /// </summary>
+ /// <param name="context">A context on the database the command ran against.</param>
+ /// <param name="query">The recorded command.</param>
+ /// <returns>One entry per plan line.</returns>
+ public static string[] Explain(JellyfinDbContext context, RecordedCommand query)
+ {
+ using var command = context.Database.GetDbConnection().CreateCommand();
+#pragma warning disable CA2100 // query.Sql is generated by EF Core; query values remain bound parameters.
+ command.CommandText = "EXPLAIN QUERY PLAN " + query.Sql;
+#pragma warning restore CA2100
+ foreach (var value in query.Parameters)
+ {
+ var parameter = command.CreateParameter();
+ parameter.ParameterName = value.Name;
+ parameter.Value = value.Value;
+ command.Parameters.Add(parameter);
+ }
+
+ using var reader = command.ExecuteReader();
+ var plan = new List<string>();
+ while (reader.Read())
+ {
+ plan.Add(reader.GetString(3));
+ }
+
+ return plan.ToArray();
+ }
+
+ /// <inheritdoc />
+ public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
+ {
+ Record(command);
+ return result;
+ }
+
+ /// <inheritdoc />
+ public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
+ {
+ Record(command);
+ return ValueTask.FromResult(result);
+ }
+
+ private void Record(DbCommand command)
+ {
+ Commands.Add(new RecordedCommand(
+ command.CommandText,
+ command.Parameters.Cast<DbParameter>().Select(p => (p.ParameterName, p.Value)).ToArray()));
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/RecordedCommand.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/RecordedCommand.cs
new file mode 100644
index 0000000000..e7e7314350
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/RecordedCommand.cs
@@ -0,0 +1,8 @@
+namespace Jellyfin.Server.Implementations.Tests.Item;
+
+/// <summary>
+/// A command captured by <see cref="QueryPlanRecorder"/>.
+/// </summary>
+/// <param name="Sql">The command text.</param>
+/// <param name="Parameters">The bound parameters.</param>
+internal sealed record RecordedCommand(string Sql, (string Name, object? Value)[] Parameters);
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Item/SqliteDbTestFixture.cs b/tests/Jellyfin.Server.Implementations.Tests/Item/SqliteDbTestFixture.cs
index 6da176b4f1..6ab0d8e9ad 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Item/SqliteDbTestFixture.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Item/SqliteDbTestFixture.cs
@@ -9,6 +9,7 @@ using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Globalization;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
@@ -67,7 +68,7 @@ public abstract class SqliteDbTestFixture : IDisposable
return factory.Object;
}
- protected BaseItemRepository CreateBaseItemRepository(ItemTypeLookup itemTypeLookup)
+ protected BaseItemRepository CreateBaseItemRepository(ItemTypeLookup itemTypeLookup, ILocalizationManager? localizationManager = null)
{
var serverConfigurationManager = new Mock<IServerConfigurationManager>();
serverConfigurationManager.Setup(c => c.Configuration).Returns(new ServerConfiguration());
@@ -77,6 +78,7 @@ public abstract class SqliteDbTestFixture : IDisposable
new Mock<IServerApplicationHost>().Object,
itemTypeLookup,
serverConfigurationManager.Object,
+ localizationManager ?? Mock.Of<ILocalizationManager>(),
NullLogger<BaseItemRepository>.Instance);
}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/DotIgnoreIgnoreRuleTest.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/DotIgnoreIgnoreRuleTest.cs
index 03c0b4af39..94de2f7b23 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Library/DotIgnoreIgnoreRuleTest.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/DotIgnoreIgnoreRuleTest.cs
@@ -470,4 +470,105 @@ public class DotIgnoreIgnoreRuleTest
Directory.Delete(tempDir, true);
}
}
+
+ [Fact]
+ public void LibraryRoot_IgnoreFileAboveRoot_IsNotConsulted()
+ {
+ var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
+ var libraryRoot = Path.Combine(tempDir, "Movies");
+ var movieDir = Path.Combine(libraryRoot, "Movie (2020)");
+ Directory.CreateDirectory(movieDir);
+
+ try
+ {
+ File.WriteAllText(Path.Combine(tempDir, ".ignore"), string.Empty);
+
+ var rule = new DotIgnoreIgnoreRule();
+ var fileInfo = new FileSystemMetadata
+ {
+ FullName = Path.Combine(movieDir, "Movie (2020).mkv"),
+ IsDirectory = false
+ };
+
+ // Without library roots the walk reaches the filesystem root
+ Assert.True(rule.ShouldIgnore(fileInfo, null));
+
+ rule.SetLibraryRootsProvider(() => [libraryRoot + Path.DirectorySeparatorChar]);
+ Assert.False(rule.ShouldIgnore(fileInfo, null));
+
+ // A .ignore in the library root itself still applies
+ File.WriteAllText(Path.Combine(libraryRoot, ".ignore"), "*.mkv");
+ rule.ClearDirectoryCache();
+ Assert.True(rule.ShouldIgnore(fileInfo, null));
+ }
+ finally
+ {
+ Directory.Delete(tempDir, true);
+ }
+ }
+
+ [Fact]
+ public void LibraryRoot_NestedRoots_StopAtOutermost()
+ {
+ var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
+ var outerRoot = Path.Combine(tempDir, "Media");
+ var innerRoot = Path.Combine(outerRoot, "Movies");
+ Directory.CreateDirectory(innerRoot);
+
+ try
+ {
+ File.WriteAllText(Path.Combine(outerRoot, ".ignore"), "*.mkv");
+
+ var rule = new DotIgnoreIgnoreRule();
+ rule.SetLibraryRootsProvider(() => [innerRoot, outerRoot]);
+ var fileInfo = new FileSystemMetadata
+ {
+ FullName = Path.Combine(innerRoot, "Movie.mkv"),
+ IsDirectory = false
+ };
+
+ Assert.True(rule.ShouldIgnore(fileInfo, null));
+ }
+ finally
+ {
+ Directory.Delete(tempDir, true);
+ }
+ }
+
+ [Fact]
+ public void LibraryRoot_PathOutsideLibraries_WalksToFilesystemRoot()
+ {
+ var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
+ var libraryRoot = Path.Combine(tempDir, "Movies");
+ var otherDir = Path.Combine(tempDir, "Other", "Sub");
+ Directory.CreateDirectory(libraryRoot);
+ Directory.CreateDirectory(otherDir);
+
+ try
+ {
+ File.WriteAllText(Path.Combine(tempDir, ".ignore"), string.Empty);
+
+ var rule = new DotIgnoreIgnoreRule();
+ rule.SetLibraryRootsProvider(() => [libraryRoot]);
+
+ // A sibling whose name only shares the root's prefix is not inside the library
+ var fileInfo = new FileSystemMetadata
+ {
+ FullName = Path.Combine(tempDir, "MoviesExtra", "Movie.mkv"),
+ IsDirectory = false
+ };
+ Assert.True(rule.ShouldIgnore(fileInfo, null));
+
+ fileInfo = new FileSystemMetadata
+ {
+ FullName = Path.Combine(otherDir, "Movie.mkv"),
+ IsDirectory = false
+ };
+ Assert.True(rule.ShouldIgnore(fileInfo, null));
+ }
+ finally
+ {
+ Directory.Delete(tempDir, true);
+ }
+ }
}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs
new file mode 100644
index 0000000000..f37aadbe8d
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs
@@ -0,0 +1,93 @@
+using System;
+using System.IO;
+using AutoFixture;
+using AutoFixture.AutoMoq;
+using Emby.Naming.Common;
+using Emby.Server.Implementations.IO;
+using Emby.Server.Implementations.Library;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Sorting;
+using MediaBrowser.Model.IO;
+using Microsoft.Extensions.Logging.Abstractions;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.Library.LibraryManager;
+
+public sealed class LibraryRootIgnoreTests : IDisposable
+{
+ private readonly string _tempDir;
+ private readonly string _userViewsPath;
+ private readonly string _libraryRoot;
+ private readonly FileSystemMetadata _movieFile;
+
+ public LibraryRootIgnoreTests()
+ {
+ _tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
+ _userViewsPath = Path.Combine(_tempDir, "root", "default");
+ _libraryRoot = Path.Combine(_tempDir, "media", "Movies");
+ var movieDir = Path.Combine(_libraryRoot, "Movie (2020)");
+ Directory.CreateDirectory(movieDir);
+
+ // Ignores everything below it, but sits above the library root
+ File.WriteAllText(Path.Combine(_tempDir, ".ignore"), string.Empty);
+
+ _movieFile = new FileSystemMetadata
+ {
+ FullName = Path.Combine(movieDir, "Movie (2020).mkv"),
+ IsDirectory = false
+ };
+ }
+
+ public void Dispose()
+ {
+ Directory.Delete(_tempDir, true);
+ }
+
+ [Fact]
+ public void IgnoreFile_IgnoreFileAboveMblinkLibraryRoot_IsNotConsulted()
+ {
+ var libraryDir = Path.Combine(_userViewsPath, "Movies");
+ Directory.CreateDirectory(libraryDir);
+ File.WriteAllText(Path.Combine(libraryDir, "Movies.mblink"), _libraryRoot);
+
+ Assert.False(CreateLibraryManager().IgnoreFile(_movieFile, null));
+ }
+
+ [Fact]
+ public void IgnoreFile_UserViewsPathMissing_WalksToFilesystemRoot()
+ {
+ // Before root/default exists there are no library roots, so the .ignore above still applies
+ Assert.False(Directory.Exists(_userViewsPath));
+ Assert.True(CreateLibraryManager().IgnoreFile(_movieFile, null));
+ }
+
+ private Emby.Server.Implementations.Library.LibraryManager CreateLibraryManager()
+ {
+ var fixture = new Fixture().Customize(new AutoMoqCustomization());
+ fixture.Register(() => new NamingOptions());
+ var configMock = fixture.Freeze<Mock<IServerConfigurationManager>>();
+ configMock.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns(_tempDir);
+ configMock.Setup(c => c.ApplicationPaths.DefaultUserViewsPath).Returns(_userViewsPath);
+ fixture.Freeze<Mock<IServerApplicationHost>>()
+ .Setup(h => h.ExpandVirtualPath(It.IsAny<string>()))
+ .Returns<string>(path => path);
+ fixture.Inject<IFileSystem>(new ManagedFileSystem(
+ NullLogger<ManagedFileSystem>.Instance,
+ configMock.Object.ApplicationPaths,
+ [new MbLinkShortcutHandler()]));
+
+ // The same instance LibraryManager hands the roots to must be the one evaluating the rules
+ var dotIgnoreRule = new DotIgnoreIgnoreRule();
+ fixture.Inject(dotIgnoreRule);
+
+ return fixture.Build<Emby.Server.Implementations.Library.LibraryManager>().Do(s => s.AddParts(
+ [dotIgnoreRule],
+ [],
+ [],
+ Array.Empty<IBaseItemComparer>(),
+ []))
+ .Create();
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManagerConvertImageTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManagerConvertImageTests.cs
new file mode 100644
index 0000000000..d374847e54
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManagerConvertImageTests.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Net;
+using System.Threading;
+using System.Threading.Tasks;
+using AutoFixture;
+using AutoFixture.AutoMoq;
+using Emby.Naming.Common;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Movies;
+using MediaBrowser.Controller.Providers;
+using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Entities;
+using Moq;
+using Xunit;
+using ServerLibraryManager = Emby.Server.Implementations.Library.LibraryManager;
+
+namespace Jellyfin.Server.Implementations.Tests.Library;
+
+public class LibraryManagerConvertImageTests
+{
+ [Theory]
+ [InlineData(HttpStatusCode.NotFound)]
+ [InlineData(HttpStatusCode.Forbidden)]
+ public async Task ConvertImageToLocal_RejectedDownload_KeepsTheResponseAsInnerException(HttpStatusCode status)
+ {
+ // The Live TV guide reads the provider's error body off the inner exception to stop
+ // requesting images from an account that is refusing them.
+ const string Body = "{\"response\":\"MAX_IMAGE_DOWNLOADS\",\"code\":5002}";
+ var rejection = new HttpResponseBodyException("test", status, Body);
+
+ var fixture = new Fixture().Customize(new AutoMoqCustomization());
+ fixture.Register(() => new NamingOptions());
+ var configuration = fixture.Freeze<Mock<IServerConfigurationManager>>();
+ configuration.Setup(c => c.Configuration).Returns(new ServerConfiguration());
+ configuration.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns("/data");
+ var providerManager = fixture.Freeze<Mock<IProviderManager>>();
+ providerManager.Setup(p => p.SaveImage(It.IsAny<BaseItem>(), It.IsAny<string>(), It.IsAny<ImageType>(), It.IsAny<int?>(), It.IsAny<CancellationToken>()))
+ .ThrowsAsync(rejection);
+ var manager = fixture.Create<ServerLibraryManager>();
+
+ var item = new Movie { Id = Guid.NewGuid(), Name = "Test" };
+ var image = new ItemImageInfo { Path = "https://example.com/image.jpg", Type = ImageType.Primary };
+
+ var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => manager.ConvertImageToLocal(item, image, 0, removeOnFailure: false));
+
+ Assert.Same(rejection, ex.InnerException);
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/MediaSourceManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/MediaSourceManagerTests.cs
index 131cb23fa4..187d44559e 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Library/MediaSourceManagerTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/MediaSourceManagerTests.cs
@@ -151,6 +151,31 @@ namespace Jellyfin.Server.Implementations.Tests.Library
Assert.Equal(expectedIndex, mediaInfo.DefaultAudioStreamIndex);
}
+ [Fact]
+ public void SetDefaultAudioStreamIndex_VariantPreference_MatchesBaseCodeTrack()
+ {
+ _mockLocalizationManager
+ .Setup(m => m.FindLanguageInfo("pt-br"))
+ .Returns(new CultureDto("pt-br", "Portuguese (Brazil)", "pt-br", ["pt-br", "por"]));
+
+ var mediaInfo = new MediaSourceInfo
+ {
+ MediaStreams = new MediaStream[]
+ {
+ new() { Index = 0, Type = MediaStreamType.Video, IsDefault = true },
+ new() { Index = 1, Type = MediaStreamType.Audio, Language = "eng", IsDefault = true },
+ new() { Index = 2, Type = MediaStreamType.Audio, Language = "por", IsDefault = false }
+ }
+ };
+
+ _user.AudioLanguagePreference = "pt-br";
+ _user.PlayDefaultAudioTrack = false;
+
+ _mediaSourceManager.SetDefaultAudioAndSubtitleStreamIndices(_item, mediaInfo, _user);
+
+ Assert.Equal(2, mediaInfo.DefaultAudioStreamIndex);
+ }
+
[Theory]
// A remembered full track must not survive a switch to "only forced" (it falls through to
// the forced track here); a remembered forced track and "off" still must.
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs
index 07feae587b..3fc474816b 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs
@@ -1,5 +1,6 @@
using System;
using Emby.Server.Implementations.Library;
+using Jellyfin.Database.Implementations.Enums;
using MediaBrowser.Model.Entities;
using Xunit;
@@ -115,4 +116,18 @@ public class MediaStreamSelectorTests
Assert.Equal(expectedScore, MediaStreamSelector.GetStreamScore(stream, languagePref));
}
+
+ [Fact]
+ public void GetDefaultSubtitleStreamIndex_VariantPreference_PrefersVariantTrackOverBaseCodeTrack()
+ {
+ var streams = new MediaStream[]
+ {
+ new() { Index = 0, Type = MediaStreamType.Video },
+ new() { Index = 1, Type = MediaStreamType.Audio, Language = "eng", IsDefault = true },
+ new() { Index = 2, Type = MediaStreamType.Subtitle, Language = "por", IsExternal = true },
+ new() { Index = 3, Type = MediaStreamType.Subtitle, Language = "pt-br", IsExternal = true }
+ };
+
+ Assert.Equal(3, MediaStreamSelector.GetDefaultSubtitleStreamIndex(streams, ["pt-br", "por"], SubtitlePlaybackMode.Always, "eng"));
+ }
}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/QueryFiltersTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/QueryFiltersTests.cs
new file mode 100644
index 0000000000..72a20d7f98
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/QueryFiltersTests.cs
@@ -0,0 +1,534 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Security.Claims;
+using AutoFixture;
+using AutoFixture.AutoMoq;
+using Emby.Naming.Common;
+using Emby.Server.Implementations.Data;
+using Jellyfin.Api.Constants;
+using Jellyfin.Api.Controllers;
+using Jellyfin.Data;
+using Jellyfin.Database.Implementations.Entities;
+using Jellyfin.Database.Implementations.Enums;
+using Jellyfin.Server.Implementations.Tests.Item;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Persistence;
+using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Globalization;
+using MediaBrowser.Model.Library;
+using MediaBrowser.Model.Querying;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Moq;
+using Xunit;
+using BaseItemKind = Jellyfin.Data.Enums.BaseItemKind;
+using ServerLibraryManager = Emby.Server.Implementations.Library.LibraryManager;
+
+namespace Jellyfin.Server.Implementations.Tests.Library;
+
+public sealed class QueryFiltersTests : SqliteDbTestFixture
+{
+ private readonly DirectoryInfo _rootDirectory;
+ private readonly ItemTypeLookup _itemTypeLookup = new();
+ private readonly Mock<IUserViewManager> _userViewManager = new();
+ private readonly User _user = new("restricted", "auth", "reset")
+ {
+ MaxParentalRatingScore = 50
+ };
+
+ private readonly Guid _firstLibraryId = Guid.NewGuid();
+ private readonly Guid _secondLibraryId = Guid.NewGuid();
+ private readonly Guid _inaccessibleLibraryId = Guid.NewGuid();
+ private readonly Folder _folder = new() { Id = Guid.NewGuid() };
+ private readonly AggregateFolder _aggregateRoot = new() { Id = Guid.NewGuid() };
+ private readonly CollectionFolder _collection;
+ private readonly UserView _view;
+ private readonly UserRootFolder _userRoot;
+ private readonly FilterController _controller;
+
+ public QueryFiltersTests()
+ {
+ _rootDirectory = Directory.CreateTempSubdirectory("jellyfin-query-filters-");
+ _user.SetPreference(PreferenceKind.BlockedTags, ["blocked"]);
+
+ var fixture = new Fixture().Customize(new AutoMoqCustomization());
+ fixture.Register(() => new NamingOptions());
+ fixture.Inject<IItemRepository>(CreateBaseItemRepository(_itemTypeLookup));
+ fixture.Inject(new Lazy<IUserViewManager>(() => _userViewManager.Object));
+ var configuration = fixture.Freeze<Mock<IServerConfigurationManager>>();
+ configuration.Setup(c => c.Configuration).Returns(new ServerConfiguration());
+ configuration.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns(_rootDirectory.FullName);
+ configuration.Setup(c => c.ApplicationPaths.DefaultUserViewsPath).Returns(_rootDirectory.FullName);
+
+ var libraryManager = fixture.Create<ServerLibraryManager>();
+ _userRoot = new UserRootFolder
+ {
+ Id = libraryManager.GetNewItemId(_rootDirectory.FullName, typeof(UserRootFolder)),
+ Path = _rootDirectory.FullName
+ };
+ _collection = new CollectionFolder
+ {
+ Id = Guid.NewGuid(),
+ PhysicalFolderIds = [_firstLibraryId]
+ };
+ _view = new UserView
+ {
+ Id = Guid.NewGuid(),
+ DisplayParentId = _collection.Id
+ };
+ var secondCollection = new CollectionFolder
+ {
+ Id = Guid.NewGuid(),
+ PhysicalFolderIds = [_secondLibraryId]
+ };
+ libraryManager.RegisterItem(_userRoot);
+ libraryManager.RegisterItem(_aggregateRoot);
+ libraryManager.RegisterItem(_collection);
+ libraryManager.RegisterItem(_view);
+ libraryManager.RegisterItem(_folder);
+
+ _userViewManager.Setup(m => m.GetUserViews(It.Is<UserViewQuery>(q => q.User == _user && q.IncludeHidden)))
+ .Returns([_collection, secondCollection]);
+ var userManager = fixture.Freeze<Mock<IUserManager>>();
+ userManager.Setup(m => m.GetUserById(_user.Id)).Returns(_user);
+ _controller = new FilterController(libraryManager, userManager.Object, Mock.Of<ILocalizationManager>())
+ {
+ ControllerContext = new ControllerContext
+ {
+ HttpContext = new DefaultHttpContext
+ {
+ User = new ClaimsPrincipal(new ClaimsIdentity(
+ [new Claim(InternalClaimTypes.UserId, _user.Id.ToString("N"))]))
+ }
+ }
+ };
+
+ using var context = CreateDbContext();
+ foreach (var id in new[] { _firstLibraryId, _secondLibraryId, _inaccessibleLibraryId, _folder.Id, _userRoot.Id, _aggregateRoot.Id })
+ {
+ context.BaseItems.Add(new BaseItemEntity
+ {
+ Id = id,
+ Type = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Folder],
+ IsFolder = true
+ });
+ }
+
+ context.SaveChanges();
+
+ SeedItems(BaseItemKind.Movie, 0, "PG", "G", "PG-13");
+ SeedItems(BaseItemKind.Series, 1, "TV-PG", "TV-G", "TV-14");
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie, null)]
+ [InlineData(BaseItemKind.Series, null)]
+ [InlineData(BaseItemKind.Movie, "library")]
+ [InlineData(BaseItemKind.Series, "library")]
+ [InlineData(BaseItemKind.Movie, "view")]
+ [InlineData(BaseItemKind.Series, "view")]
+ [InlineData(BaseItemKind.Movie, "folder")]
+ [InlineData(BaseItemKind.Series, "folder")]
+ [InlineData(BaseItemKind.Movie, "userRoot")]
+ [InlineData(BaseItemKind.Series, "userRoot")]
+ [InlineData(BaseItemKind.Movie, "aggregateRoot")]
+ [InlineData(BaseItemKind.Series, "aggregateRoot")]
+ public void GetQueryFiltersLegacy_ReturnsAccessibleYearsAndRatings(BaseItemKind itemType, string? parent)
+ {
+ // A missing userId uses the authenticated user's access restrictions.
+ var result = _controller.GetQueryFiltersLegacy(null, GetParentId(parent), [itemType], []);
+ var filters = Assert.IsType<QueryFiltersLegacy>(result.Value);
+
+ AssertYearsAndRatings(itemType, parent, filters.Years, filters.OfficialRatings);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie)]
+ [InlineData(BaseItemKind.Series)]
+ public void GetQueryFiltersLegacy_NoAccessibleLibraries_ReturnsEmptyFilters(BaseItemKind itemType)
+ {
+ _userViewManager.Setup(m => m.GetUserViews(It.IsAny<UserViewQuery>())).Returns([]);
+
+ var result = _controller.GetQueryFiltersLegacy(null, null, [itemType], []);
+ var filters = Assert.IsType<QueryFiltersLegacy>(result.Value);
+
+ Assert.Empty(filters.Years);
+ Assert.Empty(filters.OfficialRatings);
+ Assert.Empty(filters.Tags);
+ Assert.Empty(filters.Genres);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie, null, true)]
+ [InlineData(BaseItemKind.Series, null, true)]
+ [InlineData(BaseItemKind.Movie, null, false)]
+ [InlineData(BaseItemKind.Series, null, false)]
+ [InlineData(BaseItemKind.Movie, "library", true)]
+ [InlineData(BaseItemKind.Series, "library", true)]
+ [InlineData(BaseItemKind.Movie, "library", false)]
+ [InlineData(BaseItemKind.Series, "library", false)]
+ [InlineData(BaseItemKind.Movie, "view", true)]
+ [InlineData(BaseItemKind.Series, "view", true)]
+ [InlineData(BaseItemKind.Movie, "view", false)]
+ [InlineData(BaseItemKind.Series, "view", false)]
+ [InlineData(BaseItemKind.Movie, "folder", true)]
+ [InlineData(BaseItemKind.Series, "folder", true)]
+ [InlineData(BaseItemKind.Movie, "folder", false)]
+ [InlineData(BaseItemKind.Series, "folder", false)]
+ [InlineData(BaseItemKind.Movie, "userRoot", true)]
+ [InlineData(BaseItemKind.Series, "userRoot", true)]
+ [InlineData(BaseItemKind.Movie, "aggregateRoot", true)]
+ [InlineData(BaseItemKind.Series, "aggregateRoot", true)]
+ public void GetQueryFilters_ReturnsAccessibleYearsAndRatings(BaseItemKind itemType, string? parent, bool recursive)
+ {
+ var filters = GetFilters(itemType, GetParentId(parent), recursive);
+
+ AssertYearsAndRatings(itemType, parent, filters.Years, filters.OfficialRatings);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie, true)]
+ [InlineData(BaseItemKind.Series, true)]
+ [InlineData(BaseItemKind.Movie, false)]
+ [InlineData(BaseItemKind.Series, false)]
+ public void GetQueryFilters_Recursive_ControlsWhetherDescendantsContribute(BaseItemKind itemType, bool recursive)
+ {
+ using var context = CreateDbContext();
+ var nestedFolder = new BaseItemEntity
+ {
+ Id = Guid.NewGuid(),
+ Type = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Folder],
+ IsFolder = true,
+ ParentId = _folder.Id,
+ TopParentId = _firstLibraryId
+ };
+ context.BaseItems.Add(nestedFolder);
+ context.SaveChanges();
+ var descendantId = AddItem(itemType, _firstLibraryId, nestedFolder.Id, 2019, "Descendant");
+ context.AncestorIds.Add(new AncestorId
+ {
+ ItemId = descendantId,
+ Item = context.BaseItems.Find(descendantId)!,
+ ParentItemId = _folder.Id,
+ ParentItem = context.BaseItems.Find(_folder.Id)!
+ });
+ context.SaveChanges();
+
+ var filters = GetFilters(itemType, _folder.Id, recursive);
+ var firstYear = itemType == BaseItemKind.Movie ? 2001 : 2002;
+ var firstRating = itemType == BaseItemKind.Movie ? "PG" : "TV-PG";
+
+ Assert.Equal(recursive ? [firstYear, 2019] : new[] { firstYear }, filters.Years);
+ Assert.Equal(recursive ? ["Descendant", firstRating] : new[] { firstRating }, filters.OfficialRatings);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie, false)]
+ [InlineData(BaseItemKind.Series, false)]
+ [InlineData(BaseItemKind.Movie, true)]
+ [InlineData(BaseItemKind.Series, true)]
+ public void GetQueryFilters_MissingMetadata_DoesNotExcludeTheOtherField(BaseItemKind itemType, bool withParent)
+ {
+ using (var context = CreateDbContext())
+ {
+ foreach (var item in context.BaseItems)
+ {
+ item.ProductionYear = null;
+ item.OfficialRating = null;
+ }
+
+ context.SaveChanges();
+ }
+
+ AddItem(itemType, _firstLibraryId, _folder.Id, null, "PG");
+ AddItem(itemType, _firstLibraryId, _folder.Id, 2019, null);
+ AddItem(itemType, _firstLibraryId, _folder.Id, 2019, string.Empty);
+ AddItem(itemType, _firstLibraryId, _folder.Id, -1, "PG");
+
+ var filters = GetFilters(itemType, withParent ? _collection.Id : null);
+
+ Assert.Equal([2019], filters.Years);
+ Assert.Equal(["PG"], filters.OfficialRatings);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie, false)]
+ [InlineData(BaseItemKind.Series, false)]
+ [InlineData(BaseItemKind.Movie, true)]
+ [InlineData(BaseItemKind.Series, true)]
+ public void GetQueryFilters_AllowedTags_OnlyIncludesAllowedItems(BaseItemKind itemType, bool withParent)
+ {
+ _user.SetPreference(PreferenceKind.AllowedTags, ["allowed"]);
+ var allowedId = AddItem(itemType, _firstLibraryId, _folder.Id, 2017, "Allowed");
+ AddItemValue(allowedId, ItemValueType.Tags, "allowed");
+ var blockedId = AddItem(itemType, _firstLibraryId, _folder.Id, 2018, "Blocked", blockedTag: true);
+ AddItemValue(blockedId, ItemValueType.Tags, "allowed");
+
+ var filters = GetFilters(itemType, withParent ? _collection.Id : null);
+
+ Assert.Equal([2017], filters.Years);
+ Assert.Equal(["Allowed"], filters.OfficialRatings);
+ Assert.Equal(["allowed"], filters.Tags);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie)]
+ [InlineData(BaseItemKind.Series)]
+ public void GetQueryFilters_NoAccessibleLibraries_ReturnsEmptyFilters(BaseItemKind itemType)
+ {
+ _userViewManager.Setup(m => m.GetUserViews(It.IsAny<UserViewQuery>())).Returns([]);
+
+ var filters = GetFilters(itemType);
+
+ Assert.Empty(filters.Years);
+ Assert.Empty(filters.OfficialRatings);
+ Assert.Empty(filters.Genres);
+ Assert.Empty(filters.Tags);
+ Assert.Empty(filters.AudioLanguages);
+ Assert.Empty(filters.SubtitleLanguages);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie, false)]
+ [InlineData(BaseItemKind.Series, false)]
+ [InlineData(BaseItemKind.Movie, true)]
+ [InlineData(BaseItemKind.Series, true)]
+ public void GetQueryFilters_IsMovie_UsesTheSameFilterForMetadata(BaseItemKind itemType, bool withParent)
+ {
+ var result = _controller.GetQueryFilters(
+ userId: null,
+ parentId: withParent ? _collection.Id : null,
+ includeItemTypes: [itemType],
+ isAiring: null,
+ isMovie: itemType != BaseItemKind.Movie,
+ isSports: null,
+ isKids: null,
+ isNews: null,
+ isSeries: null,
+ recursive: null);
+ var filters = Assert.IsType<QueryFilters>(result.Value);
+
+ Assert.Empty(filters.Years);
+ Assert.Empty(filters.OfficialRatings);
+ }
+
+ [Theory]
+ [InlineData(BaseItemKind.Movie, false)]
+ [InlineData(BaseItemKind.Series, false)]
+ [InlineData(BaseItemKind.Movie, true)]
+ [InlineData(BaseItemKind.Series, true)]
+ public void GetQueryFilters_PreservesGenresTagsAndLanguages(BaseItemKind itemType, bool withParent)
+ {
+ var isMovie = itemType == BaseItemKind.Movie;
+ var visibleId = AddItem(itemType, _firstLibraryId, _folder.Id, isMovie ? 2001 : 2002, isMovie ? "PG" : "TV-PG");
+ AddItemValue(visibleId, ItemValueType.Tags, "visible");
+ AddItemValue(visibleId, ItemValueType.Genre, "Drama");
+ var hiddenId = AddItem(itemType, _inaccessibleLibraryId, _inaccessibleLibraryId, 2099, "Hidden");
+ AddItemValue(hiddenId, ItemValueType.Tags, "hidden");
+ AddItemValue(hiddenId, ItemValueType.Genre, "Horror");
+
+ // Languages come from episodes; years and ratings come from the series.
+ if (!isMovie)
+ {
+ visibleId = AddItem(BaseItemKind.Episode, _firstLibraryId, visibleId, 2088, "Episode rating");
+ hiddenId = AddItem(BaseItemKind.Episode, _inaccessibleLibraryId, hiddenId, 2089, "Hidden episode rating");
+ }
+
+ AddLanguages(visibleId, "eng", "spa");
+ AddLanguages(hiddenId, "ger", "fre");
+ using (var context = CreateDbContext())
+ {
+ foreach (var name in new[] { "Drama", "Horror" })
+ {
+ var id = Guid.NewGuid();
+ context.BaseItems.Add(new BaseItemEntity
+ {
+ Id = id,
+ Type = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Genre],
+ Name = name,
+ CleanName = name.ToLowerInvariant(),
+ PresentationUniqueKey = id.ToString("N")
+ });
+ }
+
+ context.SaveChanges();
+ }
+
+ var filters = GetFilters(itemType, withParent ? _collection.Id : null);
+
+ Assert.Equal(["Drama"], filters.Genres.Select(g => g.Name));
+ Assert.NotEqual(Guid.Empty, Assert.Single(filters.Genres).Id);
+ Assert.Equal(["visible"], filters.Tags);
+ var audioLanguage = Assert.Single(filters.AudioLanguages);
+ Assert.Equal("eng", audioLanguage.Value);
+ Assert.Equal("eng", audioLanguage.Name);
+ var subtitleLanguage = Assert.Single(filters.SubtitleLanguages);
+ Assert.Equal("spa", subtitleLanguage.Value);
+ Assert.Equal("spa", subtitleLanguage.Name);
+ AssertYearsAndRatings(itemType, withParent ? "library" : null, filters.Years, filters.OfficialRatings);
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ base.Dispose(disposing);
+ if (disposing)
+ {
+ _rootDirectory.Delete(true);
+ }
+ }
+
+ private QueryFilters GetFilters(BaseItemKind itemType, Guid? parentId = null, bool? recursive = null)
+ {
+ var result = _controller.GetQueryFilters(
+ userId: null,
+ parentId: parentId,
+ includeItemTypes: [itemType],
+ isAiring: null,
+ isMovie: null,
+ isSports: null,
+ isKids: null,
+ isNews: null,
+ isSeries: null,
+ recursive: recursive);
+ return Assert.IsType<QueryFilters>(result.Value);
+ }
+
+ private Guid? GetParentId(string? parent)
+ {
+ return parent switch
+ {
+ null => null,
+ "library" => _collection.Id,
+ "view" => _view.Id,
+ "folder" => _folder.Id,
+ "userRoot" => _userRoot.Id,
+ "aggregateRoot" => _aggregateRoot.Id,
+ _ => throw new ArgumentOutOfRangeException(nameof(parent))
+ };
+ }
+
+ private static void AssertYearsAndRatings(BaseItemKind itemType, string? parent, IReadOnlyList<int> years, IReadOnlyList<string> officialRatings)
+ {
+ var offset = itemType == BaseItemKind.Movie ? 0 : 1;
+ int[] expectedYears = parent switch
+ {
+ "folder" => [2001 + offset],
+ "library" or "view" => [2001 + offset, 2005 + offset],
+ _ => [2001 + offset, 2005 + offset, 2011 + offset]
+ };
+ string[] ratings = itemType == BaseItemKind.Movie ? ["PG", "G", "PG-13"] : ["TV-PG", "TV-G", "TV-14"];
+
+ Assert.Equal(expectedYears, years);
+ Assert.Equal(ratings.Take(expectedYears.Length).Order(StringComparer.Ordinal), officialRatings);
+ }
+
+ private void SeedItems(BaseItemKind kind, int yearOffset, string firstRating, string siblingRating, string secondRating)
+ {
+ // Unsorted, duplicate, and missing values exercise metadata aggregation.
+ AddItem(kind, _secondLibraryId, _secondLibraryId, 2011 + yearOffset, secondRating);
+ AddItem(kind, _firstLibraryId, _firstLibraryId, 2005 + yearOffset, siblingRating);
+ AddItem(kind, _firstLibraryId, _folder.Id, 2001 + yearOffset, firstRating);
+ AddItem(kind, _firstLibraryId, _folder.Id, 2001 + yearOffset, firstRating);
+ AddItem(kind, _firstLibraryId, _folder.Id, null, null);
+ AddItem(kind, _firstLibraryId, _folder.Id, 0, string.Empty);
+
+ // Distinct metadata reveals any restricted items in the response.
+ AddItem(kind, _inaccessibleLibraryId, _inaccessibleLibraryId, 1991 + yearOffset, "Inaccessible library");
+ AddItem(kind, _firstLibraryId, _folder.Id, 2021 + yearOffset, "Above parental limit", parentalRating: 100);
+ AddItem(kind, _firstLibraryId, _folder.Id, 2031 + yearOffset, "Blocked tag", blockedTag: true);
+ }
+
+ private Guid AddItem(BaseItemKind kind, Guid libraryId, Guid parentId, int? year, string? rating, int parentalRating = 10, bool blockedTag = false)
+ {
+ using var context = CreateDbContext();
+ var item = new BaseItemEntity
+ {
+ Id = Guid.NewGuid(),
+ Type = _itemTypeLookup.BaseItemKindNames[kind],
+ MediaType = "Video",
+ IsFolder = kind == BaseItemKind.Series,
+ IsMovie = kind == BaseItemKind.Movie,
+ IsVirtualItem = false,
+ TopParentId = libraryId,
+ ParentId = parentId,
+ ProductionYear = year,
+ OfficialRating = rating,
+ InheritedParentalRatingValue = parentalRating
+ };
+ context.BaseItems.Add(item);
+
+ // Virtual roots and collection views are not stored ancestors.
+ foreach (var ancestorId in new[] { libraryId, parentId }.Distinct())
+ {
+ context.AncestorIds.Add(new AncestorId
+ {
+ ItemId = item.Id,
+ Item = item,
+ ParentItemId = ancestorId,
+ ParentItem = context.BaseItems.Find(ancestorId)!
+ });
+ }
+
+ context.SaveChanges();
+ if (blockedTag)
+ {
+ AddItemValue(item.Id, ItemValueType.Tags, "blocked");
+ }
+
+ return item.Id;
+ }
+
+ private void AddItemValue(Guid itemId, ItemValueType type, string value)
+ {
+ using var context = CreateDbContext();
+ var itemValue = context.ItemValues.SingleOrDefault(t => t.Type == type && t.Value == value);
+ if (itemValue is null)
+ {
+ itemValue = new ItemValue
+ {
+ ItemValueId = Guid.NewGuid(),
+ Type = type,
+ Value = value,
+ CleanValue = value.ToLowerInvariant()
+ };
+ context.ItemValues.Add(itemValue);
+ }
+
+ context.ItemValuesMap.Add(new ItemValueMap
+ {
+ ItemId = itemId,
+ ItemValueId = itemValue.ItemValueId,
+ Item = context.BaseItems.Find(itemId)!,
+ ItemValue = itemValue
+ });
+ context.SaveChanges();
+ }
+
+ private void AddLanguages(Guid itemId, string audioLanguage, string subtitleLanguage)
+ {
+ using var context = CreateDbContext();
+ var item = context.BaseItems.Find(itemId)!;
+ context.MediaStreamInfos.AddRange(
+ new MediaStreamInfo
+ {
+ ItemId = itemId,
+ Item = item,
+ StreamIndex = 0,
+ StreamType = MediaStreamTypeEntity.Audio,
+ Language = audioLanguage
+ },
+ new MediaStreamInfo
+ {
+ ItemId = itemId,
+ Item = item,
+ StreamIndex = 1,
+ StreamType = MediaStreamTypeEntity.Subtitle,
+ Language = subtitleLanguage
+ });
+ context.SaveChanges();
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/SqlSearchProviderTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/SqlSearchProviderTests.cs
index 89ca037256..eefde9bed6 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Library/SqlSearchProviderTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/SqlSearchProviderTests.cs
@@ -32,12 +32,24 @@ public sealed class SqlSearchProviderTests : SqliteDbTestFixture
private static readonly Guid _primaryId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc");
private static readonly Guid _versionId = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd");
+ // A movie nested one folder below the library, so only its AncestorIds rows reach the library.
+ private static readonly Guid _franchiseFolderId = Guid.Parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee");
+ private static readonly Guid _nestedMovieId = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff");
+
+ private readonly QueryPlanRecorder _recorder;
private readonly SqlSearchProvider _provider;
private readonly Mock<ILibraryManager> _libraryManager = new();
private readonly User _user = new("test", "auth-provider", "reset-provider");
public SqlSearchProviderTests()
+ : this(new QueryPlanRecorder())
{
+ }
+
+ private SqlSearchProviderTests(QueryPlanRecorder recorder)
+ : base(recorder)
+ {
+ _recorder = recorder;
var itemTypeLookup = new ItemTypeLookup();
var movieTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Movie]!;
var folderTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Folder]!;
@@ -49,6 +61,15 @@ public sealed class SqlSearchProviderTests : SqliteDbTestFixture
context.BaseItems.Add(CreateLibrary(_movie4KLibraryId, folderTypeName, "Movies-4K", "/movies-4k"));
context.BaseItems.Add(CreateMovie(_primaryId, movieTypeName, _movie4KLibraryId, null));
context.BaseItems.Add(CreateMovie(_versionId, movieTypeName, _movieLibraryId, _primaryId));
+ context.BaseItems.Add(CreateLibrary(_franchiseFolderId, folderTypeName, "Franchise", "/movies/franchise"));
+ var nestedMovie = CreateMovie(_nestedMovieId, movieTypeName, _movieLibraryId, null);
+ nestedMovie.Name = "Moana";
+ nestedMovie.CleanName = "moana";
+ nestedMovie.SortName = "Moana";
+ nestedMovie.ParentId = _franchiseFolderId;
+ context.BaseItems.Add(nestedMovie);
+ context.AncestorIds.Add(new AncestorId { ItemId = _nestedMovieId, ParentItemId = _franchiseFolderId, Item = null!, ParentItem = null! });
+ context.AncestorIds.Add(new AncestorId { ItemId = _nestedMovieId, ParentItemId = _movieLibraryId, Item = null!, ParentItem = null! });
context.SaveChanges();
}
@@ -88,6 +109,31 @@ public sealed class SqlSearchProviderTests : SqliteDbTestFixture
Assert.Equal([_primaryId], hits);
}
+ [Fact]
+ public async Task SearchAsync_WithParent_FindsItemsNestedBelowIt()
+ {
+ RestrictUserTo(_movieLibraryId);
+
+ Assert.Equal([_nestedMovieId], await SearchAsync("moana", _movieLibraryId).ConfigureAwait(true));
+ Assert.Equal([_nestedMovieId], await SearchAsync("moana", _franchiseFolderId).ConfigureAwait(true));
+ Assert.Empty(await SearchAsync("moana", _movie4KLibraryId).ConfigureAwait(true));
+ }
+
+ [Fact]
+ public async Task SearchAsync_WithParent_SeeksDescendantsByParent()
+ {
+ RestrictUserTo(_movieLibraryId);
+ _recorder.Commands.Clear();
+
+ await SearchAsync("moana", _franchiseFolderId).ConfigureAwait(true);
+
+ var query = Assert.Single(_recorder.Commands, c => c.Sql.Contains("\"AncestorIds\"", StringComparison.Ordinal));
+ // The ParentId alternative keeps BaseItems as the driver; the descendants become a list built once, not a per-row probe.
+ var plan = Explain(query);
+ Assert.Contains(plan, line => line.Contains("COVERING INDEX IX_AncestorIds_ParentItemId_ItemId (ParentItemId=?)", StringComparison.Ordinal));
+ Assert.DoesNotContain(plan, line => line.Contains("sqlite_autoindex_AncestorIds_1", StringComparison.Ordinal));
+ }
+
private void RestrictUserTo(params Guid[] libraryIds)
{
_libraryManager
@@ -95,10 +141,10 @@ public sealed class SqlSearchProviderTests : SqliteDbTestFixture
.Callback<InternalItemsQuery, User>((query, _) => query.TopParentIds = libraryIds);
}
- private async Task<List<Guid>> SearchAsync()
+ private async Task<List<Guid>> SearchAsync(string searchTerm = "coco", Guid? parentId = null)
{
var results = await _provider.SearchAsync(
- new SearchProviderQuery { SearchTerm = "coco", UserId = _user.Id, Limit = 10 },
+ new SearchProviderQuery { SearchTerm = searchTerm, UserId = _user.Id, ParentId = parentId, Limit = 10 },
CancellationToken.None).ConfigureAwait(false);
return results.Select(r => r.ItemId).ToList();
@@ -131,4 +177,10 @@ public sealed class SqlSearchProviderTests : SqliteDbTestFixture
PresentationUniqueKey = (primaryVersionId ?? id).ToString("N"),
PrimaryVersionId = primaryVersionId
};
+
+ private string[] Explain(RecordedCommand query)
+ {
+ using var context = CreateDbContext();
+ return QueryPlanRecorder.Explain(context, query);
+ }
}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs
index 4e57f32b57..ecf8e9b0f2 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs
@@ -6,6 +6,7 @@ using BitFaster.Caching;
using Emby.Server.Implementations.Localization;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Globalization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
@@ -102,6 +103,80 @@ namespace Jellyfin.Server.Implementations.Tests.Localization
}
[Theory]
+ // Region qualified variants must stay distinguishable by ThreeLetterISOLanguageName,
+ // which is what clients use as the value of their language pickers.
+ [InlineData("pt", "por", "Portuguese")]
+ [InlineData("pt-br", "pt-br", "Portuguese (Brazil)")]
+ [InlineData("pob", "pt-br", "Portuguese (Brazil)")]
+ [InlineData("pt-pt", "pt-pt", "Portuguese (Portugal)")]
+ [InlineData("pop", "pt-pt", "Portuguese (Portugal)")]
+ [InlineData("es", "spa", "Spanish; Castilian")]
+ [InlineData("spa", "spa", "Spanish; Castilian")]
+ [InlineData("es-419", "es-419", "Spanish; Latin")]
+ [InlineData("es-mx", "es-419", "Spanish; Latin")]
+ [InlineData("zh", "zho", "Chinese")]
+ [InlineData("ze", "ze", "Chinese (Bilingual)")]
+ [InlineData("zh-cn", "zh-cn", "Chinese (Simplified)")]
+ [InlineData("zh-tw", "zh-tw", "Chinese (Traditional)")]
+ public async Task FindLanguageInfo_RegionalVariant_KeepsDistinctCode(string identifier, string expectedCode, string expectedDisplayName)
+ {
+ var localizationManager = Setup(new ServerConfiguration
+ {
+ UICulture = "en-US"
+ });
+ await localizationManager.LoadAll();
+
+ var culture = localizationManager.FindLanguageInfo(identifier);
+ Assert.NotNull(culture);
+ Assert.Equal(expectedCode, culture.ThreeLetterISOLanguageName);
+ Assert.Equal(expectedDisplayName, culture.DisplayName);
+ }
+
+ [Theory]
+ // Stream tags carry either the variant's own tag (movie.pt-br.srt) or the base code ffprobe reports.
+ [InlineData("pt-br", new[] { "pt-br", "por" })]
+ [InlineData("por", new[] { "por" })]
+ [InlineData("ger", new[] { "deu", "ger" })]
+ [InlineData("zh-tw", new[] { "zh-tw", "zho", "chi" })]
+ [InlineData("fr-ca", new[] { "fr-ca", "frc", "fra", "fre" })]
+ // Streams scanned before 12.0 keep their legacy code until the item is refreshed.
+ [InlineData("pob", new[] { "pt-br", "por", "pob" })]
+ // A display name with a hyphen does not make a culture a variant.
+ [InlineData("ell", new[] { "ell", "gre" })]
+ [InlineData("UND", new[] { "und" })]
+ [InlineData("xyz", new[] { "xyz" })]
+ public async Task GetMediaStreamLanguageCodes_Variant_IncludesBaseCode(string identifier, string[] expected)
+ {
+ var localizationManager = Setup(new ServerConfiguration
+ {
+ UICulture = "en-US"
+ });
+ await localizationManager.LoadAll();
+
+ Assert.Equal(expected, localizationManager.GetMediaStreamLanguageCodes(identifier));
+ }
+
+ [Fact]
+ public async Task GetCultures_RegionalVariants_HaveUniqueThreeLetterCodes()
+ {
+ var localizationManager = Setup(new ServerConfiguration
+ {
+ UICulture = "en-US"
+ });
+ await localizationManager.LoadAll();
+
+ // Mirrors what LocalizationController serves and what clients use as picker values.
+ var duplicates = localizationManager.GetCultures()
+ .DistinctBy(c => c.DisplayName, StringComparer.OrdinalIgnoreCase)
+ .GroupBy(c => c.ThreeLetterISOLanguageName, StringComparer.OrdinalIgnoreCase)
+ .Where(g => g.Count() > 1)
+ .Select(g => $"{g.Key}: {string.Join(", ", g.Select(c => c.DisplayName))}")
+ .ToList();
+
+ Assert.Empty(duplicates);
+ }
+
+ [Theory]
[InlineData("mul", "Multiple languages")]
[InlineData("und", "Undetermined")]
[InlineData("mis", "Uncoded languages")]
diff --git a/tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs b/tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs
new file mode 100644
index 0000000000..94672d5c7b
--- /dev/null
+++ b/tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs
@@ -0,0 +1,248 @@
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.Http;
+using System.Net.Mime;
+using System.Reflection;
+using System.Security.Claims;
+using System.Text;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using System.Threading.Tasks;
+using Jellyfin.Extensions.Json;
+using Jellyfin.Server.Extensions;
+using MediaBrowser.Common.Api;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.LiveTv;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.TestHost;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.Server.Integration.Tests.Controllers;
+
+public sealed class ConfigurationControllerTests : IDisposable
+{
+ private readonly Dictionary<string, object> _configurations = new()
+ {
+ [string.Empty] = new ServerConfiguration(),
+ ["encoding"] = new EncodingOptions(),
+ ["network"] = new NetworkConfiguration(),
+ ["metadata"] = new MetadataConfiguration(),
+ ["xbmcmetadata"] = new XbmcMetadataOptions(),
+ ["livetv"] = new LiveTvOptions()
+ };
+
+ private readonly Mock<IServerConfigurationManager> _configurationManager = new();
+ private readonly IHost _host;
+ private readonly HttpClient _client;
+
+ public ConfigurationControllerTests()
+ {
+ _configurationManager.Setup(manager => manager.Configuration)
+ .Returns(() => (ServerConfiguration)_configurations[string.Empty]);
+ _configurationManager.Setup(manager => manager.ReplaceConfiguration(It.IsAny<BaseApplicationConfiguration>()))
+ .Callback<BaseApplicationConfiguration>(configuration => _configurations[string.Empty] = configuration);
+ _configurationManager.Setup(manager => manager.GetConfiguration(It.IsAny<string>()))
+ .Returns((string key) => _configurations[key]);
+ _configurationManager.Setup(manager => manager.SaveConfiguration(It.IsAny<string>(), It.IsAny<object>()))
+ .Callback<string, object>((key, configuration) => _configurations[key] = configuration);
+
+ // Use the real API setup, but keep config in memory for these tests.
+ _host = new HostBuilder().ConfigureWebHost(webHost => webHost
+ .UseTestServer()
+ .ConfigureServices(services =>
+ {
+ services.AddSingleton(_configurationManager.Object);
+ services.AddAuthentication();
+ services.AddAuthorization(options =>
+ {
+ options.AddPolicy(Policies.RequiresElevation, policy => policy.RequireAuthenticatedUser());
+ });
+ services.AddJellyfinApi(Array.Empty<Assembly>(), new NetworkConfiguration());
+ })
+ .Configure(app =>
+ {
+ app.UseRouting();
+ app.Use((context, next) =>
+ {
+ context.User = new ClaimsPrincipal(new ClaimsIdentity("Test"));
+ return next(context);
+ });
+ app.UseAuthorization();
+ app.UseEndpoints(endpoints => endpoints.MapControllers());
+ })).Start();
+ _client = _host.GetTestClient();
+ }
+
+ [Theory]
+ [InlineData("")]
+ [InlineData("encoding")]
+ [InlineData("network")]
+ [InlineData("metadata")]
+ [InlineData("xbmcmetadata")]
+ [InlineData("livetv")]
+ public async Task UpdateConfiguration_EmptyRequest_UsesInternalDefaults(string key)
+ {
+ await AssertMatchesLegacyDeserialization(key, "{}");
+ }
+
+ [Theory]
+ [InlineData("", """
+ {"ActivityLogRetentionDays":null,"QuickConnectAvailable":false,"MinResumePct":0,
+ "SortRemoveWords":[],"TrickplayOptions":{"EnableHwAcceleration":true}}
+ """)]
+ [InlineData("encoding", """
+ {"EnableHardwareEncoding":false,"EncodingThreadCount":0,"HardwareDecodingCodecs":[],
+ "H264Crf":"19","EncoderPreset":"slow","VaapiDevice":null,"UnknownOption":true}
+ """)]
+ [InlineData("network", """
+ {"CertificatePath":null,"CertificatePassword":null,"InternalHttpPort":"8097",
+ "EnableRemoteAccess":false,"BaseUrl":"jellyfin/","KnownProxies":[]}
+ """)]
+ [InlineData("network", """
+ {"BaseUrl":"","LocalNetworkSubnets":[],"LocalNetworkAddresses":[],"KnownProxies":[],
+ "VirtualInterfaceNames":[],"PublishedServerUriBySubnet":[],"RemoteIPFilter":[]}
+ """)]
+ [InlineData("metadata", """{"UseFileCreationTimeForDateAdded":false}""")]
+ [InlineData("xbmcmetadata", """{"UserId":"","ReleaseDateFormat":"yyyy-MM-dd","SaveImagePathsInNfo":false}""")]
+ [InlineData("livetv", """
+ {"GuideDays":"","RecordingPath":null,"SaveRecordingNFO":false,
+ "TunerHosts":[{"Url":"http://tuner"}],"ListingProviders":[{"Type":"XmlTv"}]}
+ """)]
+ [InlineData("livetv", """{"TunerHosts":[],"ListingProviders":[],"MediaLocationsCreated":[]}""")]
+ public async Task UpdateConfiguration_SuppliedProperties_MatchLegacyDeserialization(string key, string json)
+ {
+ await AssertMatchesLegacyDeserialization(key, json);
+ }
+
+ [Theory]
+ [InlineData("", "ServerName", "\"My server\"")]
+ [InlineData("encoding", "H264Crf", "18")]
+ [InlineData("network", "InternalHttpPort", "8097")]
+ [InlineData("metadata", "UseFileCreationTimeForDateAdded", "false")]
+ [InlineData("xbmcmetadata", "SaveImagePathsInNfo", "false")]
+ [InlineData("livetv", "GuideDays", "7")]
+ public async Task GetAndUpdateConfiguration_LowercaseRoute_RoundTripsConfiguration(string key, string property, string value)
+ {
+ using var response = await _client.GetAsync(GetUrl(key), TestContext.Current.CancellationToken);
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
+ Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType);
+
+ var body = JsonNode.Parse(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken))!.AsObject();
+ var expected = JsonSerializer.SerializeToNode(_configurations[key], JsonDefaults.Options)!.AsObject();
+ Assert.True(JsonNode.DeepEquals(expected, body));
+
+ body[property] = JsonNode.Parse(value);
+ expected[property] = JsonNode.Parse(value);
+ await PostConfiguration(key, body.ToJsonString());
+
+ Assert.True(JsonNode.DeepEquals(expected, JsonSerializer.SerializeToNode(_configurations[key], JsonDefaults.Options)));
+ }
+
+ [Fact]
+ public async Task UpdateConfiguration_CamelCaseProperties_AreApplied()
+ {
+ await PostConfiguration("encoding", """{"h264Crf":18,"enableHardwareEncoding":false}""");
+
+ var configuration = Assert.IsType<EncodingOptions>(_configurations["encoding"]);
+ Assert.Equal(18, configuration.H264Crf);
+ Assert.False(configuration.EnableHardwareEncoding);
+ Assert.Equal(new EncodingOptions().H265Crf, configuration.H265Crf);
+
+ using var request = new HttpRequestMessage(HttpMethod.Get, GetUrl("encoding"));
+ request.Headers.Accept.ParseAdd(JsonDefaults.CamelCaseMediaType);
+ using var response = await _client.SendAsync(request, TestContext.Current.CancellationToken);
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
+ var body = JsonNode.Parse(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
+ Assert.Equal(18, body!["h264Crf"]!.GetValue<int>());
+ }
+
+ [Fact]
+ public async Task UpdateConfiguration_OmittedProperties_ResetToDefaultsRatherThanCurrentValues()
+ {
+ await PostConfiguration("encoding", """{"H264Crf":18,"EnableHardwareEncoding":false}""");
+ await AssertMatchesLegacyDeserialization("encoding", "{}");
+ }
+
+ [Fact]
+ public async Task UpdateConfiguration_DefaultObjects_AreNotSharedBetweenRequests()
+ {
+ await PostConfiguration(string.Empty, "{}");
+ var first = Assert.IsType<ServerConfiguration>(_configurations[string.Empty]);
+ first.MetadataOptions[0].ItemType = "Changed";
+ first.TrickplayOptions.WidthResolutions[0] = 123;
+
+ await AssertMatchesLegacyDeserialization(string.Empty, "{}");
+ var second = Assert.IsType<ServerConfiguration>(_configurations[string.Empty]);
+ Assert.NotSame(first.MetadataOptions, second.MetadataOptions);
+ Assert.NotSame(first.TrickplayOptions, second.TrickplayOptions);
+ }
+
+ [Theory]
+ [InlineData("encoding", "null")]
+ [InlineData("encoding", """{"H264Crf":null}""")]
+ [InlineData("network", """{"InternalHttpPort":"invalid"}""")]
+ [InlineData("network", """{"BaseUrl":null}""")]
+ [InlineData("network", """{"LocalNetworkSubnets":null}""")]
+ [InlineData("network", """{"LocalNetworkAddresses":null}""")]
+ [InlineData("network", """{"KnownProxies":null}""")]
+ [InlineData("network", """{"VirtualInterfaceNames":null}""")]
+ [InlineData("network", """{"PublishedServerUriBySubnet":null}""")]
+ [InlineData("network", """{"RemoteIPFilter":null}""")]
+ [InlineData("livetv", """{"TunerHosts":null}""")]
+ [InlineData("livetv", """{"ListingProviders":null}""")]
+ [InlineData("livetv", """{"MediaLocationsCreated":null}""")]
+ [InlineData("livetv", """{"RecordingPostProcessorArguments":null}""")]
+ [InlineData("xbmcmetadata", """{"ReleaseDateFormat":null}""")]
+ [InlineData("", """{"ServerName":null}""")]
+ public async Task UpdateConfiguration_InvalidRequest_DoesNotSave(string key, string json)
+ {
+ using var content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
+ using var response = await _client.PostAsync(GetUrl(key), content, TestContext.Current.CancellationToken);
+
+ Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+ _configurationManager.Verify(manager => manager.SaveConfiguration(It.IsAny<string>(), It.IsAny<object>()), Times.Never);
+ _configurationManager.Verify(manager => manager.ReplaceConfiguration(It.IsAny<BaseApplicationConfiguration>()), Times.Never);
+ }
+
+ public void Dispose()
+ {
+ _client.Dispose();
+ _host.Dispose();
+ }
+
+ private static string GetUrl(string key)
+ {
+ return string.IsNullOrEmpty(key) ? "/System/Configuration" : "/System/Configuration/" + key;
+ }
+
+ private async Task AssertMatchesLegacyDeserialization(string key, string json)
+ {
+ // Compare with how the old endpoint read the same JSON.
+ var type = _configurations[key].GetType();
+ var expected = JsonSerializer.Deserialize(json, type, JsonDefaults.Options);
+
+ await PostConfiguration(key, json);
+
+ var actual = _configurations[key];
+ Assert.IsType(type, actual);
+ Assert.True(JsonNode.DeepEquals(
+ JsonSerializer.SerializeToNode(expected, type, JsonDefaults.Options),
+ JsonSerializer.SerializeToNode(actual, type, JsonDefaults.Options)));
+ }
+
+ private async Task PostConfiguration(string key, string json)
+ {
+ using var content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
+ using var response = await _client.PostAsync(GetUrl(key), content, TestContext.Current.CancellationToken);
+ Assert.True(
+ response.StatusCode == HttpStatusCode.NoContent,
+ $"{response.StatusCode}: {await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken)}");
+ }
+}
diff --git a/tests/Jellyfin.Server.Integration.Tests/Controllers/FilterControllerTests.cs b/tests/Jellyfin.Server.Integration.Tests/Controllers/FilterControllerTests.cs
new file mode 100644
index 0000000000..0e174fb597
--- /dev/null
+++ b/tests/Jellyfin.Server.Integration.Tests/Controllers/FilterControllerTests.cs
@@ -0,0 +1,35 @@
+using System.Net.Http.Json;
+using System.Text.Json;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace Jellyfin.Server.Integration.Tests.Controllers;
+
+public sealed class FilterControllerTests : IClassFixture<JellyfinApplicationFactory>
+{
+ private readonly JellyfinApplicationFactory _factory;
+ private static string? _accessToken;
+
+ public FilterControllerTests(JellyfinApplicationFactory factory)
+ {
+ _factory = factory;
+ }
+
+ [Theory]
+ [InlineData("Movie")]
+ [InlineData("Series")]
+ public async Task GetQueryFilters_EmptyLibrary_SerializesEmptyArrays(string itemType)
+ {
+ using var client = _factory.CreateClient();
+ client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client));
+
+ var filters = await client.GetFromJsonAsync<JsonElement>($"/Items/Filters2?includeItemTypes={itemType}", TestContext.Current.CancellationToken);
+
+ foreach (var field in new[] { "Years", "OfficialRatings", "Genres", "Tags", "AudioLanguages", "SubtitleLanguages" })
+ {
+ var values = filters.GetProperty(field);
+ Assert.Equal(JsonValueKind.Array, values.ValueKind);
+ Assert.Equal(0, values.GetArrayLength());
+ }
+ }
+}
diff --git a/tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs b/tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs
new file mode 100644
index 0000000000..2fd598e6b2
--- /dev/null
+++ b/tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs
@@ -0,0 +1,55 @@
+using System;
+using System.IO;
+using Emby.Server.Implementations.Library;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.IO;
+using Microsoft.Extensions.DependencyInjection;
+using Xunit;
+
+namespace Jellyfin.Server.Integration.Tests;
+
+public sealed class DotIgnoreLibraryRootTests : IClassFixture<JellyfinApplicationFactory>
+{
+ private readonly JellyfinApplicationFactory _factory;
+
+ public DotIgnoreLibraryRootTests(JellyfinApplicationFactory factory)
+ {
+ _factory = factory;
+ }
+
+ [Fact]
+ public void IgnoreFile_IgnoreFileAboveLibraryRoot_IsNotConsulted()
+ {
+ _ = _factory.CreateClient();
+
+ var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
+ var libraryRoot = Path.Combine(tempDir, "Movies");
+ var movieDir = Path.Combine(libraryRoot, "Movie (2020)");
+ Directory.CreateDirectory(movieDir);
+ var libraryDir = Path.Combine(_factory.Services.GetRequiredService<IServerApplicationPaths>().DefaultUserViewsPath, "dotignore-root-test");
+ Directory.CreateDirectory(libraryDir);
+
+ try
+ {
+ File.WriteAllText(Path.Combine(tempDir, ".ignore"), string.Empty);
+ File.WriteAllText(Path.Combine(libraryDir, "Movies.mblink"), libraryRoot);
+
+ // The rule the resolver evaluates must be the singleton LibraryManager hands the roots to
+ _factory.Services.GetRequiredService<DotIgnoreIgnoreRule>().ClearDirectoryCache();
+ var fileInfo = new FileSystemMetadata
+ {
+ FullName = Path.Combine(movieDir, "Movie (2020).mkv"),
+ IsDirectory = false
+ };
+
+ Assert.False(_factory.Services.GetRequiredService<ILibraryManager>().IgnoreFile(fileInfo, new Folder { Path = movieDir }));
+ }
+ finally
+ {
+ Directory.Delete(libraryDir, true);
+ Directory.Delete(tempDir, true);
+ }
+ }
+}
diff --git a/tests/Jellyfin.Server.Tests/Migrations/FixLibrarySubtitleDownloadLanguagesTests.cs b/tests/Jellyfin.Server.Tests/Migrations/FixLibrarySubtitleDownloadLanguagesTests.cs
new file mode 100644
index 0000000000..e9b8e2f9ff
--- /dev/null
+++ b/tests/Jellyfin.Server.Tests/Migrations/FixLibrarySubtitleDownloadLanguagesTests.cs
@@ -0,0 +1,85 @@
+using System;
+using System.Threading.Tasks;
+using Emby.Server.Implementations.Localization;
+using Jellyfin.Server.Migrations.Routines;
+using Jellyfin.Server.ServerSetupApp;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Serialization;
+using Microsoft.Extensions.Logging.Abstractions;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.Server.Tests.Migrations;
+
+public sealed class FixLibrarySubtitleDownloadLanguagesTests : IDisposable
+{
+ private readonly IXmlSerializer _originalXmlSerializer = CollectionFolder.XmlSerializer;
+
+ [Theory]
+ // 12.0 stored every variant as its base code, the metadata language tells which one was picked.
+ [InlineData("por", "pt-br", "en", "pt-br")]
+ [InlineData("por", "pt-pt", "en", "pt-pt")]
+ [InlineData("por", null, "pt-br", "pt-br")]
+ [InlineData("por", "en", "pt-br", "por")]
+ [InlineData("por", "pt", "en", "por")]
+ [InlineData("spa", "es-419", "en", "es-419")]
+ [InlineData("spa", "es", "en", "spa")]
+ [InlineData("chi", "zh-tw", "en", "zh-tw")]
+ // Explicit values are never overridden by the metadata language.
+ [InlineData("pt", "pt-br", "en", "por")]
+ [InlineData("pob", "pt-pt", "en", "pt-br")]
+ [InlineData("pt-pt", "pt-br", "en", "pt-pt")]
+ [InlineData("ger", "de", "en", "deu")]
+ [InlineData("xyz", "pt-br", "en", "xyz")]
+ // 10.10 stored por, pop and pob separately, a variant next to por means por was meant generically.
+ [InlineData("por,pob", "pt-br", "en", "por,pt-br")]
+ [InlineData("por,pop", "pt-br", "en", "por,pt-pt")]
+ [InlineData("por,pop", "pt-pt", "en", "por,pt-pt")]
+ [InlineData("por,pob", "en", "en", "por,pt-br")]
+ [InlineData("ger,pob", "pt-br", "en", "deu,pt-br")]
+ [InlineData("por,xyz", "pt-br", "en", "pt-br,xyz")]
+ public async Task PerformAsync_InfersRegionalVariantFromMetadataLanguage(string stored, string? libraryLanguage, string serverLanguage, string expected)
+ {
+ var configurationManager = new Mock<IServerConfigurationManager>();
+ configurationManager.SetupGet(x => x.Configuration).Returns(new ServerConfiguration
+ {
+ UICulture = "en-US",
+ PreferredMetadataLanguage = serverLanguage
+ });
+ var localizationManager = new LocalizationManager(configurationManager.Object, NullLogger<LocalizationManager>.Instance);
+ await localizationManager.LoadAll();
+
+ var folderId = Guid.NewGuid();
+ var options = new LibraryOptions
+ {
+ PreferredMetadataLanguage = libraryLanguage,
+ SubtitleDownloadLanguages = stored.Split(',')
+ };
+ var libraryManager = new Mock<ILibraryManager>();
+ libraryManager.Setup(x => x.GetVirtualFolders(false)).Returns(
+ [
+ new VirtualFolderInfo { Name = "Movies", ItemId = folderId.ToString(), LibraryOptions = options }
+ ]);
+ libraryManager.Setup(x => x.GetItemById<CollectionFolder>(folderId)).Returns(new CollectionFolder { Path = "/config/root/movies" });
+ CollectionFolder.XmlSerializer = Mock.Of<IXmlSerializer>();
+
+ var migration = new FixLibrarySubtitleDownloadLanguages(
+ localizationManager,
+ configurationManager.Object,
+ new StartupLogger<FixLibrarySubtitleDownloadLanguages>(NullLogger<FixLibrarySubtitleDownloadLanguages>.Instance),
+ libraryManager.Object,
+ NullLogger<FixLibrarySubtitleDownloadLanguages>.Instance);
+ await migration.PerformAsync(TestContext.Current.CancellationToken);
+
+ Assert.Equal(expected.Split(','), options.SubtitleDownloadLanguages);
+ }
+
+ public void Dispose()
+ {
+ CollectionFolder.XmlSerializer = _originalXmlSerializer;
+ }
+}
diff --git a/tests/Jellyfin.Server.Tests/Migrations/MigrateRatingLevelsTests.cs b/tests/Jellyfin.Server.Tests/Migrations/MigrateRatingLevelsTests.cs
new file mode 100644
index 0000000000..da7c72c98c
--- /dev/null
+++ b/tests/Jellyfin.Server.Tests/Migrations/MigrateRatingLevelsTests.cs
@@ -0,0 +1,311 @@
+using System;
+using System.Globalization;
+using System.Linq;
+using Jellyfin.Database.Implementations;
+using Jellyfin.Database.Implementations.Entities;
+using Jellyfin.Database.Implementations.Locking;
+using Jellyfin.Database.Providers.Sqlite;
+using Jellyfin.Server.Migrations.Routines;
+using Jellyfin.Server.ServerSetupApp;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Globalization;
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging.Abstractions;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.Server.Tests.Migrations;
+
+/// <summary>
+/// Covers the recalculation of the stored parental rating levels, including items that only inherit one.
+/// </summary>
+public sealed class MigrateRatingLevelsTests : IDisposable
+{
+ private const string SeriesType = "MediaBrowser.Controller.Entities.TV.Series";
+ private const string SeasonType = "MediaBrowser.Controller.Entities.TV.Season";
+ private const string EpisodeType = "MediaBrowser.Controller.Entities.TV.Episode";
+ private const string FolderType = "MediaBrowser.Controller.Entities.Folder";
+ private const string CollectionFolderType = "MediaBrowser.Controller.Entities.CollectionFolder";
+ private const string AggregateFolderType = "MediaBrowser.Controller.Entities.AggregateFolder";
+ private const string UserRootFolderType = "MediaBrowser.Controller.Entities.UserRootFolder";
+ private const string LibraryPath = "/media/shows";
+
+ private static readonly Guid _libraryId = Guid.Parse("11111111-1111-1111-1111-111111111111");
+ private static readonly Guid _seriesId = Guid.Parse("22222222-2222-2222-2222-222222222222");
+ private static readonly Guid _seasonId = Guid.Parse("33333333-3333-3333-3333-333333333333");
+ private static readonly Guid _episodeId = Guid.Parse("44444444-4444-4444-4444-444444444444");
+ private static readonly Guid _rootId = Guid.Parse("55555555-5555-5555-5555-555555555555");
+ private static readonly Guid _collectionFolderId = Guid.Parse("66666666-6666-6666-6666-666666666666");
+
+ private readonly SqliteConnection _connection;
+ private readonly DbContextOptions<JellyfinDbContext> _dbOptions;
+ private readonly IApplicationPaths _applicationPaths;
+ private readonly Mock<ILocalizationManager> _localizationManager;
+ private readonly Mock<ILibraryManager> _libraryManager;
+ private readonly Mock<IServerApplicationHost> _appHost;
+
+ public MigrateRatingLevelsTests()
+ {
+ _applicationPaths = new Mock<IApplicationPaths>().Object;
+
+ _localizationManager = new Mock<ILocalizationManager>();
+ _localizationManager.Setup(m => m.GetRatingScore("TV-14", null)).Returns(new ParentalRatingScore(14, null));
+ _localizationManager.Setup(m => m.GetRatingScore("TV-MA", null)).Returns(new ParentalRatingScore(17, 1));
+ _localizationManager.Setup(m => m.GetRatingScore("TV-14", "DE")).Returns(new ParentalRatingScore(16, null));
+ _localizationManager.Setup(m => m.GetRatingScore("TV-14", "GB")).Returns(new ParentalRatingScore(15, null));
+
+ _libraryManager = new Mock<ILibraryManager>();
+ _libraryManager.Setup(m => m.GetVirtualFolders(false)).Returns([]);
+
+ _appHost = new Mock<IServerApplicationHost>();
+ _appHost.Setup(m => m.ReverseVirtualPath(It.IsAny<string>()))
+ .Returns((string path) => path.Replace("/var/lib/jellyfin/data", "%AppDataPath%", StringComparison.Ordinal));
+
+ // The connection owns the in-memory database, so it stays open for the whole test.
+ _connection = new SqliteConnection("Data Source=:memory:");
+ _connection.Open();
+
+ _dbOptions = new DbContextOptionsBuilder<JellyfinDbContext>()
+ .UseSqlite(_connection)
+ .Options;
+
+ using var context = CreateDbContext();
+ context.Database.EnsureCreated();
+ }
+
+ [Fact]
+ public void Perform_UnratedSeasonAndEpisode_InheritTheSeriesRating()
+ {
+ SeedSeries(seriesRating: "TV-MA", seriesCustomRating: null, inherited: null);
+
+ Perform();
+
+ using var context = CreateDbContext();
+ AssertRating(context, _seriesId, 17, 1);
+ AssertRating(context, _seasonId, 17, 1);
+ AssertRating(context, _episodeId, 17, 1);
+ AssertRating(context, _libraryId, null, null);
+ }
+
+ [Fact]
+ public void Perform_AlreadyMigrated_KeepsTheInheritedRating()
+ {
+ // A second run over a database the first run and a scan already brought up to date.
+ SeedSeries(seriesRating: "TV-MA", seriesCustomRating: null, inherited: new ParentalRatingScore(17, 1));
+
+ Perform();
+
+ using var context = CreateDbContext();
+ AssertRating(context, _seasonId, 17, 1);
+ AssertRating(context, _episodeId, 17, 1);
+ }
+
+ [Fact]
+ public void Perform_CustomRatingOnSeries_WinsOverTheOfficialOne()
+ {
+ SeedSeries(seriesRating: "TV-14", seriesCustomRating: "TV-MA", inherited: null);
+
+ Perform();
+
+ using var context = CreateDbContext();
+ AssertRating(context, _seriesId, 17, 1);
+ AssertRating(context, _episodeId, 17, 1);
+ }
+
+ [Fact]
+ public void Perform_UnratedSeries_ClearsTheInheritedRating()
+ {
+ SeedSeries(seriesRating: null, seriesCustomRating: null, inherited: new ParentalRatingScore(14, null));
+
+ Perform();
+
+ using var context = CreateDbContext();
+ AssertRating(context, _seriesId, null, null);
+ AssertRating(context, _episodeId, null, null);
+ }
+
+ [Fact]
+ public void Perform_LibraryCountry_ScoresInheritedRatingsInThatCountry()
+ {
+ SeedSeries(seriesRating: "TV-14", seriesCustomRating: null, inherited: null);
+ SeedLibrary(folderCountryCode: null, optionsCountryCode: "DE");
+
+ Perform();
+
+ using var context = CreateDbContext();
+ AssertRating(context, _seriesId, 16, null);
+ AssertRating(context, _episodeId, 16, null);
+ }
+
+ [Fact]
+ public void Perform_CountryOnTheSeries_WinsOverTheLibraryCountry()
+ {
+ SeedSeries(seriesRating: "TV-14", seriesCustomRating: null, inherited: null, seriesCountryCode: "GB");
+ SeedLibrary(folderCountryCode: "DE", optionsCountryCode: "DE");
+
+ Perform();
+
+ using var context = CreateDbContext();
+ AssertRating(context, _seriesId, 15, null);
+ AssertRating(context, _seasonId, 15, null);
+ }
+
+ [Fact]
+ public void Perform_LibraryUnderTheDataPath_ScoresInheritedRatingsInThatCountry()
+ {
+ SeedSeries(seriesRating: "TV-14", seriesCustomRating: null, inherited: null);
+ SeedLibrary(folderCountryCode: null, optionsCountryCode: "DE", location: "/var/lib/jellyfin/data/shows");
+ using (var context = CreateDbContext())
+ {
+ context.BaseItems.Where(e => e.Id.Equals(_libraryId)).ExecuteUpdate(f => f.SetProperty(e => e.Path, "%AppDataPath%/shows"));
+ }
+
+ Perform();
+
+ using var assertContext = CreateDbContext();
+ AssertRating(assertContext, _seriesId, 16, null);
+ AssertRating(assertContext, _episodeId, 16, null);
+ }
+
+ [Fact]
+ public void Perform_RatedCollectionFolder_UsesItsOwnLibraryCountryCode()
+ {
+ var userRootId = Guid.Parse("77777777-7777-7777-7777-777777777777");
+ using (var context = CreateDbContext())
+ {
+ context.BaseItems.Add(new BaseItemEntity { Id = _rootId, Type = AggregateFolderType, Name = "root" });
+ context.BaseItems.Add(new BaseItemEntity { Id = userRootId, Type = UserRootFolderType, Name = "default", Path = "/config/root/default" });
+ context.BaseItems.Add(new BaseItemEntity
+ {
+ Id = _collectionFolderId,
+ Type = CollectionFolderType,
+ Name = "Shows",
+ Path = "/config/root/default/Shows",
+ ParentId = userRootId,
+ CustomRating = "TV-14"
+ });
+ context.SaveChanges();
+ }
+
+ _libraryManager.Setup(m => m.GetVirtualFolders(false)).Returns(
+ [
+ new VirtualFolderInfo
+ {
+ Name = "Shows",
+ ItemId = _collectionFolderId.ToString("N", CultureInfo.InvariantCulture),
+ Locations = [LibraryPath],
+ LibraryOptions = new LibraryOptions { MetadataCountryCode = "DE" }
+ }
+ ]);
+
+ Perform();
+
+ using var assertContext = CreateDbContext();
+ AssertRating(assertContext, _collectionFolderId, 16, null);
+ }
+
+ public void Dispose()
+ {
+ _connection.Dispose();
+ }
+
+ private static void AssertRating(JellyfinDbContext context, Guid id, int? score, int? subScore)
+ {
+ var item = context.BaseItems.AsNoTracking().First(e => e.Id.Equals(id));
+ Assert.Equal(score, item.InheritedParentalRatingValue);
+ Assert.Equal(subScore, item.InheritedParentalRatingSubValue);
+ }
+
+ private void Perform()
+ {
+ var factory = new Mock<IDbContextFactory<JellyfinDbContext>>();
+ factory.Setup(f => f.CreateDbContext()).Returns(CreateDbContext);
+
+ new MigrateRatingLevels(factory.Object, new StartupLogger<MigrateRatingLevels>(NullLogger<MigrateRatingLevels>.Instance), _localizationManager.Object, _libraryManager.Object, _appHost.Object).Perform();
+ }
+
+ private JellyfinDbContext CreateDbContext() => new(
+ _dbOptions,
+ NullLogger<JellyfinDbContext>.Instance,
+ new SqliteDatabaseProvider(_applicationPaths, NullLogger<SqliteDatabaseProvider>.Instance),
+ new NoLockBehavior(NullLogger<NoLockBehavior>.Instance));
+
+ private void SeedLibrary(string? folderCountryCode, string? optionsCountryCode, string location = LibraryPath)
+ {
+ using (var context = CreateDbContext())
+ {
+ context.BaseItems.Add(new BaseItemEntity { Id = _rootId, Type = AggregateFolderType, Name = "root" });
+ context.BaseItems.Add(new BaseItemEntity
+ {
+ Id = _collectionFolderId,
+ Type = CollectionFolderType,
+ Name = "Shows",
+ Path = "/config/root/default/Shows",
+ PreferredMetadataCountryCode = folderCountryCode
+ });
+ context.SaveChanges();
+ context.BaseItems.Where(e => e.Id.Equals(_libraryId)).ExecuteUpdate(f => f.SetProperty(e => e.ParentId, _rootId));
+ }
+
+ _libraryManager.Setup(m => m.GetVirtualFolders(false)).Returns(
+ [
+ new VirtualFolderInfo
+ {
+ Name = "Shows",
+ ItemId = _collectionFolderId.ToString("N", CultureInfo.InvariantCulture),
+ Locations = [location],
+ LibraryOptions = new LibraryOptions { MetadataCountryCode = optionsCountryCode }
+ }
+ ]);
+ }
+
+ private void SeedSeries(string? seriesRating, string? seriesCustomRating, ParentalRatingScore? inherited, string? seriesCountryCode = null)
+ {
+ using var context = CreateDbContext();
+
+ context.BaseItems.Add(new BaseItemEntity { Id = _libraryId, Type = FolderType, Name = "Shows", Path = LibraryPath });
+ context.BaseItems.Add(new BaseItemEntity
+ {
+ Id = _seriesId,
+ Type = SeriesType,
+ Name = "Series",
+ ParentId = _libraryId,
+ OfficialRating = seriesRating,
+ CustomRating = seriesCustomRating,
+ PreferredMetadataCountryCode = seriesCountryCode,
+ InheritedParentalRatingValue = inherited?.Score,
+ InheritedParentalRatingSubValue = inherited?.SubScore
+ });
+ context.BaseItems.Add(new BaseItemEntity
+ {
+ Id = _seasonId,
+ Type = SeasonType,
+ Name = "Season 1",
+ ParentId = _seriesId,
+ SeriesId = _seriesId,
+ InheritedParentalRatingValue = inherited?.Score,
+ InheritedParentalRatingSubValue = inherited?.SubScore
+ });
+
+ // The episode's display parent is its season, not the folder it was found in.
+ context.BaseItems.Add(new BaseItemEntity
+ {
+ Id = _episodeId,
+ Type = EpisodeType,
+ Name = "Episode 1",
+ ParentId = _libraryId,
+ SeasonId = _seasonId,
+ SeriesId = _seriesId,
+ OfficialRating = string.Empty,
+ InheritedParentalRatingValue = inherited?.Score,
+ InheritedParentalRatingSubValue = inherited?.SubScore
+ });
+
+ context.SaveChanges();
+ }
+}