-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.
local lu = require 'luaunit'
local cooling_enums = require 'basic_cooling.define.cooling_enums'
local props = require 'basic_cooling.define.cooling_properties'
local pumps_config = require 'basic_cooling.cooling_config.pumps_config'
local cooling_requirement = require 'basic_cooling.cooling_requirememts'

TestCoolingRequirement = {}

function TestCoolingRequirement:test_is_component_identifier_match()
    local tmp = {
        cpu_data = {
            get_cpu_model = function()
                return {['test1'] = 1, ['test2'] = 1}
            end
        },
        memory_data = {
            get_memory_part_number = function()
                return {['test3'] = 1, ['test4'] = 1}
            end
        }
    }
    local identifiers = {'test1', 'test2'}
    local ok, res = pcall(function()
        return cooling_requirement.is_component_identifier_match(tmp, identifiers)
    end)
    lu.assertEquals(ok, true)
    lu.assertEquals(res, true)
    identifiers = {'test5'}
    ok, res = pcall(function()
        return cooling_requirement.is_component_identifier_match(tmp, identifiers)
    end)
    lu.assertEquals(ok, true)
    lu.assertEquals(res, false)
end

function TestCoolingRequirement:test_get_unique_requirement()
    local requirements = {
        [122] = {
            RequirementId = 122,
            TargetTemperatureCelsius = 85,
            SensorName = 'CPU1 Core Rem'
        },
        [123] = {
            RequirementId = 123,
            TargetTemperatureCelsius = 90,
            SensorName = 'CPU1 Core Rem'
        },
        [124] = {
            RequirementId = 124,
            TargetTemperatureCelsius = 90,
            SensorName = 'CPU1 Mem'
        },
        [125] = {
            RequirementId = 125,
            TargetTemperatureCelsius = 85,
            SensorName = 'CPU1 Mem'
        }
    }
    local ok, res = pcall(function()
        return cooling_requirement:get_unique_requirement(requirements)
    end)
    lu.assertEquals(ok, true)
    lu.assertEquals(res['CPU1 Mem'].TargetTemperatureCelsius, 85)
    lu.assertEquals(res['CPU1 Mem'].RequirementId, 125)
    lu.assertEquals(res['CPU1 Core Rem'].TargetTemperatureCelsius, 85)
    lu.assertEquals(res['CPU1 Core Rem'].RequirementId, 122)
end

function TestCoolingRequirement:test_get_requirement_invalid_cause()
    local tmp = {
        default_cpu_valid = true,
        default_memory_valid = true
    }
    local disk_temp_available = false
    local power_state = 'ON'
    local tmp_requirement = {
        ['TemperatureType'] = 1, -- 类型为CPU
        ['Enabled'] = true,
        ['IsBackupRequirement'] = false,
        ['ObtainTempFaildToValid'] = false,
        ['IsValid'] = 1
    }
    local ok, res = pcall(function()
        return cooling_requirement.get_requirement_invalid_cause(tmp, disk_temp_available, tmp_requirement, power_state)
    end)
    lu.assertEquals(ok, true)
    lu.assertEquals(res, 'None')
    tmp_requirement['TemperatureType'] = 4 -- 类型为Memory
    ok, res = pcall(function()
        return cooling_requirement.get_requirement_invalid_cause(tmp, disk_temp_available, tmp_requirement, power_state)
    end)
    lu.assertEquals(ok, true)
    lu.assertEquals(res, 'None')
end