local lu = require 'luaunit'
local retimer_upg_jbog_retimer = require 'retimer.upgrade.retimer_upg_jbog_retimer'
local mcu_upgrade_service = require 'mcu.upgrade.upgrade_service.upgrade_service_mcu'

TestUpgradeRetimerUpgJbogRetimer = {}

function TestUpgradeRetimerUpgJbogRetimer:test_wait_retimer_upgrade_result()
    local orig_get_ref_mcu = retimer_upg_jbog_retimer.get_ref_mcu

    retimer_upg_jbog_retimer.get_ref_mcu = function()
    end
    local ret = retimer_upg_jbog_retimer:wait_retimer_upgrade_result()
    lu.assertEquals(ret, -1)

    retimer_upg_jbog_retimer.get_ref_mcu = function()
        return {
            interface = {
                query_retimer_upgrade_status = function()
                    return 0x04
                end,
                query_retimer_upgrade_proportion = function()

                end
            }
        }
    end
    ret = retimer_upg_jbog_retimer:wait_retimer_upgrade_result(10, function()
    end)
    lu.assertEquals(ret, -1)

    retimer_upg_jbog_retimer.get_ref_mcu = function()
        return {
            interface = {
                query_retimer_upgrade_status = function()
                    return 0x02
                end,
                query_retimer_upgrade_proportion = function()
                end
            }
        }
    end
    ret = retimer_upg_jbog_retimer:wait_retimer_upgrade_result(10, function()
    end)
    lu.assertEquals(ret, 0)

    retimer_upg_jbog_retimer.get_ref_mcu = function()
        return {
            interface = {
                query_retimer_upgrade_status = function()
                end,
                query_retimer_upgrade_proportion = function()
                end
            }
        }
    end
    ret = retimer_upg_jbog_retimer:wait_retimer_upgrade_result(10, function()
    end)
    lu.assertEquals(ret, -1)

    retimer_upg_jbog_retimer.get_ref_mcu = orig_get_ref_mcu
end

function TestUpgradeRetimerUpgJbogRetimer:test_upgrade_prepare_func()
    local ret = retimer_upg_jbog_retimer:upgrade_prepare_func()
    lu.assertEquals(ret, true)
end

function TestUpgradeRetimerUpgJbogRetimer:test_upgrade_single_retimer_finish_func()
    local ret = retimer_upg_jbog_retimer:upgrade_single_retimer_finish_func()
    lu.assertEquals(ret, true)
end

function TestUpgradeRetimerUpgJbogRetimer:test_upgrade_finish_func()
    local ret = retimer_upg_jbog_retimer:upgrade_finish_func()
    lu.assertEquals(ret, true)
end

function TestUpgradeRetimerUpgJbogRetimer:test_upgrade_single_retimer_func()
    local orig_mcu_upgrade_service = mcu_upgrade_service.get_instance

    mcu_upgrade_service.get_instance = function()
        return {
            mcu_collection = {
                [1] = {
                    get_position = function()
                        return 'na'
                    end
                }
            }
        }
    end
    local retimer = {
        get_position = function()
            return 'position'
        end,
        name = 'name'
    }
    local ret = retimer_upg_jbog_retimer.upgrade_single_retimer_func(retimer, nil, nil, {}, nil, nil)
    lu.assertEquals(ret, false)

    mcu_upgrade_service.get_instance = function()
        return {
            mcu_collection = {
                [1] = {
                    get_position = function()
                        return 'position'
                    end,
                    get_system_id = function()
                        return 1
                    end,
                    mcu = {
                        SubCompList = {
                            [1] = {
                                Type = 1
                            }
                        }
                    }
                }
            }
        }
    end
    local ret = retimer_upg_jbog_retimer.upgrade_single_retimer_func(retimer, nil, nil, {}, {
        system_id = 1
    }, {
        process = function ()
        end,
        concurrent_run = function(cb)
            cb()
        end
    })
    lu.assertEquals(ret, false)

    mcu_upgrade_service.get_instance = orig_mcu_upgrade_service
end