name: Skill Combination Tests

on:
  pull_request:
    branches: [main]
    paths:
      - 'skills-engine/**'
      - '.claude/skills/**'
      - 'src/**'

jobs:
  generate-matrix:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.matrix.outputs.matrix }}
      has_entries: ${{ steps.matrix.outputs.has_entries }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
      - run: npm ci
      - name: Generate overlap matrix
        id: matrix
        run: |
          MATRIX=$(npx tsx scripts/generate-ci-matrix.ts)
          {
            echo "matrix<<MATRIX_EOF"
            echo "$MATRIX"
            echo "MATRIX_EOF"
          } >> "$GITHUB_OUTPUT"
          if [ "$MATRIX" = "[]" ]; then
            echo "has_entries=false" >> "$GITHUB_OUTPUT"
          else
            echo "has_entries=true" >> "$GITHUB_OUTPUT"
          fi

  test-combinations:
    needs: generate-matrix
    if: needs.generate-matrix.outputs.has_entries == 'true'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        entry: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
      - run: npm ci

      - name: Initialize nanoclaw dir
        run: npx tsx -e "import { initNanoclawDir } from './skills-engine/index.js'; initNanoclawDir();"

      - name: Apply skills in sequence
        run: |
          for skill in $(echo '${{ toJson(matrix.entry.skills) }}' | jq -r '.[]'); do
            echo "Applying skill: $skill"
            npx tsx scripts/apply-skill.ts ".claude/skills/$skill"
          done

      - name: Run skill tests
        run: npx vitest run --config vitest.skills.config.ts

  skill-tests-summary:
    needs: [generate-matrix, test-combinations]
    if: always()
    runs-on: ubuntu-latest
    steps:
      - name: Report result
        run: |
          if [ "${{ needs.generate-matrix.outputs.has_entries }}" = "false" ]; then
            echo "No overlapping skills found. Skipped combination tests."
            exit 0
          fi
          if [ "${{ needs.test-combinations.result }}" = "success" ]; then
            echo "All skill combination tests passed."
          else
            echo "Some skill combination tests failed."
            exit 1
          fi
