-- 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 node_id_identify = require 'node_id_identify'
local skynet = require 'skynet'
local client = require 'chassis.client'
local cjson = require 'cjson'
local chassis_service = require 'chassis_service'

TestNodeIdIdentify = {}

local ADC_config_obj = {
    Mode = 0,
    Voltage = 32267,
    ChannelRegMap = { 964, 980 },
    ModeConfig = 2048,
    ThresholdConfig = {
        { 0, 0, 1000 },
        { 1, 2192, 3692 },
        { 2, 3886, 5386 },
        { 3, 5580, 7080 },
        { 255, 31000, 65535 }
    }
}
local thresholdconfig = {
    {NodeId = 0, Low = 0, High = 1000},
    {NodeId = 1, Low = 2192, High = 3692},
    {NodeId = 2, Low = 3886, High = 5386},
    {NodeId = 3, Low = 5580, High = 7080},
    {NodeId = 255, Low = 31000, High = 65535}
}

local node_id_identify_service

function TestNodeIdIdentify:test_add_object()
    node_id_identify_service = node_id_identify.new()
    node_id_identify_service.thresholdconfig = {}
    node_id_identify_service.channelregmap = ADC_config_obj.ChannelRegMap
    node_id_identify_service.position = nil
    local fork_loop = skynet.fork_loop
    skynet.fork_loop = function (_, func)
        func()
    end
    local ok = pcall(function ()
        node_id_identify_service:add_object(ADC_config_obj, 1)
    end)
    lu.assertEquals(ok, true)
    skynet.fork_loop = fork_loop
end

function TestNodeIdIdentify:test_prepare_get_voltage()
    node_id_identify_service.thresholdconfig = {}
    node_id_identify_service.channelregmap = nil
    node_id_identify_service:prepare_get_voltage(ADC_config_obj)
    lu.assertEquals(node_id_identify_service.nodeid_short_circuit, 0)
    lu.assertEquals(node_id_identify_service.nodeid_not_present, 255)
    lu.assertEquals(node_id_identify_service.channelregmap, { 964, 980 })
end

function TestNodeIdIdentify:test_get_nodeid_by_adc_voltage()
    local node_id
    node_id_identify_service.thresholdconfig = thresholdconfig
    node_id = node_id_identify_service:get_nodeid_by_adc_voltage(node_id_identify_service.thresholdconfig, 2942)
    lu.assertEquals(node_id, 1)

    node_id = node_id_identify_service:get_nodeid_by_adc_voltage(node_id_identify_service.thresholdconfig, 999)
    lu.assertEquals(node_id, 0)
    
    node_id = node_id_identify_service:get_nodeid_by_adc_voltage(node_id_identify_service.thresholdconfig, 32000)
    lu.assertEquals(node_id, 255)

    node_id = node_id_identify_service:get_nodeid_by_adc_voltage(node_id_identify_service.thresholdconfig, 3700)
    lu.assertEquals(node_id, -1)
end

function TestNodeIdIdentify:test_deal_with_nodeids()
    node_id_identify_service.node_id = nil
    node_id_identify_service.card_slot = 1
    node_id_identify_service:deal_with_nodeids({2, 2})
    lu.assertEquals(node_id_identify_service.node_id, 2)
    local temp_add_event = chassis_service.retry_add_event
    chassis_service.retry_add_event = function (self)
        return true
    end
    node_id_identify_service:deal_with_nodeids({-1, 3})
    lu.assertEquals(node_id_identify_service.node_id, 2)

    node_id_identify_service:deal_with_nodeids({0, 3})
    lu.assertEquals(node_id_identify_service.node_id, 2)

    node_id_identify_service:deal_with_nodeids({255, 3})
    lu.assertEquals(node_id_identify_service.node_id, 2)

    node_id_identify_service:deal_with_nodeids({1, 3})
    lu.assertEquals(node_id_identify_service.node_id, 2)

    node_id_identify_service:deal_with_nodeids({255, 255})
    lu.assertEquals(node_id_identify_service.node_id, 255)
    chassis_service.retry_add_event = temp_add_event
end

function TestNodeIdIdentify:test_transfer_nodeid_by_mapping()
    local node_id, adc_voltage
    node_id, adc_voltage = node_id_identify_service:transfer_nodeid_by_mapping(ADC_config_obj)
    lu.assertEquals(adc_voltage, 2942)
    lu.assertEquals(node_id, 1)
end


function TestNodeIdIdentify:test_generate_event()
    local ok = pcall(function ()
        node_id_identify_service:generate_event(true, 1, 1, 'not equal')
    end)
    lu.assertEquals(ok, false)
end
