name: Tests Suite

on:
  merge_group:
  pull_request:
  push:
    branches:
      - main

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  test:
    name: Runs "cargo test"
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: nightly

      - name: Cache .cargo
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: cargo-test-${{ hashFiles('**/Cargo.toml') }}

      - name: Install grcov
        run: |
          rustup component add llvm-tools-preview
          cargo install grcov --version 0.8.20

      - name: Run tests
        run: cargo test --all-features
        env:
          RUSTFLAGS: "-Cinstrument-coverage"

      - name: Create coverage report
        run: |
          grcov . \
            --binary-path ./target/debug/deps/ \
            --source-dir . \
            --excl-start 'mod test* \{' \
            --ignore '*test*' \
            --ignore "*test.rs" \
            --ignore "*main.rs" \
            --ignore "*steps.rs" \
            --ignore "*tests.rs" \
            --ignore "*github.com*" \
            --ignore "*libcore*" \
            --ignore "*rustc*" \
            --ignore "*liballoc*" \
            --ignore "*cargo*" \
            -t coveralls+ \
            --token ? \
            -o ./cov_normal.json

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v6
        with:
          files: ./cov_normal.json
          verbose: true
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
