name: tests

permissions:
  contents: read

on:
  pull_request:
  merge_group:
  push:
    branches: [main]

env:
  CARGO_TERM_COLOR: always

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  cargo-next-test:
    name: Cargo test
    runs-on: ubuntu-latest

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.13.2
        with:
          egress-policy: audit

      - name: Fetch Repository
        uses: actions/checkout@71cf2267d89c5cb81562390fa70a37fa40b1305e

      - name: Install stable toolchain
        uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2

      - name: Install Foundry
        uses: foundry-rs/foundry-toolchain@50d5a8956f2e319df19e6b57539d7e2acb9f8c1e # v1.5.0

      - name: Cache cargo-nextest binary
        id: cache-cargo-nextest
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: ~/.cargo/bin/cargo-nextest
          key: ${{ runner.os }}-cargo-nextest-${{ hashFiles('**/Cargo.lock') }}

      - name: Install cargo-nextest
        if: steps.cache-cargo-nextest.outputs.cache-hit != 'true'
        uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49
        with:
          tool: cargo-nextest

      - name: Cargo test
        run: cargo nextest run --locked --all-targets --all-features --no-tests=pass --no-fail-fast

        # https://github.com/rust-lang/cargo/issues/6669
      - name: Run doc tests
        run: cargo test --locked --doc --all-features --no-fail-fast
