-- Copyright (c) 2025 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 c_super_pod_label_object = require 'super_pod_label_object'
local client = require 'chassis.client'

TestSuperPodLabel = {}

function TestSuperPodLabel:test_init_super_pod_label()
    c_super_pod_label_object.CrcCalcParams = {"ServerIndex", "SuperPodSize", "SuperPodId"}
    c_super_pod_label_object.SuperPodId = 1
    c_super_pod_label_object.ServerIndex = 2
    c_super_pod_label_object.SuperPodSize = 3
    c_super_pod_label_object.TopologyType = 4
    c_super_pod_label_object.NPUBootOption = 5
    c_super_pod_label_object.UBMDeployMode = 6
    c_super_pod_label_object.ChassisId = 7
    c_super_pod_label_object.PresentDPUCount = 8
    c_super_pod_label_object.InterconType = 9
    c_super_pod_label_object.ConfigurationCompletedFlag = 9

    c_super_pod_label_object.InterconTypeReg = 1
    c_super_pod_label_object.PresentDPUCountReg = 2
    c_super_pod_label_object.ChassisIdReg = 3
    c_super_pod_label_object.UBMDeployModeReg = 4
    c_super_pod_label_object.NPUBootOptionReg = 5
    c_super_pod_label_object.TopologyTypeReg = 6
    c_super_pod_label_object.SuperPodSizeAcc = 7
    c_super_pod_label_object.ServerIndexAcc = 8
    c_super_pod_label_object.SuperPodIdAcc = 9
    c_super_pod_label_object.ConfigurationCompletedReg = 9

    c_super_pod_label_object:init_super_pod_label()
    
    lu.assertEquals(c_super_pod_label_object.SuperPodIdAcc, 1)
    lu.assertEquals(c_super_pod_label_object.ServerIndexAcc, 2)
    lu.assertEquals(c_super_pod_label_object.SuperPodSizeAcc, 3)
    lu.assertEquals(c_super_pod_label_object.TopologyTypeReg, 4)
    lu.assertEquals(c_super_pod_label_object.NPUBootOptionReg, 5)
    lu.assertEquals(c_super_pod_label_object.UBMDeployModeReg, 6)
    lu.assertEquals(c_super_pod_label_object.ChassisIdReg, 7)
    lu.assertEquals(c_super_pod_label_object.PresentDPUCountReg, 8)
    lu.assertEquals(c_super_pod_label_object.InterconTypeReg, 9)
    lu.assertEquals(c_super_pod_label_object.Crc16, 1740)
end

function TestSuperPodLabel:test_restore_defaults()
    c_super_pod_label_object.CrcCalcParams = {"ServerIndex", "SuperPodSize", "SuperPodId"}
    c_super_pod_label_object.SuperPodId = 1
    c_super_pod_label_object.ServerIndex = 2
    c_super_pod_label_object.SuperPodSize = 3
    c_super_pod_label_object.TopologyType = 4
    c_super_pod_label_object.NPUBootOption = 5
    c_super_pod_label_object.UBMDeployMode = 6
    c_super_pod_label_object.ChassisId = 7
    c_super_pod_label_object.PresentDPUCount = 8
    c_super_pod_label_object.InterconType = 9

    c_super_pod_label_object.InterconTypeReg = 1
    c_super_pod_label_object.PresentDPUCountReg = 2
    c_super_pod_label_object.ChassisIdReg = 3
    c_super_pod_label_object.UBMDeployModeReg = 4
    c_super_pod_label_object.NPUBootOptionReg = 5
    c_super_pod_label_object.TopologyTypeReg = 6
    c_super_pod_label_object.SuperPodSizeAcc = 7
    c_super_pod_label_object.ServerIndexAcc = 8
    c_super_pod_label_object.SuperPodIdAcc = 9

    c_super_pod_label_object.DefaultServerIndex = 0
    c_super_pod_label_object.DefaultSuperPodId = 0
    c_super_pod_label_object.DefaultSuperPodSize = 0
    c_super_pod_label_object.DefaultTopologyType =0
    c_super_pod_label_object.DefaultUBMDeployMode = 0
    c_super_pod_label_object.DefaultChassisId = 0
    c_super_pod_label_object.DefaultPresentDPUCount = 0
    c_super_pod_label_object.DefaultNPUBootOption = 0
    c_super_pod_label_object.DefaultInterconType = 0

    c_super_pod_label_object:restore_defaults()
    
    lu.assertEquals(c_super_pod_label_object.SuperPodIdAcc, 0)
    lu.assertEquals(c_super_pod_label_object.ServerIndexAcc, 0)
    lu.assertEquals(c_super_pod_label_object.SuperPodSizeAcc, 0)
    lu.assertEquals(c_super_pod_label_object.TopologyTypeReg, 0)
    lu.assertEquals(c_super_pod_label_object.NPUBootOptionReg, 0)
    lu.assertEquals(c_super_pod_label_object.UBMDeployModeReg, 0)
    lu.assertEquals(c_super_pod_label_object.ChassisIdReg, 0)
    lu.assertEquals(c_super_pod_label_object.PresentDPUCountReg, 0)
    lu.assertEquals(c_super_pod_label_object.InterconTypeReg, 0)
    lu.assertEquals(c_super_pod_label_object.Crc16, 12606)
