name: Integration Tests

on:
  # Allow manual trigger
  workflow_dispatch:

  # Run on schedule (daily at 2 AM UTC)
  schedule:
    - cron: '0 2 * * *'

  # Optionally run on push to main (uncomment if desired)
  # push:
  #   branches: [main]

jobs:
  integration-sepolia:
    name: Integration Tests on Sepolia
    runs-on: ubuntu-latest

    # Only run if the secret is available
    if: ${{ vars.INTEGRATION_TESTS_ENABLED == 'true' || github.event_name == 'workflow_dispatch' }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20.x'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Type check
        run: npm run typecheck

      - name: Build
        run: npm run build

      - name: Run Integration Tests
        env:
          TEST_WALLET_PK: ${{ secrets.TEST_WALLET_PK }}
          ETHERSCAN_API_KEY: ${{ secrets.ETHERSCAN_API_KEY }}
          TX_SERVICE_API_KEY: ${{ secrets.TX_SERVICE_API_KEY }}
        run: npm test -- integration-*.test.ts --run
        timeout-minutes: 15

      - name: Run E2E CLI Tests
        run: npm test -- e2e-cli.test.ts --run
        timeout-minutes: 5

      - name: Upload test artifacts on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: integration-test-artifacts
          path: |
            **/integration-*.log
            **/e2e-*.log
            **/exported-*.json
          retention-days: 7
