Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 29, 2023
1 parent a5affdc commit f790539
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/nutshell-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CI Test Workflow

on: [push, pull_request]

jobs:
build-and-test:
runs-on: ubuntu-latest

services:
docker:
image: docker:19.03.12
ports:
- 3338:3338
options: --privileged

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
repository: 'cashubtc/nutshell'
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Build and Run Docker Container
run: |
docker build -t nutshell-app .
docker run -d -e MINT_LIGHTNING_BACKEND=FakeWallet -p 3338:3338 nutshell-app
- name: Execute Command Inside the Container
run: |
docker exec $(docker ps -q) poetry run mint --port 3338 --host 0.0.0.0
- name: Run Tests
uses: actions/setup-node@v3
with:
node-version: 20x
cache: 'npm'
- run: npm ci
- run: npm run compile
- run: npm test
119 changes: 119 additions & 0 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { CashuMint } from '../src/CashuMint.js';
import { CashuWallet } from '../src/CashuWallet.js';

import dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

const externalInvoice =
'lnbc20u1p3u27nppp5pm074ffk6m42lvae8c6847z7xuvhyknwgkk7pzdce47grf2ksqwsdpv2phhwetjv4jzqcneypqyc6t8dp6xu6twva2xjuzzda6qcqzpgxqyz5vqsp5sw6n7cztudpl5m5jv3z6dtqpt2zhd3q6dwgftey9qxv09w82rgjq9qyyssqhtfl8wv7scwp5flqvmgjjh20nf6utvv5daw5h43h69yqfwjch7wnra3cn94qkscgewa33wvfh7guz76rzsfg9pwlk8mqd27wavf2udsq3yeuju';

let request: Record<string, string> | undefined;
const mintUrl = 'http://localhost:3338';

describe('mint api', () => {
test('get keys', async () => {
const mint = new CashuMint(mintUrl);
const keys = await mint.getKeys();
expect(keys).toBeDefined();
});
test('get keysets', async () => {
const mint = new CashuMint(mintUrl);
const keysets = await mint.getKeySets();
expect(keysets).toBeDefined();
expect(keysets.keysets).toBeDefined();
expect(keysets.keysets.length).toBeGreaterThan(0);
});

test('get info', async () => {
const mint = new CashuMint(mintUrl);
const info = await mint.getInfo();
expect(info).toBeDefined();
});
test('request mint', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const request = await wallet.requestMint(100);
expect(request).toBeDefined();
});
test('mint tokens', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const request = await wallet.requestMint(1337);
expect(request).toBeDefined();
expect(request.request).toContain('lnbc1337');
const tokens = await wallet.requestTokens(1337, request.quote);
expect(tokens).toBeDefined();
// expect that the sum of all tokens.proofs.amount is equal to the requested amount
expect(tokens.proofs.reduce((a, b) => a + b.amount, 0)).toBe(1337);
});
test('get fee for local invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const request = await wallet.requestMint(100);
const fee = await wallet.getFee(request.request);
expect(fee).toBeDefined();
// because local invoice, fee should be 0
expect(fee).toBe(0);
});
test('get fee for external invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const fee = await wallet.getFee(externalInvoice);
expect(fee).toBeDefined();
// because external invoice, fee should be > 0
expect(fee).toBeGreaterThan(0);
});
test('pay local invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const request = await wallet.requestMint(100);
const tokens = await wallet.requestTokens(100, request.quote);

// expect no fee because local invoice
const requestToPay = await wallet.requestMint(10);
const fee = await wallet.getFee(requestToPay.request);
expect(fee).toBe(0);

const sendResponse = await wallet.send(10, tokens.proofs);
const response = await wallet.payLnInvoice(requestToPay.request, sendResponse.send);
expect(response).toBeDefined();
// expect that we have received the fee back, since it was internal
expect(response.change.reduce((a, b) => a + b.amount, 0)).toBe(fee);

// check states of spent and kept proofs after payment
const sentProofsSpent = await wallet.checkProofsSpent(sendResponse.send)
expect(sentProofsSpent).toBeDefined();
// expect that all proofs are spent, i.e. sendProofsSpent == sendResponse.send
expect(sentProofsSpent).toEqual(sendResponse.send);
// expect none of the sendResponse.returnChange to be spent
const returnChangeSpent = await wallet.checkProofsSpent(sendResponse.returnChange)
expect(returnChangeSpent).toBeDefined();
expect(returnChangeSpent).toEqual([]);
});
test('pay external invoice', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);
const request = await wallet.requestMint(3000);
const tokens = await wallet.requestTokens(3000, request.quote);

const fee = await wallet.getFee(externalInvoice);
expect(fee).toBeGreaterThan(0);

const sendResponse = await wallet.send(2000 + fee, tokens.proofs);
const response = await wallet.payLnInvoice(externalInvoice, sendResponse.send);

expect(response).toBeDefined();
// expect that we have received the fee back, since it was internal
expect(response.change.reduce((a, b) => a + b.amount, 0)).toBe(fee);

// check states of spent and kept proofs after payment
const sentProofsSpent = await wallet.checkProofsSpent(sendResponse.send)
expect(sentProofsSpent).toBeDefined();
// expect that all proofs are spent, i.e. sendProofsSpent == sendResponse.send
expect(sentProofsSpent).toEqual(sendResponse.send);
// expect none of the sendResponse.returnChange to be spent
const returnChangeSpent = await wallet.checkProofsSpent(sendResponse.returnChange)
expect(returnChangeSpent).toBeDefined();
expect(returnChangeSpent).toEqual([]);
});
});

0 comments on commit f790539

Please sign in to comment.