-- Copyright (c) 2024 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 skynet = require 'skynet'
local led_i2c_object = require 'led_i2c_object'
local led_i2c_service = require 'led_i2c_service'
local chassis_app = require 'chassis_app'


TestLedI2c = {}

function TestLedI2c:test_service_add_object()
    local service = led_i2c_service.new()
    service:init()
    local mds_obj = { name = 'test_led', LedOnValue = 0xff, CtrlValue = 0 }
    service:add_object(mds_obj, 1)
    lu.assertNotNil(service.led_i2c_list['test_led'])
    lu.assertEquals(service.led_i2c_list['test_led'].mds_obj, mds_obj)
    lu.assertEquals(service.led_i2c_list['test_led'].position, 1)
end

function TestLedI2c:test_service_delete_object()
    local service = led_i2c_service.new()
    service:init()
    local mds_obj = { name = 'test_led', LedOnValue = 0xff, CtrlValue = 0 }
    service:add_object(mds_obj, 1)
    lu.assertNotNil(service.led_i2c_list['test_led'])
    service:delete_object(mds_obj, 1)
    lu.assertNil(service.led_i2c_list['test_led'])
end

-- 测试 chassis_app:on_delete_object 对 I2cLed 类的删除逻辑
function TestLedI2c:test_chassis_app_on_delete_object_i2c_led()
    local app = setmetatable({}, { __index = chassis_app })
    app.led_i2c_service = led_i2c_service.new()
    app.led_i2c_service:init()
    local mds_obj = { name = 'test_led', LedOnValue = 0xff, CtrlValue = 0 }
    app.led_i2c_service:add_object(mds_obj, 1)
    lu.assertNotNil(app.led_i2c_service.led_i2c_list['test_led'])

    chassis_app.on_delete_object(app, 'I2cLed', mds_obj, 1)
    lu.assertNil(app.led_i2c_service.led_i2c_list['test_led'])
end

-- 测试 chassis_app:on_delete_object 对非 I2cLed 类不执行删除
function TestLedI2c:test_chassis_app_on_delete_object_other_class()
    local delete_called = false
    local mock_led_i2c_service = {
        delete_object = function()
            delete_called = true
        end
    }
    local app = setmetatable({ led_i2c_service = mock_led_i2c_service }, { __index = chassis_app })
    local mds_obj = { name = 'test_led' }

    chassis_app.on_delete_object(app, 'Led', mds_obj, 1)
    lu.assertEquals(delete_called, false)

    chassis_app.on_delete_object(app, 'Chassis', mds_obj, 1)
    lu.assertEquals(delete_called, false)
end
