Skip to content

Commit

Permalink
feat: added docker-compose detup test environment and use regtest for…
Browse files Browse the repository at this point in the history
… testing

Signed-off-by: theanmolsharma <[email protected]>
  • Loading branch information
theanmolsharma committed Jan 6, 2024
1 parent c58cbdd commit 80b8765
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Start test containers
run: docker-compose -f "./test/helpers/docker-compose.yaml" up -d
- name: Setup node
uses: actions/setup-node@v3
with:
Expand All @@ -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.yaml" logs esplora
- name: Stop test containers
if: always()
run: docker-compose -f "./test/helpers/docker-compose.yaml" down
6 changes: 3 additions & 3 deletions test/esplora.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});

Expand Down
36 changes: 36 additions & 0 deletions test/helpers/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions test/wallet-db.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WalletDB } from '../src/wallet';
import fs from 'fs';

describe('Wallet DB', () => {
let walletDB: WalletDB;
Expand All @@ -25,5 +26,6 @@ describe('Wallet DB', () => {

afterAll(async () => {
await walletDB.close();
fs.rmSync('./test/wallet-db', { recursive: true, force: true });
});
});
14 changes: 8 additions & 6 deletions test/wallet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EsploraClient, Wallet, WalletDB } from '../src/wallet';
import * as fs from 'fs';

describe('Wallet', () => {
let wallet: Wallet;
Expand All @@ -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',
}),
});
});
Expand All @@ -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 });
});
});

0 comments on commit 80b8765

Please sign in to comment.