name: erp-kit Backend Integration Test

# Manual integration run for a single app against a throwaway workspace.
# Without TAILOR_PLATFORM_WORKSPACE_ID, setup.ts creates, deploys, seeds, and
# deletes its own workspace.
#
# Runner: ERP_KIT_BUILD_RUNNER (default ubuntu-latest).

on:
  workflow_dispatch:
    inputs:
      app:
        description: "App to test (apps/<app>/backend)"
        required: true

jobs:
  integration-test:
    runs-on: ${{ vars.ERP_KIT_BUILD_RUNNER || 'ubuntu-latest' }}
    timeout-minutes: 20
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10

      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          cache: pnpm
          cache-dependency-path: ./pnpm-lock.yaml
          node-version-file: package.json

      - run: pnpm install

      - name: Fetch Tailor token
        id: token
        uses: ./.github/actions/erp-kit-fetch-tailor-token
        with:
          client_id: ${{ secrets.TAILOR_PFMU_CLIENT_ID }}
          client_secret: ${{ secrets.TAILOR_PFMU_CLIENT_SECRET }}
          token: ${{ secrets.TAILOR_PLATFORM_TOKEN }}

      - name: Run integration tests
        working-directory: apps/${{ github.event.inputs.app }}/backend
        env:
          TAILOR_PLATFORM_TOKEN: ${{ steps.token.outputs.token }}
          TAILOR_PLATFORM_ORGANIZATION_ID: ${{ vars.TAILOR_PLATFORM_ORGANIZATION_ID }}
          TAILOR_PLATFORM_FOLDER_ID: ${{ vars.TAILOR_PLATFORM_FOLDER_ID }}
        run: pnpm test:integration
