name: Global Install Upgrade Smoke

# Catches regressions where `npm install -g gitnexus@<new>` fails to upgrade
# cleanly over a prior global install. Prior precedent: issue #836 and PR #843's
# incomplete fix slipped past CI because no global-upgrade test existed.
#
# Reusable workflow — only callable from ci.yml. Concurrency is governed by the
# caller (ci.yml), so no `concurrency:` block here.

on:
  workflow_call:

jobs:
  global-upgrade:
    name: ${{ matrix.os }} / upgrade over ${{ matrix.prior }}
    strategy:
      fail-fast: false
      matrix:
        # macOS is the reporter's platform (issue #836) and the highest-risk
        # surface for npm global-install rmdir behavior. Linux and Windows
        # provide cross-platform regression coverage.
        os: [macos-latest, ubuntu-latest, windows-latest]
        # Prior version that must be upgraded OVER. Should be a published rc
        # that preceded the fix. Bump when a known-bad version changes.
        prior: ['1.6.2-rc.8']
    runs-on: ${{ matrix.os }}
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - uses: ./.github/actions/setup-gitnexus
        with:
          build: 'false'

      - name: Install prior published version globally
        run: npm install -g gitnexus@${{ matrix.prior }}

      - name: Verify prior version installed
        run: gitnexus --version

      - name: Pack current branch
        working-directory: gitnexus
        run: npm pack
        shell: bash

      - name: Compute packed tarball path
        id: tarball
        working-directory: gitnexus
        run: |
          TARBALL=$(ls gitnexus-*.tgz | head -1)
          echo "path=$(pwd)/$TARBALL" >> "$GITHUB_OUTPUT"
        shell: bash

      - name: Upgrade over prior version (the actual regression test)
        run: npm install -g "${{ steps.tarball.outputs.path }}"
        shell: bash

      - name: Verify upgraded version runs
        run: gitnexus --version

      - name: Verify vendor/ has no nested node_modules after install
        shell: bash
        run: |
          # The original #836 bug was about vendor/tree-sitter-proto/node_modules/
          # blocking rmdir on upgrade. That is what the fix eliminates. A
          # vendor/tree-sitter-proto/build/ directory can still appear because
          # node-gyp-build compiles through the npm-created symlink; the
          # contents are plain object files and .node binaries that rmdir
          # handles fine, evidenced by this test getting past the upgrade step.
          GLOBAL_PREFIX=$(npm root -g)
          if [ -d "$GLOBAL_PREFIX/gitnexus/vendor/tree-sitter-proto" ]; then
            echo "=== Contents of global vendor/tree-sitter-proto/ ==="
            ls -la "$GLOBAL_PREFIX/gitnexus/vendor/tree-sitter-proto/"
            if [ -d "$GLOBAL_PREFIX/gitnexus/vendor/tree-sitter-proto/node_modules" ]; then
              echo "::error::vendor/tree-sitter-proto/node_modules/ was created — this is the #836 hazard"
              exit 1
            fi
          fi

  ignore-scripts:
    name: ${{ matrix.os }} / --ignore-scripts degraded mode
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - uses: ./.github/actions/setup-gitnexus
        with:
          build: 'false'

      - name: Pack current branch
        working-directory: gitnexus
        run: npm pack
        shell: bash

      - name: Compute packed tarball path
        id: tarball
        working-directory: gitnexus
        run: |
          TARBALL=$(ls gitnexus-*.tgz | head -1)
          echo "path=$(pwd)/$TARBALL" >> "$GITHUB_OUTPUT"
        shell: bash

      - name: Install globally with --ignore-scripts
        run: npm install -g --ignore-scripts "${{ steps.tarball.outputs.path }}"
        shell: bash

      - name: Verify CLI boots without postinstall (proto parsing may be unavailable)
        run: gitnexus --version
