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

ADOBE CONFIDENTIAL
------------------
Copyright 2022 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.isDesktop then
	return {} -- This model is mobile-only.
end

	-- Rx
local Rx = require 'Rx'
local Observable = Rx.Observable

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

	-- WF.core.utils
local KSData = require 'KSData'
local WFFileUtils = require 'WFFileUtils'

	-- WF.oz_client.managed_catalog
local WFCatAssetAux = require 'WFCatAssetAux'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFCatSpecMock = require 'WFCatSpecMock'

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

	-- lrmobile
local LriDevelopModel = require 'LriDevelopModel'

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

local LriDevelopModelTest = {}

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

function LriDevelopModelTest.rxTestEnsureAuxFilesExistLocally( rxTest )

	local requestOptions = {
		requestHeaders = {
			{
				header = "Content-Type",
				value = "application/octet-stream",
			},
		},
	}

	local responses = {
		{
			url = "/href/bogus/aux1",
			response = KSData.make( 'foo' ),
			headers = {
				status_code = '200',
			},
			options = requestOptions
		},
		{
			url = "/href/bogus/aux1",
			response = KSData.make( 'bar' ),
			headers = {
				status_code = '200',
			},
			options = requestOptions
		}
	}

	local catalogSpec = WFCatSpecMock(
			KSIdGenerator.uuidInLowercaseHex(),
			KSIdGenerator.uuidInLowercaseHex(),
			rxTest, responses )

	local wfCatalog = WFCatMockCatalog.createWFCatalogForTestWithSpec( rxTest, catalogSpec )

	-- Attempt to Download Aux Files

	local optArguments = {
		hasMasks = true,
		noLocalMasks = true
	}

	local docValue, expectedData = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, nil, optArguments )

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

	local isLocked = false

	local mockSyncClient = {
		lockAssets = function()
			isLocked = true
			return Observable.just( true )
		end,
		unlockAssets = function()
			isLocked = false
			return Observable.just( true )
		end
	}

	local mockOzCatalogService = {
		_wfCatalogStream = Observable.just( wfCatalog ),
		getSyncClient = function()
			return mockSyncClient
		end,
		getWfCatalog = function()
			return Observable.just( wfCatalog )
		end
	}

	local model = LriDevelopModel.makeModel( mockOzCatalogService, docValue._id )
	
	local observable = model.value:take( 1 )

	rxTest:expectSingleValue( observable, {
		success = true
	} )

	-- Make sure the asset is locked form purge:
	
	rxTest:expectEqual( true, isLocked )

	-- Verify the Document Has Local Only Aux Annotations

	local function sortAux( auxiliaries )

		local sorted = {}
		
		for _, aux in pairs( auxiliaries ) do
			sorted[ #sorted + 1 ] = aux
		end
		
		table.sort( sorted, function( a, b )
				return a.relativePath < b.relativePath
			end )

		return sorted

	end

	local _, mask1RelativePath = WFCatAssetAux.calculatePathUsingDigest( 'foo', expectedData.mask1Fingerprint )
	local _, mask2RelativePath = WFCatAssetAux.calculatePathUsingDigest( 'foo', expectedData.mask2Fingerprint )

	local expected = {
		[ expectedData.mask1Fingerprint ] = {
			relativePath = mask1RelativePath
		},
		[ expectedData.mask2Fingerprint ] = {
			relativePath = mask2RelativePath
		}
	}

	rxTest:expectSingleValue( wfCatalog:docStore():docValue( docValue._id )
		:pluck( '_localOnly', 'files', 'aux' )
		:map( sortAux ), sortAux( expected ) )

	rxTest:expectEqual( 'file', WFFileUtils.exists( expectedData.mask1FilePath ) )
	rxTest:expectEqual( 'file', WFFileUtils.exists( expectedData.mask2FilePath ) )
	
	-- Verify that the sub models can be called:
	
	local submodels = model.models
	
	-- Sanity test for downloadLatestAuxFiles
	
	local downloadLatestAuxFilesObs = submodels.downloadLatestAuxFiles( model ).value
	
	rxTest:waitFor( downloadLatestAuxFilesObs )
	
	-- Sanity test for applyChanges
	
	local applyChangesObs = submodels.applyChanges( model, {}, {} ).value
	
	rxTest:waitFor( applyChangesObs )

	-- Close the model
	
	model:close()

	-- Make sure the asset is unlocked form purge:
	
	rxTest:expectEqual( false, isLocked )

end

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

return WFRxTestCase.wrapSuite( 'LriDevelopModelTest', LriDevelopModelTest )
