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

PixelProviderDocstoreInfoTaskTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.

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

	-- If ACR is not supported, disable test before we attempt to
	-- require WFAcr. (That will fail.)
if not require 'WFPlatformInfo'.hasACR then return {} end

	-- wf.core
local KSTableUtils = require 'KSTableUtils'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFRxTestCase = require 'WFRxTestCase'

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

local PixelPathProviderScheduler = require "PixelPathProviderScheduler"
local PixelPathProviderRequestTypes = require "PixelPathProviderRequestTypes"
local PixelPathProviderStateMachine = require "PixelPathProviderStateMachine"
local NmbDocValueToRenderInfo = require 'NmbDocValueToRenderInfo'

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

local PixelPathProviderRequestTypeNames = PixelPathProviderRequestTypes.names

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

local function makeSchedulerForTest( rxTest )

	local catalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )

	local scheduler = PixelPathProviderScheduler( catalog )

	rxTest:finally( function() scheduler:close() end )

	return scheduler, catalog

end

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

local function makeLocalAssetId( idSeed )

	assert( type( idSeed ) == "number", "makeLocalAssetId requires numbers" )

	return idSeed

end

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

local function makePixStreamId( pixStreamId )

	return string.format( "pixStreamId_%s", pixStreamId )

end

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

local function extractLocalAssetIdFromRequest( request )

	return request[ 2 ]

end

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

local function addRecordToDocStore( rxTest, catalog, i )

	local docValueOptions = { 
		internalXmpCameraRaw = true
	}

	local docValue = WFCatAssetTestUtils.createAssetDocument( 
						rxTest, catalog, nil, docValueOptions )

	docValue._localId = makeLocalAssetId( 1 )

	docValue.develop.croppedHeight = 100 * i
	docValue.develop.croppedWidth = 100 * i
	docValue.develop.userOrientation = i % 4
	docValue.develop.rating = i % 5

	rxTest
		:expectSingleValue(
			catalog:docStore():bulkSetDocValue( { docValue } ),
			':ignore:' )

end

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

local function docValueHasId( wantedLocalAssetId, docValue )

	if docValue._localId == wantedLocalAssetId then
		
		return wantedLocalAssetId, wantedLocalAssetId

	end

	return wantedLocalAssetId

end

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

local function updateDocstoreRecord( rxTest, catalog, i )

	local watchUpdates =
		rxTest:observe(
			catalog:docStore():scannedFilteredMappedChangesFeed(
					"PixelProviderDocstoreInfoTaskTest",
					"docValueHasId",
					makeLocalAssetId( i ) )
				:take( 1 ) )

	local docValueBefore

	rxTest
		:expectSingleValue(
			catalog
				:docStore()
				:docValue( makeLocalAssetId( i ) )
				:tap(
					function( docValue )

						docValueBefore = docValue

					end ),
			':ignore:' )

	local docValueUpdate = 
			{
				_id = makeLocalAssetId( i ),
				develop = {
					croppedHeight = 555,
					croppedWidth = 666,
				},
			}

	rxTest
		:expectSingleValue(
			catalog
				:docStore()
				:bulkApplyOverlay( { docValueUpdate } ),
			':ignore:' )

	watchUpdates:expectData( ':ignore:' )

	watchUpdates:expectCompleted()

	local docValueAfter

	rxTest
		:expectSingleValue(
			catalog
				:docStore()
				:docValue( makeLocalAssetId( i ) )
				:tap(
					function( docValue )

						docValueAfter = docValue

					end ),
			':ignore:' )

	rxTest
		:expectEqual(
			docValueBefore._localId,
			docValueAfter._localId )

end

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

local function isTestMessage( msg, renderInfoChangeType )

	return function( testMsg, testRenderInfoType, ... )

		return testMsg == msg and testRenderInfoType == renderInfoChangeType 

	end

end

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

local function takeNth( n )
	
	return function( ... )

		return select( n, ... )

	end

end

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

local function sortArray( arr, maybeSortFn )

	local sorted = KSTableUtils.deepCopy( arr )

	table.sort( sorted, maybeSortFn )

	return sorted

end

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

local function justReturn( ... )

	return ...

end

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

local function getNFromTestInfoStream( testStream, n, filterFunc, 
										maybeTransformFunc )

	local transformTestData = maybeTransformFunc or justReturn

	return testStream
			:where( filterFunc )
			:map( transformTestData )
			:take( n )
			:toArray()

