name: cli-ci

on:
  pull_request:
    paths:
      - ".github/workflows/cli-ci.yml"
      - "packages/petdex-cli/**"
  push:
    branches: [main]
    paths:
      - ".github/workflows/cli-ci.yml"
      - "packages/petdex-cli/**"
  workflow_dispatch:

permissions:
  contents: read

jobs:
  package:
    name: CLI package (${{ matrix.os }}, Node ${{ matrix.node }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        node: [20, 22, 24]

    defaults:
      run:
        working-directory: packages/petdex-cli

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}

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

      - name: Build CLI
        run: bun run build

      - name: Typecheck CLI
        run: bun run typecheck

      - name: Run CLI tests
        if: runner.os != 'Windows'
        run: bun test

      - name: Skip POSIX-only CLI tests on Windows
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Write-Host "Skipping the full Bun test suite on Windows because several current CLI tests assert POSIX-only process, shell, path, and chmod behavior."
          Write-Host "This Windows job still verifies build, typecheck, npm pack, and npx.cmd execution for the packaged CLI."

      - name: Verify built binary
        run: node dist/petdex.js --version

      - name: Verify npm package contents
        run: npm pack --silent

      - name: Verify packed CLI via npx
        if: runner.os != 'Windows'
        shell: bash
        run: |
          set -euo pipefail
          tarball="$(find . -maxdepth 1 -name 'petdex-*.tgz' -print -quit)"
          test -n "$tarball"
          npx -y --package "$tarball" petdex --version

      - name: Verify packed CLI via npx.cmd
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $tarball = Get-ChildItem -Path . -Filter "petdex-*.tgz" | Select-Object -First 1
          if (-not $tarball) {
            throw "petdex package tarball was not created"
          }
          npx.cmd -y --package $tarball.FullName petdex --version
