-- 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 work_objects = require('hwproxy_objects.work_objects')
local hwproxy_error = require('hwproxy.errors')

TestWorkObjects = {}

function TestWorkObjects:setUp() end

function TestWorkObjects:tearDown() end

function TestWorkObjects:test_plugin_request()
    local obj = {
        writeread = { { Offset = 0, InData = { 0 } } },
        fetch = {
            cb = function()
                return false
            end,
        },
    }
    work_objects.plugins = {
        power_mgmt = {
            load_soft_bin_data_by_plugins = function() end,
            has_cmd = function()
                return true
            end,
        },
    }
    work_objects.chips = {
        Chip_plugin_test = {
            plugin_request = function(fetch, func)
                fetch.result = true
                fetch.run = func
                return fetch
            end,
        },
    }
    hwproxy_error.request_timeout = function()
        return 'Timeout'
    end
    work_objects.get_chip_name = function()
        return 'Chip_plugin_test'
    end
    local ctx = { Timeout = 600 }
    local plugin_name = 'power_mgmt'
    local cmd = 'load_soft_bin_data_by_plugins'
    local params = {}
    local _, ret = pcall(work_objects.plugin_request, work_objects, obj, ctx, plugin_name, cmd, params, {})
    lu.assertNotNil(string.find(ret, 'Timeout'))
end

function TestWorkObjects:test_not_topo_data_on_add_object_complete()
    local work_obj = work_objects.new()
    local ok, ret = pcall(work_obj.on_add_object_complete, work_obj, '0101', nil, nil)
    lu.assertEquals(ok, false)
    lu.assertNotNil(string.find(ret, 'topology data not exist'))
end

function TestWorkObjects:test_update_scanner_shm_value()
    local work_obj = work_objects.new()
    local ref_chip = {
        scanners = {
            test_scanner = {
                resources = {
                    {
                        name = 'test_scanner',
                        tree_obj = {
                            update_shm_property = function() end,
                        },
                    },
                },
            },
        },
    }
    work_obj.scanner_ref_chip['test_scanner'] = ref_chip
    work_obj:update_scanner_shm_value('test_scanner')
end