# Run the Compose-packaged CLI P2P smoke benchmark.
#
# This workflow is intentionally manual-only. It exercises the local Compose
# package for CouchDB + Nostr relay + CLI runner, and uploads the benchmark JSON
# results for inspection without adding benchmark work to pull-request CI.
name: cli-p2p-compose-smoke

on:
  workflow_dispatch:
    inputs:
      cases:
        description: 'Comma-separated benchmark cases'
        required: false
        default: 'couchdb-baseline,p2p-direct-local'
      signalling_cases:
        description: 'Comma-separated signalling-shim P2P benchmark cases'
        required: false
        default: 'p2p-signalling-netem-home-wifi'
      md_files:
        description: 'Markdown file count'
        required: false
        default: '2'
      bin_files:
        description: 'Binary file count'
        required: false
        default: '1'
      couchdb_rtt_ms:
        description: 'Requested CouchDB RTT in milliseconds'
        required: false
        default: '20'

permissions:
  contents: read

jobs:
  smoke:
    runs-on: ubuntu-latest
    timeout-minutes: 45
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Show Docker versions
        run: |
          docker --version
          docker compose version

      - name: Run Compose P2P smoke benchmark
        env:
          BENCH_CASES: ${{ inputs.cases || 'couchdb-baseline,p2p-direct-local' }}
          BENCH_MD_FILE_COUNT: ${{ inputs.md_files || '2' }}
          BENCH_MD_MIN_SIZE_BYTES: '128'
          BENCH_MD_MAX_SIZE_BYTES: '256'
          BENCH_BIN_FILE_COUNT: ${{ inputs.bin_files || '1' }}
          BENCH_BIN_SIZE_BYTES: '512'
          BENCH_COUCHDB_RTT_MS: ${{ inputs.couchdb_rtt_ms || '20' }}
          BENCH_SYNC_TIMEOUT: '180'
          BENCH_PEERS_TIMEOUT: '20'
          LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS: '60000'
          BENCH_LIVESYNC_TEST_TEE: '0'
        run: docker compose -f test/bench-network/compose.yml run --build --rm bench-runner

      - name: Run Compose P2P signalling-shim smoke benchmark
        env:
          BENCH_CASES: ${{ inputs.signalling_cases || 'p2p-signalling-netem-home-wifi' }}
          BENCH_MD_FILE_COUNT: ${{ inputs.md_files || '2' }}
          BENCH_MD_MIN_SIZE_BYTES: '128'
          BENCH_MD_MAX_SIZE_BYTES: '256'
          BENCH_BIN_FILE_COUNT: ${{ inputs.bin_files || '1' }}
          BENCH_BIN_SIZE_BYTES: '512'
          BENCH_SYNC_TIMEOUT: '180'
          BENCH_PEERS_TIMEOUT: '60'
          LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS: '60000'
          BENCH_LIVESYNC_TEST_TEE: '0'
          NETEM_PROFILE: 'home-wifi'
        run: docker compose -f test/bench-network/compose.yml --profile signalling-shim run --build --rm bench-runner-signalling-shim

      - name: Show Compose diagnostics
        if: failure()
        run: |
          docker compose -f test/bench-network/compose.yml ps
          docker compose -f test/bench-network/compose.yml --profile signalling-shim logs --no-color couchdb nostr-relay p2p-signalling-shim || true

      - name: Upload benchmark results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: cli-p2p-compose-smoke-results
          path: test/bench-network/bench-results/**
          if-no-files-found: warn

      - name: Stop Compose services
        if: always()
        run: docker compose -f test/bench-network/compose.yml down -v --remove-orphans
