variables:
  DOCKER_IMAGE: registry.forge.hefr.ch/team-raclette/project-softweng/web-app:latest
  NODE_IMAGE: cypress/included:cypress-14.4.1-node-22.16.0-chrome-137.0.7151.68-1-ff-139.0.1-edge-137.0.3296.62-1

  VUE_APP_REST_USER: $REST_USER
  VUE_APP_REST_PASSWORD: $REST_PASSWORD
  VUE_APP_REST_URL: $REST_URL
  VUE_APP_REST_PAGE: $TEST_PAGE

default:
  image: $DOCKER_IMAGE

stages:
  - web-app-build
  - web-app-tests

web-app-docker-build:
  image: docker:latest
  stage: web-app-build
  services:
    - docker:dind
  script:
    - docker build -t $DOCKER_IMAGE -f web-app/Dockerfile .
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
    - docker push $DOCKER_IMAGE
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_COMMIT_BRANCH == 'main'
    - changes:
        - web-app/*

web-app-unit-tests:
  image: $NODE_IMAGE
  stage: web-app-tests
  script:
    - echo "Running unit tests"
    - cd ./web-app
    - npm install --include=dev
    - npm run test:unit

web-app-e2e-tests:
  image: $NODE_IMAGE
  stage: web-app-tests
  script:
    - export CYPRESS_TEST_URL=$TEST_URL
    - echo "Running e2e tests"
    - apt-get update
    - apt-get install curl
    - cd ./web-app
    - npm install --include=dev
    - echo "Wait web-app app is running"
    - for i in {1..6}; do curl -f $CYPRESS_TEST_URL && break || sleep 5; done
    - echo "App is running"
    - npm run test:e2e