end

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

local function rxTestHappyPath( rxTest )
	
	rxTest:setTimeLimit( 10 )
	local scheduler, catalog = makeSchedulerForTest( rxTest )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	local newRecordCount = 20 

	local expectCheckPixelCacheLocalAssetIds = {}

	for i = 1, newRecordCount do

		for j = 1, 3 do

			expectCheckPixelCacheLocalAssetIds[ #expectCheckPixelCacheLocalAssetIds + 1 ] = makeLocalAssetId( i )

			scheduler:addRequest( makePixStreamId( j ), makeLocalAssetId( i ), 
									PixelPathProviderRequestTypeNames.grid_visible, 1 )

		end
	
	end

	local watchUpdates = rxTest:observe( 
									getNFromTestInfoStream(
											scheduler._testData.testInfoStream,
											newRecordCount,
											isTestMessage( "gotDocstoreInfo", "batch" ),
											takeNth( 3 ) )
										:map( sortArray ) )

	local expectedNewRecords = {}

	for i = 1, newRecordCount + 1 do

		addRecordToDocStore( rxTest, catalog, i )

		if i <= newRecordCount then

			expectedNewRecords[ #expectedNewRecords + 1 ] = makeLocalAssetId( i )

		end

	end

	rxTest:log( "before watchUpdates" )

	SchedulerTestAPI.addRunLoopTaskToAllowlist( scheduler, 
												"PixelProviderDocstoreInfoTask" )

	SchedulerTestAPI.runScheduledRequests( scheduler )
	
	-- make sure we got doc store data for all the records
	watchUpdates:expectData( sortArray( expectedNewRecords ) )
	
	rxTest:log( "after watchUpdates" )
	
	-- all requests are now either in NeedCheckRenderSettings (1 pass through scheduled requests) or
	-- NeedCheckForPixelPathInCache (2 passes) sleeping for a moment should allow all to get 
	-- processed through NeedCheckForPixelPathInCache
	rxTest:sleep( 2 )

	rxTest:expectEqual( 
		sortArray( expectCheckPixelCacheLocalAssetIds ),
		sortArray( 
			scheduler:_mapAllRequestsWithState( 
						PixelPathProviderStateMachine.PixelCacheCheckStates.NeedCheckForPixelPathInCache,
						extractLocalAssetIdFromRequest ) ) )

end

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

local function rxTestUpdatesSeen( rxTest )

	rxTest:setTimeLimit( 11 )
	
	local scheduler, catalog = makeSchedulerForTest( rxTest )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	local newRecordCount = 20

	local pixStreamIdsCount = 3

	local expectedNewRecords = {}

	for i = 1, newRecordCount do

		for j = 1, pixStreamIdsCount do

			scheduler:addRequest( makePixStreamId( j ), makeLocalAssetId( i ), 
									PixelPathProviderRequestTypeNames.grid_visible, 1 )

		end
	
	end

	local watchAdds = rxTest:observe( 
									getNFromTestInfoStream(
											scheduler._testData.testInfoStream,
											newRecordCount,
											isTestMessage( "gotDocstoreInfo", "batch" ),
											takeNth( 3 ) )
										:map( sortArray ) )

	for i = 1, newRecordCount + 1 do

		addRecordToDocStore( rxTest, catalog, i )

		if i <= newRecordCount then

			expectedNewRecords[ #expectedNewRecords + 1 ] = makeLocalAssetId( i )

		end

	end

	SchedulerTestAPI.addRunLoopTaskToAllowlist( scheduler, 
												"PixelProviderDocstoreInfoTask" )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	rxTest:log( "before wait expectedNewRecords" )

	-- make sure we got doc store data for all the records
	watchAdds:expectData( sortArray( expectedNewRecords ) )

	rxTest:log( "after wait expectedNewRecords" )

	local origIsLostAssetIds = {}

	for i = 1, newRecordCount do

		for j = 1, pixStreamIdsCount do
	
			origIsLostAssetIds[ #origIsLostAssetIds + 1 ] = makeLocalAssetId( i )

			SchedulerTestAPI.setState( scheduler, makePixStreamId( j ), makeLocalAssetId( i ), 
						PixelPathProviderStateMachine.EndStates.FinishedSendingRender )


		end
	
	end

	-- all requests are now on NoDownloadableRenderOrBinary
	rxTest:expectEqual( 
		sortArray( origIsLostAssetIds ),
		sortArray( 
			scheduler:_mapAllRequestsWithState( 
						PixelPathProviderStateMachine.EndStates.FinishedSendingRender,
						extractLocalAssetIdFromRequest ) ) )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	-- update some records, but not all

	local updateCount = newRecordCount - 3

	local watchUpdatedRecords = rxTest:observe( 
									getNFromTestInfoStream(
											scheduler._testData.testInfoStream,
											updateCount,
											isTestMessage( "gotDocstoreInfo", "changedRenderInfos" ),
											takeNth( 3 ) )
										:map( sortArray ) )

	local expectedUpdateRecords = {}

	local expectedFetchingAssetInfoStates = {}

	local expectedUnchangedStates = {}

	for i = 1, newRecordCount do

		if i <= updateCount then

			expectedUpdateRecords[ #expectedUpdateRecords + 1 ] = makeLocalAssetId( i ) 
		
			updateDocstoreRecord( rxTest, catalog, i )

		end

		for _ = 1, pixStreamIdsCount do

			if i <= updateCount then

				table.insert( expectedFetchingAssetInfoStates, makeLocalAssetId( i ) )

			else

				table.insert( expectedUnchangedStates, makeLocalAssetId( i ) )

			end

		end

	end

	rxTest:log( "before watchUpdatedRecords" )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	-- make sure we got doc store data for all updated records
	watchUpdatedRecords:expectData( sortArray( expectedUpdateRecords ) )

	rxTest:log( "after watchUpdatedRecords" )

	-- all requests for updated records are now on NeedCheckForPixelPathInCache
	rxTest:expectEqual( 
		sortArray( expectedFetchingAssetInfoStates ),
		sortArray( 
			scheduler:_mapAllRequestsWithState( 
						PixelPathProviderStateMachine.PixelCacheCheckStates.NeedCheckForPixelPathInCache,
						extractLocalAssetIdFromRequest ) ) )

	-- all other requests are still on NoDownloadableRenderOrBinary
	rxTest:expectEqual( 
		sortArray( expectedUnchangedStates ),
		sortArray( 
			scheduler:_mapAllRequestsWithState( 
						PixelPathProviderStateMachine.EndStates.FinishedSendingRender,
						extractLocalAssetIdFromRequest ) ) )

end

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

local function runEnsureLocalXmpTest( 
				rxTest, docValueOptions, expectedStateAfterDocValueFetched )

	local scheduler, catalog = makeSchedulerForTest( rxTest )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	local pixStreamId = makePixStreamId( 1 )

	rxTest:logf( "set up docstore record" )

	local docValue = WFCatAssetTestUtils.createAssetDocument( 
						rxTest, catalog, nil, docValueOptions )

	docValue._localId = makeLocalAssetId( 1 )

	rxTest
 		:expectSingleValue(
 			catalog
 				:docStore()
 				:bulkSetDocValue( { docValue } ),
 			':ignore:' )

	local localAssetId = docValue._localId

	rxTest:logf( "scheduler:requestXmpDevelopForDevelop( ... )" )

	scheduler:requestXmpDevelopForDevelop( pixStreamId, localAssetId )

	rxTest:log( "confirm correct state after requestXmpDevelopForDevelop" )

	SchedulerTestAPI.addRunLoopTaskToAllowlist( scheduler, 
												"PixelProviderDocstoreInfoTask" )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	rxTest:log( "waiting for test to fetch docValue (this gives pixel provider a chance to fetch docValue, too)" )

 	rxTest:expectSingleValue(
 		catalog
 			:docStore()
 			:docValue( localAssetId ),
 		':ignore:' )
		
	rxTest:logf( "Checking for expected post docValue fetch state: %s",
					expectedStateAfterDocValueFetched )

	local localAssetIdRequests = 
			SchedulerTestAPI.getAllLocalAssetIdRequests( scheduler, localAssetId )
			
	rxTest:expectEqual( #localAssetIdRequests, 1 )

	local actualPixStreamId, actualLocalAssetId, _, _, actualState = 
			unpack( localAssetIdRequests[ 1 ] or {} )

	rxTest:expectEqual( pixStreamId, actualPixStreamId )

	rxTest:expectEqual( localAssetId, actualLocalAssetId )

	rxTest:logf( "Checking if actual state %s is same as expected: %s",
					actualState, expectedStateAfterDocValueFetched )

	rxTest:expectEqual( expectedStateAfterDocValueFetched, actualState )

end

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

local function rxTestEnsureLocalXmpWhenEmbeddedXmpDevelop( rxTest )

	local docValueOptions = { 
		internalXmpCameraRaw = true
	}

	runEnsureLocalXmpTest( 
		rxTest, docValueOptions,
		PixelPathProviderStateMachine.SendDataStates.HaveEmbeddedXmpDevelopNotExternal )

end

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

local function rxTestEnsureLocalXmpWhenExternalXmpDevelop( rxTest )

	local docValueOptions = { 
	}

	runEnsureLocalXmpTest( 
		rxTest, docValueOptions,
		PixelPathProviderStateMachine.LocalBinaryPathCheckStates.NeedCheckIfExternalXmpDevelopIsLocal )

end

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

local function rxTestEnsureLocalXmpWhenEmbeddedXmpDevelopAndExternalMasks( rxTest )

	local docValueOptions = { 
		hasMasks = true,
	}

	runEnsureLocalXmpTest( 
		rxTest, docValueOptions,
		PixelPathProviderStateMachine.LocalBinaryPathCheckStates.NeedCheckIfExternalXmpDevelopIsLocal )

end


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

local function rxTestEnsureLocalXmpWhenEmbeddedXmpDevelopAndExternalFilters( rxTest )

	local docValueOptions = {
		hasFilters = true,
	}

	runEnsureLocalXmpTest(
		rxTest, docValueOptions,
		PixelPathProviderStateMachine.LocalBinaryPathCheckStates.NeedCheckIfExternalXmpDevelopIsLocal )

end

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

--[[
local function logRequest( request, rxTest, desc )

	local requestPlaceHolders = "%s" .. string.rep( ", %s", #request )

	rxTest:logf( "%s: { " .. requestPlaceHolders .. " } ",
					desc, unpack( request ) )

end
--]]

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

local function runTestRenderSettingsTransition( 
				rxTest, scheduler, catalog, localAssetId, docValueOptions,
				initialState )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	local pixStreamId = string.format( "%s%s", initialState, localAssetId )

	local docValue = WFCatAssetTestUtils.createAssetDocument( 
						rxTest, catalog, nil, docValueOptions )

	docValue._localId = localAssetId

	rxTest
 		:expectSingleValue(
 			catalog
 				:docStore()
 				:bulkSetDocValue( { docValue } ),
 			':ignore:' )

	scheduler
		:addRequest( 
			pixStreamId, localAssetId,
			PixelPathProviderRequestTypeNames.grid_visible, 1, initialState )

	SchedulerTestAPI.addRunLoopTaskToAllowlist( scheduler, 
												"PixelProviderDocstoreInfoTask" )

	SchedulerTestAPI.runScheduledRequests( scheduler )

 	rxTest:expectSingleValue(
 		catalog
 			:docStore()
 			:docValue( localAssetId ),
 		':ignore:' )

	SchedulerTestAPI.runScheduledRequests( scheduler )

end

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

local function rxTestNeedsRenderSettingsStates( rxTest )

	local scheduler, catalog = makeSchedulerForTest( rxTest )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	local internalXmpCameraRawOption = { internalXmpCameraRaw = true }

	local externalXmpCameraRawOption = {}

	local hasMasksOption = { hasMasks = true }

	local hasFilterOption = { hasFilters = true }

	local hasC2paOption = { hasC2pa = true }


	local localAssetId

	local expectedExternalDevelopFilesCountForOption = {
			[ externalXmpCameraRawOption ] = 1, 
			[ hasMasksOption ] = 3,
			[ hasFilterOption ] = 3, -- externalXmp and 2 filter aux files
			[ hasC2paOption ] = 2, -- externalXmp and c2pa aux
		}

	local externalDevelopFilesXmpOnlyArray = {}

	local externalDevelopFilesAuxArray = {}

	for _, docValueOptions in pairs( 
			{ internalXmpCameraRawOption, externalXmpCameraRawOption, 
				hasMasksOption, hasC2paOption } ) do

		rxTest:logf( "run tests with docValueOption: %s",
					next( docValueOptions ) or "external xmp" )

		for _, initialState in pairs( { 
								PixelPathProviderStateMachine.DocstoreInfoStates.NeedXmpDevelopInfoFromDocstore,
								PixelPathProviderStateMachine.DocstoreInfoStates.NeedXmpDevelopInfoFromDocstore } ) do

			localAssetId = ( localAssetId or 0 ) + 1

			runTestRenderSettingsTransition( 
				rxTest, scheduler, catalog, localAssetId, docValueOptions, initialState )

			local baseRenderInfoCache = 
					SchedulerTestAPI.getBaseRenderInfoCache( scheduler )

			local baseRenderInfo = baseRenderInfoCache:peekCacheItem( localAssetId )

			rxTest:logf( "ensure proper baseRenderInfo.externalDevelopFiles for localAssetId %s inital state %s",
							localAssetId, initialState )

			rxTest:expectEqual( "table", type( baseRenderInfo ) )

			rxTest:expectEqual(
					docValueOptions ~= internalXmpCameraRawOption and "table" or nil,
					baseRenderInfo.externalDevelopFiles and
						type( baseRenderInfo.externalDevelopFiles ) )
					
			if docValueOptions ~= internalXmpCameraRawOption then

				rxTest:expectEqual( expectedExternalDevelopFilesCountForOption[ docValueOptions ],
					#baseRenderInfo.externalDevelopFiles )

			end

			if docValueOptions == externalXmpCameraRawOption then

				externalDevelopFilesXmpOnlyArray[ #externalDevelopFilesXmpOnlyArray + 1 ] =
					baseRenderInfo

			end

			if docValueOptions == hasMasksOption then

				externalDevelopFilesAuxArray[ #externalDevelopFilesAuxArray + 1 ] =
					baseRenderInfo

			end

			if docValueOptions == hasC2paOption then

				externalDevelopFilesAuxArray[ #externalDevelopFilesAuxArray + 1 ] =
					baseRenderInfo

			end

			if docValueOptions == hasFilterOption then

				externalDevelopFilesAuxArray[ #externalDevelopFilesAuxArray + 1 ] =
					baseRenderInfo

			end

		end

	end

	for i, baseRenderInfoArray in pairs( { externalDevelopFilesXmpOnlyArray, externalDevelopFilesAuxArray } ) do

		if #baseRenderInfoArray < 2 then

			rxTest:expectEqual( "",
					string.format( "Have at least 2 baseRenderInfoArray for scenario %s", i ) )

		else 

			local baseRenderInfo1, baseRenderInfo2 = unpack( externalDevelopFilesXmpOnlyArray )

			local baseRenderInfo1Copy = KSTableUtils.deepCopy( baseRenderInfo1 )

			rxTest:expectEqual( true,
					NmbDocValueToRenderInfo.areExternalDevelopFilesEqual(
						baseRenderInfo1.externalDevelopFiles, 
						baseRenderInfo1Copy.externalDevelopFiles )
					 )

			rxTest:expectEqual( false,
					NmbDocValueToRenderInfo.areExternalDevelopFilesEqual(
						baseRenderInfo1.externalDevelopFiles, 
						baseRenderInfo2.externalDevelopFiles )
					 )

		end

	end


	local permanentErrorAssetIds =
			scheduler
				:_mapAllRequestsWithState( 
					PixelPathProviderStateMachine.SendDataStates.SendPermanentError,
					function( request )

						local _, errLocalAssetID = unpack( request )

						return errLocalAssetID

					end )

	rxTest:expectEqual( {}, permanentErrorAssetIds )

end


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

local function rxTestLocalProxyPathUpdateSeen( rxTest )

	rxTest:setTimeLimit( 110 )
	
	local scheduler, catalog = makeSchedulerForTest( rxTest )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	SchedulerTestAPI.addRunLoopTaskToAllowlist( scheduler, 
												"PixelProviderDocstoreInfoTask" )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	local baseRenderInfoCache = 
			SchedulerTestAPI.getBaseRenderInfoCache( scheduler )

	local docValueOptions = { 
		internalXmpCameraRaw = true,
		hasLocal2560Proxy = true,
	}

	local docValue = WFCatAssetTestUtils.createAssetDocument( 
						rxTest, catalog, nil, docValueOptions )

	local localAssetId = makeLocalAssetId( 1 )

	docValue._localId = localAssetId

	local localProxyOverlay =
			KSTableUtils.derefOpt( docValue, '_localOnly', 'files', 'proxy' )

	rxTest:expectEqual( localProxyOverlay ~= nil, true )

	KSTableUtils.setNested( docValue, nil, '_localOnly', 'files', 'proxy' )

	rxTest
		:expectEqual(
			nil,
			KSTableUtils.derefOpt( docValue, '_localOnly', 'files', 'proxy' ) )

	rxTest:log( "Add a record without a local proxy" )

	rxTest
		:expectSingleValue(
			catalog:docStore():bulkSetDocValue( { docValue } ),
			':ignore:' )

	local pixStreamId = "localProxyPathUpdatesTest"

	scheduler
		:addRequest( 
			pixStreamId, localAssetId,
			PixelPathProviderRequestTypeNames.grid_visible, 1 )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	rxTest:log( "Fetch record without proxy to ensure scheduler fetched it too" )

 	rxTest:expectSingleValue(
 		catalog
 			:docStore()
 			:docValue( localAssetId ),
 		':ignore:' )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	local baseRenderInfo = baseRenderInfoCache:peekCacheItem( localAssetId )

	rxTest:log( "verify baseRenderInfo.proxyPath is not set" )

	rxTest:expectEqual( baseRenderInfo and next( baseRenderInfo ) ~= nil, true )
	
	rxTest:expectEqual( baseRenderInfo.proxyPath, nil )

	local docValueUpdate = 
			{
				_id = localAssetId
			}

	KSTableUtils.setNested( docValueUpdate, localProxyOverlay, '_localOnly', 'files', 'proxy' )

	rxTest:log( "Wait for updated docValue that contains proxyPath" )

	local watchUpdates =
		rxTest:observe(
			catalog:docStore():scannedFilteredMappedChangesFeed(
					"PixelProviderDocstoreInfoTaskTest",
					"docValueHasId",
					localAssetId  )
				:take( 1 ) )

	rxTest
		:expectSingleValue(
			catalog
				:docStore()
				:bulkApplyOverlay( { docValueUpdate } ),
			':ignore:' )

	watchUpdates:expectData( ':ignore:' )

	watchUpdates:expectCompleted()

	SchedulerTestAPI.runScheduledRequests( scheduler )

 	rxTest:expectSingleValue(
 		catalog
 			:docStore()
 			:docValue( localAssetId ),
 		':ignore:' )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	rxTest:log( "verify proxy path is in baseRenderInfo" )

	baseRenderInfo = baseRenderInfoCache:peekCacheItem( localAssetId )

	rxTest:expectEqual( baseRenderInfo and next( baseRenderInfo ) ~= nil, true )
	
	rxTest:expectEqual( baseRenderInfo.proxyPath ~= nil, true )

	--[[
	scheduler
		:_mapAllRequestsWithPixStreamId( 
			pixStreamId, 
				logRequest, 
					rxTest, 
					"wut" )
	--]]

end

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

return WFRxTestCase.wrapSuite( 'PixelProviderDocstoreInfoTaskTest', {

	rxTestHappyPath = rxTestHappyPath,

	rxTestUpdatesSeen = rxTestUpdatesSeen,

	rxTestEnsureLocalXmpWhenEmbeddedXmpDevelop = 
		rxTestEnsureLocalXmpWhenEmbeddedXmpDevelop,

	rxTestEnsureLocalXmpWhenExternalXmpDevelop =
		rxTestEnsureLocalXmpWhenExternalXmpDevelop,

	rxTestEnsureLocalXmpWhenEmbeddedXmpDevelopAndExternalMasks =
		rxTestEnsureLocalXmpWhenEmbeddedXmpDevelopAndExternalMasks,

	rxTestEnsureLocalXmpWhenEmbeddedXmpDevelopAndExternalFilters =
		rxTestEnsureLocalXmpWhenEmbeddedXmpDevelopAndExternalFilters,

	rxTestNeedsRenderSettingsStates = rxTestNeedsRenderSettingsStates,

	-- FIXME [jandrew 2025-03-11]: Disabled due to instability.

	DISABLED_rxTestLocalProxyPathUpdateSeen = rxTestLocalProxyPathUpdateSeen,

	docValueHasId = docValueHasId,

} )
