--[[----------------------------------------------------------------------------

PixelProviderSharedTaskUtilsTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2022 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law. Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

local WFRxTestCase = require 'WFRxTestCase'
local PixelProviderSharedTaskUtils = require 'PixelProviderSharedTaskUtils'

--==============================================================================

local function makeRenditionTest( maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName )

	local maybeGetHrefForRenditionWithSizeFromRenderInfoFunc =
			assert( 
				PixelProviderSharedTaskUtils[ maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName ],
				string.format( "PixelProviderSharedTaskUtils is missing %s", 
								maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName ) )
			
	local renditionTypeKey =
			( maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName ==
					"maybeGetHrefForInvalidRenditionWithSizeFromRenderInfo" and
				"invalid_renditions" ) or
			"renditions"

	return function( rxTest )

		rxTest:logf( "Testing %s with no renderInfo", 
						maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName )

		rxTest
			:expectError(
				function() maybeGetHrefForRenditionWithSizeFromRenderInfoFunc( nil, "640" ) end,
				':ignore:' )

		local renderInfo = {}

		rxTest:logf( "Testing %s with empty %s slot in renderInfo", 
						maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName,
						renditionTypeKey )

		rxTest
			:expectEqual(
				maybeGetHrefForRenditionWithSizeFromRenderInfoFunc( renderInfo, "640" ),
				false )
	
		local renditionsOfTypeArray = {}

		renderInfo[ renditionTypeKey ] = renditionsOfTypeArray

		rxTest:logf( "Testing %s with non-empty table with no array entries in %s slot in renderInfo", 
						maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName,
						renditionTypeKey )

		renditionsOfTypeArray = {}

		renderInfo.unusedNonArrayKey = "SomeValue"

		renderInfo[ renditionTypeKey ] = renditionsOfTypeArray

		rxTest
			:expectEqual(
				maybeGetHrefForRenditionWithSizeFromRenderInfoFunc( renderInfo, "640" ),
				false )
	
		renditionsOfTypeArray = { { size = "1280", href = "the_url" } }

		renderInfo[ renditionTypeKey ] = renditionsOfTypeArray

		rxTest:logf( "Testing %s with empty table with no matching array entries in %s slot in renderInfo", 
						maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName,
						renditionTypeKey )

		rxTest
			:expectEqual(
				maybeGetHrefForRenditionWithSizeFromRenderInfoFunc( renderInfo, "640" ),
				false )
	
		renditionsOfTypeArray = { { size = "1280", href = "the_url" } }

		renditionsOfTypeArray.unusedNonArrayKey = "SomeValue"

		renderInfo[ renditionTypeKey ] = renditionsOfTypeArray

		rxTest:logf( "Testing %s with non-empty table with no matching array entries in %s slot in renderInfo", 
						maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName,
						renditionTypeKey )

		rxTest
			:expectEqual(
				maybeGetHrefForRenditionWithSizeFromRenderInfoFunc( renderInfo, "640" ),
				false )
	
		renditionsOfTypeArray = { 
				{ size = "640", href = "the_url"}, 
				{ size = "1280", href = "not_used" } 
		}

		renderInfo[ renditionTypeKey ] = renditionsOfTypeArray

		rxTest:logf( "Testing %s with empty table matching array entries in %s slot in renderInfo", 
						maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName,
						renditionTypeKey )

		rxTest
			:expectEqual(
				maybeGetHrefForRenditionWithSizeFromRenderInfoFunc( renderInfo, "640" ),
				"the_url" )
	
		renditionsOfTypeArray = { 
				{ size = "640", href = "the_url"}, 
				{ size = "1280", href = "not_used" } 
		}

		renditionsOfTypeArray.unusedNonArrayKey = "SomeValue"

		renderInfo[ renditionTypeKey ] = renditionsOfTypeArray

		rxTest:logf( "Testing %s with non-empty table with matching array entries in %s slot in renderInfo", 
						maybeGetHrefForRenditionWithSizeFromRenderInfoFuncName,
						renditionTypeKey )

		rxTest
			:expectEqual(
				maybeGetHrefForRenditionWithSizeFromRenderInfoFunc( renderInfo, "640" ),
				"the_url" )

	end

end

--------------------------------------------------------------------------------

local _renderInfoNoProxies = {}

--------------------------------------------------------------------------------

local _renderInfoEmptyProxies = 
				{
					proxies = {}
				}

--------------------------------------------------------------------------------

local _renderInfoProxiesNoArray = 
				{
					proxies = {
						someNonArrayKey = "someValue"
					}
				}

--------------------------------------------------------------------------------

local _renderInfoProxies2560Array = 
				{
					proxies = { 
						{ size = "2560", href = "2560Href" }
					}
				} 

--------------------------------------------------------------------------------

local _renderInfoProxies2560ArrayWithExtra = 
				{
					proxies = { 
						someNonArrayKey = "someValue",
						{ size = "2560", href = "2560Href" }
					}
				} 

--------------------------------------------------------------------------------

local function makeRenderInfoWithVideoProxySizeImp( videoSubtype, videoProxySize )

	return {
		subtype = videoSubtype,
		proxies = { 
			{ 
				size = videoProxySize, 
				href = string.format( "%sHref", videoProxySize)  
			}
		}
	} 
end

--------------------------------------------------------------------------------

local _renderInfoProxies1080pVideo = 
		makeRenderInfoWithVideoProxySizeImp( "video", "1080p" )

--------------------------------------------------------------------------------

local _renderInfoProxies1080pTrashVideo = 
		makeRenderInfoWithVideoProxySizeImp( "video", "1080p" )

--------------------------------------------------------------------------------

local _renderInfoProxies720pVideo = 
		makeRenderInfoWithVideoProxySizeImp( "video", "720p" )

--------------------------------------------------------------------------------

local _renderInfoProxies360pVideo = 
		makeRenderInfoWithVideoProxySizeImp( "video", "360p" )

--------------------------------------------------------------------------------

local _renderInfoProxies1080pAnd360pVideo = 
		makeRenderInfoWithVideoProxySizeImp( "video", "1080p" )

_renderInfoProxies1080pAnd360pVideo.proxies[ #_renderInfoProxies1080pAnd360pVideo.proxies + 1 ] =
	_renderInfoProxies360pVideo.proxies[ 1 ]

-------------------------------------------------------------------------------- 

local function rxTestMaybeGetHrefForProxyTypeFromRenderInfo( rxTest )

	rxTest:log( "check bad renderInfo" )

	rxTest
		:expectError(
			function() PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( nil, "2560") end )

	rxTest:log( "check renderInfo without proxies" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoNoProxies, "2560" ),
			false )

	rxTest:log( "check renderInfo with empty proxies" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoEmptyProxies, "2560" ),
			false )

	rxTest:log( "check renderInfo with empty proxies array, but other k/v pairs" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxiesNoArray, "2560" ), 
			false )

	rxTest:log( "check renderInfo with non-empty proxies array, no other k/v pairs" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies2560Array, "2560" ),
			"2560Href" )

	rxTest:log( "check renderInfo with non-empty proxies array, ask for 5120" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies2560Array, "5120" ),
			false )

	rxTest:log( "check renderInfo with non-empty proxies array and other k/v pairs" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies2560ArrayWithExtra, "2560" ),
			"2560Href" )

	rxTest:log( "check renderInfo with 1080p proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies1080pVideo, "1080p" ),
			"1080pHref" )

	rxTest:log( "check renderInfo with 1080p trash proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies1080pTrashVideo, "1080p" ),
			"1080pHref" )

	rxTest:log( "check renderInfo with 720p proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies720pVideo, "720p" ),
			"720pHref" )

	rxTest:log( "check renderInfo with 360p proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies360pVideo, "360p" ),
			"360pHref" )

	rxTest:log( "check renderInfo with 1080p and 360p proxy ask for 1080p" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies1080pAnd360pVideo, "1080p" ),
			"1080pHref" )

	rxTest:log( "check renderInfo with 1080p and 360p proxy ask for 360p" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies1080pAnd360pVideo, "360p" ),
			"360pHref" )

	rxTest:log( "check renderInfo with 360p proxy ask for 720p" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetHrefForProxyTypeFromRenderInfo( 
				_renderInfoProxies1080pAnd360pVideo, "720p" ),
			false )

