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

PixelProviderSendDataTaskTest.lua

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

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

	-- 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 WFCatMockCatalog = require 'WFCatMockCatalog'
local WFRxTestCase = require 'WFRxTestCase'
local KSTableUtils = require 'KSTableUtils'

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

local PixelPathProviderScheduler = require 'PixelPathProviderScheduler'
local PixelPathProviderRequestTypes = require 'PixelPathProviderRequestTypes'
local PixelProviderSendDataTask = require 'PixelProviderSendDataTask'

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

local function localAssetId( i )

	return i

end

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

local function bootstrapScheduler( catalog )

	local scheduler = PixelPathProviderScheduler( catalog )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	SchedulerTestAPI.addRunLoopTaskToAllowlist( 
		scheduler, "PixelProviderSendDataTask" )

	local pixStreamId = "dummy"

	-- adding and removing a dummy request makes sure that all tasks are
	-- bootstrapped
	scheduler:addRequest( pixStreamId, localAssetId( 1 ),
							PixelPathProviderRequestTypes.names.loupe_next, 1 )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	scheduler:abandonAllLocalAssetIdRequests( localAssetId ( 1 ) )

	return scheduler

end

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

local function makeSchedulerForTest( rxTest )

	local catalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )

	local scheduler = bootstrapScheduler( catalog )

	rxTest:finally( 
		function() 

			scheduler:close() 

		end )

	return scheduler, catalog

end
--==============================================================================
-- rxTestSendErrors
--==============================================================================

local function sortArray( array )

	local sorted = KSTableUtils.deepCopy( array )

	table.sort( sorted )

	return sorted

end

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

local function rxTestSendErrors( rxTest )

	local scheduler = makeSchedulerForTest( rxTest )

	local SchedulerTestAPI = scheduler:_expose_test_API()

	local sendErrorStates = 
		PixelProviderSendDataTask.privateUnitTestExports.getAllSendErrorStates(
			scheduler )

	-- watch the schedulers output stream and capture error messages
	local errorStreamMsgs =
			rxTest:observe( 
				scheduler
					:pixelPathStream()
					:map(
						function( _, _, _, errStr )

							return errStr

						end )
					:take( #sendErrorStates )
					:toArray() 
					:map( sortArray ) )

	local expectedErrorStreamMsgs = KSTableUtils.deepCopy( sendErrorStates )

	rxTest:logf( "Adding %s send error requests to scheduler", #sendErrorStates )

	for i, sendErrorState in ipairs( sendErrorStates ) do

		local pixStreamId = sendErrorState

		scheduler
			:addRequest( 
				pixStreamId, localAssetId( i ), 
				PixelPathProviderRequestTypes.names.loupe_next, 1 )

		SchedulerTestAPI.setState( 
			scheduler, pixStreamId, localAssetId( i ),  sendErrorState )
		
	end

	rxTest:log( "run the scheduler" )

	SchedulerTestAPI.runScheduledRequests( scheduler )

	rxTest:log( "make sure scheduler emitted all expected error messages" )

	errorStreamMsgs:expectData( sortArray( expectedErrorStreamMsgs ) )

	rxTest:log( "make sure there are no reqeusts left in send error state" )

	local requestsInSendErrorState =
			SchedulerTestAPI.getRequestsWithStatesInPriorityOrder( 
				scheduler, unpack( sendErrorStates ) )

	rxTest:expectEqual( {}, requestsInSendErrorState )

end

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

local testCases = {

	rxTestSendErrors = rxTestSendErrors,

}

return WFRxTestCase.wrapSuite( 'PixelProviderDownloadToFileTaskTest', testCases )
