---
name: Test

on:
  workflow_call:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  format:
    name: Format
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6.0.2
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

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

  lint:
    name: Clippy
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6.0.2
      - uses: dtolnay/rust-toolchain@stable

      - name: Clippy
        run: cargo clippy --locked --all-targets --all-features

  check:
    name: Check
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6.0.2
      - uses: dtolnay/rust-toolchain@stable

      - name: Check
        run: cargo check --locked

  test:
    name: Test
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-24.04
          - ubuntu-24.04-arm
          - macos-15
        rust:
          - stable
    runs-on: ${{ matrix.os }}
    needs:
      - format
      - lint
      - check
    steps:
      - uses: actions/checkout@v6.0.2
      - uses: dtolnay/rust-toolchain@stable

      - name: test
        run: cargo test --locked

  explain-safety:
    name: Explain Safety (PG${{ matrix.postgres }})
    runs-on: ubuntu-24.04
    needs:
      - format
      - lint
      - check
    strategy:
      fail-fast: false
      matrix:
        postgres:
          - 14
          - 15
          - 16
          - 17
          - 18
    services:
      postgres:
        image: postgres:${{ matrix.postgres }}
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      PGMON_TEST_DSN: postgresql://postgres:postgres@localhost:5432/postgres
    steps:
      - uses: actions/checkout@v6.0.2
      - uses: dtolnay/rust-toolchain@stable

      - name: explain safety
        run: cargo test --locked --all-features explain_safety
