name: CI

on:
  push:
  pull_request:

  workflow_dispatch:

  # to execute once a day (more info see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule )
  schedule:
    - cron: "0 0 * * *"

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

jobs:
  build-check:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Update system dependencies
        run: |
          sudo apt update --yes

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Check NPM version
        run: npm --version

      - name: Install dependencies
        run: npm install

      - name: Build
        run: npm run build

      - name: Check
        run: |
          npm run check
          git diff --exit-code || (echo "Formatting did not match (see above diff), please run 'npm run check'" >&2 && exit 1)

  nodeJsBaselineAptCompatibilityCheckOfUpstreamPiRepo:
    name: NodeJS installed from stock Ubuntu-LTS packages (not external sources)

    # TODO: change to 26.04 when it is released
    runs-on: ubuntu-24.04
    container:
      image: "ubuntu:26.04"
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Load variables from .env file
        run: cat .env >> $GITHUB_ENV
      - name: Install dependencies
        run: |
          apt update --yes

          # NOTE: do not change the below with an `actions/setup-node` step! or it
          # would make this CI job entirely pointless
          apt install --yes --no-install-recommends npm git wget

      - name: Print versions
        run: node --version && npm --version

      - name: Check package versions
        run: |
          # Get versions from package.json
          PI_CODING_AGENT_VERSION=$(node -p "require('./package.json').devDependencies['@earendil-works/pi-coding-agent']")

          if [ "$PI_VERSION" != "$PI_CODING_AGENT_VERSION" ]; then
            echo "ERROR: @earendil-works/pi-coding-agent version ($PI_CODING_AGENT_VERSION) does not match PI version ($PI_VERSION)"
            exit 1
          fi

      - name: Set helper env vars
        run: |
          echo "IS_SCHEDULED=${{ github.event_name == 'schedule' }}" >> $GITHUB_ENV
          echo "GH_FULL_REPO=${{ (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name) || github.repository }}" >> $GITHUB_ENV
          echo "GH_SHA=${{ (github.event_name == 'pull_request' && github.event.pull_request.head.sha) || github.sha }}" >> $GITHUB_ENV

      - name: Get last git tag
        run: |
          git clone https://github.com/earendil-works/pi.git
          cd pi
          git describe --tags --abbrev=0 > ../last_version.txt

          if [ "$IS_SCHEDULED" != "true" ]; then
            git switch --detach v${PI_VERSION}
            echo "v${PI_VERSION}" > ../last_version.txt
          fi
          cd ..
          rm -rf pi

      - name: Download and Install Pi release
        run: |
          VERSION=$(cat last_version.txt)
          URL="https://github.com/earendil-works/pi/releases/download/${VERSION}/pi-linux-x64.tar.gz"
          wget "$URL"
          tar -xzf "pi-linux-x64.tar.gz"
          ./pi/pi install "git:github.com/$GH_FULL_REPO@$GH_SHA"
          ./pi/pi --version

      - name: Test extension loading
        run: |
          # tee makes exitCode not propagate (which is what we want because even if
          # extension loads successfully, it will abort due to 'No API Key found')
          (./pi/pi --print "dummy prompt" 2>&1 | tee output.txt)

          # therefore, we check the output for success or not
          grep --quiet Success output.txt
          # --null-data is to scan whole file, not line by line
          grep --invert-match --quiet --null-data --ignore-case error output.txt
