-- 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 rpc_service_subhealth = require 'rpc_services.rpc_service_subhealth'
local common_def = require 'common_def'

local function make_ctx()
    return {
        ChanType = 1,
        get_initiator = function() return 'tester' end
    }
end

TestRpcServiceSubhealthEx = {}

function TestRpcServiceSubhealthEx:setUp()
    -- stub mdb.get_object
    local mdb = require 'mc.mdb'
    self._orig_get_object = mdb.get_object
    mdb.get_object = function(_, path, _)
        if path == '/drive/path' then
            return { Name = 'Disk1', Presence = 1 }
        end
        return nil
    end

    -- stub drive_collection
    local drive_collection = require 'drive.drive_collection'
    self._orig_dc_get_instance = drive_collection.get_instance
    drive_collection.get_instance = function()
        local drive = {
            Name = 'Disk1',
            Presence = 1,
            Protocol = common_def.DRIVE_PROTOCOL.SAS,
            MediaType = 0,
            identify_pd = false,
            RefControllerId = 1,
            device_id = 0,
            nvme_predictive_failure_data = { foo = 'bar' },
            sata_hw_defined_smart_info = { sata = true },
            io_latency = { latency = 1 },
            collect_sata_hw_defined_smart = function() return true end,
            collect_nvme_predictive_failure_data = function() return true end,
            collect_log_from_raid = function() return true end,
            collect_log_from_bma = function() return true end
        }
        return {
            get_drive = function(_, name)
                if name == 'Disk1' then
                    return drive
                end
                return nil
            end,
            get_all_drives = function()
                return { drive }
            end
        }
    end
end

function TestRpcServiceSubhealthEx:tearDown()
    local mdb = require 'mc.mdb'
    if self._orig_get_object then
        mdb.get_object = self._orig_get_object
    end
    local drive_collection = require 'drive.drive_collection'
    if self._orig_dc_get_instance then
        drive_collection.get_instance = self._orig_dc_get_instance
    end
end

-- 覆盖 get_drive_diagnose_data 的 NVME_DATA 分支
function TestRpcServiceSubhealthEx:test_get_drive_diagnose_data_nvme()
    local data = rpc_service_subhealth:get_drive_diagnose_data(
        '/drive/path',
        make_ctx(),
        { common_def.SUBHEALTH_STR.NVME_DATA }
    )
    lu.assertNotNil(data)
    lu.assertEquals(data.foo, 'bar')
end

-- 覆盖 get_drive_diagnose_data 的 SATA_DATA 分支
function TestRpcServiceSubhealthEx:test_get_drive_diagnose_data_sata()
    local data = rpc_service_subhealth:get_drive_diagnose_data(
        '/drive/path',
        make_ctx(),
        { common_def.SUBHEALTH_STR.SATA_DATA }
    )
    lu.assertNotNil(data)
    lu.assertTrue(data.sata)
end

-- collect_drive_diagnose_data 输入 data_type 非法
function TestRpcServiceSubhealthEx:test_collect_drive_diagnose_data_invalid_type()
    local ok = pcall(rpc_service_subhealth.collect_drive_diagnose_data,
        rpc_service_subhealth, '/drive/path', make_ctx(), {})
    lu.assertFalse(ok)
end

