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

NmbCatPresetTest.lua

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

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

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

local WFPlatformInfo = require 'WFPlatformInfo'

if WFPlatformInfo.isMobile() then
	return {}
end

local KSData = require 'KSData'
local KSIdGenerator = require 'KSIdGenerator'

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

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

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

local NmbCatPreset = require 'NmbCatPreset'

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

-- @mark NmbCatPresetTest

local NmbCatPresetTest = {}

local wfCatalog

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

local function fileCleanup( rxTest, path )

	rxTest:finally( function()
		WFFileUtils.delete( path )
	end )

end

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

function NmbCatPresetTest.tearDown()

	if wfCatalog then
		wfCatalog:close()
		wfCatalog = nil
	end

end

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

function NmbCatPresetTest.rxTestAddPresetWithDigest( rxTest )

	rxTest:setTimeLimit( 5 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )

	local presetPathId = KSIdGenerator.uuidInLowercaseHex()
	local presetPath = wfCatalog:fileSpec():pathForStyle( presetPathId )
	KSData.saveToFile( KSData.make( "<xmp>foo</xmp>" ), presetPath )
	fileCleanup( rxTest, presetPath )

	local digest = 'DEADBEEF'

	local presetArray = {
		{
			filePath = presetPath,
			name = "myPreset",
			groupName = "someGroup",
			isLook = false,
			needsRename = false,
			digest = digest
		},
	}

	rxTest:waitFor( NmbCatPreset.addPresetArrayToCatalog( wfCatalog, presetArray, nil ) )

	rxTest:expectSingleValue( wfCatalog:docStore():docValue( 1 )
		:pluck( 'digest' ),
	digest )

end

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

function NmbCatPresetTest.rxTestUpdatePresetWithDigest( rxTest )

	rxTest:setTimeLimit( 5 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )

	local presetPathId = KSIdGenerator.uuidInLowercaseHex()
	local presetPath = wfCatalog:fileSpec():pathForStyle( presetPathId )
	KSData.saveToFile( KSData.make( "<xmp>foo</xmp>" ), presetPath )
	fileCleanup( rxTest, presetPath )

	local digest = 'DEADBEEF'

	local presetArray = {
		{
			filePath = presetPath,
			name = "myPreset",
			groupName = "someGroup",
			isLook = false,
			needsRename = false,
			xmpString = "<xmp>foo</xmp>",
			digest = digest
		},
	}

	rxTest:waitFor( NmbCatPreset.addPresetArrayToCatalog( wfCatalog, presetArray, nil ) )

	local id
	rxTest:waitFor( wfCatalog:docStore():docValue( 1 )
		:map( function( docValue )
			id = docValue._id
			return id
		end )
	)

	KSData.saveToFile( KSData.make( "<xmp>bar</xmp>" ), presetPath )
	
	local newDigest = "BEEFDEAD"

	rxTest:waitFor( NmbCatPreset.updatePresetSettings( wfCatalog, id, presetPath, newDigest ) )

	rxTest:expectSingleValue( wfCatalog:docStore():docValue( 1 )
		:pluck( 'digest' ),
	newDigest )

end

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

function NmbCatPresetTest.rxTestRenamePresetWithDigest( rxTest )

	rxTest:setTimeLimit( 5 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )

	local presetPathId = KSIdGenerator.uuidInLowercaseHex()
	local presetPath = wfCatalog:fileSpec():pathForStyle( presetPathId )
	KSData.saveToFile( KSData.make( "<xmp>foo</xmp>" ), presetPath )
	fileCleanup( rxTest, presetPath )

	local digest = 'DEADBEEF'

	local presetArray = {
		{
			filePath = presetPath,
			name = "myPreset",
			groupName = "someGroup",
			isLook = false,
			needsRename = false,
			xmpString = "<xmp>foo</xmp>",
			digest = digest
		},
	}

	rxTest:waitFor( NmbCatPreset.addPresetArrayToCatalog( wfCatalog, presetArray, nil ) )

	local id
	rxTest:waitFor( wfCatalog:docStore():docValue( 1 )
		:map( function( docValue )
			id = docValue._id
			return id
		end )
	)

	local newDigest = "BEEFDEAD"

	local params = {
		id = id,
		filePath = presetPath,
		newName = "renamedPreset",
		newDigest = newDigest,
	}

	rxTest:waitFor( NmbCatPreset.renamePreset( wfCatalog, params, nil) )

	rxTest:expectSingleValue( wfCatalog:docStore():docValue( 1 )
		:pluck( 'digest' ),
	newDigest )

end

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

return WFRxTestCase.wrapSuite( NmbCatPresetTest )
