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

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.

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

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'
local WFTestMetrics = require 'WFTestMetrics'

	-- WF.oz_client.managed_catalog
local WFCatMetadataPuller = require 'WFCatMetadataPuller'
local WFCatMetadataPusher = require 'WFCatMetadataPusher'

	-- WF.oz_client.user
local WFOzProductionUserLogin = require 'WFOzProductionUserLogin'

	-- WF.core.utils
local KSStringUtils = require 'KSStringUtils'

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

-- @mark WFCatMetadataPullerPerformanceTest namespace

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

local WFCatMetadataPullerPerformanceTest = {}

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

local function pullLargeCatalogWithDocumentBatchCount( rxTest, documentBatchCount )

	rxTest:setTimeLimit( 500 )
	
	rxTest:recordPerformanceReport()
	
	local email = 'wfozusertest+perf1@gmail.com'
	
	local testMetrics = WFTestMetrics.populateTestMetrics( rxTest:nativeTest() )
	
	local catalog = WFOzProductionUserLogin.createWFCatalogForCredentials( 
						rxTest, email, tostring( KSStringUtils.decodeBase64( 'QWRvYmV0M3N0' ) ) )
						
	local options = {}
	
	if not documentBatchCount then
		options = {
			isCatalogPuller = true,
		}
	else 
		local newDocumentBatchCount = documentBatchCount
		options = {
			isCatalogPuller = true,
			documentBatchCount = newDocumentBatchCount
		}
	end 
	
	local puller = WFCatMetadataPuller( catalog, "Test Puller", options )
	local pusher = WFCatMetadataPusher( catalog )
	
	rxTest:finally( function()
	
		puller:close()
		pusher:close()
		
	end )
	
	puller:getUpdatesContinuously()
	rxTest:waitFor( pusher:sendUpdatesContinuously() )
	
	local initialSyncTime
	
	local obs = rxTest:observe(
	
		catalog:docStore():ongoingLocalDocValue( 'Oz-isDoingInitialSync' )
		
			:where( function( docValue )
			
				return not docValue._empty
				
			end )
			
			:where( function( docValue ) 
			
				return not docValue.isDoingInitialSync
				
			end )
			
			:tap( function( docValue )
			
				initialSyncTime = docValue.endTimestamp - docValue.beginTimestamp
				
			end )
			
			:map( function( docValue )
				
				return docValue.isDoingInitialSync
				
			end ) )
			
	obs:expectData( false )
	
	testMetrics[ "wfp_execution_time" ] = initialSyncTime

	WFTestMetrics.writeTestMetrics( testMetrics )
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalog_testPerformance( rxTest )

	pullLargeCatalogWithDocumentBatchCount(rxTest)
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalogWithDocumentBatchCount200_testPerformance( rxTest )

	pullLargeCatalogWithDocumentBatchCount(rxTest, 200)
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalogWithDocumentBatchCount500_testPerformance( rxTest )

	pullLargeCatalogWithDocumentBatchCount(rxTest, 500)
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalogWithDocumentBatchCount800_testPerformance( rxTest )

	pullLargeCatalogWithDocumentBatchCount(rxTest, 800)
	
end

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

local function pullLargeCatalogLargeXMPWithDocumentBatchCount( rxTest, documentBatchCount )
	
	rxTest:setTimeLimit( 2000 )
	
	-- An account cloned from large catalog with large external XMP setting
	local email = 'wfoztestuser+perf1@gmail.com'
	
	local testMetrics = WFTestMetrics.populateTestMetrics( rxTest:nativeTest() )
	
	local password = tostring( KSStringUtils.decodeBase64( 'QWRvYmV0M3N0' ))
	
	local catalog = WFOzProductionUserLogin.createWFCatalogForCredentials( 
						rxTest, email, password )
		
	local options = {}
	
		if not documentBatchCount then
		options = {
			isCatalogPuller = true,
		}
	else 
		local newDocumentBatchCount = documentBatchCount
		options = {
			isCatalogPuller = true,
			documentBatchCount = newDocumentBatchCount
		}
	end 
	
	local puller = WFCatMetadataPuller( catalog, "Test Puller", options )
	local pusher = WFCatMetadataPusher( catalog )
	
	rxTest:finally( function()
	
		puller:close()
		pusher:close()
		
	end )
	
	puller:getUpdatesContinuously()
	rxTest:waitFor( pusher:sendUpdatesContinuously() )
	
	local initialSyncTime
	
	local obs = rxTest:observe(
	
		catalog:docStore():ongoingLocalDocValue( 'Oz-isDoingInitialSync' )
		
			:where( function( docValue )
			
				return not docValue._empty
				
			end )
			
			:where( function( docValue ) 
			
				return not docValue.isDoingInitialSync
				
			end )
			
			:tap( function( docValue )
			
				initialSyncTime = docValue.endTimestamp - docValue.beginTimestamp
				
			end )
			
			:map( function( docValue )
				
				return docValue.isDoingInitialSync
				
			end ) )
			
	obs:expectData( false )
	
	testMetrics[ "wfp_execution_time" ] = initialSyncTime

	WFTestMetrics.writeTestMetrics( testMetrics )
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalogLargeXMP_testPerformance( rxTest )
	
	pullLargeCatalogLargeXMPWithDocumentBatchCount( rxTest )
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalogLargeXMPWithDocumentBatchCount200_testPerformance( rxTest )
	
	pullLargeCatalogLargeXMPWithDocumentBatchCount( rxTest, 200 )
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalogLargeXMPWithDocumentBatchCount500_testPerformance( rxTest )
	
	pullLargeCatalogLargeXMPWithDocumentBatchCount( rxTest, 500 )
	
end

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

function WFCatMetadataPullerPerformanceTest.rxTestPullLargeCatalogLargeXMPWithDocumentBatchCount800_testPerformance( rxTest )
	
	pullLargeCatalogLargeXMPWithDocumentBatchCount( rxTest, 800 )
	
end

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

-- TODO [jandrew 2022-07-15]: The following test suite is currently disabled because
-- it relies on WFOzProductionUserLogin, which uses a production client Id. The secret
-- for this client Id was recently rotated, and we must create a new client Id on prod
-- in order to reenable this test suite.

return false and WFRxTestCase.wrapSuite( 'WFCatMetadataPullerPerformanceTest', WFCatMetadataPullerPerformanceTest ) or {}
