# This workflow will do a clean install of node dependencies, build the source code and run tests
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: CI Pipeline
run-name: ${{ github.actor }} is running CI pipeline
# trigger build when pushing, or when creating a pull request
on: [push, pull_request]

jobs:
    # Label of the container job
    japa-tests:
        # run build on latest ubuntu
        runs-on: ubuntu-latest

        container: node:16-bullseye
        
        services:
            mydb:
                image: postgres:latest
                container_name: mydb
                env:
                    POSTGRES_USER: alice
                    POSTGRES_PASSWORD: iEx4Vj7zBb6
                    POSTGRES_DB: tethys_dev
                ports:
                    # Maps tcp port 5432 on service container to the host
                    - 5432:5432
                # Set health checks to wait until postgres has started
                options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
                
            
        steps:
            # this will check out the current branch (https://github.com/actions/checkout#Push-a-commit-using-the-built-in-token)
            - name: 1 Check out repository code
              uses: actions/checkout@v3

            # installing Node
            # - name: 2 Use Node.js 16.x
            #   uses: actions/setup-node@v1
            #   with:
            #       # this will use the latest Node 12 version
            #       node-version: 16.x

            # install dependencies using clean install to avoid package lock updates
            # build the project if necessary
            # - run: npm run build
            # - run: cd build

            - name: 3 Install dependencies
              run: npm ci
            # - run: npm ci --omit=dev

            - name: 4 Create .env.test file
              run: cp .env.example .env.test

            - name: 5 connection
              run: echo "DB_CONNECTION=pg" >> .env.test
                  && echo "PG_HOST=172.17.0.2" >> .env.test
                  && echo "PG_PORT=5432" >> .env.test
                  && echo "PG_USER=alice" >> .env.test
                  && echo "PG_PASSWORD=iEx4Vj7zBb6" >> .env.test
                  && echo "PG_DB_NAME=tethys_dev" >> .env.test
                  && echo "NODE_ENV=test" >> .env.test
                  && echo "ASSETS_DRIVER=fake" >> .env.test
                  && echo "SESSION_DRIVER=memory" >> .env.test
                  && echo "HASH_DRIVER=bcrypt" >> .env.test
                  && echo "HOST=127.0.0.1" >> .env.test
                  && echo "PORT=3333" >> .env.test
                  && echo "APP_NAME=AdonisJs" >> .env.test
                  && echo "APP_URL=http://${HOST}:${PORT}" >> .env.test
                  && echo "CACHE_VIEWS=false" >> .env.test
                  && echo "APP_KEY=pfi5N2ACN4tMJ5d8d8BPHfh3FEuvleej" >> .env.test
                  && echo "DRIVE_DISK=local" >> .env.test

            # finally run the tests
            # - run: npm test
            - name: 6 Run tests
              run: npm run test
            #   env:
            #       DB_CONNECTION: pg
            #       PG_HOST: postgres
            #       PG_PORT: 5432
            #       PG_USER: tethys_test
            #       PG_PASSWORD: iEx4Vj7zBb6
            #       PG_DB_NAME: tethys_dev
            #       PORT: 3333
            #       HOST: 0.0.0.0
            #       APP_KEY: somefakekeythatis>16characters
            #       NODE_ENV: test
            #       ASSETS_DRIVER: fake
            #       SESSION_DRIVER: memory
            #       CACHE_VIEWS: false
            #       DRIVE_DISK: local
            # # Save coverage report in Coveralls
            # - name: Coveralls
            #   uses: coverallsapp/github-action@master
            #   with:
            #       github-token: ${{ secrets.GITHUB_TOKEN }}
