name: CI

on:
  push:
    branches: [master, main]
    tags: ['v*']
  pull_request:
    branches: [master, main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Auth for Gitea npm registry
        run: |
          echo "@rigor:registry=https://hub.kl1.tenere.ai/api/packages/Rigor/npm/" > .npmrc
          echo "//hub.kl1.tenere.ai/api/packages/Rigor/npm/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> .npmrc
          echo "@tenere:registry=https://hub.kl1.tenere.ai/api/packages/Tenere/npm/" >> .npmrc
          echo "//hub.kl1.tenere.ai/api/packages/Tenere/npm/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> .npmrc

      - run: npm ci

      - name: Full suite
        run: npm test

      - name: Rigor campaigns
        run: npm run test:rigor

  benchmark:
    runs-on: ubuntu-latest
    needs: test
    if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/master'

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Auth for Gitea npm registry
        run: |
          echo "@rigor:registry=https://hub.kl1.tenere.ai/api/packages/Rigor/npm/" > .npmrc
          echo "//hub.kl1.tenere.ai/api/packages/Rigor/npm/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> .npmrc
          echo "@tenere:registry=https://hub.kl1.tenere.ai/api/packages/Tenere/npm/" >> .npmrc
          echo "//hub.kl1.tenere.ai/api/packages/Tenere/npm/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> .npmrc

      - run: npm ci

      - name: Download previous baseline
        uses: dawidd6/action-download-artifact@v6
        with:
          name: baseline
          path: .
        continue-on-error: true

      - name: Run benchmarks (compare against baseline)
        run: node --expose-gc scripts/benchmark.js --json > bench-results.json
        continue-on-error: true

      - name: Save baseline (on tag)
        if: startsWith(github.ref, 'refs/tags/v')
        run: node --expose-gc scripts/benchmark.js --save

      - name: Upload baseline artifact
        if: startsWith(github.ref, 'refs/tags/v')
        uses: actions/upload-artifact@v3
        with:
          name: baseline
          path: .rigor-baseline.json
          retention-days: 90

      - name: Upload benchmark results
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: bench-results
          path: bench-results.json
          retention-days: 30

      - name: Benchmark summary
        if: always()
        run: |
          echo '### Benchmark Results' >> $GITHUB_STEP_SUMMARY
          if [ -f bench-results.json ]; then
            echo '```json' >> $GITHUB_STEP_SUMMARY
            cat bench-results.json >> $GITHUB_STEP_SUMMARY
            echo '```' >> $GITHUB_STEP_SUMMARY
          fi

  publish:
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [test, benchmark]

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Auth for Gitea npm registry
        run: |
          echo "@rigor:registry=https://hub.kl1.tenere.ai/api/packages/Rigor/npm/" > .npmrc
          echo "//hub.kl1.tenere.ai/api/packages/Rigor/npm/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> .npmrc
          echo "@tenere:registry=https://hub.kl1.tenere.ai/api/packages/Tenere/npm/" >> .npmrc
          echo "//hub.kl1.tenere.ai/api/packages/Tenere/npm/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> .npmrc
          echo "@arbiter:registry=https://hub.kl1.tenere.ai/api/packages/Arbiter/npm/" >> .npmrc
          echo "//hub.kl1.tenere.ai/api/packages/Arbiter/npm/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> .npmrc

      - run: npm ci

      - run: npm publish
