name: CF-API CI

permissions:
  contents: read
  pull-requests: read

on:
  pull_request:
    paths:
      - 'js/cf-api/**'
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  # This job checks if the workflow should run based on file changes
  check-changes:
    runs-on: ubuntu-latest
    outputs:
      should-run: ${{ steps.filter.outputs.cfapi }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            cfapi:
              - 'js/cf-api/**'

  # This job always runs and succeeds, allowing PRs to be merged when paths don't match
  skip-validate:
    needs: check-changes
    if: needs.check-changes.outputs.should-run != 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Skip validation
        run: echo "Skipping CF-API validation - no changes in js/cf-api/"

  validate:
    needs: check-changes
    if: needs.check-changes.outputs.should-run == 'true'
    env:
      NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: read

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: https://npm.pkg.github.com
          scope: '@codeflash-ai'
          cache: 'npm'
          cache-dependency-path: 'js/cf-api/package-lock.json'

      - name: Install dependencies
        run: |
          cd js/cf-api
          npm ci

      - name: Run tests
        run: |
          cd js/cf-api
          NODE_OPTIONS=--experimental-vm-modules npx jest --ci --config jest.config.cjs

      - name: Build
        run: |
          cd js/cf-api
          npm run build

#        - name: Type check
#            run: |
#              cd js/cf-api
#              npx tsc --noEmit
#
#         - name: Lint
#           run: |
#             cd js/cf-api
#              npx eslint . --ext .ts,.mjs
