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

WFCatAssetInfoListUtilsTest.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.

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

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

--local Rx = require 'Rx'
local WFCatAssetInfoListUtils = require "WFCatAssetInfoListUtils"

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

local function makeAssetInfo( id, optOrientation, optCaptureDate )

	return {
		id,
		id,
		id,
		'image',
		optOrientation or 1,
		optCaptureDate or "2020-05-28T20:24:00",
		0,
		"file" .. tostring( id ),
	}

end

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

local function rxTestMergeAndSplit( rxTest )

--===============================================================================
-- @mark	Split - splitPackedAssetInfoTable - first and no change to list
--===============================================================================

	-- first call to split should return all items in list
	local assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
	}

	local queryUpdateCount = 1
	local expectedAssetInfosAfterSplit = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		queryUpdateCount = queryUpdateCount,
		requestNumber = 1,
	}

	local changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- No changes, second call to split should return nil so we know to suppress processing it.
	-- Also this shouldn't increase the queryUpdateCount.

	expectedAssetInfosAfterSplit = nil

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

--===============================================================================
-- @mark	Split - splitPackedAssetInfoTable - Remove element from list
--===============================================================================

	-- Remove top element of array size 3
	assetInfos = {
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 2,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Remove top element of array size 2
	assetInfos = {
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 2,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Remove top element of array size 1
	assetInfos = {
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Add array size 3 to previously empty list
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Remove middle element from array size 3
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 1,
		keepBottomFrom = 3,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Remove bottom element from array size 2
	assetInfos = {
		makeAssetInfo( 1 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 1,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Remove element from array size 1
	assetInfos = {
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Refresh empty list with another empty list
	assetInfos = {
	}

	expectedAssetInfosAfterSplit = nil

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

--===============================================================================
-- @mark	Split - splitPackedAssetInfoTable - Add elements to list
--===============================================================================

	-- Add array size 3 to previously empty list
	assetInfos = {
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Add 3 more to front of list
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 1,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Add 3 more to the middle of the list
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 3,
		keepBottomFrom = 4,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Add 3 more to end of list
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 9,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

--===============================================================================
-- @mark	Split - Change element in list
--===============================================================================

	-- Change first element
	assetInfos = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 1, 2 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 2,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Change last element
	assetInfos = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12, 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 12, 3 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 11,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Change middle element
	assetInfos = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6, 4 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12, 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 6, 4 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 5,
		keepBottomFrom = 7,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Change elements 3 and 7
	assetInfos = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3, 5 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6, 4 ),
		makeAssetInfo( 7, 5 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12, 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 3, 5 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6, 4 ),
		makeAssetInfo( 7, 5 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 2,
		keepBottomFrom = 8,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Change all back
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

--===============================================================================
-- @mark	Split - Change to a new query (requestNumber different)
--===============================================================================

	-- Changing the requestNumber will cause the complete list to be sent
	-- ignoring previous query results.
	assetInfos = {
		makeAssetInfo( 20 ),
		makeAssetInfo( 21 ),
		makeAssetInfo( 22 ),
		makeAssetInfo( 23 ),
		makeAssetInfo( 24 ),
		makeAssetInfo( 25 ),
		makeAssetInfo( 26 ),
		makeAssetInfo( 27 ),
		makeAssetInfo( 28 ),
		makeAssetInfo( 29 ),
	}

	queryUpdateCount = 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 20 ),
		makeAssetInfo( 21 ),
		makeAssetInfo( 22 ),
		makeAssetInfo( 23 ),
		makeAssetInfo( 24 ),
		makeAssetInfo( 25 ),
		makeAssetInfo( 26 ),
		makeAssetInfo( 27 ),
		makeAssetInfo( 28 ),
		makeAssetInfo( 29 ),
		requestNumber = 2,
		queryUpdateCount = queryUpdateCount,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 2 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Change the requestNumber back to 1 for the rest of the tests below.
	-- This is essentially the same as the above test.
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

--===============================================================================
-- @mark	Split - Remove multiple element from list
--===============================================================================

	-- Remove element 3 and 7 from middle of list
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 2,
		keepBottomFrom = 8,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Remove bottom 2 elements from array size 10
	assetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 8,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

	-- Remove all elements
	assetInfos = {
	}

	queryUpdateCount = queryUpdateCount + 1
	expectedAssetInfosAfterSplit = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	changedPackedAssetInfos = WFCatAssetInfoListUtils.splitPackedAssetInfoTable( assetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterSplit, changedPackedAssetInfos )

--===============================================================================
-- @mark	merge
--===============================================================================


--===============================================================================
-- @mark	Merge - first and no change to list
--===============================================================================

	-- first call to merge should return all items in list
	queryUpdateCount = queryUpdateCount + 1
	local expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
	}

	local packedAssetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount
	}

	local mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- No changes, second call to split should return to keep all items in list
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 3
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

--===============================================================================
-- @mark	Merge - Remove element from list
--===============================================================================

	-- Remove top element of array size 3
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 2,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Remove top element of array size 2
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 2,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Remove top element of array size 1
	expectedAssetInfosAfterMerge = {
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Add array size 3 to previously empty list
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Remove middle element from array size 3
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 1,
		keepBottomFrom = 3,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Remove bottom element from array size 2
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos  = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 1,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Remove element from array size 1
	expectedAssetInfosAfterMerge = {
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Refresh empty list with another empty list
	expectedAssetInfosAfterMerge = {
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

--===============================================================================
-- @mark	Merge - Add elements to list
--===============================================================================

	-- Add array size 3 to previously empty list
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Add 3 more to front of list
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 1,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Add 3 more to the middle of the list
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 3,
		keepBottomFrom = 4,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Add 3 more to end of list
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 9,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

--===============================================================================
-- @mark	Merge - Change element in list
--===============================================================================

	-- Change first element
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 1, 2 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
		keepBottomFrom = 2,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Change last element
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12, 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 12, 3 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 11,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Change middle element
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6, 4 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12, 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 6, 4 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 5,
		keepBottomFrom = 7,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Change elements 3 and 7
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1, 2 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3, 5 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6, 4 ),
		makeAssetInfo( 7, 5 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12, 3 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 3, 5 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6, 4 ),
		makeAssetInfo( 7, 5 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 2,
		keepBottomFrom = 8,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Change all back
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )


--===============================================================================
-- @mark	Split - Change to a new query (requestNumber different)
--===============================================================================

	-- Changing the requestNumber will cause the complete list to be sent
	-- ignoring previous query results.
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 20 ),
		makeAssetInfo( 21 ),
		makeAssetInfo( 22 ),
		makeAssetInfo( 23 ),
		makeAssetInfo( 24 ),
		makeAssetInfo( 25 ),
		makeAssetInfo( 26 ),
		makeAssetInfo( 27 ),
		makeAssetInfo( 28 ),
		makeAssetInfo( 29 ),
	}

	queryUpdateCount = 1
	packedAssetInfos = {
		makeAssetInfo( 20 ),
		makeAssetInfo( 21 ),
		makeAssetInfo( 22 ),
		makeAssetInfo( 23 ),
		makeAssetInfo( 24 ),
		makeAssetInfo( 25 ),
		makeAssetInfo( 26 ),
		makeAssetInfo( 27 ),
		makeAssetInfo( 28 ),
		makeAssetInfo( 29 ),
		requestNumber = 2,
		queryUpdateCount = queryUpdateCount,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 2 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )


	-- Change the requestNumber back to 1 for the rest of the tests below.
	-- This is essentially the same as the above test.
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = 1
	packedAssetInfos = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 3 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 7 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

--===============================================================================
-- @mark	Merge - Remove multiple element from list
--===============================================================================

	-- Remove element 3 and 7 from middle of list
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
		makeAssetInfo( 11 ),
		makeAssetInfo( 12 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 2,
		keepBottomFrom = 8,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Remove bottom 2 elements from array size 10
	expectedAssetInfosAfterMerge = {
		makeAssetInfo( 1 ),
		makeAssetInfo( 2 ),
		makeAssetInfo( 4 ),
		makeAssetInfo( 5 ),
		makeAssetInfo( 6 ),
		makeAssetInfo( 8 ),
		makeAssetInfo( 9 ),
		makeAssetInfo( 10 ),
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 8,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

	-- Remove all elements
	expectedAssetInfosAfterMerge = {
	}

	queryUpdateCount = queryUpdateCount + 1
	packedAssetInfos = {
		requestNumber = 1,
		queryUpdateCount = queryUpdateCount,
		keepTopTo = 0,
	}

	mergedPackedAssetInfos = WFCatAssetInfoListUtils.mergePackedAssetInfoTable( packedAssetInfos, 1 )
	rxTest:expectEqual( expectedAssetInfosAfterMerge, mergedPackedAssetInfos )

end

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

return WFRxTestCase.wrapSuite( {

	rxTestMergeAndSplit = rxTestMergeAndSplit,

} )
