-- 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.

require('skynet.manager')
local skynet = require('skynet')
local sd_bus = require('sd_bus')
local log = require('mc.logging')
local utils = require('mc.utils')
local test_common = require('test_common.utils')
local sol_activate = require('route.ipmcset.sol_activate')

log:setPrint(log.get_default_print())

local function prepare_iam_test_data(test_data_dir)
    -- 创建/data目录,保证iam测试、文件传输服务正常
    assert(os.execute('mkdir -p /data/trust') == true)
    local iam_test_data_path = os.getenv('ROOT_DIR') .. '/test_data/apps/security/iam/unit/'
    assert(utils.copy_file(iam_test_data_path .. 'passwd', test_data_dir .. '/passwd'))
    assert(utils.copy_file(iam_test_data_path .. 'shadow', test_data_dir .. '/shadow'))
    assert(utils.copy_file(iam_test_data_path .. 'group', test_data_dir .. '/group'))
    assert(utils.copy_file(iam_test_data_path .. 'ipmi', test_data_dir .. '/ipmi'))

    utils.mkdir(test_data_dir, utils.S_IRWXU)
    utils.mkdir(test_data_dir .. '/tmp', utils.S_IRWXU)
    utils.mkdir(test_data_dir .. '/home', utils.S_IRWXU)
    utils.mkdir(test_data_dir .. '/dev', utils.S_IRWXU)
    utils.mkdir(test_data_dir .. '/dev/shm', utils.S_IRWXU)
    utils.mkdir(test_data_dir .. '/dev/shm/tmp', utils.S_IRWXU)
    local tallylog_dir = skynet.getenv('PAM_TALLY_LOG_DIR')
    utils.mkdir(tallylog_dir, utils.S_IRWXU)
    utils.mkdir('/dev/shm/tallylog', utils.S_IRWXU)
end

local function prepare_test_data()
    print('prepare test data')
    local test_data_dir = skynet.getenv('TEST_DATA_DIR')
    utils.remove_file('/dev/shm/upgrade')
    os.execute('mkdir -p ' .. test_data_dir)

    utils.copy_file('test/integration/datatocheck_default.dat', test_data_dir .. '/datatocheck_default.dat')

    -- prepare common mds files and folders
    local dir_list = { 'apps/event/mds', 'apps/sensor/mds', 'apps/hwdiscovery/mds', 'sr' }
    for _, path in pairs(dir_list) do
        os.execute('mkdir -p ' .. test_data_dir .. '/' .. path)
    end

    -- prepare hwdiscovery mds files
    utils.copy_file(
        'temp/opt/bmc/apps/hwdiscovery/mds/schema.json',
        test_data_dir .. '/apps/hwdiscovery/mds/schema.json'
    )
    utils.copy_file(
        'temp/opt/bmc/apps/hwdiscovery/mds/service.json',
        test_data_dir .. '/apps/hwdiscovery/mds/service.json'
    )

    -- prepare root.sr
    utils.copy_file('test/integration/test_data/root.sr', test_data_dir .. '/sr/root.sr')

    -- prepare event sr and mds files
    utils.copy_file('test/integration/test_data/14100513_Event_0.sr', test_data_dir .. '/sr/14100513_Event_0.sr')
    utils.copy_file('temp/opt/bmc/apps/event/mds/schema.json', test_data_dir .. '/apps/event/mds/schema.json')
    utils.copy_file('temp/opt/bmc/apps/event/mds/service.json', test_data_dir .. '/apps/event/mds/service.json')

    -- prepare sensor sr and mds files
    utils.copy_file('test/integration/test_data/14100513_Sensor_0.sr', test_data_dir .. '/sr/14100513_Sensor_0.sr')
    utils.copy_file('temp/opt/bmc/apps/sensor/mds/schema.json', test_data_dir .. '/apps/sensor/mds/schema.json')
    utils.copy_file('temp/opt/bmc/apps/sensor/mds/service.json', test_data_dir .. '/apps/sensor/mds/service.json')
    ---- 3.2 file proxy(鉴权使用)
    utils.copy_file(
        'test/integration/mock/infrastructure/file_proxy.lua',
        'temp/opt/bmc/apps/account/lualib/infrastructure/file_proxy.lua'
    )

    prepare_iam_test_data(test_data_dir)
end

local function clear_test_data(exit_test)
    print('clear test data')
    local test_data_dir = skynet.getenv('TEST_DATA_DIR')
    utils.remove_file('/dev/shm/upgrade')
    if exit_test then
        skynet.timeout(0, function()
            skynet.sleep(20)
            skynet.abort()
            utils.remove_file(test_data_dir)
        end)
    else
        utils.remove_file(test_data_dir)
    end
end

-- 运行一类测试
local function run_case_suit(case_suit, bus)
    for name, case in pairs(case_suit) do
        if name ~= '__index' then
            print('==== test ' .. name .. ' start ====')
            case(bus)
            log:notice('==== test ' .. name .. ' end ====')
        end
    end
end

local function test_cli()
    print('================ test start ================')
    local bus = sd_bus.open_user(true, true)

    -- 2025/3/29: 由于映射器迁移及资源树变动,用例全部失效,新用例添加到ut
    bus:close()
    print('================ test complete ================')
    skynet.sleep(10)
end

skynet.start(function()
    clear_test_data()
    prepare_test_data()
    test_common.dbus_launch()
    skynet.uniqueservice('sd_bus')
    skynet.uniqueservice('persistence/service/main')
    skynet.uniqueservice('maca/service/main')
    skynet.uniqueservice('key_mgmt/service/main')
    skynet.uniqueservice('firmware_mgmt/service/main')
    skynet.uniqueservice('oms/service/main')
    skynet.uniqueservice('bmc_time/service/main')
    skynet.uniqueservice('nsm/service/main')
    skynet.uniqueservice('sensor/service/main')
    skynet.uniqueservice('event/service/main')
    skynet.uniqueservice('hwdiscovery/service/main')
    skynet.uniqueservice('iam/service/main')
    skynet.uniqueservice('account/service/main')
    skynet.uniqueservice('rmcpd/service/main')
    skynet.fork(function()
        local ok, err = pcall(test_cli)
        clear_test_data(true)
        if not ok then
            error(err)
        end
    end)
end)