name: Reusable Run E2E Test Suite Workflow

on:
  workflow_call:
    inputs:
      snjs_image_tag:
        type: string
        default: latest
        description: The Docker image tag used for SNJS container
    secrets:
      DOCKER_USERNAME:
        required: true
      DOCKER_PASSWORD:
        required: true
      AWS_ACCESS_KEY_ID:
        required: true
      AWS_SECRET_ACCESS_KEY:
        required: true

jobs:
  e2e:
    name: (Docker) E2E Test Suite
    runs-on: ubuntu-latest

    services:
      snjs:
        image: standardnotes/snjs:${{ inputs.snjs_image_tag }}
        ports:
        - 9001:9001

    steps:
    - uses: actions/checkout@v3

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Install
      run: yarn install --immutable

    - name: Run Server
      run: docker compose -f docker-compose.ci.yml up -d
      env:
        DB_TYPE: mysql
        CACHE_TYPE: redis

    - name: Wait for server to start
      run: docker/is-available.sh http://localhost:3123 $(pwd)/logs

    - name: Run E2E Test Suite
      run: yarn dlx mocha-headless-chrome --timeout 1200000 -f http://localhost:9001/mocha/test.html

  e2e-home-server:
    name: (Home Server) E2E Test Suite
    strategy:
      matrix:
        db_type: [mysql, sqlite]
        cache_type: [redis, memory]

    runs-on: ubuntu-latest

    services:
      snjs:
        image: standardnotes/snjs:${{ inputs.snjs_image_tag }}
        ports:
        - 9001:9001
      cache:
        image: redis
        ports:
          - 6379:6379
      db:
        image: mysql
        ports:
          - 3306:3306
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: standardnotes
          MYSQL_USER: standardnotes
          MYSQL_PASSWORD: standardnotes

    steps:
    - uses: actions/checkout@v3

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Install Dependencies
      run: yarn install --immutable

    - name: Build
      run: yarn build

    - name: Copy dotenv file
      run: cp packages/home-server/.env.sample packages/home-server/.env

    - name: Fill in env variables
      run: |
        sed -i "s/JWT_SECRET=/JWT_SECRET=$(openssl rand -hex 32)/g" packages/home-server/.env
        sed -i "s/AUTH_JWT_SECRET=/AUTH_JWT_SECRET=$(openssl rand -hex 32)/g" packages/home-server/.env
        sed -i "s/ENCRYPTION_SERVER_KEY=/ENCRYPTION_SERVER_KEY=$(openssl rand -hex 32)/g" packages/home-server/.env
        sed -i "s/PSEUDO_KEY_PARAMS_KEY=/PSEUDO_KEY_PARAMS_KEY=$(openssl rand -hex 32)/g" packages/home-server/.env
        sed -i "s/VALET_TOKEN_SECRET=/VALET_TOKEN_SECRET=$(openssl rand -hex 32)/g" packages/home-server/.env
        echo "ACCESS_TOKEN_AGE=4" >> packages/home-server/.env
        echo "REFRESH_TOKEN_AGE=7" >> packages/home-server/.env
        echo "REVISIONS_FREQUENCY=5" >> packages/home-server/.env
        echo "DB_HOST=db" >> packages/home-server/.env
        echo "DB_PORT=3306" >> packages/home-server/.env
        echo "DB_USERNAME=standardnotes" >> packages/home-server/.env
        echo "DB_PASSWORD=standardnotes" >> packages/home-server/.env
        echo "DB_TYPE=${{ matrix.db_type }}" >> packages/home-server/.env
        echo "REDIS_URL=redis://cache" >> packages/home-server/.env
        echo "CACHE_TYPE=${{ matrix.cache_type }}" >> packages/home-server/.env

    - name: Run Server
      run: nohup yarn workspace @standardnotes/home-server start &
      env:
        PORT: 3123

    - name: Wait for server to start
      run: for i in {1..30}; do curl -s http://localhost:3123/healthcheck && break || sleep 1; done

    - name: Run E2E Test Suite
      run: yarn dlx mocha-headless-chrome --timeout 1200000 -f http://localhost:9001/mocha/test.html?skip_paid_features=true
