version: 2.1

orbs:
  ruby: circleci/ruby@2.1.4
  browser-tools: circleci/browser-tools@1.4.8

jobs:
  test:
    docker:
      - image: cimg/ruby:3.4.6-browsers
        environment:
          RAILS_ENV: test
          CI: true
          DATABASE_HOST: localhost
          DATABASE_NAME: dawarich_test
          DATABASE_USERNAME: postgres
          DATABASE_PASSWORD: mysecretpassword
          DATABASE_PORT: 5432
      - image: cimg/postgres:13.3-postgis
        environment:
          POSTGRES_USER: postgres
          POSTGRES_DB: dawarich_test
          POSTGRES_PASSWORD: mysecretpassword
      - image: redis:7.0
      - image: selenium/standalone-chrome:latest
        name: chrome
        environment:
          START_XVFB: 'false'
          JAVA_OPTS: -Dwebdriver.chrome.whitelistedIps=

    steps:
      - checkout
      - browser-tools/install-chrome
      - browser-tools/install-chromedriver
      - run:
          name: Install Bundler
          command: gem install bundler
      - run:
          name: Bundle Install
          command: bundle install --jobs=4 --retry=3
      - run:
          name: Wait for Selenium Chrome
          command: |
            dockerize -wait tcp://chrome:4444 -timeout 1m
      - run:
          name: Database Setup
          command: |
            bundle exec rails db:create RAILS_ENV=test
            bundle exec rails db:schema:load RAILS_ENV=test
            # Create the queue database manually if it doesn't exist
            PGPASSWORD=mysecretpassword createdb -h localhost -U postgres dawarich_test_queue || true
      - run:
          name: Run RSpec tests
          command: bundle exec rspec
      - store_artifacts:
          path: coverage
      - store_artifacts:
          path: tmp/capybara

workflows:
  rspec:
    jobs:
      - test
