-- 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 hwproxy = require('hwproxy')

test_topo = {}

local topo_data = {}

topo_data.Anchor = { [=[I2c_1]=], [=[I2c_2]=] }
topo_data.I2c_1 = { [=[Eeprom_0]=], [=[Smc_0]=], [=[Pca9545_1]=] }
topo_data.Smc_0 = { [=[I2cMux_Smc_0_C1]=] }
topo_data.I2cMux_Smc_0_C1 = { [=[Smc_1]=], [=[Lm75_1]=], [=[Eeprom_1]=], [=[Pca9545_0]=] }
topo_data.Pca9545_0 = { [=[I2cMux_Pca9545_0_C1]=], [=[I2cMux_Pca9545_0_C2]=] }
topo_data.I2cMux_Pca9545_0_C1 = { [=[Smc_2]=] }
topo_data.I2cMux_Pca9545_0_C2 = { [=[Lm75_2]=] }
topo_data.Pca9545_1 = { [=[I2cMux_Pca9545_1_C1]=] }
topo_data.I2cMux_Pca9545_1_C1 = { [=[Lm75_3]=], [=[Pca9545_2]=], [=[Smc_3]=] }
topo_data.I2c_2 = { [=[Lm75_4]=] }

local buses = {}
buses.I2c_1 = { Id = 1, Speed = 100, Mode = 0, SlaveAddr = 0, Type = [=[I2c]=] }
buses.I2c_2 = { Id = 2, Speed = 100, Mode = 0, SlaveAddr = 0, Type = [=[I2c]=] }

local chips = {}
chips.Smc_0 = {
    Addr = 240,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Smc]=],
}
chips.Smc_1 = {
    Addr = 241,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Smc]=],
}
chips.Smc_2 = {
    Addr = 242,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Smc]=],
}
chips.Smc_3 = {
    Addr = 243,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Smc]=],
}
chips.Eeprom_0 = {
    Addr = 174,
    AddrWidth = 1,
    OffsetWidth = 2,
    WriteTmout = 100,
    ReadTmout = 100,
    RwBlockSize = 32,
    WriteInterval = 30,
    HealthStatus = 0,
    Type = [=[Eeprom]=],
}
chips.Eeprom_1 = {
    Addr = 175,
    AddrWidth = 1,
    OffsetWidth = 2,
    WriteTmout = 100,
    ReadTmout = 100,
    RwBlockSize = 32,
    WriteInterval = 30,
    HealthStatus = 0,
    Type = [=[Eeprom]=],
}
chips.Pca9545_0 = {
    Addr = 224,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Pca9545]=],
}
chips.Pca9545_1 = {
    Addr = 225,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Pca9545]=],
}
chips.Pca9545_2 = {
    Addr = 226,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Pca9545]=],
}
chips.Pca9555_1 = {
    Addr = 227,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Pca9555]=],
}
chips.Lm75_1 = {
    Addr = 228,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Lm75]=],
}
chips.Lm75_2 = {
    Addr = 229,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Lm75]=],
}
chips.Lm75_3 = {
    Addr = 230,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Lm75]=],
}
chips.Lm75_4 = {
    Addr = 231,
    AddrWidth = 1,
    OffsetWidth = 0,
    WriteTmout = 100,
    ReadTmout = 100,
    Type = [=[Lm75]=],
}

local mux_data = {}
mux_data.I2cMux_Smc_0_C1 = { Channel = 0 }
mux_data.I2cMux_Pca9545_0_C1 = { Channel = 0 }
mux_data.I2cMux_Pca9545_0_C2 = { Channel = 1 }
mux_data.I2cMux_Pca9545_1_C1 = { Channel = 0 }

-- 删除的拓扑链路
--[[
I2c_1
    |____Eeprom_0
    |____Smc_0
    |        |____Lm75_1
    |        |____Eeprom_1
    |        |____Pca9545_0
    |                |____Smc_2
    |                |____Lm75_2
    |____Pca9545_1
             |____Lm75_3
             |____Pca9545_2
]]
local delete_topo = {}
delete_topo.Anchor = {
    [=[I2c_1]=],
    [=[I2cMux_Smc_0_C1]=],
    [=[I2cMux_Pca9545_0_C1]=],
    [=[I2cMux_Pca9545_1_C1]=],
    [=[I2cMux_Pca9545_0_C2]=],
}
delete_topo.I2c_1 = { [=[Eeprom_0]=] }
delete_topo.I2cMux_Smc_0_C1 = { [=[Lm75_1]=], [=[Eeprom_1]=], [=[Pca9545_0]=] }
delete_topo.Pca9545_0 = { [=[I2cMux_Pca9545_0_C1]=], [=[I2cMux_Pca9545_0_C2]=] }
delete_topo.I2cMux_Pca9545_0_C1 = { [=[Smc_2]=] }
delete_topo.I2cMux_Pca9545_0_C2 = { [=[Lm75_2]=] }
delete_topo.I2cMux_Pca9545_1_C1 = { [=[Lm75_3]=], [=[Pca9545_2]=] }

