# The checks every push to main and every pull request runs. Kept small on purpose: one Rust job, the SQL suite on each
# supported Postgres, and the daemon against a real SpiceDB.
name: ci

on:
  push:
    branches: [main]
  pull_request:

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  rust:
    name: cargo test and clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all-targets --locked -- -D warnings
      - run: cargo test --locked

  sql:
    name: SQL suite, Postgres ${{ matrix.pg }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        pg: [14, 15, 16, 17]
    services:
      postgres:
        image: pgvector/pgvector:pg${{ matrix.pg }}
        env:
          POSTGRES_PASSWORD: rebac
        ports: ["55433:5432"]
        options: >-
          --health-cmd "pg_isready -U postgres" --health-interval 1s --health-timeout 5s --health-retries 30
    env:
      REBAC_TEST_URL: postgres://postgres:rebac@127.0.0.1:55433
    steps:
      - uses: actions/checkout@v4
      - run: tests/sql/run.sh
      # Hosted Postgres installs as a database owner that is not a superuser: that must work, with and without pgvector
      # (install_as_owner.sh), and the whole suite must pass as that role (a superuser only creates the extensions).
      - run: tests/sql/install_as_owner.sh
      - name: SQL suite as a non-superuser owner
        run: REBAC_TEST_AS_OWNER=1 tests/sql/run.sh
      # THE REVOCATION RACES, repeated: two sessions (dblink) race a refresh against a revocation in both orders, and
      # a held lock fails the commit (fault injection); 13 races one against a revocation applied from Watch. Deterministic by construction; repeated to catch a regression
      # that only loses sometimes.
      - name: revocation vs refresh races, 20 rounds
        run: for i in $(seq 20); do tests/sql/run.sh '0[69]_*' && tests/sql/run.sh '13_*' || exit 1; done

  integration:
    name: rebacd against SpiceDB
    runs-on: ubuntu-latest
    services:
      postgres:
        image: pgvector/pgvector:pg17
        env:
          POSTGRES_PASSWORD: rebac
        ports: ["55433:5432"]
        options: >-
          --health-cmd "pg_isready -U postgres" --health-interval 1s --health-timeout 5s --health-retries 30
    steps:
      - uses: actions/checkout@v4
      # A service container cannot be given `serve` and its flags, so SpiceDB is started here (the compose file's).
      - name: SpiceDB
        run: |
          docker run -d --name spicedb -p 50061:50051 authzed/spicedb:v1.56.2 \
            serve --grpc-preshared-key rebac-test-key --datastore-engine memory
          for i in $(seq 60); do
            docker exec spicedb grpc_health_probe -addr=:50051 >/dev/null 2>&1 && exit 0
            sleep 1
          done
          docker logs spicedb; exit 1
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: REBAC_IT=1 cargo test --locked -p rebacd --test integration

  # THE QUICKSTART, both ways: the one-file demo pgrebac.com serves (released image and SQL), which must be what
  # examples/quickstart/ would generate, and the from-clone demo with rebacd built from this commit. Each must show bob
  # one row of five after retrieval and the revocation in force at commit.
  quickstart:
    name: quickstart, served and from a clone
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: examples/quickstart/bundle.sh --check
      - name: The served file, piped as a reader runs it
        run: |
          cd "$(mktemp -d)"
          docker compose -f - run --rm demo < "$GITHUB_WORKSPACE/site/public/quickstart.yml" 2>&1 | tee out
          "$GITHUB_WORKSPACE/examples/quickstart/check-output.sh" out
          docker compose -f - down -v < "$GITHUB_WORKSPACE/site/public/quickstart.yml"
      - name: From a clone
        run: |
          docker compose -f examples/quickstart/docker-compose.yml run --rm demo 2>&1 | tee out
          examples/quickstart/check-output.sh out
      - name: rebacd's log, on failure
        if: failure()
        run: docker compose -f examples/quickstart/docker-compose.yml logs rebacd | tail -100
