#!/usr/bin/env bash
# Tests para wg_status
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/wg_status.sh"

PASS=0
FAIL=0

assert_contains() {
    local test_name="$1" needle="$2" haystack="$3"
    if echo "$haystack" | grep -qF "$needle"; then
        echo "PASS: $test_name"
        PASS=$((PASS+1))
    else
        echo "FAIL: $test_name — expected to contain '$needle'"
        echo "      got: $haystack"
        FAIL=$((FAIL+1))
    fi
}

assert_not_contains() {
    local test_name="$1" needle="$2" haystack="$3"
    if ! echo "$haystack" | grep -qF "$needle"; then
        echo "PASS: $test_name"
        PASS=$((PASS+1))
    else
        echo "FAIL: $test_name — expected NOT to contain '$needle'"
        echo "      got: $haystack"
        FAIL=$((FAIL+1))
    fi
}

# --- fixtures ---
FAKE_DUMP=$(mktemp)
FAKE_DUMP_EMPTY=$(mktemp)
FAKE_CONF=$(mktemp)
trap 'rm -f "$FAKE_DUMP" "$FAKE_DUMP_EMPTY" "$FAKE_CONF"' EXIT

NOW=$(date +%s)
HS_ONLINE=$(( NOW - 60 ))    # 60s ago  → online
HS_STALE=$(( NOW - 500 ))    # 500s ago → stale

# dump con 2 peers (tabs como separador)
printf '%s\n' \
    "privKeyBase64==	ifacePubKey==	51820	off" \
    "peerKey1==	(none)	1.2.3.4:54321	10.42.0.10/32	${HS_ONLINE}	12345	67890	25" \
    "peerKey2==	(none)	5.6.7.8:12345	10.42.0.20/32	${HS_STALE}	111	222	0" \
    > "$FAKE_DUMP"

# dump vacío (solo línea de interface, sin peers)
printf '%s\n' "privKeyBase64==	ifacePubKey==	51820	off" > "$FAKE_DUMP_EMPTY"

# conf con DeviceID comments
cat > "$FAKE_CONF" <<'CONF'
[Interface]
PrivateKey = privKeyBase64==
Address = 10.42.0.1/24
ListenPort = 51820

# DeviceID:pc-aurgi
[Peer]
PublicKey = peerKey1==
AllowedIPs = 10.42.0.10/32

# DeviceID:home-wsl
[Peer]
PublicKey = peerKey2==
AllowedIPs = 10.42.0.20/32
CONF

# --- Test: interface con 2 peers online y stale ---
result=$(WG_FAKE_DUMP="$FAKE_DUMP" WG_FAKE_CONF="$FAKE_CONF" wg_status wg0)
assert_contains "interface con 2 peers online y stale" '"interface":"wg0"'          "$result"
assert_contains "interface con 2 peers online y stale" '"listen_port":51820'         "$result"
assert_contains "interface con 2 peers online y stale" '"public_key":"ifacePubKey=="' "$result"
assert_contains "interface con 2 peers online y stale" '"status":"online"'           "$result"
assert_contains "interface con 2 peers online y stale" '"status":"stale"'            "$result"
assert_contains "interface con 2 peers online y stale" '"device_id":"pc-aurgi"'      "$result"
assert_contains "interface con 2 peers online y stale" '"device_id":"home-wsl"'      "$result"
assert_contains "interface con 2 peers online y stale" '"rx_bytes":12345'            "$result"
assert_contains "interface con 2 peers online y stale" '"persistent_keepalive":25'   "$result"

# --- Test: interface sin peers devuelve array vacio ---
result_empty=$(WG_FAKE_DUMP="$FAKE_DUMP_EMPTY" WG_FAKE_CONF="$FAKE_CONF" wg_status wg0)
assert_contains     "interface sin peers devuelve array vacio" '"peers":[]' "$result_empty"
assert_not_contains "interface sin peers devuelve array vacio" '"error"'    "$result_empty"

# --- Test: interface inexistente devuelve error JSON ---
result_err=$(wg_status nonexistent_iface_xyz 2>/dev/null || true)
assert_contains "interface inexistente devuelve error JSON" '"error"' "$result_err"

# --- Test: WG_FAKE_DUMP carga dump de archivo ---
result_fake=$(WG_FAKE_DUMP="$FAKE_DUMP" WG_FAKE_CONF="$FAKE_CONF" wg_status wg0)
assert_contains "WG_FAKE_DUMP carga dump de archivo" '"public_key":"ifacePubKey=="' "$result_fake"
assert_contains "WG_FAKE_DUMP carga dump de archivo" '"peers":[{'                   "$result_fake"

echo "---"
echo "Results: $PASS passed, $FAIL failed"
[[ $FAIL -eq 0 ]] || exit 1
