version: 2

jobs:
  test_web:
    working_directory: ~/blockstack-app
    docker:
      - image: circleci/node:12.16.1
    steps:
      - checkout
      - run:
          name: Combine package.json files to single file
          command: npx lerna list -p -a | awk -F packages '{printf "\"packages%s/package.json\" ", $2}' | xargs cat > combined-yarn-lock.txt
      - restore_cache:
          # https://circleci.com/docs/2.0/caching/
          keys:
            - yarn-packages-{{ checksum "package.json" }}-{{ checksum "combined-yarn-lock.txt" }}
            - yarn-packages
      - run:
          name: Install Dependencies
          command: yarn --frozen-lockfile
      - run:
          name: Install package dependencies
          command: yarn lerna bootstrap
      - save_cache:
          key: yarn-packages-{{ checksum "package.json" }}-{{ checksum "combined-yarn-lock.txt" }}
          paths:
            - ./.cache/yarn
      - run:
          name: Update apt-get
          working_directory: /
          command: |
            sudo apt-get update -y
      - run:
          name: Install Chrome headless dependencies
          working_directory: /
          command: |
            sudo apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \
              libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \
              libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 \
              libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \
              ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
      # - run:
      #     name: Install webkit dependencies
      #     working_directory: /
      #     command: sudo apt-get install -yq libwoff1 libopus0 libwebp6 libwebpdemux2 libenchant1c2a libgudev-1.0-0 libsecret-1-0 libhyphen0 libgdk-pixbuf2.0-0 libegl1 libgles2 libevent-2.1-6 libnotify4 libxslt1.1
      - run:
          name: Build extension
          command: yarn lerna run prod:ext && zip -r extension.zip ./packages/app/dist
      - run:
          name: test server
          command: yarn lerna run dev --scope @blockstack/app
          background: true
      - run:
          name: Run test app
          command: yarn lerna run dev --scope test-app --stream
          background: true
      - run:
          name: test
          command: yarn lerna run test --scope @blockstack/app --stream
      # https://circleci.com/docs/2.0/artifacts/ 
      - store_artifacts:
          path: packages/app/tests/screenshot.png
      - store_artifacts:
          path: extension.zip


workflows:
  version: 2
  test:
    jobs:
      - test_web
