-- 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.

-- luacheck: ignore 121
local lu = require('luaunit')
local common = require('common')
local cli_utils = require('cli.utils')

TestCommon = {}

function TestCommon:test_parse_verbose_parameter()
    cli_utils.build_type_is_release = function()
        return true
    end
    lu.assertEquals(common.parse_verbose_parameter(1), {})
    lu.assertEquals(common.parse_verbose_parameter({ 1 }), { 1 })
    lu.assertEquals(common.parse_verbose_parameter({ '--verbose=debug' }), { '--verbose=debug' })

    cli_utils.build_type_is_release = function()
        return false
    end
    lu.assertEquals(common.parse_verbose_parameter(1), {})
    lu.assertEquals(common.parse_verbose_parameter({ 1 }), { 1 })
    lu.assertEquals(common.parse_verbose_parameter({ '--verbose=debug' }), {})
    lu.assertEquals(common.parse_verbose_parameter({ '--verbose=debug', '-t', 'sol' }), { '-t', 'sol' })
    lu.assertEquals(common.parse_verbose_parameter({ '--verbose=error', '-t', 'sel' }), { '-t', 'sel' })
    lu.assertEquals(common.parse_verbose_parameter({ '--verbose=', '-d', 'power' }), { '-d', 'power' })
    lu.assertEquals(
        common.parse_verbose_parameter({ '-t', '--verbose=error', 'sol' }),
        { '-t', '--verbose=error', 'sol' }
    )
end

function TestCommon:test_print_errs_message()
    self.print = print
    local catcher
    print = function(msg)
        catcher = msg
    end
    local err = {}
    err.Severity = 'OK'
    err.Message = 'Message'
    err.MessageName = 'nonono'
    local errs = {}
    errs[1] = err
    common.print_errs_message(errs, true)
    print = self.print
    lu.assertEquals(catcher, 'Message')

    err.Severity = 'Manor'
    err.MessageName = 'PasswordChangeRequired'
    common.print_errs_message(errs, true)
    lu.assertEquals(catcher, 'Message')
end

function TestCommon:test_is_interrupt_signal()
    local err_msg = 'failed by code interrupted!'
    local ok = common.is_interrupt_signal(err_msg)
    lu.assertEquals(ok, true)
    err_msg = 'failed'
    ok = common.is_interrupt_signal(err_msg)
    lu.assertEquals(ok, false)
    local err_msg_table = {}
    ok = common.is_interrupt_signal(err_msg_table)
    lu.assertEquals(ok, false)
end

function TestCommon:test_print_command_reminder()
    local reminder = 1
    local ret = common.print_command_reminder(reminder)
    lu.assertIsNil(ret)

    local reminder_tab = {
        Usage = 1,
        Brief = 2,
        v = { {
            Description = 'aaa',
            Command = 'bbb',
        } },
        Example = { 1, 2 },
        ['-v'] = { { Description = 'aaa', Command = 'nnn' } },
    }

    ret = common.print_command_reminder(reminder_tab)
    lu.assertIsNil(ret)
end

function TestCommon:test_print_sol_deactivate_help_info()
    local test_bus = {}
    local ret = common.print_sol_deactivate_help_info()
    lu.assertIsNil(ret)

    -- local reminder_tab = {
    --     Usage = 1,
    --     Brief = 2,
    --     v = {{
    --         Description = 'aaa',
    --         Command = 'bbb'
    --     }},
    --     Example = {1, 2}
    -- }

    -- ret = common.print_sol_deactivate_help_info(reminder_tab)
    -- lu.assertIsNil(ret)
end

function TestCommon:test_print_internal_err()
    local ok, ret = pcall(function()
        return common.print_internal_err()
    end)
    lu.assertEquals(ok, true)
    lu.assertIsNil(ret)
end