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

ADOBE CONFIDENTIAL
------------------
Copyright 2023 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
and its suppliers and are protected by all applicable intellectual
property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from Adobe.

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

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

local WFPlatformInfo = require 'WFPlatformInfo'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFCatPendingCommitHandler = require 'WFCatPendingCommitHandler'
local WFCatVolumeAgent = require 'WFCatVolumeAgent'
local WFCatMockCatalog = require 'WFCatMockCatalog'

local KSPathUtils = require 'KSPathUtils'

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

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

local WFCatPendingCommitHandlerTest = {}

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

function WFCatPendingCommitHandlerTest.rxTestCleanup( rxTest )

	local optArgs = {}

	if WFPlatformInfo.isiOS then
		optArgs.indexDefinitions = 'LrmCatalogIndexDefinitions.lua'
	end

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest , nil, nil, optArgs )

	local purgeAgent = WFCatVolumeAgent( wfCatalog, KSPathUtils.getStandardFilePath 'temp')

	local pendingCommitHandler = WFCatPendingCommitHandler( wfCatalog, purgeAgent )

	local assetIds = rxTest:getFirstValue( WFCatAssetTestUtils.insertAssets( rxTest, wfCatalog, 3 ) )

	local getRevId = function( assetId )
		return wfCatalog:docStore():docValue( assetId ):pluck('_revId')
	end

	local getLabels = function( assetId )
		return wfCatalog:docStore():docValue( assetId ):pluck('_labels')
	end

	-- Make is a document with a pending commit
	local asset1Rev = rxTest:getLastValue( getRevId( assetIds[ 1 ] ) )
	rxTest:waitFor( wfCatalog:docStore():setLabel( assetIds[ 1 ], '_pendingCommit', asset1Rev ) )

	-- Make is a document with a pending commit that is locked
	local asset2Rev = rxTest:getLastValue( getRevId( assetIds[ 2 ] ) )
	rxTest:waitFor( wfCatalog:docStore():setLabel( assetIds[ 2 ], '_pendingCommit', asset2Rev) )

	-- Proxy call for `lockAssets` in syncClientImp
	rxTest:waitFor( purgeAgent:protectFromPurge( { assetIds[2] } ) )

	-- Make is a document with an unrelated Label
	local asset3Rev = rxTest:getLastValue( getRevId( assetIds[ 3 ] ) )
	rxTest:waitFor( wfCatalog:docStore():setLabel( assetIds[ 3 ], 'oz', asset3Rev) )

	-- Manually trigger a cleanup - do not start task
	rxTest:waitFor( pendingCommitHandler:cleanup() )

	local assetId1Labels = getLabels( assetIds[ 1 ] )

	rxTest:expectSingleValue( assetId1Labels, nil )

	local assetId2Labels = getLabels( assetIds[ 2 ] )

	rxTest:expectSingleValue( assetId2Labels, { _pendingCommit = true } )

	local assetId3Labels = getLabels( assetIds[ 3 ] )

	rxTest:expectSingleValue( assetId3Labels, { oz = true } )
end

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

return WFRxTestCase.wrapSuite( 'WFCatPendingCommitHandlerTest', WFCatPendingCommitHandlerTest )
