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

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.oz_client.catalog
local OzCatalogUrls = require 'OzCatalogUrls'

	-- WF.core.os
local KSPathUtils = require 'KSPathUtils'

	-- WF.oz_client.managed_catalog.sync_engine
local WFCatUser = require 'WFCatUser'
local WFCatSpec = require 'WFCatSpec'
local WFCatalog = require 'WFCatalog'

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

	-- Lrmobile_models
local LrmSearchModelImp = require 'LrmSearchModelImp'

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

local LrmSearchModelTest = {}

local kUser1 = string.rep( '1', 32 )
local kCatalogId = string.rep( 'c', 32 )

local catUser, wfCatalog

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

function LrmSearchModelTest.setUp()

	local userInfoCachePath = ':memory:'
	local userId = kUser1
	local serverBaseUrl = OzCatalogUrls.baseServerUrl
	local rootCatalogPath = KSPathUtils.getStandardFilePath 'temp'
	
	local catalogOptions = {
		indexSpecScript = 'LrmCatalogIndexDefinitions.lua',
		setupConflictResolver = false,
		docStorePath = ':memory:',
	}
	
	catUser = WFCatUser( serverBaseUrl, userId, userInfoCachePath )
	
	local catSpec = WFCatSpec( catUser, kCatalogId )
	wfCatalog = WFCatalog( catSpec, rootCatalogPath, catalogOptions )
	
end

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

function LrmSearchModelTest.tearDown()

	wfCatalog:close()
	catUser:close()
	
end

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

function LrmSearchModelTest.rxTestUpdateSearchOne( rxTest )

	local searchModel
	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local assetIds = { assetId }
	
	rxTest:setTimeLimit( 10 )
	
	searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, assetIds )

		, ':ignore:' )

	
	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId FROM coreInfo 
				UNION 
				SELECT docId FROM TEMP_search]]
	})
	:expectData { { assetId } }

end

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

function LrmSearchModelTest.rxTestUpdateSearchBulk( rxTest )

	local searchModel
	
	local assetId1 = KSIdGenerator.uuidInLowercaseHex()
	local assetId2 = KSIdGenerator.uuidInLowercaseHex()
	local assetId3 = KSIdGenerator.uuidInLowercaseHex()

	local assetIds = {
		assetId1,
		assetId2,
		assetId3,
	}
	
	rxTest:setTimeLimit( 10 )
		
	searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, assetIds )

		, ':ignore:' )
		
	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId, height FROM coreInfo 
				UNION 
				SELECT docId, sortOrder FROM TEMP_search
				ORDER BY sortOrder]]
	})
	:expectData {
			{ assetId1, 1 }, 
			{ assetId2, 2 }, 
			{ assetId3, 3 }, 
	}

end

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

function LrmSearchModelTest.rxTestClearSearchTable( rxTest )
	
	local searchModel
	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local assetIds = { assetId }
	
	rxTest:setTimeLimit( 10 )
	
	searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )

	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, assetIds )

		, ':ignore:' )

	
	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId FROM coreInfo 
				UNION 
				SELECT docId FROM TEMP_search]]
	})
	:expectData { { assetId } }
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'clearSearchTable' ]()

		, ':ignore:' )
		
	rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId FROM coreInfo 
				UNION 
				SELECT docId FROM TEMP_search]]
	})
	:expectData { }
	
end

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

function LrmSearchModelTest.rxTestUpdateSearchQuery( rxTest )
	
	local searchModel
	
	local assetId1 = KSIdGenerator.uuidInLowercaseHex()
	local assetId2 = KSIdGenerator.uuidInLowercaseHex()
	local assetId3 = KSIdGenerator.uuidInLowercaseHex()
	local assetId4 = KSIdGenerator.uuidInLowercaseHex()
	local assetId5 = KSIdGenerator.uuidInLowercaseHex()
	
	rxTest:setTimeLimit( 10 )
	
	searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )
	
	local result = rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT docId, height FROM coreInfo 
				UNION 
				SELECT docId, sortOrder FROM TEMP_search
				ORDER BY sortOrder]]
	})
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 0, { assetId1 } )

		, ':ignore:' )
		
	result:expectData { { assetId1, 1 } }
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 1, { assetId2 } )

		, ':ignore:' )	
		
	result:expectData { 
		{ assetId1, 1 },
		{ assetId2, 2 },
	}
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 2, { assetId3 } )

		, ':ignore:' )
			
	result:expectData {
		{ assetId1, 1 }, 
		{ assetId2, 2 }, 
		{ assetId3, 3 }, 
	}
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 3, { assetId4 } )

		, ':ignore:' )
			
	result:expectData {
		{ assetId1, 1 }, 
		{ assetId2, 2 }, 
		{ assetId3, 3 },
		{ assetId4, 4 },
	}
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchTable' ]( nil, 4, { assetId5 } )

		, ':ignore:' )
			
	result:expectData {
		{ assetId1, 1 }, 
		{ assetId2, 2 }, 
		{ assetId3, 3 },
		{ assetId4, 4 },
		{ assetId5, 5 },
	}
end

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

function LrmSearchModelTest.rxTestUpdateSearchFilterCountQuery( rxTest )
	
	local searchModel
	
	rxTest:setTimeLimit( 10 )
	
	searchModel = LrmSearchModelImp.searchModel( { _wfCatalog = wfCatalog } )
	
	local result = rxTest
	:observe( wfCatalog:indexStore():query{
		sql = [[SELECT filterType, filterCount FROM TEMP_searchFilterCount]]
	})
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchFilterCountTable' ]( nil, "pick", 5 )

		, ':ignore:' )
		
	result:expectData { { "pick", 5 } }
	
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchFilterCountTable' ]( nil, "reject", 0 )

		, ':ignore:' )	
		
	result:expectData { 
		{ "pick", 5 },
		{ "reject", 0 },
	}		
	
	rxTest:expectSingleValue(

		searchModel.events[ 'updateSearchFilterCountTable' ]( nil, "unflagged", 10 )

		, ':ignore:' )	
	
	result:expectData { 
		{ "pick", 5 },
		{ "reject", 0 },
		{ "unflagged", 10 },
	}
	
end

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

return WFRxTestCase.wrapSuite( LrmSearchModelTest )
