version: 2.1
commands:
  install:
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-deps-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - v1-deps-{{ .Branch }}
            - v1-deps
      - run: yarn install --frozen-lockfile
      - save_cache:
          key: v1-deps-{{ .Branch }}-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache
  cypress:
    parameters:
      language:
        type: enum
        enum: [fr, en]
        default: fr
      integration_folder:
        type: enum
        enum: [mon-entreprise, external, publi.codes]
        default: mon-entreprise
      base_url:
        type: string
        default: https://mon-entreprise.fr
    steps:
      - run: |
          cd mon-entreprise
          CYPRESS_baseUrl=<< parameters.base_url >> yarn run cypress run --record --key 21660df5-36a5-4c49-b23d-801799b0c759 --env language=<< parameters.language >> --config integrationFolder=cypress/integration/<< parameters.integration_folder >>

jobs:
  lint:
    docker:
      - image: node:12.16.1-buster
    steps:
      - install
      - run: |
          yarn lint:eslintrc
          yarn lint:eslint --quiet
          yarn lint:prettier
  type-check:
    docker:
      - image: node:12.16.1-buster
    steps:
      - install
      - run: yarn test:type
  i18n-check:
    docker:
      - image: node:12.16.1-buster
    steps:
      - install
      - run: |
          cd mon-entreprise
          yarn run i18n:rules:check
          yarn run i18n:ui:check

  unit-test:
    docker:
      - image: node:12.16.1-buster
    steps:
      - install
      - run: git config --global core.quotepath false
      - run: yarn test
      - run: yarn test:regressions

  end-to-end-test:
    docker:
      - image: cypress/base:12.16.1
        environment:
          TERM: xterm
    steps:
      - install
      - run: export NODE_OPTIONS="--max-old-space-size=4096" && yarn workspace mon-entreprise build:dev
      - run:
          command: yarn workspace mon-entreprise serve:dev
          background: true
      - cypress:
          base_url: http://localhost:5000
      - cypress:
          base_url: http://localhost:5001
          language: en
      - cypress:
          base_url: http://localhost:5002
          integration_folder: publi.codes

  production-end-to-end-test:
    docker:
      - image: cypress/base:12.16.1
        environment:
          TERM: xterm
    parallelism: 3
    steps:
      - install
      - cypress:
          base_url: https://mon-entreprise.fr
      - cypress:
          base_url: https://mycompanyinfrance.fr
          language: en
      - cypress:
          integration_folder: external
      - cypress:
          base_url: https://publi.codes
          integration_folder: publi.codes
      - run:
          name: Send email if test failed
          when: on_fail
          command: >
            curl -H 'Content-Type: application/json' --request POST -d '{"success": false}' https://hooks.zapier.com/hooks/catch/3746650/qsp818/;

workflows:
  version: 2
  test:
    jobs:
      - lint
      - type-check
      - i18n-check
      - unit-test
      - end-to-end-test
      - production-end-to-end-test:
          filters:
            branches:
              only: master