-- 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 config = require('config')
local bs = require('mc.bitstring')
local log = require('mc.logging')
local utils = require('mc.utils')
local utils_core = require('utils.core')
local utils_file = require('utils.file')
local mdb_access = require('route_mapper.mdb_access')
local cli_utils = require('cli.utils')
local comm = require('cli.sol')
local common = require('common')
local sol_activate = require('route.ipmcset.sol_activate')

TestSolActivate = {}

local sol_obj = { ['Timeout'] = 1, ['SerialDirect'] = 0 }

local function get_file_content(file)
    local f = utils_file.open_s(file, 'r')
    lu.assertNotIsNil(f)
    local str = f:read('*a')
    f:close()
    return str
end

function TestSolActivate:setUp()
    self.output_file = config.PROJECT_DIR .. '/test/unit/test.dat'
    utils.remove_file(self.output_file)
    log:setPrint(nil)
    self.fd = cli_utils.redirect_stdout_to_file(self.output_file)

    self.get_object = mdb_access.get_object
    self.connect_socket = comm.connect_socket
    self.send_msg = comm.send_msg
    self.close = comm.close
    self.leave_raw_mode = comm.leave_raw_mode
    self.output = comm.output
    self.get_bmc_time = utils_core.get_bmc_uptime
    self.InitSession = sol_obj.InitSession

    mdb_access.get_object = function()
        return sol_obj
    end
    comm.connect_socket = function()
        return 0
    end
    comm.close = function()
        return
    end
    comm.leave_raw_mode = function()
        return
    end
    comm.output = function(data)
        print(data)
    end
    utils_core.get_bmc_uptime = function()
        return 2
    end
    sol_obj.InitSession = function()
        return { Result = 0 }
    end

    comm.send_msg = function(msg_data)
        -- 解包数据
        local sol_protocol = bs.new([[<<head:2/unit:8, msg_type:1/unit:8, blade:1/unit:8, com:1/unit:8,
            req:1/unit:8, uc_alg:1/unit:8, len:2/unit:8, payload:len/string>>]])
        local unpacked_data = sol_protocol:unpack(msg_data)
        if unpacked_data.msg_type == 1 then
            return 28
        end
        if unpacked_data.msg_type == 2 then
            return 9
        end
        return 0
    end
end

function TestSolActivate:tearDown()
    self.close()
    self.leave_raw_mode()
    mdb_access.get_object = self.get_object
    comm.connect_socket = self.connect_socket
    comm.send_msg = self.send_msg
    comm.close = self.close
    comm.output = self.output
    utils_core.get_bmc_uptime = self.get_bmc_time
    sol_obj.InitSession = self.InitSession

    cli_utils.stop_redirect(self.fd)
    log:setPrint(log.get_default_print())
    if self.compare then
        self:compare(get_file_content(self.output_file))
    end
    utils.remove_file(self.output_file)
end

function TestSolActivate:test_sol()
    common.get_sol_infos = function()
        return {
            [1] = 'SYS COM',
            [2] = 'BMC COM',
        }
    end
    self.compare = function(_self, output)
        lu.assertEquals(get_file_content(config.PROJECT_DIR .. '/test/unit/rsp_info/test_sol_activate'), output)
    end

    local params = { '1', '0' }
    local headers = { ['SystemId'] = { '1' } }
    sol_activate({}, params, headers)
end