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

ADOBE CONFIDENTIAL
------------------
Copyright 2025 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 KSIdGenerator = require 'KSIdGenerator'
local KSPathUtils = require 'KSPathUtils'
local WFFileUtils = require 'WFFileUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

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

    -- WF.sqlite.doc_store
local WFRxDocStoreUtils = require 'WFRxDocStoreUtils'

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

local WFRxDocStoreUtilsTest = {}

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

local _wfCatalog

local function createFakeWFCatalogForTestSuite( rxTest )

	if not _wfCatalog then

		local optArgs = {
			keepOpenAfterTestFinishes = true
		}

		if WFPlatformInfo.isMobile() then
			optArgs.indexDefinitions = 'LrmCatalogIndexDefinitions.lua'
		end

		_wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest , nil, nil, optArgs )

	end

	return _wfCatalog

end

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

local function buildDocValue( rxTest, overrides )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local wfCatalog = createFakeWFCatalogForTestSuite( rxTest )

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

	return docValue, expectedData, wfCatalog

end

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

function WFRxDocStoreUtilsTest.rxTestDumpSyncingDocsToPath( rxTest )

	rxTest:setTimeLimit( 5 )

	local overrides = {
		noMasterLink = true
	}

	local docValue, wfCatalog, _

	for i = 1, 5 do
		docValue, _, wfCatalog = buildDocValue( rxTest, overrides )
		rxTest:waitFor( wfCatalog:docStore():setDocValue( docValue ) )
	end

	local parentDir =
			KSPathUtils.child(
					KSPathUtils.getStandardFilePath( 'temp' ),
					KSIdGenerator.uuidInLowercaseHex() )

	WFFileUtils.mkdir( parentDir )

	rxTest:finally( function()
		WFFileUtils.deleteDirectory( parentDir )
	end )

	local path = KSPathUtils.child( parentDir, rxTest:testName() )

	-- Simple test that only verifies the DB is created.

	path = rxTest:getFirstValue( WFRxDocStoreUtils.dumpSyncingDocsToPath( wfCatalog, path ) )

	rxTest:expectEqual( 'file', WFFileUtils.exists( path ) )

end

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

return WFRxTestCase.wrapSuite( 'WFRxDocStoreUtilsTest', WFRxDocStoreUtilsTest )
