-- 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 c_controller = require 'controller.controller_object'

TEST_Controller_Refactor = {}

function TEST_Controller_Refactor:setUp()
    self.original_sml = require 'sml'
    self.original_ctrl_commu_loss_monitor = require 'ctrl_commu_loss_monitor'
    self.original_storageconfig = require 'storageconfig.storageconfig_object'
    self.mock_sml = {
        update_ctrl_init_state = function() return 2 end,
        get_ctrl_info = function() return {ctrl_name = 'test'} end
    }
    self.mock_ctrl_commu_loss_monitor = {
        get_instance = function()
            return {
                update = function() end,
                get_ctrl_commu_loss = function() return 0 end
            }
        end
    }
    self.mock_storageconfig = {
        get_instance = function()
            return {
                power_on = 1,
                configuration_ready_mask = 0,
                on_update_flag = {
                    emit = function() end
                }
            }
        end
    }
end

function TEST_Controller_Refactor:tearDown()
    package.loaded['sml'] = self.original_sml
    package.loaded['ctrl_commu_loss_monitor'] = self.original_ctrl_commu_loss_monitor
    package.loaded['storageconfig.storageconfig_object'] = self.original_storageconfig
end

function TEST_Controller_Refactor:test_start_update_task_structure()
    local ctrl = c_controller.new({
        Id = 1,
        DeviceName = 'test_device',
        TypeId = 1,
        OOBSupport = 1,
        position = 'test_position'
    })
    ctrl.tasks_pause = false
    ctrl.update_temp_abnormal = function() end
    ctrl.update_ctrl_info_from_sml = function() end
    ctrl.update_controller_info_via_bma = function() end
    ctrl.update_sas_addr = function() end
    ctrl.update_boot_devices = function() end
    ctrl.update_fault_code = function() end
    ctrl.update_asset_data_info = function() end
    ctrl.update_controller_battery_info = function() end
    
    package.loaded['sml'] = self.mock_sml
    package.loaded['ctrl_commu_loss_monitor'] = self.mock_ctrl_commu_loss_monitor
    package.loaded['storageconfig.storageconfig_object'] = self.mock_storageconfig
    
    ctrl:start_update_task()
    
    lu.assertNotNil(ctrl.tasks, 'Tasks should be created')
end

return lu.LuaUnit.run()
