-- 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 scan = require('scan')
local skynet = require('skynet')
local scan_context = require('scan_context')
local coroutine = require('skynet.coroutine')
mock_bus = {
    name = 'test_bus',
    set_secondary_task_cb = function(mock_bus, cb)
        mock_bus.secondary_cb = cb
    end,
    set_high_task_cb = function(mock_bus, cb)
        mock_bus.high_cb = cb
    end,
    stream = {
        bus_lock = function() end,
        bus_unlock = function() end,
    },
}

-- 初始化scan实例
scan_instance = scan.new(mock_bus)
scan_instance:run()

TestScan = {}

function TestScan:setUp() end

function TestScan:tearDown() end

function TestScan:test_set_has_secondary_task()
    mock_bus.secondary_cb()
    -- 验证标志被设置
    lu.assertEquals(scan_instance.has_secondary_task, true)
end

function TestScan:test_set_has_high_task()
    mock_bus.high_cb()
    -- 验证标志被设置
    lu.assertEquals(scan_instance.has_high_task, true)
end

mock_bus2 = {
    name = 'test_bus',
    scan_interval = 1000,
    set_secondary_task_cb = function(self, cb)
        self.secondary_cb = cb
    end,
}
-- 创建一个scan实例
mock_scan = scan.new(mock_bus2)

-- 创建一个模拟的secondary_ctx
mock_secondary_ctx = scan_context.new(mock_bus2.name, 0)
mock_secondary_ctx.is_running = false
mock_secondary_ctx.running_co = nil
mock_secondary_ctx.begin_run = function(self)
    self.is_running = true
end
mock_secondary_ctx.end_run = function(self)
    self.is_running = false
end

mock_high_ctx = scan_context.new(mock_bus2.name, 0)

-- 模拟flush_message函数
local flush_message = function()
    -- 模拟发送消息并等待
    mock_scan.wait_co = coroutine.running()
    skynet.wait()
    mock_scan.wait_co = nil
end

-- 设置secondary_ctx的step_cb
mock_secondary_ctx:set_step_cb(50, function()
    flush_message()
    mock_scan:process_high_priority_task(mock_high_ctx)
    mock_scan:process_secondary_priority_task(mock_secondary_ctx)
end)

function TestScan:test_process_secondary_priority()
    mock_scan.has_secondary_task = true
    mock_secondary_ctx:step_cb()

    lu.assertEquals(mock_scan.has_secondary_task, false)
end

function TestScan:test_not_process_secondary_priority()
    mock_scan.has_secondary_task = false

    mock_secondary_ctx:step_cb()
    lu.assertEquals(mock_scan.has_secondary_task, false)
end

function TestScan:test_process_high_priority()
    mock_scan.has_high_task = true

    mock_secondary_ctx:step_cb()
    lu.assertEquals(mock_scan.has_high_task, false)
end

function TestScan:test_set_step_cb()
    local low_ctx = {
        set_step_cb = function(_, _, cb)
            cb()
        end,
    }
    local secondary_ctx = {
        set_step_cb = function(_, _, cb)
            cb()
        end,
    }
    local high_ctx = {
        set_step_cb = function(_, _, cb)
            cb()
        end,
    }
    scan_instance.process_high_priority_task = function() end
    scan_instance.process_secondary_priority_task = function() end
    local ok, _ = pcall(scan_instance.set_step_cb, scan_instance, low_ctx, secondary_ctx, high_ctx, flush_message)
    lu.assertEquals(ok, false)
end

return TestScan