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

PixelProviderRenditionDiskStatusCacheTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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 Rx = require 'Rx' 

	-- wf.core
local KSPathUtils = require 'KSPathUtils'
local WFRxTestCase = require 'WFRxTestCase'

	-- wf.managed_catalog.pixel_provider
local PixelProviderRenditionDiskStatusCache = 
		require "PixelProviderRenditionDiskStatusCache"

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

local randomUUID = require 'KSIdGenerator'.uuidInLowercaseHex

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

local function makeCache()

	local deletedAssetIdsStream = Rx.Subject()

	local pixelsPath = KSPathUtils.child( 
						KSPathUtils.getStandardFilePath( 'temp' ), 
						'PixelProviderRenditionDiskStatusCacheTest' )

	local cache = 
			PixelProviderRenditionDiskStatusCache( 
				pixelsPath, deletedAssetIdsStream )

	return cache, deletedAssetIdsStream

end

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

local function rxTestBadInputs( rxTest )

	local diskCache = makeCache()

	for _, localAssetId in ipairs( { false, randomUUID(), 1 } ) do
		for _, longSide in ipairs( { false, 640 } ) do
			for _, digest in ipairs { false, "wut", "" } do
				for _, path in ipairs { false, 1, "wut" } do

					local args = { localAssetId, longSide, digest, path }

					for i = 1, 4 do

						if not args[ i ] then

							args[ i ] = nil

						end

					end

					rxTest:expectError(
						diskCache.addRendition,
							diskCache,
							unpack( args, 1, 4 ) )

				end
			end
		end
	end

	for _, localAssetId in ipairs( { randomUUID(), 1 } ) do
		for _, digest in ipairs { "wut", "" } do

			rxTest:expectError(
				diskCache.getRenditionStatus,
					diskCache,
					localAssetId,
					640,
					digest )

		end
	end

	rxTest:expectError(
		diskCache.maybeGetHighestResCurrentRendition,
			diskCache,
			randomUUID() )

	diskCache:close()

end

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

local function rxTestEmptyCache( rxTest )

	local diskCache = makeCache()

	rxTest:expectEqual(

		diskCache:getRenditionStatus( 1, 640, randomUUID() ),

		0 )

	rxTest:expectEqual(

		diskCache:getRenditionStatus( 1, 640 ),

		0 )

	rxTest:expectEqual(

		diskCache:maybeGetHighestResCurrentRendition( 1 ),

		false )

	diskCache:close()

end

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

local function rxTestClosedCache( rxTest )

	local diskCache = makeCache()

	diskCache:close()

	rxTest:expectEqual(

		diskCache:getRenditionStatus( 1, 640, randomUUID() ),

		0 )

	rxTest:expectEqual(

		diskCache:getRenditionStatus( 1, 640 ),

		0 )

	rxTest:expectEqual(

		diskCache:maybeGetHighestResCurrentRendition( 1 ),

		false )

end

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

local function path( s, n )

	return KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 
									string.format( "%s_%d.jpg", s, n ) )

end

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

