# This Evennia workflow will install Python dependencies, run tests with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: test-suite

on:
  push:
    branches: [main, develop]
    paths-ignore:
      - "docs/**"
  pull_request:
    branches: [main, develop]

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.10", "3.11", "3.12"]
        TESTING_DB: ["sqlite3", "mysql"]
        include:
          - python-version: "3.10"
            TESTING_DB: "sqlite3"
            coverage-test: true

    timeout-minutes: 35

    env:
      UNIT_TEST_SETTINGS: "--settings=settings --keepdb --timing"
      COVERAGE_TEST_SETTINGS: "--settings=settings --timing"

    steps:
      - uses: actions/checkout@v4

      - name: Set up database (${{ matrix.TESTING_DB }})
        uses: ./.github/actions/setup-database
        with:
          database: ${{ matrix.TESTING_DB }}
        timeout-minutes: 5

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          cache: pip
          cache-dependency-path: |
            pyproject.toml

      - name: Install package dependencies
        run: |
          python -m pip install --upgrade pip
          pip install wheel
          pip install psycopg2-binary==2.9.5   # req by postgresql
          pip install mysqlclient
          pip install coveralls
          pip install tblib
          pip install .
          pip install .[extra]

      - name: Initialize evennia
        run: |
          evennia --init testing_mygame
          cp .github/workflows/${{ matrix.TESTING_DB }}_settings.py testing_mygame/server/conf/settings.py
          cd testing_mygame
          evennia migrate
          evennia collectstatic --noinput

      # For non-coverage tests, run them in parallel.
      - name: Run test suite
        if: ${{ ! matrix.coverage-test }}
        working-directory: testing_mygame
        run: |
          evennia test ${{ env.UNIT_TEST_SETTINGS }} evennia

      # OBS - it's important to not run the coverage tests with --parallel, it messes up the coverage
      # calculation!
      - name: Run test suite with coverage
        if: ${{ matrix.coverage-test }}
        working-directory: testing_mygame
        run: |
          coverage run --rcfile=../pyproject.toml ../bin/unix/evennia test ${{ env.COVERAGE_TEST_SETTINGS }} evennia
          coverage combine
          coverage xml
          coverage --version
          coverage report | grep TOTAL

      # we only want to run coverall once, so we only do it for the designated matrix combination(s)
      # it's also not critical if pushing to either service fails (happens for PRs since env is not
      # available outside of the evennia org)
      - name: Send data to Coveralls
        if: ${{ matrix.coverage-test && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop') }}
        continue-on-error: true
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
        run: |
          cd testing_mygame
          coveralls

  deploy:
    name: Deploy Docker Image
    needs: test
    runs-on: ubuntu-latest
    if: ${{ github.repository == 'evennia/evennia' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop') }}
    steps:
      - uses: actions/checkout@v3

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push for main
        if: ${{ github.ref == 'refs/heads/main' }}
        id: docker_build_main
        uses: docker/build-push-action@v3
        with:
          push: true
          tags: evennia/evennia:latest

      - name: Build and push for develop
        if: ${{ github.ref == 'refs/heads/develop' }}
        id: docker_build_develop
        uses: docker/build-push-action@v3
        with:
          push: true
          tags: evennia/evennia:develop
