-- 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 abnormal_fan =  require "basic_cooling.cooling_abnormal_device.abnormal_fan"
local props = require 'basic_cooling.define.cooling_properties'
local enums = require 'basic_cooling.define.cooling_enums'

TestAbnormalFan = {}

function TestAbnormalFan:test_get_abnormal_fan_obj()
    abnormal_fan.objs = {[1] = nil}
    local ok = pcall(abnormal_fan.get_abnormal_fan_obj, abnormal_fan, 1, 1)
    lu.assertEquals(ok, true)   
    abnormal_fan.objs = {[1] = {[1] = {}}}
    ok = pcall(abnormal_fan.get_abnormal_fan_obj, abnormal_fan, 1, 1)
    lu.assertEquals(ok, true)    
end

function TestAbnormalFan:test_get_replacement_abnormal_fan_obj()
    -- 无该风扇异常对象时，函数应安全返回（pcall 成功，结果为 nil）
    abnormal_fan.objs = {}
    local ok, rsp = pcall(abnormal_fan.get_replacement_abnormal_fan_obj, abnormal_fan, 1)
    lu.assertEquals(ok, true)
    lu.assertEquals(rsp, nil)

    -- 存在多个异常对象时，应返回 SpeedPercentage 最大的对象
    abnormal_fan.objs = {
        [1] = {
            [1] = { SpeedPercentage = 10 },
            [2] = { SpeedPercentage = 30 },
            [3] = { SpeedPercentage = 20 }
        }
    }
    ok, rsp = pcall(abnormal_fan.get_replacement_abnormal_fan_obj, abnormal_fan, 1)
    lu.assertEquals(ok, true)
    lu.assertEquals(rsp.SpeedPercentage, 30)
end

function TestAbnormalFan:test_obj_add_callback()
    local object = {
        FanIdx = 1,
        Status = 1,
        [props.ABNORMAL_FAN_PRIORITY] = 2
    }
    abnormal_fan.objs = {
        [1] = {[1] = {[props.ABNORMAL_FAN_PRIORITY] = 1}}
    }
    local ok = pcall(abnormal_fan.obj_add_callback, abnormal_fan, 1, object, 1)
    lu.assertEquals(ok, true)    
end

function TestAbnormalFan:test_obj_delete_callback()
    local object = {
        FanIdx = 1,
        Status = 1
    }
    local ok = pcall(abnormal_fan.obj_delete_callback, abnormal_fan, 1, object, 1)
    lu.assertEquals(ok, true)
end

function TestAbnormalFan:test_gen_exp_speed_key()
    local ok = pcall(abnormal_fan.gen_exp_speed_key, nil, nil, nil)
    lu.assertEquals(ok, true)
end

function TestAbnormalFan:test_get_fan_abn_status()
    abnormal_fan.abn_status_mapping = {
        ['Normal'] = 0,
        ['AbnormalRotation'] = 1,
        ['StopRotation'] = 32
    }
    abnormal_fan.abn_presence_mapping = {[0] = 'NotInPosition', [1] = 'Normal'}
    local ok, rsp = pcall(abnormal_fan.get_fan_abn_status, abnormal_fan, 1, 1, 1, 0, 0)
    lu.assertEquals(ok, true)
    lu.assertEquals(rsp, "Normal")
    ok, rsp = pcall(abnormal_fan.get_fan_abn_status, abnormal_fan, 1, 0, 1, 0, 0)
    lu.assertEquals(ok, true)
    lu.assertEquals(rsp, "NotInPosition")
    ok, rsp = pcall(abnormal_fan.get_fan_abn_status, abnormal_fan, 1, 1, 1, 1, 0)
    lu.assertEquals(ok, true)
    lu.assertEquals(rsp, "AbnormalRotation")
    -- 测试StopRotation枚举值
    ok, rsp = pcall(abnormal_fan.get_fan_abn_status, abnormal_fan, 1, 1, 1, 32, 0)
    lu.assertEquals(ok, true)
    lu.assertEquals(rsp, "StopRotation")
    ok, rsp = pcall(abnormal_fan.get_fan_abn_status, abnormal_fan, 1, 1, 1, 0, 32)
    lu.assertEquals(ok, true)
    lu.assertEquals(rsp, "StopRotation")
end

function TestAbnormalFan:test_add_exp_speed_t()
    abnormal_fan.exp_speed_t = {[1] = ""}
    local ok = pcall(abnormal_fan.add_exp_speed_t, abnormal_fan, 1, 1)
    lu.assertEquals(ok, true)
end

function TestAbnormalFan:test_delete_exp_speed_t()
    abnormal_fan.exp_speed_t = {[1] = {[1] = "1"}}
    local ok = pcall(abnormal_fan.delete_exp_speed_t, abnormal_fan, 1)
    lu.assertEquals(ok, true)
end

function TestAbnormalFan:test_update_exp_speed_table()
    abnormal_fan.obj = {[0] = nil}
    local ok = pcall(abnormal_fan.update_exp_speed_table, abnormal_fan, 1, 0, "")
    lu.assertEquals(ok, true)
    abnormal_fan.obj = {[1] = {["1"] = {FanGroup = {}, SpeedPercentage = 1}}}
    abnormal_fan.exp_speed_t = {["AbnormalFan_1_1"] = nil}
    abnormal_fan.refresh_exp_speed_table = function(...)
        return
    end
    abnormal_fan.generate_abn_policy_table = function(...)
        return {[1] = ""}
    end
    ok = pcall(abnormal_fan.update_exp_speed_table, abnormal_fan, enums.exp_type.ABNORMAL_FAN, 1, "1")
    lu.assertEquals(ok, true)
end

function TestAbnormalFan:test_process_fan_status_signal()
    abnormal_fan.abn_status_tasks_handler = {[1] = 1}
    local ok = pcall(abnormal_fan.process_fan_status_signal, abnormal_fan, 1, 1, 1, 0, 0)
    lu.assertEquals(ok, true)
end