name: Feedser Tests CI

on:
  push:
    paths:
      - 'StellaOps.Feedser/**'
      - '.gitea/workflows/feedser-tests.yml'
  pull_request:
    paths:
      - 'StellaOps.Feedser/**'
      - '.gitea/workflows/feedser-tests.yml'

jobs:
  advisory-store-performance:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up .NET SDK
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 10.0.100-rc.1

      - name: Restore dependencies
        working-directory: StellaOps.Feedser
        run: dotnet restore StellaOps.Feedser.Tests/StellaOps.Feedser.Tests.csproj

      - name: Run advisory store performance test
        working-directory: StellaOps.Feedser
        run: |
          set -euo pipefail
          dotnet test \
            StellaOps.Feedser.Tests/StellaOps.Feedser.Tests.csproj \
            --filter "FullyQualifiedName~AdvisoryStorePerformanceTests" \
            --logger:"console;verbosity=detailed" | tee performance.log

      - name: Upload performance log
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: advisory-store-performance-log
          path: StellaOps.Feedser/performance.log

  full-test-suite:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up .NET SDK
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 10.0.100-rc.1

      - name: Restore dependencies
        working-directory: StellaOps.Feedser
        run: dotnet restore StellaOps.Feedser.Tests/StellaOps.Feedser.Tests.csproj

      - name: Run full test suite with baseline guard
        working-directory: StellaOps.Feedser
        env:
          BASELINE_SECONDS: "19.8"
          TOLERANCE_PERCENT: "25"
        run: |
          set -euo pipefail
          start=$(date +%s)
          dotnet test StellaOps.Feedser.Tests/StellaOps.Feedser.Tests.csproj --no-build | tee full-tests.log
          end=$(date +%s)
          duration=$((end-start))
          echo "Full test duration: ${duration}s"
          export DURATION_SECONDS="$duration"
          python - <<'PY'
import os, sys
duration = float(os.environ["DURATION_SECONDS"])
baseline = float(os.environ["BASELINE_SECONDS"])
tolerance = float(os.environ["TOLERANCE_PERCENT"])
threshold = baseline * (1 + tolerance / 100)
print(f"Baseline {baseline:.1f}s, threshold {threshold:.1f}s, observed {duration:.1f}s")
if duration > threshold:
    sys.exit(f"Full test duration {duration:.1f}s exceeded threshold {threshold:.1f}s")
PY

      - name: Upload full test log
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: full-test-suite-log
          path: StellaOps.Feedser/full-tests.log
