-- 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 BroadCom = require 'hardware_config.BroadCom'
local broadcom_mctp = BroadCom.mctp('endpoint')

-- 测试套件
TestBroadCom = {}

-- 测试 LinkStatus 响应解析 - 使用扩展速率编码分支
function TestBroadCom:test_link_status_extended_speed()
    -- 构造测试数据:
    -- response_code = 0, reason_code = 0
    -- extended_speed_duplex = 0x10
    -- 参考 E810 的用例，最后一个字节使用 0x0F，可以解析为 link_flag=1、speed_duplex=某有效值
    -- 其余标志位清零
    local data = string.char(
        0x00, 0x00,  -- response_code
        0x00, 0x00,  -- reason_code
        0x10,        -- extended_speed_duplex
        0x00,        -- 第二个字节（flow control 等标志位）
        0x00,        -- 第三个字节（link_partner 能力标志位）
        0x0F,        -- 第四个字节：与 E810 用例保持一致，确保 link_flag=1
        0x00, 0x00, 0x00, 0x00,  -- other indication
        0x00, 0x00, 0x00, 0x00   -- oem_link_status
    )

    local result = broadcom_mctp.properties.LinkStatus.response(data)

    lu.assertEquals(result.LinkStatus, 'Connected')
    lu.assertEquals(result.SpeedMbps, 1000)
    lu.assertEquals(result.FullDuplex, 1)
end

-- 测试模块导出函数
function TestBroadCom:test_module_functions()
    -- 验证 mctp 工厂函数存在
    lu.assertNotNil(BroadCom.mctp)

    -- 验证返回对象及其依赖、属性
    local mctp_obj = BroadCom.mctp('test_endpoint')
    lu.assertNotNil(mctp_obj)
    lu.assertNotNil(mctp_obj.protocol_dependencies)
    lu.assertNotNil(mctp_obj.protocol_dependencies.ncsi_standard)
    lu.assertEquals(mctp_obj.protocol_dependencies.ncsi_standard.endpoint, 'test_endpoint')

    -- 验证 LinkStatus 属性存在
    lu.assertNotNil(mctp_obj.properties)
    lu.assertNotNil(mctp_obj.properties.LinkStatus)
end