name: Tests

env:
  CARGO_TERM_COLOR: always

on:
  push:
    branches: ["main"]
  merge_group:
  pull_request:
  schedule:
    - cron: "32 4 * * 5"
  workflow_dispatch:

jobs:
  test:
    strategy:
      matrix:
        os: [ubuntu-latest]
        rust: ["1.89", stable, beta]

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v6
      - name: Install rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust }}
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2

      - name: Run fmt
        run: cargo fmt --check

      - name: Run clippy
        run: cargo clippy --all-targets -- -D warnings && cargo clippy --all-targets --all-features -- -D warnings

      - name: Run tests
        run: cargo test --all-targets && cargo test --all-targets --all-features

      - name: Run doctests
        run: cargo test --doc && cargo test --doc --all-features

      - name: Run docs
        run: cargo doc --no-deps -p rootcause-internals -p rootcause --all-features

      - name: Run cargo-deny
        uses: EmbarkStudios/cargo-deny-action@v2
        with:
          rust-version: ${{ matrix.rust }}

  miri:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Install rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: nightly
          components: miri rust-src
      - uses: Swatinem/rust-cache@v2

      - name: Run tests with miri
        run: |
          export MIRIFLAGS="-Zmiri-strict-provenance -Zmiri-isolation-error=warn"

          # Always run regular tests
          cargo miri test --all-targets
          cargo miri test --all-features --all-targets

          # Always run basic doctests
          cargo miri test --doc

          # Only run rootcause doctests with all features on scheduled runs or manual dispatch (slow due to backtraces)
          if [ "${{ github.event_name }}" = "schedule" ] || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "Running rootcause doctests with all features (scheduled/manual run)"
            cargo miri test --doc --all-features
          else
            echo "Skipping rootcause doctests with all features (too slow for push/PR events)"
          fi
