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

NmbDeletionBacklogUpdaterTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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'

	-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local KSTableUtils = require 'KSTableUtils'

	-- WF.oz_client.managed_catalog
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFCatMockCatalog = require 'WFCatMockCatalog'

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

local NmbDeletionBacklogUpdaterTest = {}

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

function NmbDeletionBacklogUpdaterTest.rxTestBasic( rxTest )
	
	local optOverrides = {
		writeInterval = 0.1,
		processBacklogDelay = 1
	}
	
	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, nil, optOverrides )

	local deletionBacklog = wfCatalog:getDeletionBacklog()
	rxTest:waitFor( deletionBacklog:bringDeletionBacklogUpToDate() )
	
	rxTest:finally( function()
		deletionBacklog:close()
	end )
	
	-- Get the change counter of the updater
	
	local changeCounter = rxTest:getFirstValue( wfCatalog:docStore():localDocValue( 'deletionBacklogContinueFrom' )
			:pluck( 'continueFrom' ) )
			
	rxTest:expectEqual( nil, changeCounter )
	
	-- Insert a new doc into the doc store
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	
	local docValue = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId )
	
	rxTest:waitFor( wfCatalog:docStore():setDocValue( docValue ) )
	
	-- Make sure the change counter was updated
	
	local changeCounterObserver = rxTest:observe( wfCatalog:docStore():ongoingLocalDocValue( 'deletionBacklogContinueFrom' )
			:pluck( 'continueFrom' ) )
			
	changeCounterObserver:expectDataWithinTime( 3, 1 )
	
	-- Now insert a new document into the deletion backlog at a specific path
	
	local volumeGuid = '<guid>'
	local relativePath = 'I/AM/A/PATH'
	
	rxTest:expectSingleValue( deletionBacklog:addToDeletionBacklog( volumeGuid, relativePath ), true )
	
	local dataDump = deletionBacklog:DEBUG_dumpDeletionBacklogContents()
	
	local expectedDump = {
		{
			volumeGuid = volumeGuid,
			relativePath = relativePath
		}
	}
	
	rxTest:expectSingleValue( dataDump, expectedDump )
	
	-- Now insert a new document into the doc store that references those same paths.
	-- The entry should automatically disappear from the deletion backlog.
	
	assetId = KSIdGenerator.uuidInLowercaseHex()
	
	local docValue2 = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId )
	
	KSTableUtils.setNested( docValue2, volumeGuid, '_localOnly', 'files', 'original', 'master', 'volumeGuid' )
	KSTableUtils.setNested( docValue2, relativePath, '_localOnly', 'files', 'original', 'master', 'relativePath' )
	
	rxTest:waitFor( wfCatalog:docStore():setDocValue( docValue2 ) )
	
	changeCounterObserver:expectDataWithinTime( 3, 2 )
	
	dataDump = deletionBacklog:DEBUG_dumpDeletionBacklogContents()
	
	expectedDump = {}
	
	rxTest:expectSingleValue( dataDump, expectedDump )
	
end

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

return WFRxTestCase.wrapSuite( 'NmbDeletionBacklogUpdaterTest', NmbDeletionBacklogUpdaterTest )
