name: CI

on:
  push:
    branches: [master]
  pull_request:

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # bindgen needs libclang to generate the FFI bindings against the vendored C.
      - name: Install build dependencies
        run: sudo apt-get update && sudo apt-get install -y clang libclang-dev

      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

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

      - name: Clippy
        run: cargo clippy --all-targets -- -D warnings

      - name: Doc
        run: cargo doc --no-deps --all-features
        env:
          RUSTDOCFLAGS: -D warnings

      - name: Test
        run: cargo test

      - name: Build CLI
        run: cargo build --features cli

  regression:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install build dependencies
        run: sudo apt-get update && sudo apt-get install -y clang libclang-dev

      - uses: dtolnay/rust-toolchain@stable

      # Fast smoke test (4 cases, ~1s) so an obvious break fails early, before the full ~363-case
      # suite below spends the time to build and run everything.
      - name: Quick regression (fail-early smoke test)
        run: ./tests/quick_regression.sh

      # Builds the reference C binary (espresso-src/Makefile) and the Rust CLI, then asserts the Rust
      # CLI's PLA output is byte-identical to C across every -o {f,fd,fr,fdr} variant.
      - name: Regression (Rust CLI vs C reference)
        run: ./tests/regression_test.sh

      # Proves the Rust CLI at the native width (64-bit on this runner) reproduces the 32-bit C
      # reference's minimised covers byte-identically — the permanent cross-width oracle. The
      # default run above only proves same-width parity; on a 32-bit host this leg degenerates
      # to 32-vs-32.
      - name: Regression (native-width Rust vs 32-bit C reference)
        run: ESPRESSO_REF_BPI=32 ./tests/regression_test.sh
