name: CI

'on':
  pull_request:
    types: [opened, synchronize, reopened]
  workflow_dispatch:

jobs:
  build-test:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    env:
      CARGO_TERM_COLOR: always
      BUILD_PROFILE: debug
      WHITAKER_INSTALLER_REV: f768c2e53c47df13658af1168a67851d388750bf
    steps:
      - uses: actions/checkout@1cce3390c2bfda521930d01229c073c7ff920824
        with:
          persist-credentials: false
      - name: Setup Rust
        uses: leynos/shared-actions/.github/actions/setup-rust@eff100c965da05e14fd4e07d7ea518408b312cb8
      - name: Install mold linker
        if: runner.os == 'Linux'
        run: |
          set -euo pipefail
          export DEBIAN_FRONTEND=noninteractive
          # mold accelerates dev builds; coverage uses lld for llvm-tools compatibility.
          sudo apt-get update \
            && sudo apt-get install --yes --no-install-recommends clang lld mold
      - name: Format
        run: make check-fmt
      - name: Markdown lint
        uses: DavidAnson/markdownlint-cli2-action@c1571a598939a086fb0e1b45429a244e3d24bad4
        with:
          globs: |
            **/*.md
            !**/target/**
            !**/dist/**
      - name: Setup uv
        uses: astral-sh/setup-uv@a92cb430984b4b2b34c9c47e020cf035089958fa
      - name: Install test runner
        run: cargo binstall --no-confirm cargo-nextest
      - name: Cache Whitaker installation
        id: cache-whitaker
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae
        with:
          path: |
            ~/.cargo/bin/whitaker-installer
            ~/.local/bin/whitaker
            ~/.dylint_drivers
            ~/.local/share/whitaker
          key: whitaker-v2-${{ runner.os }}-${{ env.WHITAKER_INSTALLER_REV }}
      - name: Install Whitaker
        run: |
          set -euo pipefail
          echo "Whitaker cache hit: ${{ steps.cache-whitaker.outputs.cache-hit }}"
          if [ "${{ steps.cache-whitaker.outputs.cache-hit }}" != "true" ]; then
            echo "Installing Whitaker installer at ${WHITAKER_INSTALLER_REV}"
            cargo install --locked \
              --git https://github.com/leynos/whitaker \
              --rev "${WHITAKER_INSTALLER_REV}" \
              whitaker-installer
          fi
          whitaker-installer --cranelift
          echo "Whitaker binary: $(command -v whitaker || true)"
      - name: Lint
        run: make lint
      - name: Log coverage linker configuration
        run: |
          echo "Coverage linker: clang"
          echo "Coverage RUSTFLAGS: -C link-arg=-fuse-ld=lld"
          echo "Coverage CFLAGS: -fuse-ld=lld"
          echo "Coverage LDFLAGS: -fuse-ld=lld"
      - name: Test and Measure Coverage
        uses: leynos/shared-actions/.github/actions/generate-coverage@eff100c965da05e14fd4e07d7ea518408b312cb8
        env:
          CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER: clang
          RUSTFLAGS: -C link-arg=-fuse-ld=lld
          CFLAGS: -fuse-ld=lld
          LDFLAGS: -fuse-ld=lld
        with:
          output-path: lcov.info
          format: lcov
      - name: Upload coverage data to CodeScene
        env:
          CS_ACCESS_TOKEN: ${{ secrets.CS_ACCESS_TOKEN }}
        if: ${{ env.CS_ACCESS_TOKEN }}
        uses: leynos/shared-actions/.github/actions/upload-codescene-coverage@eff100c965da05e14fd4e07d7ea518408b312cb8
        with:
          format: lcov
          access-token: ${{ env.CS_ACCESS_TOKEN }}
          installer-checksum: ${{ vars.CODESCENE_CLI_SHA256 }}
