name: Install Dependencies

on:
  pull_request:
    paths:
      - install.sh
      - scripts/install-deps.sh
      - packaging/linux/control
      - .github/workflows/install-deps.yml
  push:
    branches:
      - main
    paths:
      - install.sh
      - scripts/install-deps.sh
      - packaging/linux/control
      - .github/workflows/install-deps.yml

jobs:
  apt-node-bootstrap:
    name: ${{ matrix.image }}
    runs-on: ubuntu-latest
    container:
      image: ${{ matrix.image }}
      options: --user root
    strategy:
      fail-fast: false
      matrix:
        image:
          - docker.io/library/ubuntu:22.04
          - docker.io/library/ubuntu:24.04
          - docker.io/library/debian:12
    env:
      DEBIAN_FRONTEND: noninteractive
    steps:
      - uses: actions/checkout@v4

      - name: Bootstrap sudo
        run: |
          apt-get update -qq >/dev/null
          apt-get install -y sudo ca-certificates >/dev/null

      - name: Install dependencies
        run: bash scripts/install-deps.sh

      - name: Verify Node.js toolchain
        run: |
          node -p "process.versions.node"
          node -e 'process.exit(Number(process.versions.node.split(".")[0]) >= 20 ? 0 : 1)'
          npm -v
          npx -v
          dpkg-query -W -f='${Provides}\n' nodejs | grep -E '(^|[,[:space:]])npm([,[:space:](]|$)'

      - name: Verify installer passes Node preflight
        run: |
          export PATH="$HOME/.local/bin:$PATH"
          output=$(./install.sh /tmp/nope.dmg 2>&1) && status=0 || status=$?
          printf '%s\n' "$output"
          test "$status" -ne 0
          printf '%s\n' "$output" | grep -q "Provided DMG not found: /tmp/nope.dmg"
          ! printf '%s\n' "$output" | grep -q "Node.js 20+ required"
