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

WFCatKeywordTaskTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains 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 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 Systems Incorporated.

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

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

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

local WFCatKeywordTask = require 'WFCatKeywordTask'
local WFCatTaskManager = require 'WFCatTaskManager'
local WFCatAsset = require 'WFCatAsset'

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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog

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

local WFCatKeywordTaskTest = {}

local kDefaultDescription = "WFCatTask"

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

function WFCatKeywordTaskTest.rxTestCreateKeywordTask( rxTest )
	
	local timeOut = 5
	local undoTokenOffset = 0
	
	rxTest:setTimeLimit( timeOut )
	
	local fakeWFCatalog = createFakeWFCatalog( rxTest )
	
	local docStore = fakeWFCatalog:docStore()
	
	docStore:setDocValue {
		_id = "doc_1",
		type = 'asset',
		subtype = 'image',
		_localOnly = {
			files = {
				proxy = {
					pano = {
						relativePath = 'foo',
						volumeGuid = 'bar'
					}
				}
			}
		},
	}
	
	local assetIds = { 1 }
	local keywords = { 'KeywordA' }
	local delete = false
	local optUndoStateToken = nil
	
	local testTask = WFCatKeywordTask.createKeywordTask( fakeWFCatalog, assetIds, keywords, delete, optUndoStateToken )
	
	local manager = WFCatTaskManager.getCatTaskManager( fakeWFCatalog )
	
	manager:addToQueue( testTask )
	
	local taskObs = rxTest:observe( testTask:taskObservable()
		:map( function( data )
			return data
		end )
	)
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0,
		processedCount = 0,
		totalCount = 1,
	})
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 1,
		processedCount = 1,
		totalCount = 1,
	})
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		extraInfo = {
			totalCount = 1,
			undoToken = undoTokenOffset + 1,
		},
		percentComplete = 1,
		processedCount = 1,
		totalCount = 1,
	})
	
	taskObs:expectCompleted()
	
	-- Check the result data
	rxTest:observe( WFCatAsset.collectInfoForAssetsFromDocStore( fakeWFCatalog, {
		assetIds = { 1 },
		keywords = true,
	} ) )
	:expectData {
		{
			keywords = { "KeywordA" },
		},
	}
	
end

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

function WFCatKeywordTaskTest.rxTestCreateUndoKeywordTask( rxTest )
	
	local timeOut = 5
	local undoTokenOffset = 1
	
	rxTest:setTimeLimit( timeOut )
	
	local fakeWFCatalog = createFakeWFCatalog( rxTest )
	
	local docStore = fakeWFCatalog:docStore()
	
	docStore:setDocValue {
		_id = "doc_1",
		type = 'asset',
		subtype = 'image',
		_localOnly = {
			files = {
				proxy = {
					pano = {
						relativePath = 'foo',
						volumeGuid = 'bar'
					}
				}
			}
		},
	}
	
	local manager = WFCatTaskManager.getCatTaskManager( fakeWFCatalog )
	
	-- 1) Create the keyword 'KeywordA'
	local assetIds = { 1 }
	local keywords = { 'KeywordA' }
	local delete = false
	local optUndoStateToken = nil
	
	local task = WFCatKeywordTask.createKeywordTask( fakeWFCatalog, assetIds, keywords, delete, optUndoStateToken )

	manager:addToQueue( task )
	
	local taskObs = rxTest:observe( task:taskObservable()
		:takeLast( 1 )
		:map( function( data )
			return data
		end )
	)
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		extraInfo = {
			totalCount = 1,
			undoToken = undoTokenOffset + 1,
		},
		percentComplete = 1,
		processedCount = 1,
		totalCount = 1,
	})
	
	taskObs:expectCompleted()
	
	-- 2) Undo the 'add keyword' operation
	optUndoStateToken = undoTokenOffset + 1
	
	local undoTask = WFCatKeywordTask.createKeywordTask( fakeWFCatalog, assetIds, nil, nil, optUndoStateToken )

	manager:addToQueue( undoTask )
	
	local undoTaskObs = rxTest:observe( undoTask:taskObservable()
		:map( function( data )
			return data
		end )
	)
	
	undoTaskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0,
		processedCount = 0,
		totalCount = 1,
	})
	
	undoTaskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 1,
		processedCount = 1,
		totalCount = 1,
	})
	
	undoTaskObs:expectCompleted()
	
	-- 3) Check the result data
	rxTest:observe( WFCatAsset.collectInfoForAssetsFromDocStore( fakeWFCatalog, {
		assetIds = { 1 },
		keywords = true,
	} ) )
	:expectData {
		{
			keywords = { },
		},
	}
	
end

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

return WFRxTestCase.wrapSuite( WFCatKeywordTaskTest )


