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

LrClassicSyncStatusTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2024 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
local KSIdGenerator = require 'KSIdGenerator'
local WFCapabilities = require 'WFCapabilities'
local KSTableUtils 	= require 'KSTableUtils'

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

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

local LrClassicSyncStatus = require 'LrClassicSyncStatus'

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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog

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

-- @mark LrClassicSyncStatusTest

local LrClassicSyncStatusTest = {}

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

local function buildDocValue( rxTest, wfCatalog, overrides )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local docValue, expectedData = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId, overrides )

	return docValue, expectedData, wfCatalog

end

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

function LrClassicSyncStatusTest.rxTestGetSyncStatusForAssets_testSerially( rxTest )

	rxTest:setTimeLimit( 10 )

	WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', 'true' )

	rxTest:finally( function()
		WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', nil )
	end )

	local catalogOptions = {
		indexDefinitions = 'LrClassicIndexDefinitions.lua'
	}

	local wfCatalog = createFakeWFCatalog( rxTest, nil, nil, catalogOptions )
	rxTest:addCloseTarget( wfCatalog )

		local syncStatusObservable = LrClassicSyncStatus.getSyncStatusForAssets( wfCatalog )
		:map( function( statuses )
			local nonZeroStatuses = {}
			for k, v in pairs( statuses ) do
				if v ~= 0 then
					nonZeroStatuses[ k ] = v
				end
			end
			return nonZeroStatuses
		end )

	local docValue = buildDocValue( rxTest, wfCatalog )

	rxTest:waitFor( wfCatalog:docStore():setDocValue( docValue ) )

	rxTest:observe( syncStatusObservable )
		:expectDataWithinTime( 5,
			{
				{
					needsDevelopXmpUpload = 0,
					storageLimitExceeded = 0,
					syncError = 0,
					synced = 1,
					syncing = 0,
					trialOrSubscriptionExpired = 0,
					uploading = 0,
				},
			} )

end

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

function LrClassicSyncStatusTest.rxTestIndexMissingBinaries_testSerially( rxTest )

	WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', 'true' )

	rxTest:finally( function()
		WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', nil )
	end )

	local catalogOptions = {
		indexDefinitions = 'LrClassicIndexDefinitions.lua'
	}

	local wfCatalog = createFakeWFCatalog( rxTest, nil, nil, catalogOptions )
	rxTest:addCloseTarget( wfCatalog )

	local optArguments = {
		hasMasks = true,
		noMaskLinks = true,
		noProxyLink = true,
		noLocalProxy = true,
		willNotUploadMaster = true,
	}

	local docValue, localOnlySettings = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, nil, optArguments )
	KSTableUtils.setNested( docValue, nil, '_localOnly', 'files', 'aux' )

	rxTest:waitFor( wfCatalog:docStore():setDocValue( docValue ) )

	local kSubDelimiter = "::"

	local missingAux1 = ",missingAux" .. kSubDelimiter .. localOnlySettings.mask1Fingerprint
	local missingAux2 = ",missingAux" .. kSubDelimiter .. localOnlySettings.mask2Fingerprint

	local expectedResult1 = "missingProxy" .. missingAux1 .. missingAux2
	local expectedResult2 = "missingProxy" .. missingAux2 .. missingAux1

	rxTest:expectSingleValue( wfCatalog:indexStore():queryOnce{
			sql = [[SELECT missingBinaries FROM classic_asset_sync_status]],
			singleColumnQuery = true
		}
		:map( function( result )

			return result and result[ 1 ]
				and ( result[ 1 ] == expectedResult1 or result[ 1 ] == expectedResult2 )

		end ),
		true
		)

end

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

function LrClassicSyncStatusTest.rxTestGetMissingProxyList_testSerially( rxTest )

	WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', 'true' )

	rxTest:finally( function()
		WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', nil )
	end )

	local catalogOptions = {
		indexDefinitions = 'LrClassicIndexDefinitions.lua'
	}

	local wfCatalog = createFakeWFCatalog( rxTest, nil, nil, catalogOptions )
	rxTest:addCloseTarget( wfCatalog )

	local optArguments = {
		hasMasks = true,
		noMaskLinks = true,
		noProxyLink = true,
		noLocalProxy = true,
		willNotUploadMaster = true,
	}

	local docValue = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, nil, optArguments )
	KSTableUtils.setNested( docValue, nil, '_localOnly', 'files', 'aux' )

	rxTest:waitFor( wfCatalog:docStore():setDocValue( docValue ) )

	rxTest:expectSingleValue( LrClassicSyncStatus.getMissingProxyList( wfCatalog ),
		{ docValue._id }
	)


end


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

function LrClassicSyncStatusTest.rxTestGetMissingAuxList_testSerially( rxTest )

	WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', 'true' )

	rxTest:finally( function()
		WFCapabilities.setCapability( 'WFPlatformInfo.isClassic', nil )
	end )

	local catalogOptions = {
		indexDefinitions = 'LrClassicIndexDefinitions.lua'
	}

	local wfCatalog = createFakeWFCatalog( rxTest, nil, nil, catalogOptions )
	rxTest:addCloseTarget( wfCatalog )

	local optArguments = {
		hasMasks = true,
		noMaskLinks = true,
		noProxyLink = true,
		noLocalProxy = true,
		willNotUploadMaster = true,
	}

	local docValue, localOnlySettings = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, nil, optArguments )
	KSTableUtils.setNested( docValue, nil, '_localOnly', 'files', 'aux' )

	rxTest:waitFor( wfCatalog:docStore():setDocValue( docValue ) )

	local function sortAuxList( tbl )

		table.sort( tbl, function( a, b )

			return a.digest > b.digest

		end )

		return tbl

	end

	local expectedAux = {
		{
			digest = localOnlySettings.mask1Fingerprint,
			filePath = localOnlySettings.mask1FilePath
		},
		{
			digest = localOnlySettings.mask2Fingerprint,
			filePath = localOnlySettings.mask2FilePath
		}
	}

	local expectData = {
		[ docValue._id ] = sortAuxList( expectedAux )
	}

	rxTest:expectSingleValue( LrClassicSyncStatus.getMissingAuxList( wfCatalog )
		:map( function( result )

			local auxList = KSTableUtils.derefOpt( result, docValue._id )
			KSTableUtils.setNested( result, sortAuxList( auxList ), docValue._id )

			return result

		end ),
		expectData
	)


end

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

return WFRxTestCase.wrapSuite( 'LrClassicSyncStatusTest', LrClassicSyncStatusTest )

