name: Test latest master branch aimodelshare

on:
  workflow_dispatch: # Manual trigger only

permissions:
  contents: read

concurrency:
  group: master-aimodelshare-tests
  cancel-in-progress: true

jobs:
  run-tests:
    runs-on: ubuntu-latest
    timeout-minutes: 120

    env:
      USERNAME: ${{ secrets.AIMODELSHARE_USERNAME }}
      PASSWORD: ${{ secrets.AIMODELSHARE_PASSWORD }}
      AWS_ACCESS_KEY_ID: ${{ secrets.DATA_AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.DATA_AWS_SECRET_ACCESS_KEY }}
      AWS_REGION: us-east-1

    steps:
      - name: Checkout repository (master)
        uses: actions/checkout@v4
        with:
          # Uncomment the next line if you want to ALWAYS test master even when dispatching from another ref
          # ref: master
          fetch-depth: 0

      - name: Show commit being tested
        run: |
          echo "Testing commit:"
          git --no-pager log -1 --oneline
          echo "Current branch (if detached this may be a commit SHA):"
          git branch --show-current || true

      - 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
          python -c "import aimodelshare, os; print('aimodelshare imported from:', aimodelshare.__file__)"

      - name: Verify import now points to repository source (not PyPI)
        run: |
          python - <<'PYCODE'
          import aimodelshare, os
          path = aimodelshare.__file__
          print("Resolved aimodelshare.__file__:", path)
          repo_root = os.getcwd()
          assert repo_root in os.path.abspath(path), f"aimodelshare does not appear to come from the checked-out repository root: {repo_root}"
          assert "site-packages" not in path, "aimodelshare should NOT be the PyPI site-packages build."
          print("Verification passed: using local master branch source.")
          PYCODE

      - name: Run targeted tests against local master branch
        run: |
          echo "Starting pytest for tests/test_playgrounds_nodataimport.py"
          pytest -vv tests/test_aimsonnx.py
          pytest -vv tests/test_playgrounds_nodataimport.py

      - name: Post-run diagnostics (always runs)
        if: always()
        run: |
          echo "Job conclusion: ${{ job.status }}"
          python - <<'PYCODE'
          import aimodelshare
          print("aimodelshare version attribute (may reflect local dev state):", getattr(aimodelshare, "__version__", "unknown"))
          print("Module file path:", aimodelshare.__file__)
          PYCODE
