-- Copyright (c) 2026 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 EventUB = require 'ub_cable.infrastructure.event'
local client = require 'pcie_device.client'

TestEventUB = {}

function TestEventUB:setupClass()
    self.GetEventsEventsObject = client.GetEventsEventsObject
    self.event_obj = client.event_obj
end

function TestEventUB:teardownClass()
    client.GetEventsEventsObject = TestEventUB.GetEventsEventsObject
    client.event_obj = self.event_obj
end

function TestEventUB:test_get_latest_alarm_list_not_EventsObject()
    EventUB.event_obj = nil
    client.GetEventsEventsObject = function ()
        return nil
    end
    local list = EventUB.get_latest_alarm_list()
    lu.assertEquals(list, false)
end

function TestEventUB:test_get_latest_alarm_list_not_ok()
    EventUB.event_obj = nil
    local temp_express = {}
    temp_express.GetAlarmList_PACKED = function()
        return false, {}
    end
    client.GetEventsEventsObject = function ()
        return {pcall = temp_express}
    end
    local list = EventUB.get_latest_alarm_list()
    lu.assertEquals(list, false)
end

function TestEventUB:test_get_latest_alarm_list_not_ret_list()
    EventUB.event_obj = nil
    local temp_express = {}
    temp_express.GetAlarmList_PACKED = function()
        return true, {}
    end
    client.GetEventsEventsObject = function ()
        return {pcall = temp_express}
    end
    local list = EventUB.get_latest_alarm_list()
    lu.assertEquals(list, false)
end

function TestEventUB:test_get_latest_alarm_list_exist_list_but_not_content()
    EventUB.event_obj = nil
    local temp_express = {}
    temp_express.GetAlarmList_PACKED = function()
        return true, {
            EventList = {}
        }
    end
    client.GetEventsEventsObject = function ()
        return {pcall = temp_express}
    end
    local list = EventUB.get_latest_alarm_list()
    lu.assertEquals(list, {})
end

function TestEventUB:test_get_latest_alarm_list_exist_list_and_have_content()
    EventUB.event_obj = nil
    local temp_express = {}
    temp_express.GetAlarmList_PACKED = function()
        return true, {
            EventList = {
                {
                    MappingTable = {
                        {
                            Key = 'end_flag',
                            Value = 0
                        }
                    }
                },
                {
                    MappingTable = {
                        {
                            Key = 'end_flag',
                            Value = 1
                        }
                    }
                }
            }
        }
    end
    client.GetEventsEventsObject = function ()
        return {pcall = temp_express}
    end
    local list = EventUB.get_latest_alarm_list()
    local text_express = {
        {end_flag = 0},
        {end_flag = 1}
    }
    lu.assertEquals(list, text_express)
end

function TestEventUB:test_add_not_event()
    local param = 0
    client.GetEventsEventsObject = function ()
        return nil
    end
    local ok, _ = pcall(EventUB.generate_event, param)
    lu.assertEquals(ok, false)
end

function TestEventUB:test_add_event_successfully()
    local param = 0
    local temp_express_event = {}
    local temp_express_event_obj = {}
    temp_express_event_obj.unpack = function()
        return '1'
    end
    temp_express_event.AddEvent_PACKED = function()
        return temp_express_event_obj
    end
    client.GetEventsEventsObject = function ()
        return temp_express_event
    end
    local ok, _ = pcall(EventUB.generate_event, param)
    lu.assertEquals(ok, false)
end

function TestEventUB:test_add_event_fail()
    local param = 0
    local temp_express_event = {}
    local temp_express_event_obj = {}
    temp_express_event_obj.unpack = function()
        return '0'
    end
    temp_express_event.AddEvent_PACKED = function()
        return temp_express_event_obj
    end
    client.GetEventsEventsObject = function ()
        return temp_express_event
    end
    local ok, _ = pcall(EventUB.generate_event, param)
    lu.assertEquals(ok, false)
end

function TestEventUB:test_generate_soft_event()
    local com_name = '1'
    local is_assert = '1'
    local event_key_id = '1'
    local msg_args = {}
    local subj_type = '1'
    local ok = pcall(function ()
        EventUB.generate_soft_event(com_name, is_assert, event_key_id, msg_args, subj_type)
    end)
    lu.assertEquals(ok, true)
    ok = pcall(function ()
        EventUB.get_latest_alarm_list(0)
    end)
    lu.assertEquals(ok, true)
end