name: CI

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

permissions:
  contents: read
  actions: write

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo check --all-targets 
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    env:
      RUST_BACKTRACE: 1
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      # v0_12_2_apis.rs uses 5 shared `static AtomicU32` counters, so parallel
      # test execution race-counts and intermittently fails (PIPELINE_BEGIN_CALLS
      # observes 2 when 1 is expected). Run all tests with --test-threads=1 to
      # serialize across files. Slower but deterministic; matrix already runs
      # two OSes in parallel.
      - run: cargo test --locked --no-fail-fast -- --test-threads=1

  doc:
    name: Doc
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - env:
          RUSTDOCFLAGS: -D warnings
        # Build docs with all features so intra-doc links into cfg-gated items
        # (e.g. JIT-only types behind `feature = "jit"`) resolve.
        run: cargo doc --no-deps --all-features 

  docs-gates:
    name: Docs gates
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: bash tests/docs-target-blank-rel-noopener.sh
      - run: bash tests/docs-no-http-links.sh
      - run: bash tests/docs-no-inline-handlers.sh

  polish-gates:
    name: Polish gates
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: bash tests/man-page-no-trailing-whitespace.sh
      - run: bash tests/man-page-final-newline.sh
      - run: bash tests/man-page-synopsis-section.sh
      - run: bash tests/tests-shell-executable.sh
      - run: bash tests/tests-shell-shebang.sh

  semantic-gates:
    name: Semantic gates
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: bash tests/docs-has-h1.sh
      - run: bash tests/docs-no-deprecated-tags.sh
      - run: bash tests/docs-no-placeholder-href.sh

  newline-gates:
    name: Newline + README gates
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: bash tests/readme-final-newline.sh
      - run: bash tests/docs-final-newline.sh
      - run: bash tests/workflow-final-newline.sh
      - run: bash tests/cargo-final-newline.sh
      - run: bash tests/readme-has-https-link.sh

  structure-gates:
    name: Structure gates
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: bash tests/readme-has-h2-section.sh
      - run: bash tests/readme-has-badges.sh
      - run: bash tests/docs-has-html-closing.sh
      - run: bash tests/docs-has-body-tag.sh
      - run: bash tests/workflow-no-tabs.sh
