name: Floccus

on:
  pull_request:
  push:
    branches:
      - master
      - develop

env:
  APP_NAME: bookmarks

jobs:
  php:
    runs-on: ubuntu-latest

    env:
      SELENIUM_HUB_HOST: hub
      TEST_HOST: nextcloud
      SERVER_BRANCH: ${{ matrix.server-versions }}
      SELENIUM_VERSION: 3.141.59-20200409
      MYSQL_PASSWORD: root

    strategy:
      # do not stop on another job's failure
      fail-fast: false
      matrix:
        floccus-ref: ['master', 'develop']
        node-versions: [13.x]
        server-versions: ['19']
        floccus-adapter:
          - nextcloud-folders
        test-name:
          - standard
          - parallel
        browsers: ['chrome']


    name: floccus@${{matrix.floccus-ref}} ${{matrix.floccus-adapter}}:${{ matrix.test-name}} ${{matrix.browsers}}

    services:
      hub:
        image: selenium/hub:3.141.59-20200409
        ports:
          - 4444:4444
      chrome:
        image: selenium/node-chrome:3.141.59-20200409
        env:
          HUB_HOST: hub
          HUB_PORT: 4444
        options: -v /dev/shm:/dev/shm
      nextcloud:
        image: nextcloud:${{ matrix.server-versions }}
        env:
          NEXTCLOUD_ADMIN_USER: admin
          NEXTCLOUD_ADMIN_PASSWORD: admin
          MYSQL_DATABASE: nextcloud
          MYSQL_USER: root
          MYSQL_PASSWORD: ${{env.MYSQL_PASSWORD}}
          MYSQL_HOST: mysql
          NEXTCLOUD_TRUSTED_DOMAINS: nextcloud
        volumes:
          - /home/runner/work/bookmarks/bookmarks/apps:/var/www/html/custom_apps
        options: --name nextcloud
      mysql:
        image: mariadb:latest
        env:
          MYSQL_ROOT_PASSWORD: ${{env.MYSQL_PASSWORD}}

    steps:
      - name: Checkout floccus
        uses: actions/checkout@v2
        with:
          repository: marcelklehr/floccus
          ref: ${{matrix.floccus-ref}}
          path: floccus

      - name: Checkout bookmarks app
        uses: actions/checkout@v2
        with:
          path: ${{ env.APP_NAME }}
        if: matrix.floccus-adapter == 'nextcloud-folders'

      - name: Enable bookmarks app
        shell: bash
        run: |
          cd ${{ env.APP_NAME }}
          composer install
        if: matrix.floccus-adapter == 'nextcloud-folders'

      - name: Enable bookmarks app
        shell: bash
        run: |
          sudo cp -R ${{ env.APP_NAME }} apps/
          NEXT_WAIT_TIME=0
          until [ $NEXT_WAIT_TIME -eq 25 ] || docker exec --user www-data nextcloud php occ app:enable ${{ env.APP_NAME }}; do
              sleep $(( NEXT_WAIT_TIME++ ))
          done
          [ $NEXT_WAIT_TIME -lt 25 ]
        if: matrix.floccus-adapter == 'nextcloud-folders'

      - name: List apps
        shell: bash
        run: |
          docker exec --user www-data nextcloud php occ app:list

      - name: Enable APCu
        run: |
          docker exec --user www-data nextcloud php occ config:system:set --value "\\OC\\Memcache\\APCu" memcache.local
        if: matrix.floccus-adapter != 'fake'

      - name: Set up node ${{ matrix.node-versions }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-versions }}

      - name: Install dependencies & build
        working-directory: floccus
        run: |
          npm ci
          npm run build-release --if-present

      - name: Run tests
        working-directory: floccus
        env:
          SELENIUM_BROWSER: ${{ matrix.browsers }}
          FLOCCUS_TEST: ${{matrix.floccus-adapter}} ${{ matrix.test-name}}
          GIST_TOKEN: "foo"
        run: |
          npm run test