end

--------------------------------------------------------------------------------

local function rxTestMaybeGetBinarySubtypeForProxy( rxTest )

	rxTest:log( "check bad renderInfo" )

	local localAssetId = 1

	local baseRenderInfoCache = PixelProviderSharedTaskUtils.makeBaseRenderInfoCache()

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			false )

	rxTest:log( "check renderInfo without proxies" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoNoProxies )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			false )

	rxTest:log( "check renderInfo with empty proxies" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoEmptyProxies )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			false )

	rxTest:log( "check renderInfo with empty proxies array, but other k/v pairs" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxiesNoArray )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			false )

	rxTest:log( "check renderInfo with non-empty proxies array, no other k/v pairs" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxies2560Array )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			"2560" )

	rxTest:log( "check renderInfo with non-empty proxies array and other k/v pairs" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxies2560ArrayWithExtra )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			"2560" )

	rxTest:log( "check renderInfo with 1080p proxy" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxies1080pVideo )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			"1080p" )

	rxTest:log( "check renderInfo with 1080p trash proxy" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxies1080pTrashVideo )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			"1080p" )

	rxTest:log( "check renderInfo with 720p proxy" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxies720pVideo )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			"720p" )

	rxTest:log( "check renderInfo with 360p proxy" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxies360pVideo )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			false )

	rxTest:log( "check renderInfo with 1080p and 360p proxy" )

	baseRenderInfoCache
		:pushCacheItem( localAssetId, _renderInfoProxies1080pAnd360pVideo )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.maybeGetBinarySubtypeForProxy( baseRenderInfoCache, localAssetId ),
			"1080p" )

