-- 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 smc_dump = require 'smc_dump'
local utils_core = require 'utils.core'
local file_sec = require 'utils.file'

TestSmcDump = {}
local smc_dump_service
local smc_cmd_list_obj = {
    Type = 'BCU',
    Slot = 1,
    Config = {
        [1] = {Function = 0, Command = 1, MS = 0, RW = 1, Parameter = 0, Length = 2, Collected = 1},
        [2] = {Function = 0, Command = 2, MS = 0, RW = 1, Parameter = 0, Length = 4, Collected = 1}
    },
    Chip = {Read = function ()
        return '12'
    end}
}

function TestSmcDump:test_on_add_obj()
    smc_dump_service = smc_dump.new()
    smc_dump_service:on_add_object('SmcCmdList', smc_cmd_list_obj, '0101')
    lu.assertEquals(smc_dump_service.dfx_info_list['BCU0101'], smc_cmd_list_obj)
    lu.assertEquals(#smc_dump_service.cmd_key_list['BCU0101'], 2)
end

function TestSmcDump:test_on_dump_cb_process()
    local path = '/data/var/log/general_hardware'
    local ori_open_s = file_sec.open_s
    file_sec.open_s = function ()
        return {write=function () return 0 end,
                close=function () return 0 end}
    end
    local ok = pcall(function ()
        smc_dump_service:on_dump_cb_process(path)
    end)
    lu.assertEquals(ok, true)
    file_sec.open_s = ori_open_s
end