From de8e5ab85c1c24d75ece81eef6639b6e2f7ab174 Mon Sep 17 00:00:00 2001 From: oabrivard Date: Thu, 26 Mar 2026 10:22:40 +0100 Subject: [PATCH] feat: add test runner scripts for integration and E2E tests - scripts/run-integration-tests.sh: spins up Postgres, runs cargo tests - scripts/run-e2e-tests.sh: builds Docker, starts stack, runs Playwright - fix: remove SESSION_SECRET from e2e docker-compose.test.yml Co-Authored-By: Claude Opus 4.6 (1M context) --- e2e/docker-compose.test.yml | 1 - scripts/run-e2e-tests.sh | 61 ++++++++++++++++++++++++++++ scripts/run-integration-tests.sh | 68 ++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100755 scripts/run-e2e-tests.sh create mode 100755 scripts/run-integration-tests.sh diff --git a/e2e/docker-compose.test.yml b/e2e/docker-compose.test.yml index fc456bd..edbeef7 100644 --- a/e2e/docker-compose.test.yml +++ b/e2e/docker-compose.test.yml @@ -8,7 +8,6 @@ services: environment: DATABASE_URL: postgres://ai_synth_test:testpassword@db:5432/ai_synth_test MASTER_ENCRYPTION_KEY: "0000000000000000000000000000000000000000000000000000000000000001" - SESSION_SECRET: "e2e-test-session-secret-at-least-64-characters-long-for-security-purposes-here" APP_URL: http://localhost:8080 PORT: "8080" RUST_LOG: "info,ai_synth_backend=debug" diff --git a/scripts/run-e2e-tests.sh b/scripts/run-e2e-tests.sh new file mode 100755 index 0000000..002e0a3 --- /dev/null +++ b/scripts/run-e2e-tests.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# +# Run E2E tests (Playwright) against the Docker-composed test stack. +# Builds the app, starts the stack, runs tests, then cleans up. +# +# Usage: +# ./scripts/run-e2e-tests.sh # run all E2E tests +# ./scripts/run-e2e-tests.sh --headed # run with browser visible +# ./scripts/run-e2e-tests.sh generation-live # run specific test file +# +# Prerequisites: +# - Docker and docker compose +# - Node.js (for Playwright) +# - OPENAI_API_KEY env var (for generation-live test) +# + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" +E2E_DIR="$PROJECT_DIR/e2e" + +cleanup() { + echo "" + echo "=== Cleaning up test stack ===" + cd "$E2E_DIR" + docker compose -f docker-compose.test.yml down -v --remove-orphans 2>/dev/null || true +} +trap cleanup EXIT + +echo "=== Building test Docker image ===" +cd "$E2E_DIR" +docker compose -f docker-compose.test.yml build + +echo "=== Starting test stack ===" +docker compose -f docker-compose.test.yml up -d + +echo "Waiting for app to be healthy..." +for i in $(seq 1 60); do + if curl -sf http://localhost:8080/api/v1/health >/dev/null 2>&1; then + echo "App is healthy." + break + fi + if [ "$i" -eq 60 ]; then + echo "ERROR: App did not become healthy in time." + echo "--- App logs ---" + docker compose -f docker-compose.test.yml logs app | tail -30 + exit 1 + fi + sleep 2 +done + +echo "=== Installing Playwright dependencies ===" +cd "$E2E_DIR" +npm install --silent +npx playwright install chromium --with-deps 2>/dev/null || npx playwright install chromium + +echo "=== Running E2E tests ===" +npx playwright test "$@" + +echo "=== All E2E tests passed ===" diff --git a/scripts/run-integration-tests.sh b/scripts/run-integration-tests.sh new file mode 100755 index 0000000..052700a --- /dev/null +++ b/scripts/run-integration-tests.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# +# Run backend integration tests (Rust). +# Starts a Postgres container, runs tests, then cleans up. +# +# Usage: +# ./scripts/run-integration-tests.sh # run all integration tests +# ./scripts/run-integration-tests.sh pipeline # run only pipeline_test +# + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" + +CONTAINER_NAME="ai-synth-integration-db" +PG_PORT=5434 +PG_USER="test_user" +PG_PASS="test_password" +PG_DB="postgres" + +cleanup() { + echo "Cleaning up..." + docker rm -f "$CONTAINER_NAME" 2>/dev/null || true +} +trap cleanup EXIT + +echo "=== Starting Postgres for integration tests ===" +docker rm -f "$CONTAINER_NAME" 2>/dev/null || true +docker run -d \ + --name "$CONTAINER_NAME" \ + -e POSTGRES_USER="$PG_USER" \ + -e POSTGRES_PASSWORD="$PG_PASS" \ + -e POSTGRES_DB="$PG_DB" \ + -p "127.0.0.1:${PG_PORT}:5432" \ + --health-cmd "pg_isready -U $PG_USER -d $PG_DB" \ + --health-interval 5s \ + --health-timeout 3s \ + --health-retries 10 \ + postgres:17-alpine + +echo "Waiting for Postgres to be ready..." +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 + echo "Postgres is ready." + break + fi + if [ "$i" -eq 30 ]; then + echo "ERROR: Postgres did not become ready in time." + exit 1 + fi + sleep 1 +done + +export TEST_DATABASE_URL="postgres://${PG_USER}:${PG_PASS}@127.0.0.1:${PG_PORT}/${PG_DB}" + +echo "=== Running integration tests ===" +cd "$PROJECT_DIR/backend" + +if [ $# -gt 0 ]; then + echo "Running: cargo test --test $1 -- --nocapture" + cargo test --test "$1" -- --nocapture +else + echo "Running: cargo test (all integration + unit tests)" + cargo test -- --nocapture +fi + +echo "=== All tests passed ==="