# Runs unit tests.

name: Unit Tests

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

permissions: {}

jobs:
  test:
    name: Run unit tests and generate report (${{ matrix.version }})
    runs-on: ubuntu-latest
    timeout-minutes: 60 # better fail-safe than the default 360 in github actions
    strategy:
      fail-fast: false
      matrix:
        include:
          - version: "Bitcoin Core 29"
            feature: "29_0"
          - version: "Bitcoin Core 30.2"
            feature: "30_2"
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4
        with:
          persist-credentials: false

      - name: Set up Rust
        run: |
          rustup toolchain install 1.85.0 --profile minimal
          rustup default 1.85.0

      - name: Install latest nextest release
        uses: taiki-e/install-action@58e862542551f667fa44c8a2a4a1d64ad477c96a # v2.75.17
        with:
          tool: nextest

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2
        with:
          cache-on-failure: true

      - name: Run tests
        run: |
          cargo --locked nextest run --no-default-features --features ${{ matrix.feature }}

  doc:
    name: Run doc tests (${{ matrix.version }})
    runs-on: ubuntu-latest
    env:
      RUST_BACKTRACE: 1
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        include:
          - version: "Bitcoin Core 29"
            feature: "29_0"
          - version: "Bitcoin Core 30.2"
            feature: "30_2"
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4
        with:
          persist-credentials: false

      - name: Set up Rust
        run: |
          rustup toolchain install 1.85.0 --profile minimal
          rustup default 1.85.0

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2
        with:
          cache-on-failure: true

      - name: Run doctests
        run: cargo test --doc --no-default-features --features ${{ matrix.feature }}

  unit-success:
    name: Check that unit tests pass
    runs-on: ubuntu-latest
    if: always()
    needs: [test, doc]
    timeout-minutes: 60
    steps:
      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # release/v1
        with:
          jobs: ${{ toJSON(needs) }}
