name: Qdrant Edge Tests

on:
  push:
    branches: [ master, dev ]
  pull_request:
    branches: [ '**' ]

permissions:
  contents: read

jobs:
  edge-test:
    name: Test Qdrant Edge

    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        # For PRs, only run on ubuntu to save CI time
        os: >-
          ${{
            github.event_name == 'pull_request'
            && fromJSON('["ubuntu-latest"]')
            || fromJSON('["ubuntu-latest", "macos-latest", "windows-latest"]')
          }}

    steps:
      # Building the examples links several large statically-linked binaries,
      # which gets close to the runner's disk limit. Only the fast `rm -rf`
      # based options are enabled to keep this step under a minute.
      - name: Free disk space
        if: runner.os == 'Linux'
        uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
        with:
          android: true
          dotnet: true
          haskell: true
          tool-cache: false
          large-packages: false
          docker-images: false
          swap-storage: false

      - name: Checkout Qdrant
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Install Rust nightly (for fmt)
        uses: dtolnay/rust-toolchain@0f1b44df7e9cbb178d781a242338dfa5e243ad7f # nightly
        with:
          components: rustfmt

      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable
        with:
          components: rustfmt, clippy

      - name: Install mold
        if: runner.os == 'Linux'
        uses: rui314/setup-mold@9c9c13bf4c3f1adef0cc596abc155580bcb04444 # v1

      - name: Enable mold
        if: runner.os == 'Linux'
        run: |
          mkdir .cargo

          cat >.cargo/config.toml <<-EOF
          [target.x86_64-unknown-linux-gnu]
          linker = "clang"
          rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/mold"]
          EOF

      - name: Install Python
        uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
        with:
          python-version: '3.14'

      - name: Install Just
        uses: ./.github/actions/setup-just

      - name: Install uv
        uses: astral-sh/setup-uv@11f9893b081a58869d3b5fccaea48c9e9e46f990 # v8.3.2

      - name: Install tools
        run: |
          uv tool install ast-grep-cli
          uv tool install maturin

      - name: Setup Qdrant
        uses: ./.github/actions/setup-qdrant

      - name: Restore Rust build cache
        uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
        with:
          shared-key: edge
          # Only save the cache on dev; feature-branch caches would just get evicted under the 10 GB budget
          save-if: ${{ github.ref == 'refs/heads/dev' }}

      - name: Prepare data files
        id: rs-prepare-data
        working-directory: lib/edge
        run: just prepare-data

      - name: 🦀 Check Rust
        working-directory: lib/edge
        run: just rs-check

      - name: 🦀 Build Rust examples
        working-directory: lib/edge
        run: just rs-examples-build

      - name: 🦀 Run Rust examples
        working-directory: lib/edge
        run: just rs-examples

      - name: 🐍 Build Python edge
        working-directory: lib/edge/python
        run: just py-build

      - name: 🐍 Run Python examples
        working-directory: lib/edge
        run: just py-examples

      - name: Check for uncommitted changes
        if: always()
        run: git diff --exit-code
