name: Basic tests

on:
  push:
    branches: [main]
  pull_request:

concurrency:
  group: btest_rust-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  btest_rust:
    runs-on: ubuntu-22.04
    name: Run basic tests and build

    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Setup sccache
        uses: mozilla-actions/sccache-action@v0.0.9
        with:
          version: "v0.10.0"

      - name: Setup sccache environment
        run: |
          echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV
          echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV

      - name: Install dependencies
        run: |
          python -m pip install meson ninja
          cargo install cbindgen
          sudo snap install valgrind --classic
          sudo apt-get update && sudo apt-get install -y libc6-dbg --fix-missing

      - name: Verify shipped C header matches cbindgen output
        run: scripts/regen-capi-headers.sh --check

      - name: Run Rust tests
        run: cargo test

      - name: Build and test FFI bindings
        id: btest_rust
        run: |
          meson setup bbdir \
          -Dwith_tests=False \
          -Dwith_cpp=True \
          -Dwith_examples=True \
          --buildtype=release
          meson test -C bbdir \
          --wrap="valgrind --leak-check=full --show-leak-kinds=definite,indirect,possible --track-origins=yes --error-exitcode=1 --suppressions=$GITHUB_WORKSPACE/valgrind.supp --gen-suppressions=all"
