-- 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 bus = require('hwproxy_objects.work_bus')

TestWorkBus = {}

function TestWorkBus:test_notify_secondary_task_with_callback()
    -- 初始化一个bus实例
    local test_bus = bus.new({
        name = 'test_bus',
        class_name = 'I2c',
        need_scan = true,
        scan_interval = 1000,
    })

    -- 设置二次任务回调函数
    local callback_called = false
    local test_chip = 'test Chip'
    test_bus:set_secondary_task_cb(function(chip)
        callback_called = true
        lu.assertEquals(chip, test_chip)
    end)

    -- 调用notify_secondary_task
    test_bus:notify_secondary_task(test_chip)

    -- 验证回调函数被调用
    lu.assertEquals(callback_called, true)
end

function TestWorkBus:test_notify_secondary_task_without_callback()
    -- 初始化一个bus实例,不设置secondary_task_cb
    local test_bus = bus.new({
        name = 'test_bus',
        class_name = 'I2c',
        need_scan = true,
        scan_interval = 1000,
    })

    -- 调用notify_secondary_task,此时secondary_task_cb未设置
    local ok, _ = pcall(test_bus.notify_secondary_task, test_bus, {})

    -- 验证没有错误发生
    lu.assertEquals(ok, true)
end

return TestWorkBus