-- 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 log = require('mc.logging')
local utils = require('mc.utils')
local utils_file = require('utils.file')
local cli_utils = require('cli.utils')
local cli = require('ipmc')
local sd_bus = require('sd_bus')
local common = require('common')

TestIpmcset = {}

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

local function get_bus()
    return {
        close = function()
            return
        end,
    }
end

function TestIpmcset:setUp()
    self.output_file = config.PROJECT_DIR .. '/test/unit/test.dat'
    utils.remove_file(self.output_file)
    log:setPrint(nil)
    sd_bus.open_user = get_bus
    self.bus = {
        pcall = function()
            return -1
        end,
        timeout_call = function()
            error('unexpected route mapper bus call')
        end,
    }
    self.temp_cli_new = cli.new
    self.cli_obj = self.cli_obj or cli.new(config.IPMCSET_PATH, self.bus)
    cli.new = function()
        return self.cli_obj
    end
    self.fd = cli_utils.redirect_stdout_to_file(self.output_file)
end

function TestIpmcset:tearDown()
    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)
    cli.new = self.temp_cli_new
end

function TestIpmcset:test_ipmcset_sol_timeout()
    self.compare = function(_self, output)
        lu.assertEquals(get_file_content(config.PROJECT_DIR .. '/test/unit/rsp_info/ipmcset_sol_timeout'), output)
    end

    loadfile(config.PROJECT_DIR .. '/src/service/ipmcset.lua')('-t', 'sol', '-d', 'timeout', '-v', '0')
end

function TestIpmcset:test_ipmcset_sol_help_info()
    self.compare = function(_self, output)
        lu.assertEquals(output, get_file_content(config.PROJECT_DIR .. '/test/unit/rsp_info/ipmcset_sol_help_info'))
    end

    loadfile(config.PROJECT_DIR .. '/src/service/ipmcset.lua')('-t', 'sol', '-d', 'activate', '-v')
end

function TestIpmcset:test_ipmcset_reset()
    self.compare = function(_self, output)
        lu.assertEquals(get_file_content(config.PROJECT_DIR .. '/test/unit/rsp_info/ipmcset_reset'), output)
    end

    local temp_get_user_input = common.get_user_input
    common.get_user_input = function(prompt)
        print(prompt .. 'yes')
        return 0, 'y'
    end
    loadfile(config.PROJECT_DIR .. '/src/service/ipmcset.lua')('-d', 'reset')
    common.get_user_input = temp_get_user_input
end

function TestIpmcset:test_ipmcset_user_unlock()
    self.compare = function(_self, output)
        lu.assertEquals(get_file_content(config.PROJECT_DIR .. '/test/unit/rsp_info/ipmcset_user_unlock'), output)
    end

    local temp_get_line_without_echo = cli_utils.get_line_without_echo
    cli_utils.get_line_without_echo = function(description)
        print(description)
        return 0, 'psw123'
    end
    loadfile(config.PROJECT_DIR .. '/src/service/ipmcset.lua')('-t', 'user', '-d', 'unlock', '-v', 'Admin123')
    cli_utils.get_line_without_echo = temp_get_line_without_echo
end