-- 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.
-- See the Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2

local lu = require 'luaunit'

local PATH = '/bmc/kepler/Systems/1/Events'

-- Stub used only in this test file; installed in setUp, removed in tearDown to avoid affecting other tests
local function make_client_stub()
    return {
        ForeachEventsObjects = function(_, cb)
            cb({ path = PATH, AddEvent_PACKED = function(_, param)
                return { unpack = function() return 'id123' end }
            end })
        end
    }
end

TestAddEvent = {}

function TestAddEvent:setUp()
    package.loaded['add_event'] = nil
    package.loaded['storage.client'] = nil
    package.preload['storage.client'] = function()
        return make_client_stub()
    end
end

function TestAddEvent:tearDown()
    package.loaded['add_event'] = nil
    package.loaded['storage.client'] = nil
    package.preload['storage.client'] = nil
end

-- generate_bbu_lower_voltage + get_pcie_card_name coverage
function TestAddEvent:test_generate_bbu_lower_voltage()
    local add_event = require 'add_event'
    local ok = pcall(add_event.generate_bbu_lower_voltage, add_event, 'true', '0', 0)
    lu.assertTrue(ok)
    ok = pcall(add_event.generate_bbu_lower_voltage, add_event, 'false', '1', 96)
    lu.assertTrue(ok)
    ok = pcall(add_event.generate_bbu_lower_voltage, add_event, 'true', '2', 999)
    lu.assertTrue(ok)
end

-- get_pcie_card_name: cover map entries and Unknown
function TestAddEvent:test_get_pcie_card_name_coverage()
    local add_event = require 'add_event'
    local ids = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20,
                  96, 97, 98, 99, 100, 101, 102, 103, 113 }
    for _, id in ipairs(ids) do
        pcall(add_event.generate_bbu_lower_voltage, add_event, 'true', '0', id)
    end
    pcall(add_event.generate_bbu_lower_voltage, add_event, 'true', '0', 999)
end

function TestAddEvent:test_generate_drive_max_temp()
    local add_event = require 'add_event'
    local ok = pcall(add_event.generate_drive_max_temp, add_event, 'true', 60)
    lu.assertTrue(ok)
    ok = pcall(add_event.generate_drive_max_temp, add_event, 'false', 70)
    lu.assertTrue(ok)
end

function TestAddEvent:test_generate_phy_increase_too_fast()
    local add_event = require 'add_event'
    local ok = pcall(add_event.generate_phy_increase_too_fast, add_event, 'true', 'desc')
    lu.assertTrue(ok)
end

-- generate_percentage_threshold returns add_event(param); stub makes it succeed
function TestAddEvent:test_generate_percentage_threshold()
    local add_event = require 'add_event'
    local ret = add_event.generate_percentage_threshold(add_event, true, 'Disk0')
    lu.assertEquals(ret, true)
    ret = add_event.generate_percentage_threshold(add_event, false, 'Disk1', '80')
    lu.assertEquals(ret, true)
end

-- generate_nvme_link_fault returns add_event(param)
function TestAddEvent:test_generate_nvme_link_fault()
    local add_event = require 'add_event'
    local ret = add_event.generate_nvme_link_fault(add_event, 'true', 'Disk0')
    lu.assertEquals(ret, true)
end
