## docker-compose.test.yml
##
## Isolated test / performance environment with configurable resource limits.
##
## Resource knobs (set via env vars or .env file):
##   CPU_LIMIT    — number of CPUs (default: 2)
##   MEM_LIMIT    — memory limit (default: 1g)
##   MEM_RESERVE  — memory reservation/guarantee (default: 512m)
##   DISK_SIZE    — tmpfs data-dir size (default: 256m)
##   BENCH_FILTER — criterion bench filter (default: empty = all)
##   TEST_FILTER  — test name filter (default: empty = all)
##   TEST_THREADS — number of test threads (default: unset = auto)
##
## Usage examples:
##   # Run all tests with defaults
##   docker compose -f docker-compose.test.yml up test
##
##   # Run benchmarks with 4 CPUs and 4 GB RAM
##   CPU_LIMIT=4 MEM_LIMIT=4g docker compose -f docker-compose.test.yml up bench
##
##   # Run a specific test
##   TEST_FILTER=tcp_integration docker compose -f docker-compose.test.yml up test

services:
  # ── Run tests ──────────────────────────────────────────────────────────────
  test:
    build:
      context: .
      target: test
    cpus: ${CPU_LIMIT:-2}
    mem_limit: ${MEM_LIMIT:-1g}
    mem_reservation: ${MEM_RESERVE:-512m}
    tmpfs:
      - /app/data:size=${DISK_SIZE:-256m}
    environment:
      - FAFB_DATA_DIR=/app/data
      - FAFB_TCP_ADDRESS=127.0.0.1
      - FAFB_HTTP_ADDRESS=127.0.0.1
      - RUST_LOG=info
      - RUST_BACKTRACE=1
    command: >
      cargo test --release
      ${TEST_FILTER:+-- ${TEST_FILTER}}
      -- --nocapture
      ${TEST_THREADS:+--test-threads=${TEST_THREADS}}

  # ── Run benchmarks ────────────────────────────────────────────────────────
  bench:
    build:
      context: .
      target: test
    cpus: ${CPU_LIMIT:-2}
    mem_limit: ${MEM_LIMIT:-1g}
    mem_reservation: ${MEM_RESERVE:-512m}
    tmpfs:
      - /app/data:size=${DISK_SIZE:-256m}
    environment:
      - FAFB_DATA_DIR=/app/data
      - FAFB_TCP_ADDRESS=127.0.0.1
      - FAFB_HTTP_ADDRESS=127.0.0.1
      - RUST_LOG=warn
    command: >
      cargo bench
      ${BENCH_FILTER:+-- ${BENCH_FILTER}}

  # ── Run load tests ──────────────────────────────────────────────────────────
  load_test:
    build:
      context: .
      target: test
    cpus: ${CPU_LIMIT:-4}
    mem_limit: ${MEM_LIMIT:-2g}
    mem_reservation: ${MEM_RESERVE:-1g}
    tmpfs:
      - /app/data:size=${DISK_SIZE:-256m}
    environment:
      - FAFB_DATA_DIR=/app/data
      - FAFB_TCP_ADDRESS=127.0.0.1
      - FAFB_HTTP_ADDRESS=127.0.0.1
      - RUST_LOG=info
      - RUST_BACKTRACE=1
      - LOAD_TEST_CONNECTIONS=${LOAD_TEST_CONNECTIONS:-100}
      - LOAD_TEST_DURATION_SECS=${LOAD_TEST_DURATION_SECS:-30}
    command: >
      cargo test --release --test load_test
      -- --ignored --nocapture
      ${TEST_FILTER}

  # ── Run the server (for manual / ad-hoc perf testing against it) ──────────
  server:
    build:
      context: .
      target: runtime
    cpus: ${CPU_LIMIT:-2}
    mem_limit: ${MEM_LIMIT:-1g}
    mem_reservation: ${MEM_RESERVE:-512m}
    tmpfs:
      - /app/data:size=${DISK_SIZE:-256m}
    ports:
      - "${TCP_PORT:-9876}:9876"
      - "${HTTP_PORT:-8080}:8080"
    environment:
      - FAFB_DATA_DIR=/app/data
      - FAFB_TCP_ADDRESS=0.0.0.0
      - FAFB_HTTP_ADDRESS=0.0.0.0
      - RUST_LOG=info

  # ── E2E: rolling-upgrade.sh ────────────────────────────────────────────────
  # Tests the full upgrade cycle: start → drain → stop → install → restart → health.
  # Uses the same binary as "old" and "new" — tests orchestration, not version change.
  #
  # Usage: docker compose -f docker-compose.test.yml up --build e2e-upgrade
  e2e-upgrade:
    build:
      context: .
      target: runtime
    entrypoint: ["/bin/sh"]
    command: ["/app/tests/e2e/test_rolling_upgrade_e2e.sh"]
    tmpfs:
      - /app/data:size=64m
    environment:
      - FAFB_DATA_DIR=/app/data
      - FAFB_TCP_ADDRESS=127.0.0.1
      - FAFB_HTTP_ADDRESS=127.0.0.1
      - RUST_LOG=info
    volumes:
      - ./scripts:/app/scripts:ro
      - ./tests/e2e:/app/tests/e2e:ro
