# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
---
version: 2.1

workflows:
  build-test-publish:
    jobs:
      - build
      - test:
          name: test-chrome
          browser: chrome
          requires:
            - build
      - test:
          name: test-edge
          browser: edge
          requires:
            - build
      - test:
          name: test-firefox
          browser: firefox
          requires:
            - build

jobs:
  build:
    docker:
      - image: cimg/python:3.12
    resource_class: medium
    steps:
      - checkout
      - inline-orb-uv/setup-uv
      - run:
          name: install cucu with Python 3.12
          command: uv sync -p 3.12 --dev
      - run:
          name: run pre-commit validations
          command: uv run pre-commit run --show-diff-on-failure --from-ref origin/HEAD --to-ref HEAD
      - run:
          name: build the cucu python package
          command: uv build
      - run:
          name: install cucu globally
          command: pip install dist/cucu-*.tar.gz

  test:
    parameters:
      browser:
        type: string
        default: "chrome"
    docker:
      - image: cimg/python:3.12
      - image: selenium/standalone-<<parameters.browser>>:124.0
        environment:
          SE_NODE_MAX_SESSIONS: 12
          SE_NODE_SESSION_TIMEOUT: 300
          SCREEN_WIDTH: 1366
          SCREEN_HEIGHT: 768
    resource_class: xlarge
    steps:
      - setup_remote_docker
      - checkout
      - inline-orb-uv/setup-uv
      - run:
          name: install cucu dependencies
          command: uv sync
      - run:
          name: install test dependencies
          command: |
            set +eo pipefail
            sudo apt-get update
            sudo apt-get install -y expect
      - run:
          name: wait_for_selenium
          # retry connection every 5s x 60 which is a total of 5 minutes
          command: curl --retry 60 --retry-delay 5 --retry-connrefused http://localhost:4444
      - run:
          name: run_functional_tests
          command: uv run cucu run features --workers 6 --selenium-remote-url http://localhost:4444 --generate-report --junit junit_results --browser "<<parameters.browser>>"
          environment:
            COVERAGE_PROCESS_START: pyproject.toml  # set to config file
      - run:
          name: run unit tests
          command: uv run coverage run -m pytest --junit-xml=results/unit-tests.xml
      - run:
          name: code_coverage_check
          command: |
            set +eo pipefail
            uv run coverage combine .coverage.*
            uv run coverage html
            uv run coverage report
            uv run coverage xml
            mv coverage.xml coverage-<<parameters.browser>>.xml
      - persist_to_workspace:
          root: .
          paths:
            - coverage-<<parameters.browser>>.xml
      - run:
          name: tar up results and reporting
          command: |
            tar cvfz results.tgz results
            tar cvfz report.tgz report
            tar cvfz junit_results.tgz junit_results
          when: always
      - store_artifacts:
          path: coverage-<<parameters.browser>>.xml
      - store_artifacts:
          path: results.tgz
      - store_artifacts:
          path: junit_results.tgz
      - store_artifacts:
          path: report.tgz
      - store_test_results:
          path: junit_results

orbs:
  inline-orb-uv:
    commands:
      setup-uv:
        steps:
          - restore_cache:
              keys:
                - v1-uv-cache-{{ checksum "uv.lock" }}
          - run:
              name: Install uv
              command: |
                curl -LsSf https://astral.sh/uv/install.sh | sh
                echo 'source $HOME/.cargo/env' >> $BASH_ENV
                source $BASH_ENV
          - save_cache:
              paths:
                - $HOME/.cargo/env
              key: v1-uv-cache-{{ checksum "uv.lock" }}
          - run:
              name: Verify uv installation
              command: uv --version
