# AIWG Test Generation with Codex
#
# Automatically generate tests for changed files using Codex.
# Creates a PR with new test files.
#
# Prerequisites:
# - OPENAI_API_KEY secret configured
# - GH_PAT secret with repo write access (for creating PR)
#
# Usage:
# Copy this file to .github/workflows/aiwg-codex-tests.yml

name: AIWG Test Generation (Codex)

on:
  workflow_dispatch:
    inputs:
      target_files:
        description: 'Files to generate tests for (space-separated)'
        required: false
        default: ''
      test_framework:
        description: 'Test framework'
        required: true
        default: 'vitest'
        type: choice
        options:
          - vitest
          - jest
          - pytest
          - mocha

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

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Install Codex CLI
        run: npm install -g @openai/codex

      - name: Determine target files
        id: targets
        run: |
          if [ -n "${{ github.event.inputs.target_files }}" ]; then
            echo "files=${{ github.event.inputs.target_files }}" >> $GITHUB_OUTPUT
          else
            # Find source files without corresponding test files
            echo "files=$(find src -name '*.ts' -o -name '*.js' | head -10 | tr '\n' ' ')" >> $GITHUB_OUTPUT
          fi

      - name: Generate Tests
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          codex exec "Generate comprehensive tests for the following files:

          Files: ${{ steps.targets.outputs.files }}
          Test Framework: ${{ github.event.inputs.test_framework }}

          Requirements:
          1. Unit tests for all public functions/methods
          2. Edge case coverage (null, undefined, empty, boundary values)
          3. Error handling tests
          4. Integration tests where appropriate
          5. Mock external dependencies

          Test structure:
          - Use describe/it blocks
          - Clear test names explaining expected behavior
          - Arrange-Act-Assert pattern
          - One assertion per test where practical

          Output:
          - Create test files in the appropriate test directory
          - Match the source file structure
          - Use .test.ts or .spec.ts extension

          Generate the actual test files, not just descriptions." \
            --full-auto \
            --sandbox workspace-write

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

      - name: Create Pull Request
        if: steps.check.outputs.has_tests == 'true'
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }}
          commit-message: 'test: add generated tests via AIWG Codex'
          title: 'test: add generated tests'
          body: |
            ## Generated Tests

            This PR adds tests generated by AIWG + OpenAI Codex.

            **Target files**: ${{ steps.targets.outputs.files }}
            **Test framework**: ${{ github.event.inputs.test_framework }}

            ### Review Checklist
            - [ ] Tests are meaningful and cover important cases
            - [ ] Mocks are appropriate and not over-mocking
            - [ ] Edge cases are covered
            - [ ] Tests pass locally
            - [ ] No flaky tests

            ---
            *Generated by [AIWG](https://aiwg.io) + OpenAI Codex*
          branch: aiwg/generated-tests
          delete-branch: true
