@ -1,66 +1,87 @@
#!/usr/bin/env bash
#!/usr/bin/env bash
#
#
# Run backend integration tests (Rust).
# Run backend integration tests (Rust).
# Starts a Postgres container, runs tests, then cleans up .
# Uses the e2e/docker-compose.test.yml for Postgres (port 5433) .
#
#
# Usage:
# Usage:
# ./scripts/run-integration-tests.sh # all tests
# ./scripts/run-integration-tests.sh # all tests
# ./scripts/run-integration-tests.sh --test pipeline_test # one test file
# ./scripts/run-integration-tests.sh --test pipeline_test # one test file
# ./scripts/run-integration-tests.sh --test api_admin_test config_providers # one test by name
# ./scripts/run-integration-tests.sh --test api_admin_test config_providers # one test by name
# ./scripts/run-integration-tests.sh --test api_admin_test -- --test-threads=1 # extra cargo args
# ./scripts/run-integration-tests.sh --lib # unit tests only
# ./scripts/run-integration-tests.sh --lib # unit tests only
# ./scripts/run-integration-tests.sh --db-check # just check DB connectivity
#
#
# All arguments are passed directly to `cargo test`.
# All arguments (except --db-check) are passed directly to `cargo test`.
#
#
set -euo pipefail
set -euo pipefail
SCRIPT_DIR = " $( cd " $( dirname " $0 " ) " && pwd ) "
SCRIPT_DIR = " $( cd " $( dirname " $0 " ) " && pwd ) "
PROJECT_DIR = " $( dirname " $SCRIPT_DIR " ) "
PROJECT_DIR = " $( dirname " $SCRIPT_DIR " ) "
E2E_DIR = " $PROJECT_DIR /e2e "
CONTAINER_NAME= "ai-synth-integration-db "
PG_HOST= "127.0.0.1 "
PG_PORT = 543 4
PG_PORT = 543 3
PG_USER = " test_user "
PG_USER = " ai_synth_test "
PG_PASS = "test _ password"
PG_PASS = "test password"
PG_DB = " postgres "
PG_DB = " ai_synth_test "
cleanup( ) {
export TEST_DATABASE_URL = " postgres:// ${ PG_USER } : ${ PG_PASS } @ ${ PG_HOST } : ${ PG_PORT } / ${ PG_DB } "
echo "Cleaning up..."
docker rm -f " $CONTAINER_NAME " 2>/dev/null || true
}
trap cleanup EXIT
echo "=== Starting Postgres for integration tests ==="
# ── DB check mode ──────────────────────────────────────────────────
docker rm -f " $CONTAINER_NAME " 2>/dev/null || true
if [ " ${ 1 :- } " = "--db-check" ] ; then
docker run -d \
echo " Checking DB connectivity at ${ PG_HOST } : ${ PG_PORT } ... "
--name " $CONTAINER_NAME " \
if docker exec ai-synth-test-db pg_isready -U " $PG_USER " -d " $PG_DB " 2>/dev/null; then
-e POSTGRES_USER = " $PG_USER " \
echo "DB is ready."
-e POSTGRES_PASSWORD = " $PG_PASS " \
echo "Testing SQL query..."
-e POSTGRES_DB = " $PG_DB " \
docker exec ai-synth-test-db psql -U " $PG_USER " -d " $PG_DB " -c "SELECT 1 AS ok" 2>& 1
-p " 127.0.0.1: ${ PG_PORT } :5432 " \
echo ""
--health-cmd " pg_isready -U $PG_USER -d $PG_DB " \
echo "Testing connection from host..."
--health-interval 5s \
if command -v psql >/dev/null 2>& 1; then
--health-timeout 3s \
PGPASSWORD = " $PG_PASS " psql -h " $PG_HOST " -p " $PG_PORT " -U " $PG_USER " -d " $PG_DB " -c "SELECT 1 AS ok" 2>& 1 || echo "Host psql connection failed"
--health-retries 10 \
else
postgres:17-alpine
echo "(psql not installed on host, skipping host connection test)"
fi
else
echo "DB container not running. Start it with:"
echo " cd $E2E_DIR && docker compose -f docker-compose.test.yml up -d db "
fi
exit 0
fi
# ── Start DB if not running ────────────────────────────────────────
echo "=== Ensuring test Postgres is running ==="
cd " $E2E_DIR "
# Start only the DB service (not the app)
docker compose -f docker-compose.test.yml up -d db
echo "Waiting for Postgres to be ready..."
echo "Waiting for Postgres to be ready..."
for i in $( seq 1 30) ; do
for i in $( seq 1 30) ; do
if docker exec " $CONTAINER_NAME " pg_isready -U " $PG_USER " -d " $PG_DB " >/dev/null 2>& 1; then
if docker exec ai-synth-test-db pg_isready -U " $PG_USER " -d " $PG_DB " >/dev/null 2>& 1; then
echo "Postgres is ready."
echo " Postgres is ready on port ${ PG_PORT } . "
break
break
fi
fi
if [ " $i " -eq 30 ] ; then
if [ " $i " -eq 30 ] ; then
echo "ERROR: Postgres did not become ready in time."
echo "ERROR: Postgres did not become ready in time."
docker compose -f docker-compose.test.yml logs db | tail -10
exit 1
exit 1
fi
fi
sleep 1
sleep 1
done
done
export TEST_DATABASE_URL = " postgres:// ${ PG_USER } : ${ PG_PASS } @127.0.0.1: ${ PG_PORT } / ${ PG_DB } "
# Verify connectivity from host
echo "Verifying host connectivity..."
cd " $PROJECT_DIR /backend "
if ! cargo sqlx database exists " $TEST_DATABASE_URL " 2>/dev/null; then
# sqlx-cli not installed, try a simple Rust connection test
echo "(sqlx-cli not available, will rely on cargo test to verify connectivity)"
fi
# ── Run tests ──────────────────────────────────────────────────────
echo ""
echo "=== Running integration tests ==="
echo "=== Running integration tests ==="
cd " $PROJECT_DIR /backend "
echo " TEST_DATABASE_URL= $TEST_DATABASE_URL "
echo ""
if [ $# -gt 0 ] ; then
if [ $# -gt 0 ] ; then
echo " Running: cargo test $* "
echo " Running: cargo test $* "
@ -70,4 +91,8 @@ else
cargo test
cargo test
fi
fi
echo "=== All tests passed ==="
echo ""
echo "=== Tests complete ==="
echo ""
echo "Note: DB container is still running. To stop it:"
echo " cd $E2E_DIR && docker compose -f docker-compose.test.yml down "