name: Tests
on:
  push:
    branches-ignore:
      - "dependabot/**"
      - translatewiki
  pull_request:
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true
jobs:
  test:
    name: Ruby ${{ matrix.ruby }}
    strategy:
      matrix:
        ruby: ['3.3', '3.4', '4.0']
      fail-fast: false
    runs-on: ubuntu-latest
    env:
      RAILS_ENV: test
      OPENSTREETMAP_MEMCACHE_SERVERS: 127.0.0.1
    timeout-minutes: 20
    steps:
    - name: Checkout source
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Setup ruby
      uses: ruby/setup-ruby@6aaa311d81eba98ae12eaffbcb63296ace0efcde # v1.307.0
      with:
        ruby-version: ${{ matrix.ruby }}
        rubygems: 3.4.10
        bundler-cache: true
    - name: Cache node modules
      uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
      with:
        cache: yarn
    - name: Install packages
      run: |
        sudo apt-get -yqq update
        sudo apt-get -yqq install memcached libvips-dev xvfb mesa-utils libgl1-mesa-dri postgresql-postgis
    - name: Create database
      run: |
        sudo systemctl start postgresql
        sudo -u postgres createuser -s $(id -un)
        createdb openstreetmap
    - name: Configure rails
      run: |
        cp config/github.database.yml config/database.yml
        cp config/example.storage.yml config/storage.yml
        touch config/settings.local.yml
    - name: Install node modules
      run: bundle exec bin/yarn install
    - name: Populate database
      run: |
        sed -f script/normalise-structure db/structure.sql > db/structure.expected
        rm -f db/structure.sql
        bundle exec rails db:migrate
        sed -f script/normalise-structure db/structure.sql > db/structure.actual
        diff -uw db/structure.expected db/structure.actual
    - name: Export javascript strings
      run: bundle exec i18n export
    - name: Compile assets
      run: bundle exec rails assets:precompile
      timeout-minutes: 10
    - name: Create tmp/pids directory
      run: mkdir -p tmp/pids
    - name: Enable virtual display
      run: Xvfb :99 -screen 0 1024x768x24 &
    - name: Run tests
      run: bundle exec rails test:all
      env:
        DISPLAY: ":99"
    - name: Run javascript tests
      run: bundle exec teaspoon
      env:
        DISPLAY: ":99"
    - name: Upload screenshots
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      if: failure()
      with:
        name: screenshots
        path: tmp/screenshots
        if-no-files-found: ignore
    - name: Report completion to Coveralls
      uses: coverallsapp/github-action@5cbfd81b66ca5d10c19b062c04de0199c215fb6e # v2.3.7
      with:
        github-token: ${{ secrets.github_token }}
        flag-name: ruby-${{ matrix.ruby }}
        format: lcov
        parallel: true
  finish:
    name: Finalise
    needs: test
    runs-on: ubuntu-latest
    timeout-minutes: 1
    steps:
    - name: Report completion to Coveralls
      uses: coverallsapp/github-action@5cbfd81b66ca5d10c19b062c04de0199c215fb6e # v2.3.7
      with:
        github-token: ${{ secrets.github_token }}
        parallel-finished: true