-- 生成的拓扑链路
--[[
I2c_1
    |____Eeprom_0
    |____Smc_0
    |        |____Smc_1
    |        |____Lm75_1
    |        |____Eeprom_1
    |        |____Pca9545_0
    |                |____Smc_2
    |                |____Lm75_2
    |____Pca9545_1
             |____Lm75_3
             |____Pca9545_2
             |____Smc_3

I2c_2
    |____Lm75_4
]]
function test_topo:test_generate_topo()
    local dispatch = hwproxy:new()
    dispatch:update(buses, chips, topo_data, {}, {}, mux_data)

    -- 验证生成的拓扑结构与预期一致
    local scanner1_chips = dispatch.scanner.I2c_1.chips
    lu.assertEquals(#scanner1_chips, 3)
    lu.assertEquals(scanner1_chips[1].Obj.name, 'Eeprom_0')
    lu.assertEquals(scanner1_chips[2].Obj.name, 'Smc_0')
    lu.assertEquals(scanner1_chips[3].Obj.name, 'Pca9545_1')
    lu.assertEquals(scanner1_chips[2].Obj.r_chips[0][1].name, 'Smc_1')
    lu.assertEquals(scanner1_chips[2].Obj.r_chips[0][2].name, 'Lm75_1')
    lu.assertEquals(scanner1_chips[2].Obj.r_chips[0][3].name, 'Eeprom_1')
    lu.assertEquals(scanner1_chips[2].Obj.r_chips[0][4].name, 'Pca9545_0')
    lu.assertEquals(scanner1_chips[2].Obj.r_chips[0][4].r_chips[0][1].name, 'Smc_2')
    lu.assertEquals(scanner1_chips[2].Obj.r_chips[0][4].r_chips[1][1].name, 'Lm75_2')
    lu.assertEquals(scanner1_chips[3].Obj.r_chips[0][1].name, 'Lm75_3')
    lu.assertEquals(scanner1_chips[3].Obj.r_chips[0][2].name, 'Pca9545_2')
    lu.assertEquals(scanner1_chips[3].Obj.r_chips[0][3].name, 'Smc_3')

    local scanner2_chips = dispatch.scanner.I2c_2.chips
    lu.assertEquals(#scanner2_chips, 1)
    lu.assertEquals(scanner2_chips[1].Obj.name, 'Lm75_4')
end

-- 删除后的拓扑链路
--[[
I2c_1
    |____Smc_0
    |        |____Smc_1
    |____Pca9545_1
             |____Smc_3

I2c_2
    |____Lm75_4
]]
function test_topo:test_delete_topo()
    local dispatch = hwproxy:new()
    dispatch:update(buses, chips, topo_data, {}, {}, mux_data)
    dispatch:delete(delete_topo)

    -- 验证删除后的拓扑与预期一致
    local scanner1_chips = dispatch.scanner.I2c_1.chips
    lu.assertEquals(#scanner1_chips, 2)
    lu.assertEquals(scanner1_chips[1].Obj.name, 'Smc_0')
    lu.assertEquals(scanner1_chips[2].Obj.name, 'Pca9545_1')
    lu.assertEquals(#scanner1_chips[1].Obj.r_chips[0], 1)
    lu.assertEquals(scanner1_chips[1].Obj.r_chips[0][1].name, 'Smc_1')
    lu.assertEquals(#scanner1_chips[2].Obj.r_chips[0], 1)
    lu.assertEquals(scanner1_chips[2].Obj.r_chips[0][1].name, 'Smc_3')

    local scanner2_chips = dispatch.scanner.I2c_2.chips
    lu.assertEquals(#scanner2_chips, 1)
    lu.assertEquals(scanner2_chips[1].Obj.name, 'Lm75_4')
end

return test_topo