name: Test Cache Integrity

on:
  workflow_dispatch:
  pull_request:
    paths:
      - 'tests/verify_cache_integrity.py'
      - 'convert_db.py'

jobs:
  verify-cache:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install --no-cache-dir -r requirements-apps.txt
          pip install --no-cache-dir aimodelshare --no-dependencies
          pip install --no-cache-dir pytest
          

      - name: Download Prediction Cache
        uses: dawidd6/action-download-artifact@v6
        with:
          workflow: build_model_cache.yml
          name: prediction-cache-file
          path: .
          search_artifacts: true
          if_no_artifact_found: warn

      - name: Convert to SQLite
        run: |
          if [ -f "prediction_cache.json.gz" ]; then
            echo "✅ found cache file, converting..."
            python convert_db.py
          else
            echo "❌ Cache file missing, skipping test."
            exit 1
          fi

      - name: Run Verification Script
        env:
          # Inject the secret here so Python can read os.environ["SESSION_ID"]
          SESSION_ID: ${{ secrets.MC_TEST_SESSIONID }}
        run: python tests/verify_cache_integrity.py
