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

KSReactivePromiseBridgeTest.lua

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

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

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

	-- wichitafoundation.core.reactive
local KSReactivePromiseBridge = require 'KSReactivePromiseBridge'

	-- wichitafoundation.core.self_test
local WFAsyncTestCase = require 'WFAsyncTestCase'

	-- wichitafoundation.core.threading
local WFWorkerThread = require 'WFWorkerThread'

	-- wichitafoundation.core.utils
local KSTestUtils = require 'KSTestUtils'


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

local KSReactivePromiseBridgeTest = {}

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

local assert = assert

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

local requestTestHandler = WFWorkerThread( 'WFRequestTestHandler.lua' )

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

-- @mark promiseToBehavior

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

function KSReactivePromiseBridgeTest.testPromiseToBehaviorImmediateData( test )

	local KSPromises = require 'KSPromises'

	local p = KSPromises.resolvedPromiseP( 'data', "Greetings", "Salutations" )
	
	local b = KSReactivePromiseBridge.promiseToBehavior( p, "Not Yet!" )
	
	KSTestUtils.assertMatchArgs( "Greetings", "Salutations" )( b:valueNow() )

end

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

function KSReactivePromiseBridgeTest.testPromiseToBehaviorImmediateError( test )

	local KSPromises = require 'KSPromises'

	local p = KSPromises.resolvedPromiseP( 'error', "Not so fast" )
	
	local b = KSReactivePromiseBridge.promiseToBehavior( p, "Not Yet!" )
	
	KSTestUtils.assertMatchArgs( true, "Not so fast" )( b:isErrorNow() )

end

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

function KSReactivePromiseBridgeTest.testPromiseToBehaviorWithRequest( test )

	WFAsyncTestCase( test )
	
		:waitFor( function()

			local p = requestTestHandler:requestP( 'echo', "The data value" )
			
			return KSReactivePromiseBridge.promiseToBehavior( p, "Not Yet!" )
		
		end )
		:continueWhenValueMatches( 1, "The data value" )
	
end

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

function KSReactivePromiseBridgeTest.testPromiseToBehaviorWithRequestError( test )

	WFAsyncTestCase( test )
	
		:waitFor( function()
		
			local p = requestTestHandler:requestP( 'sendError', "Failure!" )
			
			return KSReactivePromiseBridge.promiseToBehavior( p, "Not Yet!" )
		
		end )
		:continueWhenValueMatchesError( 'Failure!' )
	
end

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

-- @mark promiseToEvent

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

function KSReactivePromiseBridgeTest.testPromiseToEventWithRequest( test )

	WFAsyncTestCase( test )

		:waitFor( function()

			local p = requestTestHandler:requestP( 'echo', "The data value" )
			
			return KSReactivePromiseBridge.promiseToEvent( p )
		
		end )
		:continueWhenValueMatches( 1, "The data value" )
	
end

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

function KSReactivePromiseBridgeTest.testPromiseToEventWithRequestError( test )

	WFAsyncTestCase( test )

		:waitFor( function()
		
			local p = requestTestHandler:requestP( 'sendError', "Failure!" )
			
			return KSReactivePromiseBridge.promiseToEvent( p )
		
		end )
		:continueWhenValueMatchesError( 'Failure!' )
			-- FIXME [scouten 2012-02-09]: Don't love hard-coding the line number in,
			-- but I don't have a better solution at this time.
	
end

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

--[[

function KSReactivePromiseBridgeTest.testEventToPromiseLost()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local e = KSReactive.sourceE()
	
	local p = KSReactivePromiseBridge.eventToPromise( e )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( )( ... )
		didFire = true
	end ):runUntilDone()
	
	e = nil
	p = nil
	
	collectgarbage()
	collectgarbage()
	collectgarbage()
	
	assert( didFire, "Promise never resolved" )

	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

end

]]

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

-- @mark behaviorToPromise

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