local function rxTestGetRenditionStatus( rxTest )

	local diskCache = makeCache()

	local digest = randomUUID()

	local otherDigest = randomUUID()

	for _, shouldAdd in ipairs( { true, false } ) do

		local addAPI = shouldAdd and "addRendition" or "addNoRendition"

		local args640 = { diskCache, 1, 640, digest }

		if shouldAdd then

			args640[ #args640 + 1 ] = path( "path", 1 )

		end

		diskCache[ addAPI ]( unpack( args640 ) )

		local args320 = { diskCache, 1, 320, digest }

		if shouldAdd then

			args320[ #args320 + 1 ] = path( "path", 2 )

		end

		diskCache[ addAPI ]( unpack( args320 ) )

		local expectedNonZeroStatusValue = shouldAdd and 1 or -1

		for _, longSide in ipairs( { 320, 640, 1280 } ) do
			for _, aDigest in ipairs( { digest, otherDigest } )  do

				rxTest:logf( "testing getRenditionStatus long side: %s", longSide )

				rxTest:expectEqual(

					longSide ~= 1280 and aDigest == digest and 
						expectedNonZeroStatusValue or 0,

					diskCache:getRenditionStatus( 1, longSide, aDigest ) )

				rxTest:expectEqual(

					longSide ~= 1280 and expectedNonZeroStatusValue or 0,

					diskCache:getRenditionStatus( 1, longSide ) )


			end

		end

		diskCache[ addAPI ]( diskCache, 1, 640, otherDigest, path( "path", 3 ) )

		for _, longSide in ipairs( { 320, 640, 1280 } ) do
			for _, aDigest in ipairs( { digest, otherDigest } )  do

				rxTest:expectEqual(

					( ( longSide == 320 and aDigest == digest ) or
						( longSide == 640 and aDigest == otherDigest ) ) and 
							expectedNonZeroStatusValue or 0,

					diskCache:getRenditionStatus( 1, longSide, aDigest ) )

				rxTest:expectEqual(

					( longSide == 320 or longSide == 640 ) and 
							expectedNonZeroStatusValue or 0,

					diskCache:getRenditionStatus( 1, longSide ) )

			end

		end
	
	end

	diskCache:close()

end

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

local function rxTestHaveCheckedAllRenditionSizes( rxTest )

	local renderSizes = { 320, 640, 1280 }

	local addAPIs = { false, "addRendition", "addNoRendition" }

	for i = 1, ( #addAPIs ) ^ ( #renderSizes ) do

		local diskCache = makeCache()

		local expectedValue = true

		for j = 1, #renderSizes do

			local addAPI = 
				addAPIs[ ( math.floor( i / j  ^ ( #renderSizes - 1 ) ) % ( #addAPIs ) ) + 1 ]

			expectedValue = expectedValue and addAPI ~= false

			if addAPI then

				local digest = randomUUID()
				
				local args = { diskCache, 1, renderSizes[ j ], digest }
									
				if addAPI == "addRendition" then
				
					args[ #args + 1 ] = path( "path", i )

				end

				diskCache[ addAPI ]( unpack( args ) )

			end

		end

		rxTest:expectEqual(

			expectedValue,

			diskCache:haveCheckedAllRenditionSizes( 1, renderSizes ) )

		diskCache:close()

	end

end

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

local function rxTestMaybeGetPath( rxTest )

	local sizes = { 320, 640, 1280 }

	for _, longSide in ipairs( sizes ) do

		local diskCache = makeCache()

		local digest = randomUUID()

		diskCache:addRendition( 1, longSide, digest, path( "path", longSide ) )

		for _, longSideCheck in ipairs( sizes ) do
		
			local expected = 
					longSide == longSideCheck and 
						{ path( "path", longSide ), longSide, digest } or
						{ false }

			rxTest:expectEqual(

				expected,

				{ diskCache:maybeGetPath( 1, longSideCheck ) } )


		end

		diskCache:close()

	end

end

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

local function rxTestMaybeGetBestPath( rxTest )

	local diskCache = makeCache()

	rxTest:expectEqual(

		diskCache:maybeGetHighestResCurrentRendition( 1 ),

		false )

	local digest = randomUUID()

	diskCache:addRendition( 1, 1280, digest, path( "path", 1280 ) )

	diskCache:addRendition( 1, 640, digest, path( "path", 640 ) )

	diskCache:addRendition( 1, 320, digest, path( "path", 320 ) )

	rxTest:log( "Testing expected longSide 1280" )

	rxTest:expectEqual(

		diskCache:maybeGetHighestResCurrentRendition( 1 ),

		path( "path", 1280 ) )

	local otherDigest = randomUUID()

	-- now test that we pick the most recent digest, even if it's smaller than
	-- others with older digests

	diskCache:addRendition( 1, 320, otherDigest, path( "other", 320 )  )

	rxTest:log( "Testing expected longSide 320" )

	rxTest:expectEqual(

		diskCache:maybeGetHighestResCurrentRendition( 1 ),

		path( "other", 320 ) )


	diskCache:close()

end

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

local function getPathName( prefix, id )

	return string.format( "%s%s", prefix, id )

end

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

local function makeFakePath( fileName, longSide )

	return path( fileName, longSide )

end

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

local function rxTestRemoveAllRenditionsForLocalAssetId( rxTest )

	local diskCache, deletedAssetIdsStream = makeCache()

	local longSides = { 1280, 640, 320 }

	for id = 1, 3 do

		rxTest
			:expectEqual(
				diskCache
					:maybeGetHighestResCurrentRendition( id ),
				false )

		local digest = randomUUID()

		local pathName = getPathName( "path", id )

		rxTest:logf( "Testing all expected paths exist for id %s", id )

		for _, longSide in pairs( longSides ) do

			diskCache
				:addRendition( 
					id, longSide, digest, makeFakePath( pathName, longSide ) )

			rxTest
				:expectEqual(
					diskCache
						:maybeGetPath( id, longSide ),
					makeFakePath( pathName, longSide ) )

		end

		rxTest:logf( "Testing expected highest res longSide 1280 for id %s", id )

		rxTest
			:expectEqual(
				diskCache
					:maybeGetHighestResCurrentRendition( id ),
				makeFakePath( pathName, 1280 ) )

		local otherPathName = getPathName( "other", id )

		local otherDigest = randomUUID()

		diskCache
			:addRendition( 
				id, 320, otherDigest, makeFakePath( otherPathName, 320 )  )

		rxTest:logf( "Testing expected highest res longSide 320 for id %s after new digest", id )

		rxTest
			:expectEqual(
				diskCache
					:maybeGetHighestResCurrentRendition( id ),
				makeFakePath( otherPathName, 320 ) )

		rxTest:logf( "Testing expected paths for all longSides for id %s after new digest", id )

		for _, longSide in pairs( longSides ) do

			local whichPathName = longSide == 320 and 
									otherPathName or pathName

			rxTest
				:expectEqual(
					diskCache
						:maybeGetPath( id, longSide ),
					makeFakePath( whichPathName, longSide ) )

		end

	end

	rxTest:log( "Remove all cached rendition info for ids 1, 2  and confirm they return no paths" )

	deletedAssetIdsStream:onNext( { 1, 2 } )

	for id = 1, 2 do

		rxTest
			:expectEqual(
				diskCache
					:maybeGetHighestResCurrentRendition( id ),
				false )

		for _, longSide in pairs( longSides ) do

			rxTest:expectEqual(
				diskCache
					:maybeGetPath( id, longSide ),
				false )

		end

	end

	rxTest:log( "Confirm all cached rendition info for id 3 still exists" )

	for _, longSide in pairs( longSides ) do

		local whichPathName = longSide == 320 and 
								getPathName( "other", 3 ) or
								getPathName( "path", 3 )

		rxTest
			:expectEqual(
				diskCache
					:maybeGetPath( 3, longSide ),
				makeFakePath( whichPathName, longSide ) )

	end

	rxTest
		:expectEqual(
			diskCache
				:maybeGetHighestResCurrentRendition( 3 ),
			makeFakePath( getPathName( "other", 3 ), 320 ) )

	diskCache:close()

end

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

return WFRxTestCase.wrapSuite( 'PixelProviderRenditionDiskStatusCacheTest', {

	rxTestBadInputs = rxTestBadInputs,

	rxTestEmptyCache = rxTestEmptyCache,

	rxTestClosedCache = rxTestClosedCache,

	rxTestGetRenditionStatus = rxTestGetRenditionStatus,

	rxTestHaveCheckedAllRenditionSizes = rxTestHaveCheckedAllRenditionSizes,

	rxTestMaybeGetPath = rxTestMaybeGetPath,

	rxTestRemoveAllRenditionsForLocalAssetId = 
		rxTestRemoveAllRenditionsForLocalAssetId,

	rxTestMaybeGetBestPath = rxTestMaybeGetBestPath,

} )
