2021-12-05 16:19:40 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2021-12-31 23:22:46 +00:00
using System.Net.Http ;
using System.Runtime.CompilerServices ;
2021-12-20 23:24:07 +00:00
using System.Threading ;
using System.Threading.Tasks ;
2021-12-31 23:22:46 +00:00
using MediaBrowser.Controller ;
2021-12-05 23:09:46 +00:00
using MediaBrowser.Controller.BaseItemManager ;
2021-12-05 16:19:40 +00:00
using MediaBrowser.Controller.Configuration ;
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Entities.Movies ;
using MediaBrowser.Controller.Library ;
2024-02-26 12:09:40 +00:00
using MediaBrowser.Controller.Lyrics ;
2021-12-05 16:19:40 +00:00
using MediaBrowser.Controller.Providers ;
2021-12-31 23:22:46 +00:00
using MediaBrowser.Controller.Subtitles ;
2021-12-05 16:19:40 +00:00
using MediaBrowser.Model.Configuration ;
2021-12-31 23:22:46 +00:00
using MediaBrowser.Model.IO ;
2021-12-05 16:19:40 +00:00
using MediaBrowser.Providers.Manager ;
2024-03-03 20:32:55 +00:00
using Microsoft.Extensions.Caching.Memory ;
2021-12-20 23:24:07 +00:00
using Microsoft.Extensions.Logging ;
2021-12-05 16:19:40 +00:00
using Microsoft.Extensions.Logging.Abstractions ;
using Moq ;
using Xunit ;
2021-12-31 23:22:46 +00:00
// Allow Moq to see internal class
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
2021-12-05 16:19:40 +00:00
namespace Jellyfin.Providers.Tests.Manager
{
public class ProviderManagerTests
{
2021-12-20 23:24:07 +00:00
private static readonly ILogger < ProviderManager > _logger = new NullLogger < ProviderManager > ( ) ;
2022-11-23 14:59:50 +00:00
public static TheoryData < Mock < IMetadataService > [ ] , int > RefreshSingleItemOrderData ( )
2021-12-31 23:22:46 +00:00
= > new ( )
2021-12-20 23:24:07 +00:00
{
// no order set, uses provided order
{
new [ ]
{
MockIMetadataService ( true , true ) ,
MockIMetadataService ( true , true )
} ,
0
} ,
// sort order sets priority when all match
{
new [ ]
{
MockIMetadataService ( true , true , 1 ) ,
MockIMetadataService ( true , true , 0 ) ,
MockIMetadataService ( true , true , 2 )
} ,
1
} ,
// CanRefreshPrimary prioritized
{
new [ ]
{
MockIMetadataService ( false , true ) ,
MockIMetadataService ( true , true ) ,
} ,
1
} ,
// falls back to CanRefresh
{
new [ ]
{
MockIMetadataService ( false , false ) ,
MockIMetadataService ( false , true )
} ,
1
} ,
} ;
[Theory]
[MemberData(nameof(RefreshSingleItemOrderData))]
2022-11-23 14:59:50 +00:00
public async Task RefreshSingleItem_ServiceOrdering_FollowsPriority ( Mock < IMetadataService > [ ] servicesList , int expectedIndex )
2021-12-20 23:24:07 +00:00
{
var item = new Movie ( ) ;
using var providerManager = GetProviderManager ( ) ;
AddParts ( providerManager , metadataServices : servicesList . Select ( s = > s . Object ) . ToArray ( ) ) ;
var refreshOptions = new MetadataRefreshOptions ( Mock . Of < IDirectoryService > ( MockBehavior . Strict ) ) ;
2023-09-18 15:55:52 +00:00
var actual = await providerManager . RefreshSingleItem ( item , refreshOptions , CancellationToken . None ) ;
2021-12-20 23:24:07 +00:00
2022-11-23 14:59:50 +00:00
Assert . Equal ( ItemUpdateType . MetadataDownload , actual ) ;
2021-12-20 23:24:07 +00:00
for ( var i = 0 ; i < servicesList . Length ; i + + )
{
2022-01-16 13:18:44 +00:00
var times = i = = expectedIndex ? Times . Once ( ) : Times . Never ( ) ;
servicesList [ i ] . Verify ( mock = > mock . RefreshMetadata ( It . IsAny < BaseItem > ( ) , It . IsAny < MetadataRefreshOptions > ( ) , It . IsAny < CancellationToken > ( ) ) , times ) ;
2021-12-20 23:24:07 +00:00
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
2022-11-23 14:59:50 +00:00
public async Task RefreshSingleItem_RefreshMetadata_WhenServiceFound ( bool serviceFound )
2021-12-20 23:24:07 +00:00
{
var item = new Movie ( ) ;
var servicesList = new [ ] { MockIMetadataService ( false , serviceFound ) } ;
using var providerManager = GetProviderManager ( ) ;
AddParts ( providerManager , metadataServices : servicesList . Select ( s = > s . Object ) . ToArray ( ) ) ;
var refreshOptions = new MetadataRefreshOptions ( Mock . Of < IDirectoryService > ( MockBehavior . Strict ) ) ;
2023-09-18 15:55:52 +00:00
var actual = await providerManager . RefreshSingleItem ( item , refreshOptions , CancellationToken . None ) ;
2021-12-20 23:24:07 +00:00
var expectedResult = serviceFound ? ItemUpdateType . MetadataDownload : ItemUpdateType . None ;
2022-11-23 14:59:50 +00:00
Assert . Equal ( expectedResult , actual ) ;
2021-12-20 23:24:07 +00:00
}
2022-11-23 14:59:50 +00:00
public static TheoryData < int , int [ ] ? , int [ ] ? , int? [ ] ? , int [ ] > GetImageProvidersOrderData ( )
2021-12-31 23:22:46 +00:00
= > new ( )
2021-12-05 16:19:40 +00:00
{
2021-12-05 20:33:31 +00:00
{ 3 , null , null , null , new [ ] { 0 , 1 , 2 } } , // no order options set
2021-12-05 16:19:40 +00:00
// library options ordering
2021-12-05 20:33:31 +00:00
{ 3 , Array . Empty < int > ( ) , null , null , new [ ] { 0 , 1 , 2 } } , // no order provided
{ 3 , new [ ] { 1 } , null , null , new [ ] { 1 , 0 , 2 } } , // one item in order
{ 3 , new [ ] { 2 , 1 , 0 } , null , null , new [ ] { 2 , 1 , 0 } } , // full reverse order
2021-12-05 16:19:40 +00:00
// server options ordering
2021-12-05 20:33:31 +00:00
{ 3 , null , Array . Empty < int > ( ) , null , new [ ] { 0 , 1 , 2 } } , // no order provided
{ 3 , null , new [ ] { 1 } , null , new [ ] { 1 , 0 , 2 } } , // one item in order
{ 3 , null , new [ ] { 2 , 1 , 0 } , null , new [ ] { 2 , 1 , 0 } } , // full reverse order
2021-12-05 16:19:40 +00:00
// IHasOrder ordering
2021-12-05 20:33:31 +00:00
{ 3 , null , null , new int? [ ] { null , 1 , null } , new [ ] { 1 , 0 , 2 } } , // one item with defined order
{ 3 , null , null , new int? [ ] { 2 , 1 , 0 } , new [ ] { 2 , 1 , 0 } } , // full reverse order
2021-12-05 16:19:40 +00:00
// multiple orders set
2021-12-07 23:52:57 +00:00
{ 3 , new [ ] { 1 } , new [ ] { 2 , 0 , 1 } , null , new [ ] { 1 , 0 , 2 } } , // partial library order first, server order ignored
2021-12-05 20:33:31 +00:00
{ 3 , new [ ] { 1 } , null , new int? [ ] { 2 , 0 , 1 } , new [ ] { 1 , 2 , 0 } } , // library order first, then orderby
{ 3 , new [ ] { 2 , 1 , 0 } , new [ ] { 1 , 2 , 0 } , new int? [ ] { 2 , 0 , 1 } , new [ ] { 2 , 1 , 0 } } , // library order wins
2021-12-05 16:19:40 +00:00
} ;
[Theory]
[MemberData(nameof(GetImageProvidersOrderData))]
2021-12-05 20:33:31 +00:00
public void GetImageProviders_ProviderOrder_MatchesExpected ( int providerCount , int [ ] ? libraryOrder , int [ ] ? serverOrder , int? [ ] ? hasOrderOrder , int [ ] expectedOrder )
2021-12-05 16:19:40 +00:00
{
var item = new Movie ( ) ;
var nameProvider = new Func < int , string > ( i = > "Provider" + i ) ;
var providerList = new List < IImageProvider > ( ) ;
for ( var i = 0 ; i < providerCount ; i + + )
{
var order = hasOrderOrder ? [ i ] ;
2021-12-18 21:30:06 +00:00
providerList . Add ( MockIImageProvider < ILocalImageProvider > ( nameProvider ( i ) , item , order : order ) ) ;
2021-12-05 16:19:40 +00:00
}
2021-12-09 23:38:41 +00:00
var libraryOptions = CreateLibraryOptions ( item . GetType ( ) . Name , imageFetcherOrder : libraryOrder ? . Select ( nameProvider ) . ToArray ( ) ) ;
var serverConfiguration = CreateServerConfiguration ( item . GetType ( ) . Name , imageFetcherOrder : serverOrder ? . Select ( nameProvider ) . ToArray ( ) ) ;
2021-12-05 16:19:40 +00:00
2021-12-09 23:38:41 +00:00
using var providerManager = GetProviderManager ( serverConfiguration : serverConfiguration , libraryOptions : libraryOptions ) ;
2021-12-05 16:19:40 +00:00
AddParts ( providerManager , imageProviders : providerList ) ;
var refreshOptions = new ImageRefreshOptions ( Mock . Of < IDirectoryService > ( MockBehavior . Strict ) ) ;
var actualProviders = providerManager . GetImageProviders ( item , refreshOptions ) . ToList ( ) ;
Assert . Equal ( providerList . Count , actualProviders . Count ) ;
2021-12-07 23:52:57 +00:00
var actualOrder = actualProviders . Select ( i = > providerList . IndexOf ( i ) ) . ToArray ( ) ;
Assert . Equal ( expectedOrder , actualOrder ) ;
2021-12-05 16:19:40 +00:00
}
2021-12-05 23:09:46 +00:00
[Theory]
[InlineData(true, false, true)]
[InlineData(false, false, false)]
[InlineData(true, true, false)]
2021-12-06 21:31:16 +00:00
public void GetImageProviders_CanRefreshImagesBasic_WhenSupportsWithoutError ( bool supports , bool errorOnSupported , bool expected )
2021-12-05 23:09:46 +00:00
{
2021-12-19 22:33:27 +00:00
GetImageProviders_CanRefreshImages_Tester ( nameof ( IImageProvider ) , supports , expected , errorOnSupported : errorOnSupported ) ;
2021-12-05 23:09:46 +00:00
}
[Theory]
2021-12-19 22:33:27 +00:00
[InlineData(nameof(ILocalImageProvider), false, true)]
[InlineData(nameof(ILocalImageProvider), true, true)]
[InlineData(nameof(IImageProvider), false, false)]
[InlineData(nameof(IImageProvider), true, true)]
public void GetImageProviders_CanRefreshImagesLocked_WhenLocalOrFullRefresh ( string providerType , bool fullRefresh , bool expected )
2021-12-05 23:09:46 +00:00
{
2021-12-06 21:31:16 +00:00
GetImageProviders_CanRefreshImages_Tester ( providerType , true , expected , itemLocked : true , fullRefresh : fullRefresh ) ;
2021-12-05 23:09:46 +00:00
}
[Theory]
2021-12-19 22:33:27 +00:00
[InlineData(nameof(ILocalImageProvider), false, true)]
[InlineData(nameof(IRemoteImageProvider), true, true)]
[InlineData(nameof(IDynamicImageProvider), true, true)]
[InlineData(nameof(IRemoteImageProvider), false, false)]
[InlineData(nameof(IDynamicImageProvider), false, false)]
public void GetImageProviders_CanRefreshImagesBaseItemEnabled_WhenLocalOrEnabled ( string providerType , bool enabled , bool expected )
2021-12-05 23:09:46 +00:00
{
2021-12-06 21:31:16 +00:00
GetImageProviders_CanRefreshImages_Tester ( providerType , true , expected , baseItemEnabled : enabled ) ;
2021-12-05 23:09:46 +00:00
}
2021-12-09 23:38:41 +00:00
private static void GetImageProviders_CanRefreshImages_Tester (
2021-12-19 22:33:27 +00:00
string providerType ,
2021-12-09 23:38:41 +00:00
bool supports ,
bool expected ,
bool errorOnSupported = false ,
bool itemLocked = false ,
bool fullRefresh = false ,
bool baseItemEnabled = true )
2021-12-05 23:09:46 +00:00
{
var item = new Movie
{
IsLocked = itemLocked
} ;
var providerName = "provider" ;
2021-12-19 22:33:27 +00:00
IImageProvider provider = providerType switch
2021-12-05 23:09:46 +00:00
{
"IImageProvider" = > MockIImageProvider < IImageProvider > ( providerName , item , supports : supports , errorOnSupported : errorOnSupported ) ,
"ILocalImageProvider" = > MockIImageProvider < ILocalImageProvider > ( providerName , item , supports : supports , errorOnSupported : errorOnSupported ) ,
"IRemoteImageProvider" = > MockIImageProvider < IRemoteImageProvider > ( providerName , item , supports : supports , errorOnSupported : errorOnSupported ) ,
"IDynamicImageProvider" = > MockIImageProvider < IDynamicImageProvider > ( providerName , item , supports : supports , errorOnSupported : errorOnSupported ) ,
_ = > throw new ArgumentException ( "Unexpected provider type" )
} ;
var refreshOptions = new ImageRefreshOptions ( Mock . Of < IDirectoryService > ( MockBehavior . Strict ) )
{
ImageRefreshMode = fullRefresh ? MetadataRefreshMode . FullRefresh : MetadataRefreshMode . Default
} ;
var baseItemManager = new Mock < IBaseItemManager > ( MockBehavior . Strict ) ;
2021-12-18 20:40:27 +00:00
baseItemManager . Setup ( i = > i . IsImageFetcherEnabled ( item , It . IsAny < TypeOptions > ( ) , providerName ) )
2021-12-05 23:09:46 +00:00
. Returns ( baseItemEnabled ) ;
2021-12-09 23:38:41 +00:00
using var providerManager = GetProviderManager ( baseItemManager : baseItemManager . Object ) ;
2021-12-05 23:09:46 +00:00
AddParts ( providerManager , imageProviders : new [ ] { provider } ) ;
2021-12-09 23:38:41 +00:00
var actualProviders = providerManager . GetImageProviders ( item , refreshOptions ) . ToArray ( ) ;
2021-12-05 23:09:46 +00:00
2021-12-09 23:38:41 +00:00
Assert . Equal ( expected ? 1 : 0 , actualProviders . Length ) ;
2021-12-05 23:09:46 +00:00
}
2022-11-23 14:59:50 +00:00
public static TheoryData < string [ ] , int [ ] ? , int [ ] ? , int [ ] ? , int [ ] ? , int? [ ] ? , int [ ] > GetMetadataProvidersOrderData ( )
2021-12-07 23:52:57 +00:00
{
2021-12-08 15:49:09 +00:00
var l = nameof ( ILocalMetadataProvider ) ;
var r = nameof ( IRemoteMetadataProvider ) ;
2021-12-31 23:22:46 +00:00
return new ( )
2021-12-07 23:52:57 +00:00
{
{ new [ ] { l , l , r , r } , null , null , null , null , null , new [ ] { 0 , 1 , 2 , 3 } } , // no order options set
// library options ordering
{ new [ ] { l , l , r , r } , Array . Empty < int > ( ) , Array . Empty < int > ( ) , null , null , null , new [ ] { 0 , 1 , 2 , 3 } } , // no order provided
// local only
{ new [ ] { r , l , l , l } , new [ ] { 2 } , null , null , null , null , new [ ] { 2 , 0 , 1 , 3 } } , // one item in order
{ new [ ] { r , l , l , l } , new [ ] { 3 , 2 , 1 } , null , null , null , null , new [ ] { 3 , 2 , 1 , 0 } } , // full reverse order
// remote only
{ new [ ] { l , r , r , r } , null , new [ ] { 2 } , null , null , null , new [ ] { 2 , 0 , 1 , 3 } } , // one item in order
{ new [ ] { l , r , r , r } , null , new [ ] { 3 , 2 , 1 } , null , null , null , new [ ] { 3 , 2 , 1 , 0 } } , // full reverse order
// local and remote, note that results will be interleaved (odd but expected)
{ new [ ] { l , l , r , r } , new [ ] { 1 } , new [ ] { 3 } , null , null , null , new [ ] { 1 , 3 , 0 , 2 } } , // one item in each order
{ new [ ] { l , l , l , r , r , r } , new [ ] { 2 , 1 , 0 } , new [ ] { 5 , 4 , 3 } , null , null , null , new [ ] { 2 , 5 , 1 , 4 , 0 , 3 } } , // full reverse order
// // server options ordering
{ new [ ] { l , l , r , r } , null , null , Array . Empty < int > ( ) , Array . Empty < int > ( ) , null , new [ ] { 0 , 1 , 2 , 3 } } , // no order provided
// local only
{ new [ ] { r , l , l , l } , null , null , new [ ] { 2 } , null , null , new [ ] { 2 , 0 , 1 , 3 } } , // one item in order
{ new [ ] { r , l , l , l } , null , null , new [ ] { 3 , 2 , 1 } , null , null , new [ ] { 3 , 2 , 1 , 0 } } , // full reverse order
// remote only
{ new [ ] { l , r , r , r } , null , null , null , new [ ] { 2 } , null , new [ ] { 2 , 0 , 1 , 3 } } , // one item in order
{ new [ ] { l , r , r , r } , null , null , null , new [ ] { 3 , 2 , 1 } , null , new [ ] { 3 , 2 , 1 , 0 } } , // full reverse order
// local and remote, note that results will be interleaved (odd but expected)
{ new [ ] { l , l , r , r } , null , null , new [ ] { 1 } , new [ ] { 3 } , null , new [ ] { 1 , 3 , 0 , 2 } } , // one item in each order
{ new [ ] { l , l , l , r , r , r } , null , null , new [ ] { 2 , 1 , 0 } , new [ ] { 5 , 4 , 3 } , null , new [ ] { 2 , 5 , 1 , 4 , 0 , 3 } } , // full reverse order
// IHasOrder ordering (not interleaved, doesn't care about types)
2021-12-08 15:49:09 +00:00
{ new [ ] { l , l , r , r } , null , null , null , null , new int? [ ] { 2 , null , 1 , null } , new [ ] { 2 , 0 , 1 , 3 } } , // partially defined
2021-12-07 23:52:57 +00:00
{ new [ ] { l , l , r , r } , null , null , null , null , new int? [ ] { 3 , 2 , 1 , 0 } , new [ ] { 3 , 2 , 1 , 0 } } , // full reverse order
// multiple orders set
{ new [ ] { l , l , l , r , r , r } , new [ ] { 1 } , new [ ] { 4 } , new [ ] { 2 , 1 , 0 } , new [ ] { 5 , 4 , 3 } , null , new [ ] { 1 , 4 , 0 , 2 , 3 , 5 } } , // partial library order first, server order ignored
{ new [ ] { l , l , l } , new [ ] { 1 } , null , null , null , new int? [ ] { 2 , 0 , 1 } , new [ ] { 1 , 2 , 0 } } , // library order first, then orderby
{ new [ ] { l , l , l , r , r , r } , new [ ] { 2 , 1 , 0 } , new [ ] { 5 , 4 , 3 } , new [ ] { 1 , 2 , 0 } , new [ ] { 4 , 5 , 3 } , new int? [ ] { 5 , 4 , 1 , 6 , 3 , 2 } , new [ ] { 2 , 5 , 4 , 1 , 0 , 3 } } , // library order wins (with orderby between local/remote)
} ;
}
[Theory]
[MemberData(nameof(GetMetadataProvidersOrderData))]
2021-12-09 23:38:41 +00:00
public void GetMetadataProviders_ProviderOrder_MatchesExpected (
string [ ] providers ,
int [ ] ? libraryLocalOrder ,
int [ ] ? libraryRemoteOrder ,
int [ ] ? serverLocalOrder ,
int [ ] ? serverRemoteOrder ,
int? [ ] ? hasOrderOrder ,
int [ ] expectedOrder )
2021-12-07 23:52:57 +00:00
{
var item = new MetadataTestItem ( ) ;
var nameProvider = new Func < int , string > ( i = > "Provider" + i ) ;
var providerList = new List < IMetadataProvider < MetadataTestItem > > ( ) ;
for ( var i = 0 ; i < providers . Length ; i + + )
{
var order = hasOrderOrder ? [ i ] ;
2021-12-08 15:49:09 +00:00
providerList . Add ( MockIMetadataProviderMapper < MetadataTestItem , MetadataTestItemInfo > ( providers [ i ] , nameProvider ( i ) , order : order ) ) ;
2021-12-07 23:52:57 +00:00
}
2021-12-09 23:38:41 +00:00
var libraryOptions = CreateLibraryOptions (
item . GetType ( ) . Name ,
localMetadataReaderOrder : libraryLocalOrder ? . Select ( nameProvider ) . ToArray ( ) ,
metadataFetcherOrder : libraryRemoteOrder ? . Select ( nameProvider ) . ToArray ( ) ) ;
var serverConfiguration = CreateServerConfiguration (
item . GetType ( ) . Name ,
localMetadataReaderOrder : serverLocalOrder ? . Select ( nameProvider ) . ToArray ( ) ,
metadataFetcherOrder : serverRemoteOrder ? . Select ( nameProvider ) . ToArray ( ) ) ;
2021-12-07 23:52:57 +00:00
var baseItemManager = new Mock < IBaseItemManager > ( MockBehavior . Strict ) ;
2021-12-18 20:40:27 +00:00
baseItemManager . Setup ( i = > i . IsMetadataFetcherEnabled ( item , It . IsAny < TypeOptions > ( ) , It . IsAny < string > ( ) ) )
2021-12-07 23:52:57 +00:00
. Returns ( true ) ;
2021-12-09 23:38:41 +00:00
using var providerManager = GetProviderManager ( serverConfiguration : serverConfiguration , baseItemManager : baseItemManager . Object ) ;
2021-12-07 23:52:57 +00:00
AddParts ( providerManager , metadataProviders : providerList ) ;
var actualProviders = providerManager . GetMetadataProviders < MetadataTestItem > ( item , libraryOptions ) . ToList ( ) ;
Assert . Equal ( providerList . Count , actualProviders . Count ) ;
var actualOrder = actualProviders . Select ( i = > providerList . IndexOf ( i ) ) . ToArray ( ) ;
Assert . Equal ( expectedOrder , actualOrder ) ;
}
2021-12-09 23:38:41 +00:00
[Theory]
2021-12-19 22:33:27 +00:00
[InlineData(nameof(IMetadataProvider))]
[InlineData(nameof(ILocalMetadataProvider))]
[InlineData(nameof(IRemoteMetadataProvider))]
[InlineData(nameof(ICustomMetadataProvider))]
public void GetMetadataProviders_CanRefreshMetadataBasic_ReturnsTrue ( string providerType )
2021-12-09 23:38:41 +00:00
{
GetMetadataProviders_CanRefreshMetadata_Tester ( providerType , true ) ;
}
[Theory]
2021-12-19 22:33:27 +00:00
[InlineData(nameof(ILocalMetadataProvider), false, true)]
[InlineData(nameof(IRemoteMetadataProvider), false, false)]
[InlineData(nameof(ICustomMetadataProvider), false, false)]
[InlineData(nameof(ILocalMetadataProvider), true, true)]
[InlineData(nameof(ICustomMetadataProvider), true, false)]
public void GetMetadataProviders_CanRefreshMetadataLocked_WhenLocalOrForced ( string providerType , bool forced , bool expected )
2021-12-09 23:38:41 +00:00
{
GetMetadataProviders_CanRefreshMetadata_Tester ( providerType , expected , itemLocked : true , providerForced : forced ) ;
}
[Theory]
2021-12-19 22:33:27 +00:00
[InlineData(nameof(ILocalMetadataProvider), false, true)]
[InlineData(nameof(ICustomMetadataProvider), false, true)]
[InlineData(nameof(IRemoteMetadataProvider), false, false)]
[InlineData(nameof(IRemoteMetadataProvider), true, true)]
public void GetMetadataProviders_CanRefreshMetadataBaseItemEnabled_WhenEnabledOrNotRemote ( string providerType , bool baseItemEnabled , bool expected )
2021-12-09 23:38:41 +00:00
{
GetMetadataProviders_CanRefreshMetadata_Tester ( providerType , expected , baseItemEnabled : baseItemEnabled ) ;
}
[Theory]
2021-12-19 22:33:27 +00:00
[InlineData(nameof(IRemoteMetadataProvider), false, true)]
[InlineData(nameof(ICustomMetadataProvider), false, true)]
[InlineData(nameof(ILocalMetadataProvider), false, false)]
[InlineData(nameof(ILocalMetadataProvider), true, true)]
public void GetMetadataProviders_CanRefreshMetadataSupportsLocal_WhenSupportsOrNotLocal ( string providerType , bool supportsLocalMetadata , bool expected )
2021-12-09 23:38:41 +00:00
{
GetMetadataProviders_CanRefreshMetadata_Tester ( providerType , expected , supportsLocalMetadata : supportsLocalMetadata ) ;
}
[Theory]
2021-12-19 22:33:27 +00:00
[InlineData(nameof(ICustomMetadataProvider), true)]
2021-12-18 21:30:06 +00:00
[InlineData(nameof(IRemoteMetadataProvider), true)]
2023-02-26 20:13:21 +00:00
[InlineData(nameof(ILocalMetadataProvider), true)]
public void GetMetadataProviders_CanRefreshMetadataOwned ( string providerType , bool expected )
2021-12-09 23:38:41 +00:00
{
GetMetadataProviders_CanRefreshMetadata_Tester ( providerType , expected , ownedItem : true ) ;
}
private static void GetMetadataProviders_CanRefreshMetadata_Tester (
2021-12-19 22:33:27 +00:00
string providerType ,
2021-12-09 23:38:41 +00:00
bool expected ,
bool itemLocked = false ,
bool baseItemEnabled = true ,
bool providerForced = false ,
bool supportsLocalMetadata = true ,
bool ownedItem = false )
{
var item = new MetadataTestItem
{
IsLocked = itemLocked ,
OwnerId = ownedItem ? Guid . NewGuid ( ) : Guid . Empty ,
EnableLocalMetadata = supportsLocalMetadata
} ;
var providerName = "provider" ;
2021-12-19 22:33:27 +00:00
var provider = MockIMetadataProviderMapper < MetadataTestItem , MetadataTestItemInfo > ( providerType , providerName , forced : providerForced ) ;
2021-12-09 23:38:41 +00:00
var baseItemManager = new Mock < IBaseItemManager > ( MockBehavior . Strict ) ;
2021-12-18 20:40:27 +00:00
baseItemManager . Setup ( i = > i . IsMetadataFetcherEnabled ( item , It . IsAny < TypeOptions > ( ) , providerName ) )
2021-12-09 23:38:41 +00:00
. Returns ( baseItemEnabled ) ;
using var providerManager = GetProviderManager ( baseItemManager : baseItemManager . Object ) ;
AddParts ( providerManager , metadataProviders : new [ ] { provider } ) ;
var actualProviders = providerManager . GetMetadataProviders < MetadataTestItem > ( item , new LibraryOptions ( ) ) . ToArray ( ) ;
Assert . Equal ( expected ? 1 : 0 , actualProviders . Length ) ;
}
2021-12-20 23:24:07 +00:00
private static Mock < IMetadataService > MockIMetadataService ( bool refreshPrimary , bool canRefresh , int order = 0 )
{
var service = new Mock < IMetadataService > ( MockBehavior . Strict ) ;
service . Setup ( s = > s . Order )
. Returns ( order ) ;
service . Setup ( s = > s . CanRefreshPrimary ( It . IsAny < Type > ( ) ) )
. Returns ( refreshPrimary ) ;
service . Setup ( s = > s . CanRefresh ( It . IsAny < BaseItem > ( ) ) )
. Returns ( canRefresh ) ;
service . Setup ( s = > s . RefreshMetadata ( It . IsAny < BaseItem > ( ) , It . IsAny < MetadataRefreshOptions > ( ) , It . IsAny < CancellationToken > ( ) ) )
. Returns ( Task . FromResult ( ItemUpdateType . MetadataDownload ) ) ;
return service ;
}
2021-12-07 23:52:57 +00:00
private static IImageProvider MockIImageProvider < TProviderType > ( string name , BaseItem expectedType , bool supports = true , int? order = null , bool errorOnSupported = false )
where TProviderType : class , IImageProvider
2021-12-05 16:19:40 +00:00
{
Mock < IHasOrder > ? hasOrder = null ;
2022-12-05 14:01:13 +00:00
if ( order is not null )
2021-12-05 16:19:40 +00:00
{
hasOrder = new Mock < IHasOrder > ( MockBehavior . Strict ) ;
hasOrder . Setup ( i = > i . Order )
. Returns ( ( int ) order ) ;
}
2022-12-05 14:00:20 +00:00
var provider = hasOrder is null
2021-12-07 23:52:57 +00:00
? new Mock < TProviderType > ( MockBehavior . Strict )
: hasOrder . As < TProviderType > ( ) ;
2021-12-05 16:19:40 +00:00
provider . Setup ( p = > p . Name )
. Returns ( name ) ;
2021-12-05 23:09:46 +00:00
if ( errorOnSupported )
{
provider . Setup ( p = > p . Supports ( It . IsAny < BaseItem > ( ) ) )
2021-12-09 23:38:41 +00:00
. Throws ( new ArgumentException ( "Provider threw exception on Supports(item)" ) ) ;
2021-12-05 23:09:46 +00:00
}
else
{
provider . Setup ( p = > p . Supports ( expectedType ) )
. Returns ( supports ) ;
}
2021-12-05 16:19:40 +00:00
return provider . Object ;
}
2021-12-09 23:38:41 +00:00
private static IMetadataProvider < TItemType > MockIMetadataProviderMapper < TItemType , TLookupInfoType > ( string typeName , string providerName , int? order = null , bool forced = false )
2021-12-07 23:52:57 +00:00
where TItemType : BaseItem , IHasLookupInfo < TLookupInfoType >
where TLookupInfoType : ItemLookupInfo , new ( )
= > typeName switch
{
2021-12-09 23:38:41 +00:00
"ILocalMetadataProvider" = > MockIMetadataProvider < ILocalMetadataProvider < TItemType > , TItemType > ( providerName , order , forced ) ,
"IRemoteMetadataProvider" = > MockIMetadataProvider < IRemoteMetadataProvider < TItemType , TLookupInfoType > , TItemType > ( providerName , order , forced ) ,
"ICustomMetadataProvider" = > MockIMetadataProvider < ICustomMetadataProvider < TItemType > , TItemType > ( providerName , order , forced ) ,
_ = > MockIMetadataProvider < IMetadataProvider < TItemType > , TItemType > ( providerName , order , forced )
2021-12-07 23:52:57 +00:00
} ;
2021-12-09 23:38:41 +00:00
private static IMetadataProvider < TItemType > MockIMetadataProvider < TProviderType , TItemType > ( string name , int? order = null , bool forced = false )
2021-12-07 23:52:57 +00:00
where TProviderType : class , IMetadataProvider < TItemType >
where TItemType : BaseItem
{
2021-12-09 23:38:41 +00:00
Mock < IForcedProvider > ? forcedProvider = null ;
if ( forced )
{
forcedProvider = new Mock < IForcedProvider > ( ) ;
}
2021-12-07 23:52:57 +00:00
Mock < IHasOrder > ? hasOrder = null ;
2022-12-05 14:01:13 +00:00
if ( order is not null )
2021-12-07 23:52:57 +00:00
{
2022-12-05 14:00:20 +00:00
hasOrder = forcedProvider is null ? new Mock < IHasOrder > ( ) : forcedProvider . As < IHasOrder > ( ) ;
2021-12-07 23:52:57 +00:00
hasOrder . Setup ( i = > i . Order )
. Returns ( ( int ) order ) ;
}
2022-12-05 14:00:20 +00:00
var provider = hasOrder is null
2021-12-07 23:52:57 +00:00
? new Mock < TProviderType > ( MockBehavior . Strict )
: hasOrder . As < TProviderType > ( ) ;
provider . Setup ( p = > p . Name )
. Returns ( name ) ;
return provider . Object ;
}
2021-12-09 23:38:41 +00:00
private static LibraryOptions CreateLibraryOptions (
string typeName ,
string [ ] ? imageFetcherOrder = null ,
string [ ] ? localMetadataReaderOrder = null ,
string [ ] ? metadataFetcherOrder = null )
{
var libraryOptions = new LibraryOptions
{
LocalMetadataReaderOrder = localMetadataReaderOrder
} ;
// only create type options if populating it with something
2022-12-05 14:01:13 +00:00
if ( imageFetcherOrder is not null | | metadataFetcherOrder is not null )
2021-12-09 23:38:41 +00:00
{
imageFetcherOrder ? ? = Array . Empty < string > ( ) ;
metadataFetcherOrder ? ? = Array . Empty < string > ( ) ;
libraryOptions . TypeOptions = new [ ]
{
new TypeOptions
{
Type = typeName ,
ImageFetcherOrder = imageFetcherOrder ,
MetadataFetcherOrder = metadataFetcherOrder
}
} ;
}
return libraryOptions ;
}
private static ServerConfiguration CreateServerConfiguration (
string typeName ,
string [ ] ? imageFetcherOrder = null ,
string [ ] ? localMetadataReaderOrder = null ,
string [ ] ? metadataFetcherOrder = null )
{
var serverConfiguration = new ServerConfiguration ( ) ;
// only create type options if populating it with something
2022-12-05 14:01:13 +00:00
if ( imageFetcherOrder is not null | | localMetadataReaderOrder is not null | | metadataFetcherOrder is not null )
2021-12-09 23:38:41 +00:00
{
imageFetcherOrder ? ? = Array . Empty < string > ( ) ;
localMetadataReaderOrder ? ? = Array . Empty < string > ( ) ;
metadataFetcherOrder ? ? = Array . Empty < string > ( ) ;
serverConfiguration . MetadataOptions = new [ ]
{
new MetadataOptions
{
ItemType = typeName ,
ImageFetcherOrder = imageFetcherOrder ,
LocalMetadataReaderOrder = localMetadataReaderOrder ,
MetadataFetcherOrder = metadataFetcherOrder
}
} ;
}
return serverConfiguration ;
}
private static ProviderManager GetProviderManager (
ServerConfiguration ? serverConfiguration = null ,
LibraryOptions ? libraryOptions = null ,
IBaseItemManager ? baseItemManager = null )
2021-12-05 16:19:40 +00:00
{
var serverConfigurationManager = new Mock < IServerConfigurationManager > ( MockBehavior . Strict ) ;
serverConfigurationManager . Setup ( i = > i . Configuration )
. Returns ( serverConfiguration ? ? new ServerConfiguration ( ) ) ;
var libraryManager = new Mock < ILibraryManager > ( MockBehavior . Strict ) ;
libraryManager . Setup ( i = > i . GetLibraryOptions ( It . IsAny < BaseItem > ( ) ) )
. Returns ( libraryOptions ? ? new LibraryOptions ( ) ) ;
var providerManager = new ProviderManager (
2021-12-31 23:22:46 +00:00
Mock . Of < IHttpClientFactory > ( ) ,
Mock . Of < ISubtitleManager > ( ) ,
2021-12-05 16:19:40 +00:00
serverConfigurationManager . Object ,
2021-12-31 23:22:46 +00:00
Mock . Of < ILibraryMonitor > ( ) ,
2021-12-20 23:24:07 +00:00
_logger ,
2021-12-31 23:22:46 +00:00
Mock . Of < IFileSystem > ( ) ,
Mock . Of < IServerApplicationPaths > ( ) ,
2021-12-05 16:19:40 +00:00
libraryManager . Object ,
2024-02-26 12:09:40 +00:00
baseItemManager ! ,
2024-03-03 20:32:55 +00:00
Mock . Of < ILyricManager > ( ) ,
Mock . Of < IMemoryCache > ( ) ) ;
2021-12-05 16:19:40 +00:00
return providerManager ;
}
private static void AddParts (
ProviderManager providerManager ,
IEnumerable < IImageProvider > ? imageProviders = null ,
IEnumerable < IMetadataService > ? metadataServices = null ,
IEnumerable < IMetadataProvider > ? metadataProviders = null ,
IEnumerable < IMetadataSaver > ? metadataSavers = null ,
2024-07-17 13:48:31 +00:00
IEnumerable < IExternalId > ? externalIds = null ,
IEnumerable < IExternalUrlProvider > ? externalUrlProviders = null )
2021-12-05 16:19:40 +00:00
{
imageProviders ? ? = Array . Empty < IImageProvider > ( ) ;
metadataServices ? ? = Array . Empty < IMetadataService > ( ) ;
metadataProviders ? ? = Array . Empty < IMetadataProvider > ( ) ;
metadataSavers ? ? = Array . Empty < IMetadataSaver > ( ) ;
externalIds ? ? = Array . Empty < IExternalId > ( ) ;
2024-07-17 13:48:31 +00:00
externalUrlProviders ? ? = Array . Empty < IExternalUrlProvider > ( ) ;
2021-12-05 16:19:40 +00:00
2024-07-17 13:48:31 +00:00
providerManager . AddParts ( imageProviders , metadataServices , metadataProviders , metadataSavers , externalIds , externalUrlProviders ) ;
2021-12-05 16:19:40 +00:00
}
2021-12-07 23:52:57 +00:00
/// <summary>
2021-12-09 23:38:41 +00:00
/// Simple <see cref="BaseItem"/> extension to make SupportsLocalMetadata directly settable.
2021-12-07 23:52:57 +00:00
/// </summary>
2021-12-31 23:22:46 +00:00
internal class MetadataTestItem : BaseItem , IHasLookupInfo < MetadataTestItemInfo >
2021-12-07 23:52:57 +00:00
{
2021-12-09 23:38:41 +00:00
public bool EnableLocalMetadata { get ; set ; } = true ;
public override bool SupportsLocalMetadata = > EnableLocalMetadata ;
2021-12-07 23:52:57 +00:00
public MetadataTestItemInfo GetLookupInfo ( )
{
return GetItemLookupInfo < MetadataTestItemInfo > ( ) ;
}
}
2021-12-31 23:22:46 +00:00
internal class MetadataTestItemInfo : ItemLookupInfo
2021-12-07 23:52:57 +00:00
{
}
2021-12-05 16:19:40 +00:00
}
}