function KSReactivePromiseBridgeTest.testBehaviorToPromiseImmediate()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( 'a', 'b', 'c' )
	
	local p = KSReactivePromiseBridge.behaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( 'data', 'a', 'b', 'c' )( ... )
		didFire = true
	end ):runUntilDone()
	
	vB:close()
	
	assert( didFire, "Promise never resolved" )
	
	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

end

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

function KSReactivePromiseBridgeTest.testBehaviorToPromiseWithData()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( nil )
	
	local p = KSReactivePromiseBridge.behaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( 'data', 'a', 'b', 'c' )( ... )
		didFire = true
	end ):runUntilDone()
	
	vB:set( 'a', 'b', 'c' )
	
	vB:set( 'd', 'e', 'f' )
	
	vB:close()
	
	assert( didFire, "Promise never resolved" )
	
	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

end

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

function KSReactivePromiseBridgeTest.testBehaviorToPromiseWithError()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( nil )
	
	local p = KSReactivePromiseBridge.behaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( 'error', 'Oops!' )( ... )
		didFire = true
	end ):runUntilDone()
	
	vB:setError( 'Oops!' )
	
	vB:close()
	
	assert( didFire, "Promise never resolved" )
	
	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

end

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

function KSReactivePromiseBridgeTest.testBehaviorToPromiseCanceled()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( nil )
	
	local p = KSReactivePromiseBridge.behaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( )( ... )
		didFire = true
	end ):runUntilDone()
	
	vB:close()
	
	assert( didFire, "Promise never resolved" )

	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

end

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

function KSReactivePromiseBridgeTest.testClosingBehaviorToPromiseImmediate()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( 'a', 'b', 'c' )
	
	local p = KSReactivePromiseBridge.closingBehaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( 'data', 'a', 'b', 'c' )( ... )
		didFire = true
	end ):runUntilDone()
	
	assert( didFire, "Promise never resolved" )
	
	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

	assert( WFCloseSupport.hasBeenOrWillBeClosed( vB ), "Variable has not closed" )

end

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

function KSReactivePromiseBridgeTest.testClosingBehaviorToPromiseWithData()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( nil )
	
	local p = KSReactivePromiseBridge.closingBehaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( 'data', 'a', 'b', 'c' )( ... )
		didFire = true
	end ):runUntilDone()
	
	vB:set( 'a', 'b', 'c' )
	
	assert( didFire, "Promise never resolved" )
	
	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

	assert( WFCloseSupport.hasBeenOrWillBeClosed( vB ), "Variable has not closed" )

end

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

function KSReactivePromiseBridgeTest.testClosingBehaviorToPromiseWithError()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( nil )
	
	local p = KSReactivePromiseBridge.closingBehaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( 'error', 'Oops!' )( ... )
		didFire = true
	end ):runUntilDone()
	
	vB:setError( 'Oops!' )
	
	assert( didFire, "Promise never resolved" )
	
	assert( WFCloseSupport.hasBeenOrWillBeClosed( a ), "Action has not closed" )

	assert( WFCloseSupport.hasBeenOrWillBeClosed( vB ), "Variable has not closed" )

end

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

function KSReactivePromiseBridgeTest.testClosingBehaviorToPromiseCanceled()

	local KSReactive = require 'KSReactive'
	local WFCloseSupport = require 'WFCloseSupport'
	
	local function nonNil( data )
		return data ~= nil
	end
	
	local vB = KSReactive.variableB( nil )
	
	local p = KSReactivePromiseBridge.closingBehaviorToPromise( vB, nonNil )
	
	local didFire = false
	
	local a = p:whenResolvedDo( function( ... )
		KSTestUtils.assertMatchArgs( )( ... )
		didFire = true
	end ):runUntilDone()
	
	a:close()
	
	assert( WFCloseSupport.hasBeenOrWillBeClosed( vB ), "Variable has not closed" )

end

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

return KSReactivePromiseBridgeTest
