# This file was auto-generated, do not edit it directly.
# Instead run bin/update_build_scripts from
# https://github.com/overleaf/internal/

version: "2.3"

services:
  test_unit:
    build:
      context: ../..
      dockerfile: services/history-v1/Dockerfile
      target: base
    volumes:
      - .:/overleaf/services/history-v1
      - ../../node_modules:/overleaf/node_modules
      - ../../libraries:/overleaf/libraries
    working_dir: /overleaf/services/history-v1
    environment:
      MOCHA_GREP: ${MOCHA_GREP}
      NODE_ENV: test
      NODE_OPTIONS: "--unhandled-rejections=strict"
    command: npm run --silent test:unit
    user: node

  test_acceptance:
    build:
      context: ../..
      dockerfile: services/history-v1/Dockerfile
      target: base
    volumes:
      - .:/overleaf/services/history-v1
      - ../../node_modules:/overleaf/node_modules
      - ../../libraries:/overleaf/libraries
      - ./test/acceptance/certs:/certs
    working_dir: /overleaf/services/history-v1
    environment:
      ELASTIC_SEARCH_DSN: es:9200
      MONGO_HOST: mongo
      POSTGRES_HOST: postgres
      AWS_S3_ENDPOINT: https://minio:9000
      AWS_S3_PATH_STYLE: 'true'
      AWS_ACCESS_KEY_ID: OVERLEAF_HISTORY_S3_ACCESS_KEY_ID
      AWS_SECRET_ACCESS_KEY: OVERLEAF_HISTORY_S3_SECRET_ACCESS_KEY
      MINIO_ROOT_USER: MINIO_ROOT_USER
      MINIO_ROOT_PASSWORD: MINIO_ROOT_PASSWORD
      GCS_API_ENDPOINT: http://gcs:9090
      GCS_PROJECT_ID: fake
      STORAGE_EMULATOR_HOST: http://gcs:9090/storage/v1
      MOCHA_GREP: ${MOCHA_GREP}
      LOG_LEVEL: ERROR
      NODE_ENV: test
      NODE_OPTIONS: "--unhandled-rejections=strict"
    user: node
    depends_on:
      mongo:
        condition: service_healthy
      postgres:
        condition: service_healthy
      certs:
        condition: service_completed_successfully
      minio:
        condition: service_started
      minio_setup:
        condition: service_completed_successfully
      gcs:
        condition: service_healthy
    command: npm run --silent test:acceptance

  mongo:
    image: mongo:6.0.13
    command: --replSet overleaf
    healthcheck:
      test: "mongosh --quiet localhost/test --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 1)'"
      interval: 1s
      retries: 20

  postgres:
    image: postgres:10
    environment:
      POSTGRES_USER: overleaf
      POSTGRES_PASSWORD: overleaf
      POSTGRES_DB: overleaf-history-v1-test
    volumes:
      - ./test/acceptance/pg-init/:/docker-entrypoint-initdb.d/
    healthcheck:
      test: pg_isready --host=localhost --quiet
      interval: 1s
      retries: 20

  certs:
    image: node:20.18.2
    volumes:
      - ./test/acceptance/certs:/certs
    working_dir: /certs
    entrypoint: sh
    command:
      - '-cex'
      - |
        if [ ! -f ./certgen ]; then
          wget -O ./certgen "https://github.com/minio/certgen/releases/download/v1.3.0/certgen-linux-$(dpkg --print-architecture)"
          chmod +x ./certgen
        fi
        if [ ! -f private.key ] || [ ! -f public.crt ]; then
          ./certgen -host minio
        fi

  minio:
    image: minio/minio:RELEASE.2024-10-13T13-34-11Z
    command: server /data
    volumes:
      - ./test/acceptance/certs:/root/.minio/certs
    environment:
      MINIO_ROOT_USER: MINIO_ROOT_USER
      MINIO_ROOT_PASSWORD: MINIO_ROOT_PASSWORD
    depends_on:
      certs:
        condition: service_completed_successfully

  minio_setup:
    depends_on:
      certs:
        condition: service_completed_successfully
      minio:
        condition: service_started
    image: minio/mc:RELEASE.2024-10-08T09-37-26Z
    volumes:
      - ./test/acceptance/certs:/root/.mc/certs/CAs
    entrypoint: sh
    command:
      - '-cex'
      - |
        sleep 1
        mc alias set s3 https://minio:9000 MINIO_ROOT_USER MINIO_ROOT_PASSWORD \
        || sleep 3 && \
        mc alias set s3 https://minio:9000 MINIO_ROOT_USER MINIO_ROOT_PASSWORD \
        || sleep 3 && \
        mc alias set s3 https://minio:9000 MINIO_ROOT_USER MINIO_ROOT_PASSWORD \
        || sleep 3 && \
        mc alias set s3 https://minio:9000 MINIO_ROOT_USER MINIO_ROOT_PASSWORD
        mc mb --ignore-existing s3/overleaf-test-history-chunks
        mc mb --ignore-existing s3/overleaf-test-history-deks
        mc mb --ignore-existing s3/overleaf-test-history-global-blobs
        mc mb --ignore-existing s3/overleaf-test-history-project-blobs
        mc admin user add s3 \
          OVERLEAF_HISTORY_S3_ACCESS_KEY_ID \
          OVERLEAF_HISTORY_S3_SECRET_ACCESS_KEY
        echo '
          {
            "Version": "2012-10-17",
            "Statement": [
              {
                "Effect": "Allow",
                "Action": [
                  "s3:ListBucket"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-chunks"
              },
              {
                "Effect": "Allow",
                "Action": [
                  "s3:PutObject",
                  "s3:GetObject",
                  "s3:DeleteObject"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-chunks/*"
              },
              {
                "Effect": "Allow",
                "Action": [
                  "s3:ListBucket"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-deks"
              },
              {
                "Effect": "Allow",
                "Action": [
                  "s3:PutObject",
                  "s3:GetObject",
                  "s3:DeleteObject"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-deks/*"
              },
              {
                "Effect": "Allow",
                "Action": [
                  "s3:ListBucket"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-global-blobs"
              },
              {
                "Effect": "Allow",
                "Action": [
                  "s3:PutObject",
                  "s3:GetObject",
                  "s3:DeleteObject"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-global-blobs/*"
              },
              {
                "Effect": "Allow",
                "Action": [
                  "s3:ListBucket"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-project-blobs"
              },
              {
                "Effect": "Allow",
                "Action": [
                  "s3:PutObject",
                  "s3:GetObject",
                  "s3:DeleteObject"
                ],
                "Resource": "arn:aws:s3:::overleaf-test-history-project-blobs/*"
              }
            ]
          }' > policy-history.json

        mc admin policy create s3 overleaf-history policy-history.json
        mc admin policy attach s3 overleaf-history \
          --user=OVERLEAF_HISTORY_S3_ACCESS_KEY_ID
  gcs:
    image: fsouza/fake-gcs-server:1.45.2
    command: ["--port=9090", "--scheme=http"]
    healthcheck:
      test: wget --quiet --output-document=/dev/null http://localhost:9090/storage/v1/b
      interval: 1s
      retries: 20
