name: Build & test

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  ubuntu:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [20.x, 22.x, 24.x]

    steps:
      # Setup the environment
      - uses: actions/checkout@v6
      - name: update version in package.json
        run: |
          LATEST_VERSION=$(curl -s https://registry.npmjs.org/@rootplatform/cli | jq -r '.["dist-tags"].latest')
          jq --arg version "$LATEST_VERSION" '.version = $version' package.json > package.json.tmp && mv package.json.tmp package.json
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v6
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      # Build the library locally
      - run: npm run build --if-present
      # Lunk the npm library locally
      - run: npm link
      # Run the test suite
      - run: npm test
      # Run actual production tests
      - name: Log the Workbench version
        run: rp -V
      # Clone a test product module from api.rootplatform.com

      - name: Clone the test product module locally
        run: rp clone ${{ secrets.ROOT_API_KEY }} ${{ vars.PRODUCT_MODULE_KEY }}

  windows:
    runs-on: windows-latest

    strategy:
      matrix:
        node-version: [20.x, 22.x, 24.x]

    steps:
      # Setup the environment
      - uses: actions/checkout@v6
      - name: update version in package.json
        run: |
          $response = Invoke-RestMethod -Uri 'https://registry.npmjs.org/@rootplatform/cli'
          $LATEST_VERSION = $response.'dist-tags'.latest
          $packageJson = Get-Content package.json | ConvertFrom-Json
          $packageJson.version = $LATEST_VERSION
          $packageJson | ConvertTo-Json -Depth 100 | Set-Content package.json
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v6
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      # Build the library locally
      - run: npm run build --if-present
      # Lunk the npm library locally
      - run: npm link
      # Run the test suite
      - run: npm test
        shell: cmd
      # Run actual production tests
      - name: Log the Workbench version
        run: rp -V
        shell: bash
        # Clone a test product module from api.rootplatform.com
      - name: Clone the test product module locally
        run: rp clone ${{ secrets.ROOT_API_KEY }} ${{ vars.PRODUCT_MODULE_KEY }}
        shell: cmd
