parameters:
  cache-version:
    type: string
    default: << pipeline.parameters.cache-version >>
  cypress-path:
    type: string
    default: ./cypress
  api-mock-path:
    type: string
    default: ./api-mock
  inject-env-variables-script-path:
    type: string
    default: ./scripts/inject-env-variables.js
  build-path:
    type: string
    default: ./build
  market:
    type: string
    default: fr
  browser:
    type: string
    default: chrome

steps:
  - checkout

  - restore-cache:
      version: << parameters.cache-version >>
      cache-name: dependencies
  - restore-cache:
      version: << parameters.cache-version >>
      cache-name: api-mock-dependencies
      lockfile-parent-path: << parameters.api-mock-path >>
  - restore-cache:
      version: << parameters.cache-version >>
      cache-name: cypress

  - attach_workspace:
      at: .

  - run:
      name: Launch API Mock
      command: yarn --cwd << parameters.api-mock-path >> start-no-watch
      background: true

  - run:
      name: Inject env variables to index.html
      command: << parameters.inject-env-variables-script-path >> << parameters.market >> --destination index.html

  - run:
      name: Serve static
      command: yarn serve -s -S << parameters.build-path >>
      background: true

  - run:
      name: Run cypress on Chrome
      command: yarn cypress run --browser << parameters.browser >>

  - run:
      name: Kill background
      command: killall node

  - store_artifacts:
      path: << parameters.cypress-path >>/videos
      destination: << parameters.cypress-path >>/videos

  - store_artifacts:
      path: << parameters.cypress-path >>/screenshots
      destination: << parameters.cypress-path >>/screenshots
