version: 2
jobs:
  unit-test:
    docker:
      - image: cypress/base:8
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-deps-{{ .Branch }}-{{ checksum "package.json" }}
            - v1-deps-{{ .Branch }}
            - v1-deps
      - run: yarn install --frozen-lockfile
      - save_cache:
          key: v1-deps-{{ .Branch }}-{{ checksum "package.json" }}
          paths:
            - ~/.cache
      - run: |
          yarn run flow-typed install        
          git config --global core.quotepath false
          yarn test

  end-to-end-test:
    docker:
      - image: cypress/base:8
        environment:
          ## this enables colors in the output
          TERM: xterm
          CYPRESS_baseUrl: http://localhost:5000
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-deps-{{ .Branch }}-{{ checksum "package.json" }}
            - v1-deps-{{ .Branch }}
            - v1-deps
      - run: yarn install --frozen-lockfile
      - save_cache:
          key: v1-deps-{{ .Branch }}-{{ checksum "package.json" }}
          paths:
            - ~/.cache
      - run: |
          yarn run compile
          yarn run serve:embauche &
          yarn run cypress run --record --key 21660df5-36a5-4c49-b23d-801799b0c759 --spec 'cypress/integration/**/!(external-integrations.js)' 

  test-production:
    docker:
      - image: cypress/base:8
        environment:
          ## this enables colors in the output
          TERM: xterm
          CYPRESS_baseUrl: http://embauche.beta.gouv.fr
    steps:
      - checkout
      - run: npm install --save-dev cypress
      - run: |
          if ./node_modules/.bin/cypress run --record --key 21660df5-36a5-4c49-b23d-801799b0c759;
          then
            echo "SUCCESS \o/"
          else
            curl -H 'Content-Type: application/json' --request POST -d '{"success": false}' https://hooks.zapier.com/hooks/catch/3746650/qsp818/;
            exit 1;
          fi

workflows:
  version: 2
  test:
    jobs:
      - unit-test
      - end-to-end-test
      - test-production:
          filters:
            branches:
              only: master

experimental:
  notify:
    webhooks:
      - url: https://hooks.zapier.com/hooks/catch/3746650/qsp818/
    branches:
      only:
        - master