on:
  workflow_call:
    inputs:
      test-file:
        description: 'The test file'
        required: true
        type: string
      release-version:
        description: 'the release tag, e.g. release-v7.3.0'
        required: true
        type: string
      chain:
        description: 'Should be one of chain-a, chain-b or all. Split up workflows into multiple (chain-a and chain-b) versions if the job limit is exceeded.'
        required: false
        type: string
        default: all

jobs:
  load-test-matrix:
    outputs:
      test-matrix: ${{ steps.set-test-matrix.outputs.test-matrix }}
    runs-on: depot-ubuntu-22.04-4
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.10'
      - run: pip install -r requirements.txt
      - run: |
          # use jq -c to compact the full json contents into a single line. This is required when using the json body
          # to create the matrix in the following job.
          test_matrix="$(python scripts/generate-compatibility-json.py --file ${{ inputs.test-file }} --release-version ${{ inputs.release-version }} --chain ${{ inputs.chain }})"
          echo "test-matrix=$test_matrix" >> $GITHUB_OUTPUT
        id: set-test-matrix

  e2e:
    runs-on: depot-ubuntu-22.04-4
    needs: load-test-matrix
    # this job is skipped if the test-matrix generated is empty. i.e. if the file was not present.
    # this allows us to not have to handle special case versions which may not have certain tests run against them.
    if: needs.load-test-matrix.outputs.test-matrix
    strategy:
      fail-fast: false
      matrix: ${{ fromJSON(needs.load-test-matrix.outputs.test-matrix) }}
    steps:
      - name: Checkout the ibc-go repo
        uses: actions/checkout@v4
        with:
          repository: cosmos/ibc-go
      - uses: actions/setup-go@v5
        with:
          go-version: '1.23'
          cache-dependency-path: 'e2e/go.sum'
      - name: Run e2e Test
        run: |
          cd e2e
          make e2e-test test=${{ matrix.test }}
        env:
          # each test has its own set of variables to specify which images are used.
          # Note: this is significant as the standard behaviour when running e2es on PRs
          # is that there is a set of env vars that are the same for each run. e.g. the same docker image is used
          # for every test. With compatibility tests, each test may be running different combinations of images.
          CHAIN_A_TAG: '${{ matrix.chain-a }}'
          CHAIN_B_TAG: '${{ matrix.chain-b }}'
          RELAYER_ID: '${{ matrix.relayer-type }}'
      - name: Upload Diagnostics
        uses: actions/upload-artifact@v4
        # we only want to upload logs on test failures.
        if: ${{ failure() }}
        continue-on-error: true
        with:
          name: '${{ matrix.entrypoint }}-${{ matrix.test }}'
          path: e2e/diagnostics
          retention-days: 5
