name: CI

on:
  push:
    branches:
      - main
      - nightly
  pull_request:
    branches:
      - main
      - nightly

permissions:
  contents: read

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

jobs:
  go-test:
    name: Go tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.24"
          cache-dependency-path: core/go.sum

      - name: Vet
        working-directory: core
        run: go vet ./...

      - name: Test
        working-directory: core
        run: go test -race -timeout 5m ./...

  sdk-build:
    name: SDK typecheck, build, unit tests
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: sdk
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Typecheck
        run: pnpm typecheck

      - name: Build
        run: pnpm build

      - name: Unit tests
        run: pnpm vitest run tests/unit

  version-sanity:
    name: Verify VERSION ↔ sdk/package.json sync
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Compare versions
        run: |
          ROOT=$(tr -d '[:space:]' < VERSION)
          SDK=$(node -p "require('./sdk/package.json').version")
          if [ "$ROOT" != "$SDK" ]; then
            echo "::warning::/VERSION ($ROOT) and sdk/package.json ($SDK) differ. Run 'make -C core bump VER=$ROOT' to sync."
          else
            echo "Versions in sync: $ROOT"
          fi
