-- 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 app_objects = require('hwproxy_objects.app_objects')
local hwproxy_work_app = require('hwproxy_work_app')
local scanner = require('access.scanner')
local chip = require('chip')

TestTraceDebounce = {}

function TestTraceDebounce:setUp() end

function TestTraceDebounce:tearDown()
    -- hwproxy_work_app会将chip的update_resource方法覆盖,这里需要覆盖回去
    chip.update_resource = chip.old_update_resource
end

local test_data = {
    Smc_ExpBoardSMC_obj = {
        name = 'Smc_ExpBoardSMC',
        Address = 96,
        AddrWidth = 1,
        OffsetWidth = 1,
        WriteTmout = 0,
        ReadTmout = 0,
    },
    Scanner_PowerGd_obj = {
        name = 'Scanner_PowerGd',
        Chip = 'Smc_ExpBoardSMC',
        Offset = 469765888,
        Size = 1,
        Mask = 255,
        Type = 0,
        Period = 100,
        Debounce = 'None',
        Value = 0,
        path = '/bmc/kepler/Scanner/Scanner_PowerGd',
        ScanEnabled = 1,
    },
}

function TestTraceDebounce:test_trace_debounce()
    local test_chip = chip.new(test_data.Smc_ExpBoardSMC_obj)
    test_chip:add_scanner(test_data.Scanner_PowerGd_obj, nil)
    local test_scanner = scanner.new(test_data.Scanner_PowerGd_obj, test_chip)
    hwproxy_work_app:ctor()
    hwproxy_work_app:create_hwproxy_object('I2c_1')
    local obj = { path = '/bmc/kepler/Scanner/Scanner_PowerGd' }
    app_objects.scanner_ref_chip = {
        Scanner_PowerGd = {
            host_bus = {
                stream = {
                    call = function(work, func, ...)
                        hwproxy_work_app:trace_scanner_debounce('I2c_1', ...)
                    end,
                },
            },
        },
    }
    hwproxy_work_app.hwproxy_objects.I2c_1.scanner_ref_chip = {
        Scanner_PowerGd = {
            scanners = {
                Scanner_PowerGd = test_scanner,
            },
        },
    }
    app_objects:trace_scanner_debounce(obj, {}, 'start')

    lu.assertEquals(test_scanner.resources[1].is_trace_dbd, 'start')
end