name: CI

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

# Cancel superseded PR runs; never cancel a main run mid-flight.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - run: cargo fmt --all --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all-targets --all-features -- -D warnings

  test:
    name: Integration tests
    runs-on: ubuntu-latest
    env:
      RUN_INTEGRATION_TESTS: "1"
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - uses: actions/setup-go@v6
        with:
          go-version: stable
      - name: Install IRMA server
        env:
          IRMAGO_VERSION: v0.19.2
        run: |
          mkdir -p "$RUNNER_TEMP/irma-bin"
          echo "$RUNNER_TEMP/irma-bin" >> "$GITHUB_PATH"
          curl -fsSL "https://github.com/privacybydesign/irmago/releases/download/${IRMAGO_VERSION}/irma-linux-amd64" \
            -o "$RUNNER_TEMP/irma-bin/irma"
          chmod +x "$RUNNER_TEMP/irma-bin/irma"
      - name: Set up IRMA configuration
        # Pass explicit scheme URLs rather than relying on the binary's
        # compiled-in defaults: irmago < ~v0.13 pointed at the now-defunct
        # privacybydesign.foundation host, and pinning the URL here keeps
        # CI independent of any future default-URL drift.
        run: |
          mkdir -p temp_testing/client temp_testing/irma_configuration
          irma scheme download temp_testing/irma_configuration \
            https://schemes.yivi.app/irma-demo \
            https://schemes.yivi.app/pbdf
      - name: Build test client emulator
        working-directory: test_tools/client_emulator
        run: go build
      - name: Run tests
        run: cargo test --all-features
