name: Build and Test

on:
  push:
    branches: [master, main]
  pull_request:
    branches: [master, main]
  workflow_dispatch:

jobs:
  test-frontend:
    if: ${{ !startsWith(gitea.event.head_commit.message, 'chore: release v') }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "npm"
          cache-dependency-path: frontend/package-lock.json

      - name: Install deps
        run: |
          cd frontend
          npm ci

      - name: Svelte check
        run: |
          cd frontend
          npm run check

      - name: Build
        run: |
          cd frontend
          npm run build

  test-backend:
    if: ${{ !startsWith(gitea.event.head_commit.message, 'chore: release v') }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      # Editable installs of packages/core + packages/server are extremely slow
      # on the hosted runner — measured 4-6x slower than building wheels first
      # because hatchling's editable hook re-resolves on every collection. We
      # build wheels once into an isolated venv, then install them (and only
      # the test deps). The venv isolation also prevents broken-wheel installs
      # from leaking dist-info across runs on the persistent Gitea runner
      # (pip can't uninstall a wheel that landed without a RECORD file). The
      # wheels themselves are NOT cached because their hashes depend on every
      # file under packages/ — invalidates on basically every PR. Pip's HTTP
      # cache for the test deps is enough.
      - name: Build wheels in isolated venv
        run: |
          python -m venv /tmp/venv
          /tmp/venv/bin/pip install --upgrade pip build
          mkdir -p /tmp/wheels
          /tmp/venv/bin/pip wheel --no-deps -w /tmp/wheels packages/core packages/server

      - name: Install backend + test deps
        run: |
          /tmp/venv/bin/pip install /tmp/wheels/*.whl pytest pytest-asyncio httpx aioresponses prometheus_client

      - name: Run pytest
        env:
          NOTIFY_BRIDGE_DATA_DIR: /tmp/nb-test-data
          NOTIFY_BRIDGE_SECRET_KEY: ci-secret-key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          NOTIFY_BRIDGE_DEBUG: "false"
          NOTIFY_BRIDGE_CORS_ALLOWED_ORIGINS: "http://localhost:8420"
        run: |
          cd packages/server
          /tmp/venv/bin/pytest tests --tb=short

  build-image:
    if: ${{ !startsWith(gitea.event.head_commit.message, 'chore: release v') }}
    needs: [test-frontend, test-backend]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build Docker image (no push)
        uses: docker/build-push-action@v5
        with:
          context: .
          push: false
          tags: notify-bridge:ci-${{ gitea.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
