variables:
  - &opencloud_image 'quay.io/opencloudeu/opencloud-rolling:latest'
  - &build_image 'opencloudeu/desktop-client-build:ubuntu-24.04-qt6.10'
  - &minio_image 'minio/mc:RELEASE.2021-10-07T04-19-58Z'
  - &alpine_image 'alpine:3.22.4'
  - &minio_environment
    AWS_ACCESS_KEY_ID:
      from_secret: cache_s3_access_key
    AWS_SECRET_ACCESS_KEY:
      from_secret: cache_s3_secret_key
    CACHE_BUCKET:
      from_secret: cache_s3_bucket
    MC_HOST: 'https://s3.ci.opencloud.eu'
    PUBLIC_BUCKET: public

when:
  - branch:
      - main
      - stable-*
    event:
      - push
      - manual
  - event: pull_request
  - event: tag
  - event: cron
    cron: nightly*

depends_on:
  - build
  - cache-python

workspace:
  base: /woodpecker
  path: desktop

matrix:
  include:
    - MATRIX_NAME: gui-tests-1
      SUITES: 'features/activity features/add-account features/delete-files-folders features/edit-files features/login-logout features/move-files-folders'
    - MATRIX_NAME: gui-tests-2
      SUITES: 'features/remove-account-connection features/spaces features/sync-resources features/tabs-settings features/vfs'

steps:
  - name: restore-python-cache
    image: *minio_image
    environment:
      <<: *minio_environment
    commands:
      - requirements_sha=$(sha1sum test/gui/requirements.txt | cut -d" " -f1)
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - mc cp -a s3/$CACHE_BUCKET/desktop/python-cache/$requirements_sha/python-cache.tar.gz $CI_WORKSPACE

  - name: restore-browsers-cache
    image: *minio_image
    environment:
      <<: *minio_environment
    commands:
      - playwright_version=$(bash test/gui/woodpecker/script.sh get_playwright_version)
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - mc cp -r -a s3/$CACHE_BUCKET/desktop/browsers-cache/$playwright_version/playwright-browsers.tar.gz $CI_WORKSPACE

  - name: unzip-browsers-cache
    image: owncloud/ubuntu:20.04
    commands:
      - cd test/gui
      - tar -xf $CI_WORKSPACE/playwright-browsers.tar.gz -C ./

  - name: install-python-modules
    image: *build_image
    environment:
      PLAYWRIGHT_BROWSERS_PATH: /woodpecker/desktop/test/gui/.playwright
    commands:
      - cd test/gui
      - tar -xf $CI_WORKSPACE/python-cache.tar.gz -C ./
      - python3 -m venv .venv --system-site-packages
      - . .venv/bin/activate
      - make install
      - python3 -m pip list -v

  - name: opencloud
    image: *opencloud_image
    detach: true
    environment:
      FRONTEND_SEARCH_MIN_LENGTH: '2'
      GRAPH_AVAILABLE_ROLES: b1e2218d-eef8-4d4c-b82d-0f1a1b48f3b5,a8d5fe5e-96e3-418d-825b-534dbdf22b99,fb6c3e19-e378-47e5-b277-9732f9de6e21,58c63c02-1d89-4572-916a-870abc5a1b7d,2d00ce52-1fc2-4dbc-8b95-a73b73395f5a,1c996275-f1c9-4e71-abdf-a42f6495e960,312c0871-5ef7-4b3a-85b6-0e4074c64049,aa97fe03-7980-45ac-9e50-b325749fd7e6,63e64e19-8d43-42ec-a738-2b6af2610efa
      IDM_ADMIN_PASSWORD: admin
      LDAP_GROUP_SUBSTRING_FILTER_TYPE: any
      LDAP_USER_SUBSTRING_FILTER_TYPE: any
      OC_INSECURE: true
      OC_JWT_SECRET: some-opencloud-jwt-secret
      OC_LOG_LEVEL: error
      OC_SHOW_USER_EMAIL_IN_RESULTS: true
      OC_URL: https://opencloud:9200
      PROXY_ENABLE_BASIC_AUTH: true
      WEB_UI_CONFIG_FILE: /woodpecker/desktop/test/gui/woodpecker/config-opencloud.json
    commands:
      - opencloud init
      - opencloud server

  - name: wait-for-opencloud
    image: owncloudci/alpine:latest
    commands:
      - timeout 300 bash -c 'while [ $(curl -sk -uadmin:admin https://opencloud:9200/graph/v1.0/users/admin -w %{http_code} -o /dev/null) != 200 ]; do sleep 1; done'

  - name: restore-desktop-client
    image: *minio_image
    environment:
      <<: *minio_environment
    commands:
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - mc cp -a -r s3/$CACHE_BUCKET/desktop/client-build/${CI_COMMIT_SHA}/ $CI_WORKSPACE/build
      - ls -lh $CI_WORKSPACE/build/bin

  - name: gui-tests
    image: *build_image
    environment:
      # system cache environment variables
      PYTHONUSERBASE: /woodpecker/desktop/test/gui/.venv
      PLAYWRIGHT_BROWSERS_PATH: /woodpecker/desktop/test/gui/.playwright
      # webdriver environment variables
      WEBDRIVER_HOST: 0.0.0.0
      WEBDRIVER_PORT: 4723
      WEBDRIVER_RUNNER: /woodpecker/desktop/test/gui/woodpecker/run_atspi_webdriver.sh
      # test environment variables
      APP_PATH: /woodpecker/desktop/build/bin/qsfera
      BACKEND_HOST: https://opencloud:9200
      GUI_TEST_REPORT_DIR: /woodpecker/desktop/test/gui/reports
      BEHAVE_TEST_DIR: /woodpecker/desktop/test/gui
      # Cannot handle this tags format inside a container: --tags='@smoke and not @skip'
      BEHAVE_PARAMETERS: '--tags=@smoke --tags=~@skip ${SUITES}'

  # - name: crash-log
  #   image: *alpine_image
  #   when:
  #     - status:
  #         - success
  #         - failure
  #   commands:
  #     - cat test/gui/tmp/QSfera-crash.log 2>/dev/null || exit 0

  - name: upload-test-reports
    image: *minio_image
    when:
      - status: failure
    environment:
      <<: *minio_environment
    commands:
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - mc cp -a -r $CI_WORKSPACE/test/gui/reports s3/$PUBLIC_BUCKET/desktop/testlogs/$CI_PIPELINE_NUMBER/$MATRIX_NAME

  - name: gui-test-reports
    image: *minio_image
    when:
      - status: failure
    environment:
      <<: *minio_environment
    commands:
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - bash $CI_WORKSPACE/test/gui/woodpecker/gui_test_reports.sh
