name: CI Bun

on:
  push:
    branches: [main]

concurrency:
  group: ci-bun-push-${{ github.run_id }}
  cancel-in-progress: false

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"

jobs:
  preflight:
    runs-on: blacksmith-16vcpu-ubuntu-2404
    timeout-minutes: 20
    outputs:
      run_bun_checks: ${{ steps.manifest.outputs.run_bun_checks }}
      bun_checks_matrix: ${{ steps.manifest.outputs.bun_checks_matrix }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: false

      - name: Setup Node environment
        uses: ./.github/actions/setup-node-env
        with:
          install-bun: "false"
          install-deps: "false"
          use-sticky-disk: "false"

      - name: Build Bun CI manifest
        id: manifest
        env:
          OPENCLAW_CI_DOCS_ONLY: "false"
          OPENCLAW_CI_DOCS_CHANGED: "false"
          OPENCLAW_CI_RUN_NODE: "true"
          OPENCLAW_CI_RUN_MACOS: "false"
          OPENCLAW_CI_RUN_ANDROID: "false"
          OPENCLAW_CI_RUN_WINDOWS: "false"
          OPENCLAW_CI_RUN_SKILLS_PYTHON: "false"
          OPENCLAW_CI_HAS_CHANGED_EXTENSIONS: "false"
          OPENCLAW_CI_CHANGED_EXTENSIONS_MATRIX: '{"include":[]}'
        run: node scripts/ci-write-manifest-outputs.mjs --workflow ci-bun

  build-bun-artifacts:
    needs: [preflight]
    if: needs.preflight.outputs.run_bun_checks == 'true'
    runs-on: blacksmith-16vcpu-ubuntu-2404
    timeout-minutes: 20
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: false

      - name: Setup Node environment
        uses: ./.github/actions/setup-node-env
        with:
          install-bun: "false"
          use-sticky-disk: "false"

      - name: Build A2UI bundle
        run: pnpm canvas:a2ui:bundle

      - name: Upload A2UI bundle artifact
        uses: actions/upload-artifact@v7
        with:
          name: canvas-a2ui-bundle
          path: src/canvas-host/a2ui/
          include-hidden-files: true
          retention-days: 1

  bun-checks:
    name: ${{ matrix.check_name }}
    needs: [preflight, build-bun-artifacts]
    if: needs.preflight.outputs.run_bun_checks == 'true'
    runs-on: blacksmith-16vcpu-ubuntu-2404
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix: ${{ fromJson(needs.preflight.outputs.bun_checks_matrix) }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: false

      - name: Setup Node environment
        uses: ./.github/actions/setup-node-env
        with:
          install-bun: "true"
          use-sticky-disk: "false"

      - name: Download A2UI bundle artifact
        uses: actions/download-artifact@v8
        with:
          name: canvas-a2ui-bundle
          path: src/canvas-host/a2ui/

      - name: Run Bun test shard
        env:
          SHARD_COUNT: ${{ matrix.shard_count }}
          SHARD_INDEX: ${{ matrix.shard_index }}
        shell: bash
        run: |
          set -euo pipefail
          OPENCLAW_TEST_ISOLATE=1 bunx vitest run --config vitest.unit.config.ts --shard "$SHARD_INDEX/$SHARD_COUNT"
