name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main, develop]

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

jobs:
  node:
    name: Node — lint, typecheck, test, build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm

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

      - name: Lint
        run: pnpm lint

      - name: Typecheck
        run: pnpm typecheck

      - name: Unit tests
        run: pnpm test

      - name: Build all apps
        run: pnpm build

  go-node-agent:
    name: Go — node-agent tests
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: apps/node-agent
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: "1.23"
          cache-dependency-path: apps/node-agent/go.sum

      - name: Run tests
        run: go test ./... -count=1 -race

      - name: Build binary
        run: go build -o /tmp/hexahost-node-agent ./cmd/agent

  go-edge-gateway:
    name: Go — edge-gateway build
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: apps/edge-gateway
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: "1.23"

      - name: Build binary
        run: go build -o /tmp/hexahost-edge-gateway ./cmd/gateway

      - name: Verify disabled exit
        run: |
          /tmp/hexahost-edge-gateway
          test $? -eq 0
