diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f8ad63f..b9655d2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,6 +10,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + - name: Start test containers + run: docker-compose -f "./test/helpers/docker-compose.yml" up -d - name: Setup node uses: actions/setup-node@v3 with: @@ -24,5 +26,21 @@ jobs: - name: Install Dependencies if: steps.cache-node-modules.outputs.cache-hit != 'true' run: npm install + - name: Mine 10 blocks + run: | + curl --header "Content-Type: application/json" -X POST http://alice:password@localhost:18443 -d '{"method": "generatetoaddress", "params": [10, "bcrt1qq2yshcmzdlznnpxx258xswqlmqcxjs4dssfxt2"]}' + - name: Wait for esplora to become available + uses: nick-fields/retry@v2 + with: + timeout_minutes: 3 + retry_wait_seconds: 15 + max_attempts: 15 + command: curl --fail -X GET http://localhost:8094/regtest/api/blocks/tip/height - name: Run unit tests run: npm run test + - name: Fetch esplora logs + if: always() + run: docker-compose -f "./test/helpers/docker-compose.yml" logs esplora + - name: Stop test containers + if: always() + run: docker-compose -f "./test/helpers/docker-compose.yml" down diff --git a/test/esplora.spec.ts b/test/esplora.spec.ts index 9e45696..3a8359e 100644 --- a/test/esplora.spec.ts +++ b/test/esplora.spec.ts @@ -5,9 +5,9 @@ describe('EsploraClient', () => { beforeAll(() => { client = new EsploraClient({ - protocol: 'https', - host: 'blockstream.info', - network: 'testnet', + protocol: 'http', + host: '127.0.0.1:8094', + network: 'regtest', }); }); diff --git a/test/helpers/docker-compose.yaml b/test/helpers/docker-compose.yaml new file mode 100644 index 0000000..365c1c1 --- /dev/null +++ b/test/helpers/docker-compose.yaml @@ -0,0 +1,36 @@ +services: + bitcoin: + image: btcpayserver/bitcoin:24.0.1-1 + environment: + BITCOIN_NETWORK: regtest + BITCOIN_EXTRA_ARGS: | + server=1 + rest=1 + rpcbind=0.0.0.0:18443 + rpcallowip=0.0.0.0/0 + rpcauth=alice:88cae77e34048eff8b9f0be35527dd91$$d5c4e7ff4dfe771808e9c00a1393b90d498f54dcab0ee74a2d77bd01230cd4cc + debug=1 + logips=1 + logtimemicros=1 + blockmintxfee=0 + deprecatedrpc=signrawtransaction + listenonion=0 + fallbackfee=0.00001 + txindex=1 + ports: + - "18443:18443" + esplora: + image: blockstream/esplora:5ec810278c737c78ed54e82749581f1a52ce1b54 + environment: + ELECTRS_ARGS: --jsonrpc-import --daemon-rpc-addr bitcoin:18443 --cookie alice:password + ports: + - "50001:50001" + - "8094:80" + command: + bash -c "/srv/explorer/run.sh bitcoin-regtest explorer" + healthcheck: + test: [ "CMD", "curl", "-f", "http://localhost:80" ] + interval: 1m + timeout: 10s + retries: 3 + start_period: 40s \ No newline at end of file diff --git a/test/wallet-db.spec.ts b/test/wallet-db.spec.ts index cfdc5f5..64c098d 100644 --- a/test/wallet-db.spec.ts +++ b/test/wallet-db.spec.ts @@ -1,4 +1,5 @@ import { WalletDB } from '../src/wallet'; +import fs from 'fs'; describe('Wallet DB', () => { let walletDB: WalletDB; @@ -25,5 +26,6 @@ describe('Wallet DB', () => { afterAll(async () => { await walletDB.close(); + fs.rmSync('./test/wallet-db', { recursive: true, force: true }); }); }); diff --git a/test/wallet.spec.ts b/test/wallet.spec.ts index 63ce524..12c5390 100644 --- a/test/wallet.spec.ts +++ b/test/wallet.spec.ts @@ -1,4 +1,5 @@ import { EsploraClient, Wallet, WalletDB } from '../src/wallet'; +import * as fs from 'fs'; describe('Wallet', () => { let wallet: Wallet; @@ -11,9 +12,9 @@ describe('Wallet', () => { wallet = new Wallet({ db: walletDB, networkClient: new EsploraClient({ - protocol: 'https', - host: 'blockstream.info', - network: 'main', + protocol: 'http', + host: '127.0.0.1:8094', + network: 'regtest', }), }); }); @@ -30,20 +31,21 @@ describe('Wallet', () => { it('should derive first receive address', async () => { const address = await wallet.deriveReceiveAddress(); - expect(address).toBe('bc1qcr8te4kr609gcawutmrza0j4xv80jy8z306fyu'); + expect(address).toBe('bcrt1qcr8te4kr609gcawutmrza0j4xv80jy8zeqchgx'); }); it('should derive second receive address', async () => { const address = await wallet.deriveReceiveAddress(); - expect(address).toBe('bc1qnjg0jd8228aq7egyzacy8cys3knf9xvrerkf9g'); + expect(address).toBe('bcrt1qnjg0jd8228aq7egyzacy8cys3knf9xvr3v5hfj'); }); it('should derive first change address', async () => { const address = await wallet.deriveChangeAddress(); - expect(address).toBe('bc1q8c6fshw2dlwun7ekn9qwf37cu2rn755upcp6el'); + expect(address).toBe('bcrt1q8c6fshw2dlwun7ekn9qwf37cu2rn755ufhry49'); }); afterAll(async () => { await wallet.close(); + fs.rmSync('./test/wallet', { recursive: true, force: true }); }); });