name: e2e-tests

on:
  push:
    branches: [ develop, main ]
  pull_request:
    branches: [ develop, main ]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x]

    steps:
    - uses: actions/checkout@v2
      with:
        path: '.sdk'

    - name: Checkout demo-application repo
      uses: actions/checkout@v2
      with:
        repository: causalfoundry/js-sdk-wrapper
        ref: main
        ssh-key: ${{ secrets.DEMO_APP_SSH_KEY }}
        path: '.app'

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}

    - name: Install library dependencies
      run: |
        cd $GITHUB_WORKSPACE/.sdk
        npm install

    - name: Build library
      run: |
        cd $GITHUB_WORKSPACE/.sdk
        npm run build


    - name: Update package to use local library
      run: |
        node $GITHUB_WORKSPACE/.sdk/scripts/updatePackageForLocalLib.js $GITHUB_WORKSPACE/.app/package.json $GITHUB_WORKSPACE/.sdk

    - name: Install library dependencies
      run: |
        cd $GITHUB_WORKSPACE/.app
        npm install

    - name: Run webserver
      run: |
        cd $GITHUB_WORKSPACE/.app
        npm start &

    - name: Test
      run: |
        cd $GITHUB_WORKSPACE/.app
        CYPRESS_BASE_URL=https://localhost:3000 $(npm bin)/cypress run
