name: regtest

on:
  workflow_call:
    inputs:
      make:
        default: test
        type: string
      python-version:
        default: "3.9"
        type: string
      os-version:
        default: "ubuntu-latest"
        type: string
      backend-wallet-class:
        required: true
        type: string

jobs:
  regtest:
    runs-on: ${{ inputs.os-version }}
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v3

      - name: Set up Docker Buildx
        if: ${{ inputs.backend-wallet-class == 'LNbitsWallet' }}
        uses: docker/setup-buildx-action@v2

      - name: Build and push
        if: ${{ inputs.backend-wallet-class == 'LNbitsWallet' }}
        uses: docker/build-push-action@v4
        with:
            context: .
            push: false
            tags: lnbits/lnbits:latest
            cache-from: type=registry,ref=lnbits/lnbits:latest
            cache-to: type=inline

      - name: Setup Regtest
        run: |
          git clone https://github.com/lnbits/legend-regtest-enviroment.git docker
          cd docker
          chmod +x ./tests
          ./tests
          sudo chmod -R a+rwx .

      - uses: ./.github/actions/prepare
        with:
          python-version: ${{ inputs.python-version }}

      - name: Create fake admin
        if: ${{ inputs.backend-wallet-class == 'LNbitsWallet' }}
        run: docker exec lnbits-lnbits-1 poetry run python tools/create_fake_admin.py

      - name: Run Tests
        env:
          LNBITS_DATABASE_URL: ${{ inputs.db-url }}
          LNBITS_BACKEND_WALLET_CLASS: ${{ inputs.backend-wallet-class }}
          LND_REST_ENDPOINT: https://localhost:8081/
          LND_REST_CERT: ./docker/data/lnd-3/tls.cert
          LND_REST_MACAROON: ./docker/data/lnd-3/data/chain/bitcoin/regtest/admin.macaroon
          LND_GRPC_ENDPOINT: localhost
          LND_GRPC_PORT: 10009
          LND_GRPC_CERT: docker/data/lnd-3/tls.cert
          LND_GRPC_MACAROON: docker/data/lnd-3/data/chain/bitcoin/regtest/admin.macaroon
          CORELIGHTNING_RPC: ./docker/data/clightning-1/regtest/lightning-rpc
          CORELIGHTNING_REST_URL: https://localhost:3001
          CORELIGHTNING_REST_MACAROON: ./docker/data/clightning-2-rest/access.macaroon
          CORELIGHTNING_REST_CERT: ./docker/data/clightning-2-rest/certificate.pem
          LNBITS_ENDPOINT: http://localhost:5001
          LNBITS_KEY: "d08a3313322a4514af75d488bcc27eee"
          ECLAIR_URL: http://127.0.0.1:8082
          ECLAIR_PASS: lnbits
        run: make test-real-wallet

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: ./coverage.xml
