name: Test

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - '*'

env:
  TESTING: 1

jobs:
  tslint:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@master
    - name: Install NPM dependencies
      run: npm ci
    - name: Install eslint
      run: npm install eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
    - name: Run ESlint
      run: '"./node_modules/.bin/eslint" "./**/*.ts"'
    - name: Run addons-linter
      run: 'npm run build && npm run pack && "./node_modules/.bin/addons-linter" target/xpi/firefox-latest.xpi' 

  test:
    needs: tslint

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu, macos, windows]
        browser: [firefox, chrome]
        neovim: [stable, nightly]

    runs-on: ${{ matrix.os }}-latest

    steps:
    - name: Checkout
      uses: actions/checkout@master
    - name: Install ffmpeg (Linux)
      if: matrix.os == 'ubuntu'
      run: sudo apt update -y && sudo apt install -y ffmpeg
    - name: Install ffmpeg (MacOs)
      if: matrix.os == 'macos'
      run: curl -JL https://evermeet.cx/ffmpeg/get/zip --output ffmpeg.zip && unzip ffmpeg.zip && echo "$PWD" >> $GITHUB_PATH
    - name: Install ffmpeg (Windows)
      if: matrix.os == 'windows'
      run: choco install ffmpeg
    - name: Install Firefox Dev Edition (Linux)
      if: matrix.browser == 'firefox' && matrix.os == 'ubuntu'
      run: |
        sudo npm install -g get-firefox
        get-firefox --platform linux --branch devedition --extract --target $HOME
        echo "$HOME/firefox" >> $GITHUB_PATH
    - name: Install Firefox Dev Edition (MacOS)
      if: matrix.browser == 'firefox' && matrix.os == 'macos'
      run: |
        brew install --cask homebrew/cask-versions/firefox-developer-edition
        echo "/Applications/Firefox Developer Edition.app/Contents/MacOS/" >> $GITHUB_PATH
    - name: Install Firefox Dev Edition (Windows)
      if: matrix.browser == 'firefox' && matrix.os == 'windows'
      run: |
        choco install firefox-dev --pre
        echo "C:\Program Files\Firefox Dev Edition" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
    - name: Install Neovim
      uses: rhysd/action-setup-vim@v1.2.13
      with:
        neovim: true
        version: ${{ matrix.neovim }}
    - name: Install NPM dependencies
      timeout-minutes: 5
      run: npm ci
    - name: Build Firenvim
      timeout-minutes: 1
      run: npm run webpack -- --env=${{ matrix.browser }}-testing
    - name: Instrument Firenvim (Linux/Firefox/Stable only)
      if: matrix.os == 'ubuntu' && matrix.browser == 'firefox' && matrix.neovim == 'stable'
      run: npm run nyc -- instrument --in-place target/${{ matrix.browser }}
    - name: Pack Firenvim (Firefox)
      if: matrix.browser == 'firefox'
      run: npm run pack
    - name: Install Manifest
      run: npm run install_manifests
    - name: Test (Linux)
      timeout-minutes: 15
      if: matrix.os == 'ubuntu'
      # Multiple attempts to increase FPS to 60 have been made: use libx264,
      # using raw output... Nothing worked. Spending more effort on this will
      # only result in suffering and wasted time.
      run: xvfb-run --auto-servernum -s "-screen 0 920x690x24" sh -c '(sleep 3 && ffmpeg -loglevel error -t 240 -an -video_size 920x690 -framerate 30 -f x11grab -i $DISPLAY video.webm) & npm run jest -- ${{ matrix.browser }}'
    - name: Test (MacOS)
      if: matrix.os == 'macos'
      timeout-minutes: 15
      run: |
        ffmpeg -loglevel warning -t 240 -framerate 30 -f avfoundation -i "0:0" video.webm &
        exit_code=0 && npm run jest -- ${{ matrix.browser }} || exit_code=$? && sleep 20 && exit $exit_code
    - name: Test (Windows)
      if: matrix.os == 'windows'
      timeout-minutes: 15
      run: |
        Start-Job { ffmpeg -loglevel warning -t 240 -framerate 30 -f gdigrab -i desktop D:\a\firenvim\firenvim\video.webm }
        npm run jest -- ${{ matrix.browser }}
        Get-Job | Wait-Job
    - name: Artifact upload (failure only)
      uses: actions/upload-artifact@v2
      if: failure()
      with:
        name: ${{ matrix.os }}-${{ matrix.browser }}-${{ matrix.neovim }}
        path: |
          video.webm
          failures.txt
        retention-days: 2
    - name: Process Coverage Report (Coverage only)
      uses: glacambre/action-compare-coverage@master
      if: matrix.os == 'ubuntu' && matrix.browser == 'firefox' && matrix.neovim == 'stable'
      with:
        github_token: ${{secrets.GITHUB_TOKEN}}
