name: Downstream Project - SPL

on:
  push:
    branches:
      - master
      - v[0-9]+.[0-9]+
  pull_request:
    branches:
      - master
      - v[0-9]+.[0-9]+
    paths:
      - "**.rs"
      - "rust-toolchain.toml"
      - "Cargo.toml"
      - "Cargo.lock"
      - "platform-tools-sdk/cargo-build-sbf"
      - "platform-tools-sdk/cargo-test-sbf"
      - "ci/downstream-projects/run-spl.sh"
      - ".github/workflows/downstream-project-spl.yml"
      - ".github/scripts/downstream-project-spl-common.sh"
      - ".github/scripts/downstream-project-spl-install-deps.sh"
  workflow_call:
    inputs:
      branch:
        required: false
        type: string
        default: "master"

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  SHELL: /bin/bash
  SCCACHE_GHA_ENABLED: "true"
  RUSTC_WRAPPER: "sccache"

jobs:
  check:
    #if: github.repository == 'anza-xyz/agave'
    if: false
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      matrix:
        programs:
          - associated-token-account
          - feature-proposal
          - instruction-padding
          - memo
          - record
          - single-pool
          - slashing
          - stake-pool
          - token-2022
          # re-enable with https://github.com/buffalojoec/mollusk/pull/74
          # - token
    steps:
      - uses: actions/checkout@v5

      - shell: bash
        run: |
          .github/scripts/purge-ubuntu-runner.sh

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

      - shell: bash
        run: |
          source .github/scripts/downstream-project-spl-install-deps.sh
          source .github/scripts/downstream-project-spl-common.sh "${{ matrix.programs }}"
          if [ -n "$SKIP_SPL_DOWNSTREAM_PROJECT_TEST" ]; then
            exit 0
          fi
          cargo check

  test_cli:
    #if: github.repository == 'anza-xyz/agave'
    if: false
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      matrix:
        programs:
          - single-pool
          - token-2022
    steps:
      - uses: actions/checkout@v5

      - shell: bash
        run: |
          .github/scripts/purge-ubuntu-runner.sh

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

      - shell: bash
        run: |
          source .github/scripts/downstream-project-spl-install-deps.sh
          source .github/scripts/downstream-project-spl-common.sh "${{ matrix.programs }}"
          if [ -n "$SKIP_SPL_DOWNSTREAM_PROJECT_TEST" ]; then
            exit 0
          fi
          $CARGO_BUILD_SBF --manifest-path program/Cargo.toml
          cargo test --manifest-path clients/cli/Cargo.toml

  cargo-test-sbf:
    #if: github.repository == 'anza-xyz/agave'
    if: false
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      matrix:
        programs:
          - associated-token-account
          - feature-proposal
          - instruction-padding
          - memo
          - record
          - single-pool
          - slashing
          - stake-pool
          - token-2022
          # re-enable with https://github.com/buffalojoec/mollusk/pull/74
          # - token
    steps:
      - uses: actions/checkout@v5

      - shell: bash
        run: |
          .github/scripts/purge-ubuntu-runner.sh

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

      - name: Install dependencies
        shell: bash
        run: |
          source .github/scripts/downstream-project-spl-install-deps.sh
          source .github/scripts/downstream-project-spl-common.sh "${{ matrix.programs }}"
          if [ -n "$SKIP_SPL_DOWNSTREAM_PROJECT_TEST" ]; then
            exit 0
          fi

      - name: Test SBPFv0
        shell: bash
        run: |
          source ci/downstream-projects/common.sh
          cd "${{ matrix.programs }}"
          $CARGO_BUILD_SBF --arch v0 --manifest-path program/Cargo.toml
          RUSTFLAGS="-Awarnings" SBF_OUT_DIR="../target/deploy"  cargo test --features test-sbf --manifest-path program/Cargo.toml

      - name: Test SBPFv1
        shell: bash
        run: |
          source ci/downstream-projects/common.sh
          cd "${{ matrix.programs }}"
          rm -rf target/deploy target/sbpf*
          $CARGO_BUILD_SBF --arch v1 --manifest-path program/Cargo.toml
          RUSTFLAGS="-Awarnings" SBF_OUT_DIR="../target/deploy"  cargo test --features test-sbf --manifest-path program/Cargo.toml

      - name: Test SBPFv2
        shell: bash
        run: |
          source ci/downstream-projects/common.sh
          cd "${{ matrix.programs }}"
          rm -rf target/deploy target/sbpf*
          $CARGO_BUILD_SBF --arch v2 --manifest-path program/Cargo.toml
          RUSTFLAGS="-Awarnings" SBF_OUT_DIR="../target/deploy"  cargo test --features test-sbf --manifest-path program/Cargo.toml
