on:
  push:
    branches:
      - master
    paths-ignore:
      - "**.md"
      - "!.github/workflows/*.yml"
      - "!rln-wasm/**"
      - "!rln/src/**"
      - "!rln/resources/**"
      - "!utils/src/**"
  pull_request:
    paths-ignore:
      - "**.md"
      - "!.github/workflows/*.yml"
      - "!rln-wasm/**"
      - "!rln/src/**"
      - "!rln/resources/**"
      - "!utils/src/**"

name: Tests

jobs:
  utils-test:
    strategy:
      matrix:
        platform: [ ubuntu-latest, macos-latest ]
        crate: [ utils ]
    runs-on: ${{ matrix.platform }}
    timeout-minutes: 60

    name: test - ${{ matrix.crate }} - ${{ matrix.platform }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v3
      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - uses: Swatinem/rust-cache@v2
      - name: Install dependencies
        run: make installdeps
      - name: cargo-make test
        run: |
          cargo make test --release
        working-directory: ${{ matrix.crate }}

  rln-test:
    strategy:
      matrix:
        platform: [ ubuntu-latest, macos-latest ]
        crate: [ rln ]
        feature: [ "default", "arkzkey", "stateless" ]
    runs-on: ${{ matrix.platform }}
    timeout-minutes: 60

    name: test - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v3
      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - uses: Swatinem/rust-cache@v2
      - name: Install dependencies
        run: make installdeps
      - name: cargo-make test
        run: |
          cargo make test_${{ matrix.feature }} --release
        working-directory: ${{ matrix.crate }}

  rln-wasm:
    strategy:
      matrix:
        platform: [ ubuntu-latest, macos-latest ]
    runs-on: ${{ matrix.platform }}
    timeout-minutes: 60

    name: test - rln-wasm - ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v3
      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          # TODO: Update to stable once wasmer supports it
          toolchain: 1.82.0
          override: true
      - uses: Swatinem/rust-cache@v2
      - name: Install Dependencies
        run: make installdeps
      - name: Install wasm-pack
        uses: jetli/wasm-pack-action@v0.3.0
      - run: cargo make build
        working-directory: rln-wasm
      - run: cargo make test --release
        working-directory: rln-wasm

  lint:
    strategy:
      matrix:
        # we run lint tests only on ubuntu
        platform: [ ubuntu-latest ]
        crate: [ rln, utils ]
    runs-on: ${{ matrix.platform }}
    timeout-minutes: 60

    name: lint - ${{ matrix.crate }} - ${{ matrix.platform }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v3
      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - name: Install Dependencies
        run: make installdeps
      - name: cargo fmt
        if: success() || failure()
        run: cargo fmt -- --check
        working-directory: ${{ matrix.crate }}
      - name: cargo clippy
        if: success() || failure()
        run: |
          cargo clippy --release -- -D warnings
        working-directory: ${{ matrix.crate }}
        # We skip clippy on rln-wasm, since wasm target is managed by cargo make
        # Currently not treating warnings as error, too noisy
        # -- -D warnings

  benchmark-utils:
    # run only in pull requests
    if: github.event_name == 'pull_request'
    strategy:
      matrix:
        # we run benchmark tests only on ubuntu
        platform: [ ubuntu-latest ]
        crate: [ utils ]
    runs-on: ${{ matrix.platform }}
    timeout-minutes: 60

    name: benchmark - ${{ matrix.platform }} - ${{ matrix.crate }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v3
      - uses: Swatinem/rust-cache@v2
      - uses: boa-dev/criterion-compare-action@v3
        with:
          branchName: ${{ github.base_ref }}
          cwd: ${{ matrix.crate }}

  benchmark-rln:
    # run only in pull requests
    if: github.event_name == 'pull_request'
    strategy:
      matrix:
        # we run benchmark tests only on ubuntu
        platform: [ ubuntu-latest ]
        crate: [ rln ]
        feature: [ "default", "arkzkey" ]
    runs-on: ${{ matrix.platform }}
    timeout-minutes: 60

    name: benchmark - ${{ matrix.platform }} - ${{ matrix.crate }} - ${{ matrix.feature }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v3
      - uses: Swatinem/rust-cache@v2
      - uses: boa-dev/criterion-compare-action@v3
        with:
          branchName: ${{ github.base_ref }}
          cwd: ${{ matrix.crate }}
          features: ${{ matrix.feature }}