on:
  workflow_call:
    secrets:
      stripeTestPublicKey:
        required: true
      stripeTestSecretKey:
        required: true
    inputs:
      registry:
        default: ghcr.io
        type: string
      imageName:
        default: ${{ github.repository_owner }}
        type: string
      imageTag:
        required: true
        type: string
      configureEnv:
        default: ''
        type: string

jobs:
  integration:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        # Divide integration tests into 3 shards, to run them in parallel.
        shardIndex: [1, 2, 3, 'apollo-router']

    env:
      DOCKER_REGISTRY: ${{ inputs.registry }}/${{ inputs.imageName }}/
      DOCKER_TAG: :${{ inputs.imageTag }}

    steps:
      - name: checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          fetch-depth: 2

      - name: configure env vars
        if: ${{ inputs.configureEnv != '' }}
        env:
          CONFIGURE_ENV: ${{ inputs.configureEnv }}
        run: $CONFIGURE_ENV

      - name: setup environment
        uses: ./.github/actions/setup
        with:
          actor: test-integration

      - name: prepare packages
        run: pnpm --filter integration-tests prepare:env

      - name: patch compose file volumes
        uses: mikefarah/yq@4839dbbf80445070a31c7a9c1055da527db2d5ee # v4.44.6
        with:
          cmd: yq -i 'del(.services.*.volumes)' docker/docker-compose.community.yml
      # tests need to access host machine for OIDC stuff
      - name: make host machine accessible to server container
        uses: mikefarah/yq@4839dbbf80445070a31c7a9c1055da527db2d5ee # v4.44.6
        with:
          cmd:
            yq -i '.services.server.extra_hosts[] |= sub("host-gateway"; "172.17.0.1")'
            ./integration-tests/docker-compose.integration.yaml

      - name: get cpu count for vitest
        id: cpu-cores
        uses: SimenB/github-actions-cpu-cores@97ba232459a8e02ff6121db9362b09661c875ab8 # v2

      - name: run containers
        timeout-minutes: 8
        run: |
          docker compose -f docker/docker-compose.community.yml -f ./integration-tests/docker-compose.integration.yaml --env-file ./integration-tests/.env up -d --wait

      - name: troubleshoot containers
        if: ${{ failure() }}
        run: |
          docker compose -f docker/docker-compose.community.yml -f ./integration-tests/docker-compose.integration.yaml --env-file ./integration-tests/.env ps

      ##  ---- START ---- Apollo Router specific steps

      - if: matrix.shardIndex == 'apollo-router'
        name: Install Protoc
        uses: arduino/setup-protoc@v3
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - if: matrix.shardIndex == 'apollo-router'
        name: Install Rust
        uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # v1
        with:
          toolchain: '1.91.1'
          default: true
          override: true

      - if: matrix.shardIndex == 'apollo-router'
        name: Cache Rust
        uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2

      # ---- START ---- Disk space cleanup before apollo router tests
      - if: matrix.shardIndex == 'apollo-router'
        name: Before cleanup disk space
        run: df -h
      - if: matrix.shardIndex == 'apollo-router'
        name: Cleanup disk space
        run: |
          sudo rm -rf \
            /usr/lib/jvm \
            /usr/share/swift \
            /usr/local/julia* \
            /usr/local/share/chromium \
            /opt/az \
            /usr/local/share/powershell \
            /opt/microsoft /opt/google \
            /usr/local/lib/android \
            /usr/local/.ghcup \
            /usr/share/dotnet \
            /usr/local/lib/android \
            /opt/ghc /opt/hostedtoolcache/CodeQL
          sudo docker image prune --all --force
          sudo docker builder prune -a
      - if: matrix.shardIndex == 'apollo-router'
        name: After cleanup disk space
        run: df -h
      # ---- END ---- Disk space cleanup before apollo router tests

      - if: matrix.shardIndex == 'apollo-router'
        name: build apollo router
        run: |
          cargo build

      - if: matrix.shardIndex == 'apollo-router'
        name: run apollo router integration tests
        timeout-minutes: 30
        run: |
          pnpm test:integration:apollo-router

      ## ---- END ---- Apollo Router specific steps

      - if: matrix.shardIndex != 'apollo-router'
        name: run integration tests
        timeout-minutes: 30
        run: |
          pnpm test:integration --shard=${{ matrix.shardIndex }}/3
        env:
          VITEST_MAX_THREADS: ${{ steps.cpu-cores.outputs.count }}

      - name: log dump
        if: ${{ failure() }}
        run: |
          docker compose -f docker/docker-compose.community.yml -f ./integration-tests/docker-compose.integration.yaml --env-file ./integration-tests/.env logs

      - name: inspect health
        if: ${{ failure() }}
        run: |
          docker inspect $(docker compose -f ./docker/docker-compose.community.yml -f ./integration-tests/docker-compose.integration.yaml --env-file ./integration-tests/.env ps -aq) | jq
