-- 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 proxy = require 'fructrl_proxy'
local context = require 'mc.context'
local cq = require 'chain_queue'
local skynet = require 'skynet'

TestFructrlProxy = {}

local function check_action_equal(a, b)
    if a.system_id == b.system_id then
        return true
    end
    return false
end

function TestFructrlProxy:test_all_host_power_off()
    local temp_func = skynet.fork_once
    skynet.fork_once = function(func, ...)
        func(...)
    end
    local ctx = context.get_context_or_default()
    ctx.Requestor = 'bmc.kepler.fructrl'
    local RestartCause = 'Unknown'
    local type = 'GracefulShutdown'
    local multihost_obj = {
        get_host_count = function() return 4 end
    }
    proxy.multihost_obj = multihost_obj
    proxy.power_on_cq = cq.new(check_action_equal)
    proxy.restart_cq = cq.new(check_action_equal)
    proxy.cycle_cq = cq.new(check_action_equal)
    proxy.host_objs = {}
    proxy.staggered_poweroff_interval = 1
    local obj = {}
    proxy.new(obj, multihost_obj, proxy.host_objs)
    local ok  = pcall(proxy.all_host_power_off, proxy, ctx, RestartCause, type)
    lu.assertEquals(ok, true)

    skynet.fork_once = temp_func
end