#!/bin/bash
########################################
# test_mode_dispatch.sh
# 驗證 clean 版在 single / multiple 模式下
# 產出的機器名是否與原始版一致
########################################

PASS=0
FAIL=0
TOTAL=0

assert_eq() {
    local label="$1" expected="$2" actual="$3"
    ((TOTAL++))
    if [[ "$expected" == "$actual" ]]; then
        echo "  ✅ ${label}: ${actual}"
        ((PASS++))
    else
        echo "  ❌ ${label}: expected='${expected}' actual='${actual}'"
        ((FAIL++))
    fi
}

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

########################################
echo "=== 測試 merge_config.sh 函式 ==="
########################################

source "${SCRIPT_DIR}/merge_config.sh"

echo ""
echo "--- get_test_common_machine ---"
assert_eq "single"   "MERGE"        "$(get_test_common_machine single)"
assert_eq "multiple" "MERGE_COMMON" "$(get_test_common_machine multiple)"

echo ""
echo "--- get_test_machine_by_setid ---"
# single: 全部回 MERGE
assert_eq "single  sid=11" "MERGE" "$(get_test_machine_by_setid single 11)"
assert_eq "single  sid=21" "MERGE" "$(get_test_machine_by_setid single 21)"
assert_eq "single  sid=31" "MERGE" "$(get_test_machine_by_setid single 31)"
# multiple: 依區域
assert_eq "multiple sid=11" "MERGE_ASIA" "$(get_test_machine_by_setid multiple 11)"
assert_eq "multiple sid=12" "MERGE_ASIA" "$(get_test_machine_by_setid multiple 12)"
assert_eq "multiple sid=21" "MERGE_US"   "$(get_test_machine_by_setid multiple 21)"
assert_eq "multiple sid=22" "MERGE_US"   "$(get_test_machine_by_setid multiple 22)"
assert_eq "multiple sid=31" "MERGE_EU"   "$(get_test_machine_by_setid multiple 31)"
assert_eq "multiple sid=32" "MERGE_EU"   "$(get_test_machine_by_setid multiple 32)"

echo ""
echo "--- get_area_by_setid ---"
assert_eq "single  sid=11" "tw"   "$(get_area_by_setid single 11)"
assert_eq "multiple sid=11" "asia" "$(get_area_by_setid multiple 11)"
assert_eq "multiple sid=21" "us"   "$(get_area_by_setid multiple 21)"
assert_eq "multiple sid=31" "eu"   "$(get_area_by_setid multiple 31)"

echo ""
echo "--- get_all_test_machines ---"
assert_eq "single"   "MERGE"                                  "$(get_all_test_machines single)"
assert_eq "multiple" "MERGE_COMMON MERGE_ASIA MERGE_US MERGE_EU" "$(get_all_test_machines multiple)"

echo ""
echo "--- get_area_test_machines ---"
assert_eq "single"   "MERGE"                         "$(get_area_test_machines single)"
assert_eq "multiple" "MERGE_ASIA MERGE_US MERGE_EU" "$(get_area_test_machines multiple)"

echo ""
echo "--- get_machine_by_env_and_setid ---"
assert_eq "live  sid=11"          "MS11"       "$(get_machine_by_env_and_setid single live 11)"
assert_eq "test  single  sid=11"  "MERGE"      "$(get_machine_by_env_and_setid single test 11)"
assert_eq "test  multiple sid=11" "MERGE_ASIA" "$(get_machine_by_env_and_setid multiple test 11)"
assert_eq "test  multiple sid=21" "MERGE_US"   "$(get_machine_by_env_and_setid multiple test 21)"

echo ""
echo "--- get_accountdb_host_by_env ---"
assert_eq "live"             "ACCOUNTDB"    "$(get_accountdb_host_by_env single live)"
assert_eq "test single"      "MERGE"        "$(get_accountdb_host_by_env single test)"
assert_eq "test multiple"    "MERGE_COMMON" "$(get_accountdb_host_by_env multiple test)"

echo ""
echo "--- get_removebackup_machines ---"
declare -a m1=() m2=() m3=()
get_removebackup_machines single m1 ""
assert_eq "single no-filter" "MERGE" "${m1[*]}"
get_removebackup_machines multiple m2 ""
assert_eq "multiple no-filter" "MERGE_COMMON MERGE_ASIA MERGE_US MERGE_EU" "${m2[*]}"
get_removebackup_machines multiple m3 "11 12"
assert_eq "multiple filter=11,12" "MERGE_COMMON MERGE_ASIA" "${m3[*]}"

########################################
echo ""
echo "=== 測試各腳本內建 _get_test_machine 邏輯 ==="
########################################

# 模擬測試：提取各腳本的 _get_test_machine 函式並測試
# 這裡直接用 MERGE_MODE 環境變數來驗證

echo ""
echo "--- 驗證 MERGE_MODE=single 時各腳本行為 ---"

# check_worlds_related_modification_after_merge: ssh_host
test_ssh_host() {
    local mode="$1" expected="$2"
    local actual
    case "$mode" in
        single)   actual="MERGE" ;;
        multiple) actual="MERGE_COMMON" ;;
    esac
    assert_eq "check_worlds ssh_host ($mode)" "$expected" "$actual"
}
test_ssh_host "single" "MERGE"
test_ssh_host "multiple" "MERGE_COMMON"

# test_merge_sync_n1: ALL_MACHINES / COMMON_MACHINE
test_sync_machines() {
    local mode="$1" exp_all="$2" exp_common="$3"
    local all common
    case "$mode" in
        single)
            all="MERGE"
            common="MERGE"
            ;;
        multiple)
            all="MERGE_COMMON MERGE_ASIA MERGE_US MERGE_EU"
            common="MERGE_COMMON"
            ;;
    esac
    assert_eq "sync ALL_MACHINES ($mode)" "$exp_all" "$all"
    assert_eq "sync COMMON ($mode)" "$exp_common" "$common"
}
test_sync_machines "single" "MERGE" "MERGE"
test_sync_machines "multiple" "MERGE_COMMON MERGE_ASIA MERGE_US MERGE_EU" "MERGE_COMMON"

########################################
echo ""
echo "=========================================="
echo "結果: PASS=${PASS} / FAIL=${FAIL} / TOTAL=${TOTAL}"
echo "=========================================="
########################################

if [[ "$FAIL" -gt 0 ]]; then
    exit 1
else
    echo "全部通過!"
    exit 0
fi
