name: (CI) Cypress Run
on:
  workflow_call:
    inputs:
      projects:
        required: true
        type: string
      skip-cache:
        required: false
        type: boolean
      tags:
        required: false
        type: string

jobs:
  e2e:
    strategy:
      fail-fast: false
      matrix:
        project: ${{ fromJSON(inputs.projects) }}
    name: ${{ matrix.project }}
    runs-on: self-hosted-runner
    timeout-minutes: 40
    steps:
      # Checks if skip cache was requested
      - name: Set skip-nx-cache flag
        if: ${{ inputs.skip-cache == true }}
        run: echo "SKIP_CACHE=--skip-nx-cache" >> $GITHUB_ENV

      # Checkout front ends
      - name: Checkout frontend mono repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          path: './frontend-monorepo'

      # Restore node_modules from cache if possible
      - name: Restore node_modules from cache
        uses: actions/cache@v3
        with:
          path: |
            frontend-monorepo/node_modules
            /home/runner/.cache/Cypress
          key: node_modules_cypress-${{ hashFiles('frontend-monorepo/yarn.lock', 'frontend-monorepo/package.json') }}

      # Install frontend dependencies
      - name: Install root dependencies
        run: yarn install --frozen-lockfile
        working-directory: frontend-monorepo

      # Make sure that all Cypress binaries are installed properly
      - name: Install cypress bins
        run: yarn cypress install
        working-directory: frontend-monorepo

      ######
      ## Setup Vegacapsule and Vega wallet
      ######

      - name: Run Vegacapsule network and Vega wallet
        id: setup-vega
        uses: ./frontend-monorepo/.github/actions/run-vegacapsule

      ######
      ## Run some tests
      ######

      - name: Run Cypress tests
        run: yarn nx run ${{ matrix.project }}:e2e ${{ env.SKIP_CACHE }} --record --key ${{ secrets.CYPRESS_RECORD_KEY }} --browser chrome --env.grepTags="${{ inputs.tags }}"
        working-directory: frontend-monorepo
        env:
          CYPRESS_SLACK_WEBHOOK: ${{ secrets.CYPRESS_SLACK_WEBHOOK }}
          CYPRESS_VEGA_WALLET_API_TOKEN: ${{ steps.setup-vega.outputs.token }}

      ######
      ## Upload logs
      ######

      # Artifact path is not valid: /ganache-1/capsule-logscolletor.stderr-2022-12-22T10:20:30Z.log. Contains the following character:  Colon :
      - name: Rename files to allow archive
        if: ${{ always() }}
        run: |
          while read -r file; do
            mv "${file}" "$(echo ${file} | sed 's|:|-|g')"
          done< <(find /home/runner/.vegacapsule/testnet/logs -type f)

      - name: Print logs files
        if: ${{ always() }}
        run: ls -alsh /home/runner/.vegacapsule/testnet/logs/

      - uses: actions/upload-artifact@v3
        if: ${{ failure() }}
        with:
          name: logs-${{ matrix.project }}
          path: /home/runner/.vegacapsule/testnet/logs

      - uses: actions/upload-artifact@v3
        if: ${{ failure() }}
        with:
          name: test-report-${{ matrix.project }}
          path: frontend-monorepo/apps/trading-e2e/cypress/reports