# AIWG Test Generation with Cursor Agent
#
# Automated test generation using Cursor CLI with AIWG standards.
# This workflow can be triggered manually to generate tests for changed files.
#
# Prerequisites:
# - CURSOR_API_KEY secret configured
# - Cursor CLI installed
#
# Usage:
# Copy this file to .github/workflows/aiwg-cursor-tests.yml

name: AIWG Test Generation (Cursor)

on:
  workflow_dispatch:
    inputs:
      target_path:
        description: 'Path to generate tests for (e.g., src/auth)'
        required: false
        default: ''
      test_type:
        description: 'Type of tests to generate'
        required: true
        default: 'unit'
        type: choice
        options:
          - unit
          - integration
          - e2e
  pull_request:
    types: [opened, synchronize]
    paths:
      - 'src/**/*.ts'
      - 'src/**/*.tsx'
      - 'src/**/*.js'
      - 'src/**/*.jsx'

permissions:
  contents: write
  pull-requests: write

jobs:
  generate-tests:
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ github.head_ref || github.ref }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: npm ci || yarn install || pnpm install || echo "No lock file"

      - name: Install Cursor CLI
        run: |
          curl https://cursor.com/install -fsS | bash
          echo "$HOME/.cursor/bin" >> $GITHUB_PATH

      - name: Configure git
        run: |
          git config user.name "Cursor Agent"
          git config user.email "cursoragent@cursor.com"

      - name: Determine target files
        id: target
        run: |
          if [ -n "${{ github.event.inputs.target_path }}" ]; then
            echo "path=${{ github.event.inputs.target_path }}" >> $GITHUB_OUTPUT
          else
            # Get changed source files
            CHANGED=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.(ts|tsx|js|jsx)$' | grep -v '\.test\.' | grep -v '\.spec\.' | head -10 | tr '\n' ' ')
            echo "path=$CHANGED" >> $GITHUB_OUTPUT
          fi

      - name: Generate Tests
        env:
          CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          TEST_TYPE="${{ github.event.inputs.test_type || 'unit' }}"
          TARGET="${{ steps.target.outputs.path }}"

          cursor-agent -p --force --output-format text "Generate $TEST_TYPE tests for the following files/paths:
          $TARGET

          Requirements:
          1. Follow existing test patterns in this repository
          2. Use the testing framework already configured (jest, vitest, mocha, etc.)
          3. Achieve high coverage of edge cases and error paths
          4. Include descriptive test names
          5. Mock external dependencies appropriately

          For each source file, create a corresponding test file:
          - src/module.ts -> test/unit/module.test.ts (or similar pattern)

          Do NOT modify source files, only create test files.
          After creating tests, run them to verify they pass."

      - name: Check for new tests
        id: changes
        run: |
          if git diff --name-only | grep -E '\.(test|spec)\.(ts|tsx|js|jsx)$'; then
            echo "has_changes=true" >> $GITHUB_OUTPUT
          else
            echo "has_changes=false" >> $GITHUB_OUTPUT
          fi

      - name: Commit and push tests
        if: steps.changes.outputs.has_changes == 'true' && github.event_name == 'pull_request'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git add -A '*.test.*' '*.spec.*'
          git commit -m "test: add generated tests for changed files

          Generated by AIWG Cursor Agent"
          git push

      - name: Post PR comment
        if: steps.changes.outputs.has_changes == 'true' && github.event_name == 'pull_request'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh pr comment ${{ github.event.pull_request.number }} --body "## Test Generation Complete

          New test files have been generated and committed to this branch.

          Please review the generated tests and ensure they meet your standards.

          ---
          *Generated by [AIWG](https://aiwg.io) + Cursor Agent*"
