-- Copyright (c) 2025 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 jbog_device_mgmt = require 'device.jbog_device_mgmt'

TestJBOGDeviceMgmt = {}

function TestJBOGDeviceMgmt:test_get_pcie_led_param()
    lu.assertEquals(jbog_device_mgmt.get_pcie_led_param(1), "q01")
    lu.assertEquals(jbog_device_mgmt.get_pcie_led_param(32), "qFF")
    lu.assertEquals(jbog_device_mgmt.get_pcie_led_param(11), "q11")
end

function TestJBOGDeviceMgmt:test_get_jbog_slot()
    local device_info = jbog_device_mgmt.get_jbog_slot(1, 12, 
    {bus = 26, device = 0, func = 0},
    {bus = 22, device = 4, func = 0}
        )
    lu.assertEquals(device_info.bus, 26)
    lu.assertEquals(device_info.slot_number, 1 & 0x1f)
    lu.assertEquals(device_info.switch_id, 0)
end

function TestJBOGDeviceMgmt:test_get_physical_slot_number_info()
    local test_topo_info = {
        Slot = 1,
        Reserved1 = 12,
        Bus = 26,
        Device = 0,
        Functions = 0
    }
    local local_db = {
        select = function()
            return {
                where = function()
                    return {
                        first = function()
                            return test_topo_info
                        end
                    }
                end
            }
        end,
        SystemsPCIeTopoInfo = {}
    }
    
    local device_info = jbog_device_mgmt.get_physical_slot_number_info(0,0, 26, 0,0, local_db)

    lu.assertEquals(device_info.bus, 26)
    lu.assertEquals(device_info.slot_number, 1 & 0x1f)
    lu.assertEquals(device_info.switch_id, 0)
end

function TestJBOGDeviceMgmt:test_find_alarm_list()
    lu.assertEquals(jbog_device_mgmt.find_alarm_list(), true)
end

function TestJBOGDeviceMgmt:test_jbog_generate_ras_error_code()
    lu.assertEquals(jbog_device_mgmt.jbog_generate_ras_error_code(), 1)
end

function TestJBOGDeviceMgmt:test_set_jbog_predictive_fault()
    local device_info = {
        box_id = 0,
        switch_id = 0,
        type_id = 1,
        slot_number = 0,
        bus = 26,
        device = 0,
        func = 0,
        root_bus = 22,
        root_device = 1,
        root_func = 0
    }
    lu.assertEquals(jbog_device_mgmt.set_jbog_predictive_fault(device_info, 1), 1)
end

function TestJBOGDeviceMgmt:test_set_jbog_diagnostic_fault()
    local device_info = {
        box_id = 0,
        switch_id = 0,
        type_id = 1,
        slot_number = 0,
        bus = 26,
        device = 0,
        func = 0,
        root_bus = 22,
        root_device = 1,
        root_func = 0
    }
    lu.assertEquals(jbog_device_mgmt.set_jbog_diagnostic_fault(device_info, 1), 1)
end

function TestJBOGDeviceMgmt:test_set_jbog_bandwidth_reduction()
    local device_info = {
        box_id = 0,
        switch_id = 0,
        type_id = 1,
        slot_number = 0,
        bus = 26,
        device = 0,
        func = 0,
        root_bus = 22,
        root_device = 1,
        root_func = 0
    }
    lu.assertEquals(jbog_device_mgmt.set_jbog_bandwidth_reduction(device_info, 1), 1)
end

function TestJBOGDeviceMgmt:test_set_jbog_link_speed_reduced()
    local device_info = {
        box_id = 0,
        switch_id = 0,
        type_id = 1,
        slot_number = 0,
        bus = 26,
        device = 0,
        func = 0,
        root_bus = 22,
        root_device = 1,
        root_func = 0
    }
    lu.assertEquals(jbog_device_mgmt.set_jbog_link_speed_reduced(device_info, 1), 1)
end

function TestJBOGDeviceMgmt:test_set_jbog_fault()
    local device_info = {
        box_id = 0,
        switch_id = 0,
        type_id = 1,
        slot_number = 0,
        bus = 26,
        device = 0,
        func = 0,
        root_bus = 22,
        root_device = 1,
        root_func = 0
    }
    lu.assertEquals(jbog_device_mgmt.set_jbog_fault(device_info, 1, 0x08000001), 1)
end

function TestJBOGDeviceMgmt:test_cancel_soft_alarm()
    lu.assertEquals(jbog_device_mgmt.cancel_soft_alarm(), nil)
end