-- 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 nmi = require 'fructrl.nmi'
local fructrl_capabilities = require 'fructrl_capabilities'
local power_api = require 'powerapi'

TestChassisNmi = {}

function TestChassisNmi:test_retry_set_nmi_interrupt()
    nmi.obj = {
        NmiInt = 0
    }
    nmi.system_id = 1
    local ok = nmi:retry_set_nmi_interrupt(1)
    lu.assertEquals(ok, true)
    lu.assertEquals(nmi.obj.NmiInt, 1)
end

function TestChassisNmi:test_nmi_interrupt()
    nmi.obj = {
        NmiInt = 1
    }
    nmi.system_id = 1
    local ok = nmi:nmi_interrupt()
    lu.assertEquals(ok, 0)
    lu.assertEquals(nmi.obj.NmiInt, 0)
end

function TestChassisNmi:test_register_object()
    fructrl_capabilities.obj = {
        NMISupported = true
    }
    testobj = {
        NMISupported = false
    }
    fructrl_capabilities:register_object(testobj)
    lu.assertEquals(fructrl_capabilities.obj.NMISupported, false)
end

function TestChassisNmi:test_is_nmi_supported()
    fructrl_capabilities.obj = {
        NMISupported = true
    }
    local ok = fructrl_capabilities:is_nmi_supported()
    lu.assertEquals(ok, true)
end

function TestChassisNmi:test_execute_nmi()
    ctx = {
        get_initiator = function() return{} end
    }
    pctrl_type = 'Nmi'
    restart_cause = 'Unknown'
    local fructrl = {
        set_CurrentRestartType = function() return true end,
        set_CurrentRestartCauseId = function() return true end,
        add_RestartCauseRecords = function() return true end,
        get_FruPowerState = function() return "ON" end
    }
    power_api.fructrl = fructrl
    local ok = pcall(power_api.system_reset, power_api, ctx, pctrl_type, restart_cause)
    lu.assertEquals(ok, false)
end