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

LrClassicCatalogTest.lua

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

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

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

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

	-- WF.core.utils
local WFPlatformInfo = require 'WFPlatformInfo'
local KSTableUtils = require 'KSTableUtils'

	-- WF.oz_client.managed_catalog.sync
local WFCatOzTestSession = require 'WFCatOzTestSession'

	-- WF.web.json
local KSJsonEncoder = require 'KSJsonEncoder'
local KSJsonDecoder = require 'KSJsonDecoder'

	-- WF.lrclassic
local LrClassicCatalog = require 'LrClassicCatalog'

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

local LrClassicCatalogTest = {}

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

function LrClassicCatalogTest.rxTestSetAndGetCatalogName_testNightly( rxTest )

	rxTest:setTimeLimit( 45 )
	
	local testSession, catalog, catalogSpec, pusher, puller = WFCatOzTestSession.setupOzTest( rxTest )
	assert( testSession )

	local rootCatalogPath = catalog:fileSpec():rootPath()
	local catalogOptArgs = {
		docStorePath = catalog:docStore():path(),
		indexSpecScript = catalog:indexStore():indexSpecScript(),
	}
	
	local classicCatalog = LrClassicCatalog( catalogSpec, rootCatalogPath, catalogOptArgs )
	
	rxTest:waitFor( puller:getUpdatesOnce() )
	
	rxTest:expectSingleValue( classicCatalog:getCatalogName():take( 1 ), 'WFCatTestSession' )
	
	-- setCatalogName
	
	rxTest:waitFor( classicCatalog:setCatalogName( 'lightroom_foo' ) )
	
	rxTest:expectSingleValue( classicCatalog:getCatalogName():take( 1 ), 'lightroom_foo' )
	
	rxTest:waitFor( pusher:_DEBUG_sendUpdatesOnce( 1 ) )
	
	-- verify via oz api
	
	local catalogDocUrl = string.format( '/v2/catalogs/%s',
										  catalogSpec:catalogId() )

	rxTest:expectSingleValue(
		catalogSpec:get( catalogDocUrl )
			:map( KSJsonDecoder.decodeWebSafeJson )
			:map( function( docValue )
				return KSTableUtils.derefOpt( docValue, 'payload', 'name' ) == 'lightroom_foo'
			end ),
			true )
	
	-- update catalog name via oz api
	
	local catalogDocContent = KSJsonEncoder.encodeOzUploadJson {
		payload = {
			name = "lightroom_bar",
		},
	}
	
	local catalogDocPutArgs = {
		requestHeaders = {
			{ header = 'Content-Type', value = 'application/json' },
		},
		requestMethod = 'POST',
	}
	
	rxTest:expectOnlyCompleted(
		catalogSpec:put( catalogDocUrl, catalogDocContent, catalogDocPutArgs )
			:filter( function() return false end ) )
	
	-- verify via puller
	
	puller:getUpdatesContinuously()
	
	local catalogNameObserve = rxTest:observe( classicCatalog:getCatalogName() )

	catalogNameObserve:expectDataWithinTime( 10, 'lightroom_bar' )
	
	puller:stopContinuousUpdates()
	
end

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

if WFPlatformInfo.isDesktop then
	return WFRxTestCase.wrapSuite( 'LrClassicCatalogTest', LrClassicCatalogTest )
else
	return {}
end
