package.path = "../?.lua;" .. package.path
require("init")

math.randomseed(123)

--mocking
local rand1 = {}
function rand1:next(min, max) return math.random(min, max) end
_G.ItemStack = function(str)
	return {
		string = str,
		count = 1,
		set_count = function(stack, count) stack.count = count end,
		get_count = function(stack) return stack.count end,
	}
end

describe("get_items1", function()
	local loot_table =
	{
		rolls = 1,
		rolls_max = 2,
		loots =
		{
			{
				weight = 10,
				loot = "teststring",
				repetitions = 1,
			},
			{
				weight = 1,
				loot = "teststring2",
				repetitions = 1,
				repetitions_max = 10,
			},
			accumulated_weight = 11,
		},
		
	}
		it("picks inside_counts", function()
			local min_count = math.huge
			local max_count = -math.huge
			
			local min_length = math.huge
			local max_length = -math.huge
			
			for i = 1, 1000
			do
				local items = inventory_populator.get_items(loot_table, rand1)
				for i, v in ipairs(items)
				do
					min_count = math.min(min_count, v.count)
					max_count = math.max(max_count, v.count)
				end
				min_length = math.min(min_length, #items)
				max_length = math.max(max_length, #items)
			end
				
				assert.equals(1, min_length)
				assert.equals(1, min_count)
				assert.equals(2, max_length)
				assert.equals(10, max_count)
			end)
		local loot_table2 =
		{
			rolls = 5,
			rolls_max = 5,
			loots =
			{
				{
					weight = 2,
					loot = "teststring",
					repetitions = 1,
				},
				accumulated_weight = 2,
			},
		}
		local loot_table3 =
		{
			rolls = 1,
			rolls_max = 1,
			loots =
			{
				{
					weight = 1,
					loot = loot_table2,
					repetitions = 1,
				},
				accumulated_weight = 1,
			},
		}
		it("recurs", function()
				local items = inventory_populator.get_items(loot_table3, rand1)
				assert.equals(5, #items)
			end)
	end)
