name: Acceptance Tests

on:
  workflow_dispatch:

concurrency:
  group: acceptance-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  acceptance:
    name: CLI acceptance tests (Base Sepolia)
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-acceptance-${{ hashFiles('**/Cargo.lock', 'src/**/*.rs') }}

      - name: Build binary
        run: cargo build

      - name: Create config (remit init gate)
        run: |
          mkdir -p ~/.remit
          echo 'network = "testnet"' > ~/.remit/config.toml

      - name: Run acceptance tests
        id: tests
        run: |
          cargo test --test acceptance -- --ignored --test-threads=1 --nocapture 2>&1 | tee acceptance-output.txt
          echo "exit_code=${PIPESTATUS[0]}" >> "$GITHUB_OUTPUT"
        env:
          REMITMD_KEY: "unused-tests-generate-own-keys"
          ACCEPTANCE_API_URL: "https://testnet.remit.md/api/v1"

      - name: Generate step summary
        if: always()
        run: |
          echo "## CLI Acceptance Tests — Base Sepolia" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "| Test | Status |" >> "$GITHUB_STEP_SUMMARY"
          echo "|------|--------|" >> "$GITHUB_STEP_SUMMARY"

          tests=(
            "t01_wallet_generation"
            "t02_mint_and_balance"
            "t03_status"
            "t04_pay_direct"
            "t05_escrow_lifecycle"
            "t06_tab_lifecycle"
            "t07_stream_lifecycle"
            "t08_bounty_lifecycle"
            "t09_deposit_create"
            "t10_cli_mint"
          )

          for test in "${tests[@]}"; do
            if grep -q "${test} \.\.\. ok" acceptance-output.txt; then
              echo "| \`${test}\` | :white_check_mark: PASS |" >> "$GITHUB_STEP_SUMMARY"
            elif grep -q "${test} \.\.\. FAILED" acceptance-output.txt; then
              echo "| \`${test}\` | :x: FAIL |" >> "$GITHUB_STEP_SUMMARY"
            elif grep -q "${test}" acceptance-output.txt; then
              echo "| \`${test}\` | :warning: UNKNOWN |" >> "$GITHUB_STEP_SUMMARY"
            else
              echo "| \`${test}\` | :black_circle: NOT RUN |" >> "$GITHUB_STEP_SUMMARY"
            fi
          done

          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "### Transaction Log" >> "$GITHUB_STEP_SUMMARY"
          echo '```' >> "$GITHUB_STEP_SUMMARY"
          grep "\[ACCEPTANCE\]" acceptance-output.txt >> "$GITHUB_STEP_SUMMARY" || echo "No [ACCEPTANCE] logs found" >> "$GITHUB_STEP_SUMMARY"
          echo '```' >> "$GITHUB_STEP_SUMMARY"

      - name: Fail if tests failed
        if: always()
        run: |
          if grep -q "test result: FAILED" acceptance-output.txt; then
            exit 1
          fi
