version: 2

aliases:
  - &restore_yarn_cache
    name: Restore yarn cache
    keys:
      - yarn-packages-{{ checksum "yarn.lock" }}
      - yarn-packages-
  - &save_yarn_cache
    name: Save yarn cache
    paths:
      - ~/.cache/yarn
    key: yarn-packages-{{ checksum "yarn.lock" }}
  - &filter_master
    branches:
      only: master
  - &attach
    at: ~/project

jobs:
  build:
    docker:
      - image: circleci/node:10.19.0
      - image: qlikcore/engine:12.612.0
        command: -S AcceptEULA=yes
      - image: browserless/chrome:1.28.0-puppeteer-1.20.0

    working_directory: ~/project

    steps:
      - checkout

      - restore_cache: *restore_yarn_cache

      - run:
          name: Install dependencies
          command: yarn install --frozen-lockfile

      - save_cache: *save_yarn_cache

      - run:
          name: Build
          command: yarn run build

      - run:
          name: Lint
          command: yarn run lint

      - run:
          name: Locale
          command: yarn run locale:verify

      - run:
          name: Unit tests
          command: |
            yarn run test:unit --mocha.bail false --mocha.reporter mocha-junit-reporter --mocha.reporterOptions.mochaFile ./coverage/junit/junit.xml
            if [ ! -z "$COVERALLS_REPO_TOKEN" ]; then
              echo "Uploading coverage results to coveralls.io..."
              cat ./coverage/unit/lcov.info | npx coveralls
            fi

      - store_test_results:
          path: coverage/junit

      - run:
          name: Import app
          command: |
            DocId=$(curl -s --data-binary @commands/serve/data/apps/ctrl00.qvf http://localhost:9076/v1/apps/import | jq -r '.attributes.id')
            echo "export DOC_ID=$DocId" >> $BASH_ENV

      - run:
          name: Test component
          command: yarn run test:component --chrome.browserWSEndpoint "ws://localhost:3000" --no-launch

      - run:
          name: Test mashup
          command: yarn run test:mashup --chrome.browserWSEndpoint "ws://localhost:3000" --no-launch

      - run:
          name: Test integration
          command: APP_ID=$DOC_ID yarn run test:integration --chrome.browserWSEndpoint "ws://localhost:3000" --no-launch

      # - run:
      #     name: Create a hello supernova project from latest release
      #     command: |
      #       set -e
      #       npx nebula create temp/hello --pkgm yarn --picasso none
      #       cd temp/hello
      #       APP_ID=$DOC_ID yarn run test:integration --chrome.browserWSEndpoint "ws://localhost:3000" --no-launch

      - run:
          name: Create a picasso barchart supernova project from current commit
          command: |
            set -e
            ./commands/cli/lib/index.js create generated/barchart --picasso barchart --no-install
            yarn
            cd generated/barchart
            yarn run build
            APP_ID=$DOC_ID yarn run test:integration --mocha.timeout 30000 --chrome.browserWSEndpoint "ws://localhost:3000" --no-launch

      - store_artifacts:
          path: generated/barchart/screenshots

      - store_artifacts:
          path: test/mashup/__artifacts__
