#!/bin/bash
# shellcheck disable=SC2030,SC2031

set -eo pipefail

# Run tests with various configurations.

if [[ -z "$TEST_COMMAND" ]]; then
    if [[ -z "$CARGO_HOME" ]]; then
        CARGO_HOME="$HOME/.cargo"
    fi

    if command -v cargo-nextest || [[ -e "$CARGO_HOME/bin/cargo-nextest" ]]; then
        TEST_COMMAND="cargo nextest run"
    else
        TEST_COMMAND="cargo test"
    fi
fi

# Common variables:
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres"
export SVIX_JWT_SECRET="test value"
export SVIX_LOG_LEVEL="info"
export SVIX_WHITELIST_SUBNETS="[127.0.0.1/32]"
export SVIX_DB_POOL_MAX_SIZE="500"
export SVIX_REDIS_POOL_MAX_SIZE="10000"

echo "*********** RUN 1/7: queue=redis,cache=redis ***********"
(
    export SVIX_QUEUE_TYPE="redis"
    export SVIX_CACHE_TYPE="redis"
    export SVIX_REDIS_DSN="redis://localhost:6379"
    ${TEST_COMMAND} "$@"
    if [[ -z "$*" ]]; then
        ${TEST_COMMAND} -- --ignored redis
    fi
)

echo "*********** RUN 2/7: queue=redis,cache=memory ***********"
(
    export SVIX_QUEUE_TYPE="redis"
    export SVIX_CACHE_TYPE="memory"
    export SVIX_REDIS_DSN="redis://localhost:6379"
    ${TEST_COMMAND} "$@"
)

echo "*********** RUN 3/7: queue=redis,cache=none ***********"
(
    export SVIX_QUEUE_TYPE="redis"
    export SVIX_CACHE_TYPE="none"
    export SVIX_REDIS_DSN="redis://localhost:6379"
    ${TEST_COMMAND} "$@"
)

echo "*********** RUN 4/7: queue=rediscluster,cache=rediscluster ***********"
(
    export SVIX_QUEUE_TYPE="rediscluster"
    export SVIX_CACHE_TYPE="rediscluster"
    export SVIX_REDIS_DSN="redis://localhost:6380"
    ${TEST_COMMAND} "$@"
    if [[ -z "$*" ]]; then
        ${TEST_COMMAND} -- --ignored redis
    fi
)

echo "*********** RUN 5/7: queue=memory,cache=none ***********"
(
    export SVIX_QUEUE_TYPE="memory"
    export SVIX_CACHE_TYPE="none"
    ${TEST_COMMAND} "$@"
)

echo "*********** RUN 6/7: queue=rabbitmq,cache=redis ***********"
(
    export SVIX_QUEUE_TYPE="rabbitmq"
    export SVIX_CACHE_TYPE="redis"
    export SVIX_REDIS_DSN="redis://localhost:6379"
    export SVIX_RABBIT_DSN="amqp://xivs:xivs@localhost:5672/%2f"
    ${TEST_COMMAND} "$@"
    if [[ -z "$*" ]]; then
        ${TEST_COMMAND} -- --ignored rabbitmq
    fi
)

echo "*********** RUN 7/7: queue=redissentinel,cache=redissentinel ***********"
(
    export SVIX_QUEUE_TYPE="redissentinel"
    export SVIX_CACHE_TYPE="redissentinel"
    export SVIX_REDIS_DSN="redis://localhost:26379"
    export SVIX_SENTINEL_SERVICE_NAME="master0"

    ${TEST_COMMAND} "$@"
    if [[ -z "$*" ]]; then
        ${TEST_COMMAND} -- --ignored redis
    fi
)