end

-------------------------------------------------------------------------------- 

local function rxTestRenderInfoHasDownloadableProxy( rxTest )

	rxTest:log( "check bad renderInfo" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy(),
			false )

	rxTest:log( "check renderInfo without proxies" )


	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoNoProxies ),
			false )

	rxTest:log( "check renderInfo with empty proxies" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoEmptyProxies ),
			false )

	rxTest:log( "check renderInfo with empty proxies array, but other k/v pairs" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoProxiesNoArray ), 
			false )

	rxTest:log( "check renderInfo with non-empty proxies array, no other k/v pairs" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoProxies2560Array ),
			true )

	rxTest:log( "check renderInfo with non-empty proxies array and other k/v pairs" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoProxies2560ArrayWithExtra ),
			true )

	rxTest:log( "check renderInfo with 1080p proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoProxies1080pVideo ),
			true )

	rxTest:log( "check renderInfo with 1080p trash proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoProxies1080pTrashVideo ),
			true )

	rxTest:log( "check renderInfo with 720p proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoProxies720pVideo ),
			true )

	rxTest:log( "check renderInfo with 360p proxy" )

	rxTest
		:expectEqual(
			PixelProviderSharedTaskUtils.renderInfoHasDownloadableProxy( 
				_renderInfoProxies360pVideo ),
			false )

end

--==============================================================================

return WFRxTestCase.wrapSuite( 'PixelProviderSharedTaskUtilsTest', {


	rxTestMaybeGetHrefForValidRenditionWithSizeFromRenderInfo =
		makeRenditionTest( "maybeGetHrefForValidRenditionWithSizeFromRenderInfo" ),

	rxTestMaybeGetHrefForInvalidRenditionWithSizeFromRenderInfo =
		makeRenditionTest( "maybeGetHrefForInvalidRenditionWithSizeFromRenderInfo" ),

	rxTestMaybeGetHrefForProxyTypeFromRenderInfo =
		rxTestMaybeGetHrefForProxyTypeFromRenderInfo,

	rxTestMaybeGetBinarySubtypeForProxy = rxTestMaybeGetBinarySubtypeForProxy,

	rxTestRenderInfoHasDownloadableProxy = 
		rxTestRenderInfoHasDownloadableProxy,

} )
