services:
  # PostgreSQL 测试数据库
  postgres-test:
    image: postgres:15-alpine
    container_name: presence-postgres-test
    environment:
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test
      POSTGRES_DB: presence_test
    ports:
      - "5434:5432"
    volumes:
      - postgres-test-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U test -d presence_test"]
      interval: 5s
      timeout: 5s
      retries: 5

  # Redis 测试实例
  redis-test:
    image: redis:7-alpine
    container_name: presence-redis-test
    ports:
      - "6381:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  # 测试运行容器
  test:
    build:
      context: .
      dockerfile: Dockerfile.test
    container_name: presence-test-runner
    depends_on:
      postgres-test:
        condition: service_healthy
      redis-test:
        condition: service_healthy
    environment:
      NODE_ENV: test
      DATABASE_URL: postgresql://test:test@postgres-test:5432/presence_test?schema=public
      REDIS_HOST: redis-test
      REDIS_PORT: 6379
      RUN_MIGRATIONS: "true"
    volumes:
      - ./coverage:/app/coverage
    command: ["npm", "test", "--", "--coverage"]

volumes:
  postgres-test-data:
