name: Tests

on:
  push:
    paths:
      - ".cargo/*.toml"
      - ".github/workflows/*.yaml"
      - "Cargo.*"
      - "mutants_attrs/**"
      - "**/src/**"
      - "**/tests/**"

# see https://matklad.github.io/2021/09/04/fast-rust-builds.html
env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  CI: 1
  RUST_BACKTRACE: short
  RUSTUP_MAX_RETRIES: 10
  CARGO_MUTANTS_MINIMUM_TEST_TIMEOUT: 60
  RUSTFLAGS: "-D warnings"
  RUSTDOCFLAGS: "-D warnings"
  SURE_BASE_URL: "http://localhost:3000"
  SURE_TOKEN: "df6d599619c456672fc0594df18c322874a3fa61dca71196e50dca6871c54d5a"

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    concurrency:
      group: ci-${{ github.repository }}-${{ github.ref }}
      cancel-in-progress: true

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: sure_user
          POSTGRES_PASSWORD: sure_password
          POSTGRES_DB: sure_production
        options: >-
          --health-cmd "pg_isready -U sure_user -d sure_production"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

      redis:
        image: redis:latest
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 6379:6379

      web:
        image: ghcr.io/josiahbull/sure:stable
        env:
          POSTGRES_USER: sure_user
          POSTGRES_PASSWORD: sure_password
          POSTGRES_DB: sure_production
          SECRET_KEY_BASE: a7523c3d0ae56415046ad8abae168d71074a79534a7062258f8d1d51ac2f76d3c3bc86d86b6b0b307df30d9a6a90a2066a3fa9e67c5e6f374dbd7dd4e0778e13
          SELF_HOSTED: "true"
          RAILS_FORCE_SSL: "false"
          RAILS_ASSUME_SSL: "false"
          DB_HOST: postgres
          DB_PORT: 5432
          REDIS_URL: redis://redis:6379/1
          OPENAI_ACCESS_TOKEN: "DISABLED"
          SETUP_ADMIN_EMAIL: test@example.com
          SETUP_ADMIN_PASSWORD: TestPass123
          SETUP_API_KEY: df6d599619c456672fc0594df18c322874a3fa61dca71196e50dca6871c54d5a
        ports:
          - 3000:3000

    # Setup Rust
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          components: rustfmt, clippy, rust-docs

      - uses: Swatinem/rust-cache@v2
        with:
          cache-all-crates: true
          cache-bin: true
          cache-targets: true

      - name: Show Cargo and rustc version
        run: |
          cargo --version
          rustc --version

      - name: Rustfmt
        run: cargo fmt --all -- --check

      - name: Test
        run: cargo test --all-targets --all-features --workspace

      - name: Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Try building the docs
        run: cargo doc

      - name: Check semver
        uses: obi1kenobi/cargo-semver-checks-action@v2

      # - name: Install cargo-mutants
      #   run: cargo install cargo-mutants

      # - run: cargo mutants -vV --in-place --all-features
      # - uses: actions/upload-artifact@v4
      #   with:
      #     name: mutants-out
      #     path: mutants.out
