name: CI

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

jobs:
  lint-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm --filter @vtit-agent-coding/shared build
      - run: pnpm run lint
      - run: pnpm run test -- --run

  cli-windows:
    # node-gyp 11 does not yet recognize the Visual Studio 2026 installation
    # on windows-latest. Pin the supported VS 2022 image for native builds.
    runs-on: windows-2022
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm --filter @vtit-agent-coding/shared build
      - run: pnpm --filter vtit-agent-coding build
      - run: pnpm --filter vtit-agent-coding exec node-gyp rebuild --directory native/process-tree --release
      - name: Stage native addon
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force packages/cli/dist/native/win32-x64
          Copy-Item packages/cli/native/process-tree/build/Release/process_tree.node packages/cli/dist/native/win32-x64/process-tree.node
      - name: Load native addon on Node 22
        shell: pwsh
        run: node -e "const addon=require('./packages/cli/dist/native/win32-x64/process-tree.node'); const rows=addon.getProcessAncestry(process.pid,32); const first=rows[0]; if (first?.pid !== process.pid || first.ppid !== process.ppid || !first.commandLine?.includes('ak-native-smoke-210') || rows.length < 2) process.exit(1)" ak-native-smoke-210
      - run: pnpm --filter vtit-agent-coding lint
      - run: pnpm exec vitest run packages/cli/tests
