name: Chatbot Unit testing

# Only trigger the workflow in the case of a version tag pushed 
on:
  push:
    tags:
      - "v*.*.*"
  #   branches:
  #     - 'rasa'
  # pull_request:
  #   branches:
  #     - 'rasa'

  # Workflow can be run manually with REST API and 'ref' as request body parameters.
  # The reference should be tag name,
  # Reference: https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
  workflow_dispatch:


jobs:
  build:
    name: Run Unittests on the chatbot
    # Builds run on latest Ubuntu instance
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7, 3.8]

    steps:
      # Checks-out repository under $GITHUB_WORKSPACE, so the job can access it
      # We don't use 're: rasa' in this step, refs should be the tags 
      - name: Check Out Repo 
        uses: actions/checkout@v2
      
      # Store the github repository tag
      - uses: actions/checkout@v2
      - name: Set output
        id: vars
        run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
        
      # Set python version
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
          
      # Install chatbot dependencies
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          if [ -f ./production/rasa-server/requirements.txt ]; then pip install -r ./production/rasa-server/requirements.txt; fi
      
      # Download chatbot weights
      - name: Download model weights
        run: |
          mkdir ./production/rasa-server/rasa/models/
          wget -q ftps://nabot.ml/medbot_weights/${{ steps.vars.outputs.tag }}.tar.gz \
          -P ./production/rasa-server/rasa/models/
      
      # Download and extract ConveRT weights
      - name: Download ConveRT weights
        run: |
          mkdir -p ./production/rasa-server/rasa/weights/model 
          wget -q https://github.com/connorbrinton/polyai-models/releases/download/v1.0/model.tar.gz \
          -P ./production/rasa-server/rasa/weights/
          tar -xvzf ./production/rasa-server/rasa/weights/model.tar.gz \
          -C ./production/rasa-server/rasa/weights/model 
          rm ./production/rasa-server/rasa/weights/model.tar.gz
      
      # Copy domain and weight files to the test directory
      - name: Copy domain.yml file and convert weights
        run: |
          cp ./production/rasa-server/rasa/domain.yml .
          cp -r ./production/rasa-server/rasa/weights .
      
      # Run test units
      - name: Test with unittest
        run: |
          python ./development/testcase/simple_test.py
