name: Checks
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    branches: [main]
  pull_request:

jobs:
  build-and-test:
    timeout-minutes: 15
    strategy:
      matrix:
        os: [ubuntu-24.04, macos-26]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4

      - uses: jdx/mise-action@v2

      - name: Install Rust components
        if: matrix.os == 'ubuntu-24.04'
        run: rustup component add rustfmt clippy

      - uses: Swatinem/rust-cache@v2

      - name: Format
        if: matrix.os == 'ubuntu-24.04'
        run: cargo fmt --check

      - name: Clippy
        if: matrix.os == 'ubuntu-24.04'
        run: cargo clippy --release -- -D warnings

      - name: Build
        run: cargo build --release

      - name: Test
        run: cargo test --release

  integration:
    timeout-minutes: 15
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - uses: jdx/mise-action@v2

      - uses: Swatinem/rust-cache@v2

      - name: Build
        run: cargo build --release

      - name: Install kap
        run: cargo install --path . --force

      - name: Build sidecar image
        run: docker build --target proxy -t kap-test -f .devcontainer/Dockerfile .

      - name: Set up test environment
        run: tests/integration/setup.sh

      - name: Run integration tests
        run: tests/integration/run.sh

      - name: Container logs
        if: failure()
        run: |
          DC_DIR=tests/integration/project/.devcontainer
          docker compose --project-name kap-integration \
            -f $DC_DIR/docker-compose.yml -f $DC_DIR/docker-compose.kap.yml \
            logs --no-color

      - name: Teardown
        if: always()
        run: tests/integration/teardown.sh
