name: Unit Tests for Playground Components

on:
  workflow_dispatch: # Manual trigger

permissions:
  contents: read

concurrency:
  group: unit-tests-${{ github.ref }}
  cancel-in-progress: true

jobs:
  unit-tests:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    env:
      TF_CPP_MIN_LOG_LEVEL: "2"
      AIMODELSHARE_SUPPRESS_OPTIONAL_WARNINGS: "1"

    strategy:
      matrix:
        test-group:
          - setup_sanity
          - credentials
          - playground_init
          - data_preprocessing
          - model_training
          - playground_operations

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

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"
          cache: "pip"

      - name: Upgrade pip
        run: |
          python -m pip install --upgrade pip

      - name: Install local aimodelshare (editable) + test dependencies
        run: |
          pip install boto3 onnx onnxmltools onnxruntime Pympler scikeras shortuuid skl2onnx tf2onnx wget
          pip install -e .
          pip install pytest scikit-learn pandas numpy opencv-python-headless tensorflow pydot regex psutil IPython "PyJWT<2.0" matplotlib seaborn importlib_resources onnxscript
          pip install --index-url https://download.pytorch.org/whl/cpu torch torchvision

      - name: Run unit tests for ${{ matrix.test-group }}
        run: |
          pytest -vv tests/unit/test_${{ matrix.test-group }}.py --tb=short

      - name: Run unit tests with coverage
        if: matrix.test-group == 'credentials'
        run: |
          pytest tests/unit/ --cov=aimodelshare --cov-report=xml --cov-report=term

      - name: Upload coverage report
        if: matrix.test-group == 'credentials'
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage.xml

  summary:
    needs: unit-tests
    runs-on: ubuntu-latest
    if: always()
    
    steps:
      - name: Check test results
        run: |
          echo "All unit test groups completed"
          echo "Review individual test results above"