end


function TestSuperPodLabel:test_set_superPod_label()
    c_super_pod_label_object.CrcCalcParams = {"ServerIndex", "SuperPodSize", "SuperPodId"}
    c_super_pod_label_object.SuperPodId = 1
    c_super_pod_label_object.ServerIndex = 2
    c_super_pod_label_object.SuperPodSize = 3
    c_super_pod_label_object.TopologyType = 4
    c_super_pod_label_object.NPUBootOption = 5
    c_super_pod_label_object.UBMDeployMode = 6
    c_super_pod_label_object.ChassisId = 7
    c_super_pod_label_object.PresentDPUCount = 8
    c_super_pod_label_object.InterconType = 9

    c_super_pod_label_object.ServerIndexAcc = 8

    local superpod_labels = {{Key = 'ServerIndex', Value = 1}, {Key = 'IsUBFabricMode', Value = 1}, {Key = 'PresentDPUCount', Value = 1}}
    c_super_pod_label_object:set_superPod_label(2, superpod_labels)
    lu.assertEquals(c_super_pod_label_object.ServerIndex, 2)
    lu.assertEquals(c_super_pod_label_object.ServerIndexAcc, 1)
    lu.assertEquals(c_super_pod_label_object.PresentDPUCountReg, 1)
    lu.assertEquals(c_super_pod_label_object.PresentDPUCount, 8)
    lu.assertEquals(c_super_pod_label_object.Crc16, 52921)
    lu.assertEquals(c_super_pod_label_object.IsUBFabricMode, true)

    c_super_pod_label_object:set_superPod_label(3, superpod_labels)
    lu.assertEquals(c_super_pod_label_object.ServerIndex, 1)
    lu.assertEquals(c_super_pod_label_object.ServerIndexAcc, 1)
    lu.assertEquals(c_super_pod_label_object.PresentDPUCountReg, 1)
    lu.assertEquals(c_super_pod_label_object.PresentDPUCount, 1)
    lu.assertEquals(c_super_pod_label_object.Crc16, 52921)
end

function TestSuperPodLabel:test_update_super_pod_enabled()
    client:SetPowerState("OFF")
    local ok = pcall(function()
        c_super_pod_label_object:update_super_pod_enabled(true)
    end)
    lu.assertEquals(ok, true)
   local testpowerstate = "ON"
    client.GetFruCtrlObjects = function ()
        local objs = {{PowerState = testpowerstate}}
        return objs
    end
    ok = pcall(function()
        c_super_pod_label_object:update_super_pod_enabled(true)
    end)
    lu.assertEquals(ok, false)
end
