#!/bin/bash
# Test: Environment variable substitution in config files (E2E)
# Tests that ${VAR} syntax works in config files for URL, headers, etc.

source "$(dirname "$0")/../../lib/framework.sh"
test_init "basic/config-env-vars" --isolated

# Start test server
start_test_server

# =============================================================================
# Test: Environment variable in URL
# =============================================================================

test_case "env var substitution in URL"
# Set up environment variable
export TEST_SERVER_HOST="localhost"
export TEST_SERVER_PORT="$TEST_SERVER_PORT"

# Create config with env var in URL
CONFIG_FILE="$(to_native_path "$TEST_TMP/env-url-config.json")"
cat > "$CONFIG_FILE" <<'EOF'
{
  "mcpServers": {
    "env-test": {
      "url": "http://${TEST_SERVER_HOST}:${TEST_SERVER_PORT}"
    }
  }
}
EOF

# Test that we can connect using the config
SESSION=$(session_name "env-url")
run_mcpc connect "$CONFIG_FILE:env-test" "$SESSION"
assert_success
_SESSIONS_CREATED+=("$SESSION")

# Verify connection works
run_mcpc "$SESSION" tools-list
assert_success
assert_contains "$STDOUT" "echo"

# Clean up
run_mcpc "$SESSION" close >/dev/null 2>&1
_SESSIONS_CREATED=("${_SESSIONS_CREATED[@]/$SESSION}")
test_pass

# =============================================================================
# Test: Environment variable in headers
# =============================================================================

test_case "env var substitution in headers"
export MY_API_TOKEN="secret-token-12345"

# Create config with env var in header
CONFIG_FILE="$(to_native_path "$TEST_TMP/env-header-config.json")"
cat > "$CONFIG_FILE" <<EOF
{
  "mcpServers": {
    "header-test": {
      "url": "http://localhost:$TEST_SERVER_PORT",
      "headers": {
        "Authorization": "Bearer \${MY_API_TOKEN}",
        "X-Custom": "static-value"
      }
    }
  }
}
EOF

# Test that we can connect using the config
SESSION=$(session_name "env-hdr")
run_mcpc connect "$CONFIG_FILE:header-test" "$SESSION"
assert_success
_SESSIONS_CREATED+=("$SESSION")

# Verify connection works
run_mcpc "$SESSION" ping
assert_success

# Clean up
run_mcpc "$SESSION" close >/dev/null 2>&1
_SESSIONS_CREATED=("${_SESSIONS_CREATED[@]/$SESSION}")
unset MY_API_TOKEN
test_pass

# =============================================================================
# Test: Missing environment variable defaults to empty string
# =============================================================================

test_case "missing env var defaults to empty string"
# Ensure variable is not set
unset NONEXISTENT_VAR

# Create config with missing env var
CONFIG_FILE="$(to_native_path "$TEST_TMP/env-missing-config.json")"
cat > "$CONFIG_FILE" <<EOF
{
  "mcpServers": {
    "missing-test": {
      "url": "http://localhost:$TEST_SERVER_PORT",
      "headers": {
        "X-Missing": "\${NONEXISTENT_VAR}"
      }
    }
  }
}
EOF

# Should still connect (empty string is valid header value)
SESSION=$(session_name "env-miss")
run_mcpc connect "$CONFIG_FILE:missing-test" "$SESSION"
assert_success
_SESSIONS_CREATED+=("$SESSION")

# Clean up
run_mcpc "$SESSION" close >/dev/null 2>&1
_SESSIONS_CREATED=("${_SESSIONS_CREATED[@]/$SESSION}")
test_pass

# =============================================================================
# Test: Multiple env vars in same value
# =============================================================================

test_case "multiple env vars in same value"
export MY_HOST="localhost"
export MY_PORT="$TEST_SERVER_PORT"

# Create config with multiple env vars in URL
CONFIG_FILE="$(to_native_path "$TEST_TMP/env-multi-config.json")"
cat > "$CONFIG_FILE" <<EOF
{
  "mcpServers": {
    "multi-test": {
      "url": "http://\${MY_HOST}:\${MY_PORT}"
    }
  }
}
EOF

SESSION=$(session_name "env-mult")
run_mcpc connect "$CONFIG_FILE:multi-test" "$SESSION"
assert_success
_SESSIONS_CREATED+=("$SESSION")

# Verify session was created with correct URL
run_mcpc --json
assert_success
# The URL should have substituted values
# Note: We can't easily check the resolved URL in session, but the fact
# that the session was created successfully proves substitution worked

# Clean up
run_mcpc "$SESSION" close >/dev/null 2>&1
_SESSIONS_CREATED=("${_SESSIONS_CREATED[@]/$SESSION}")
unset MY_HOST
unset MY_PORT
test_pass

test_done
