Skip to content

Commit

Permalink
fix: mocha tests
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Jul 24, 2024
1 parent 843306b commit ccbb8db
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 77 deletions.
7 changes: 4 additions & 3 deletions example/Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,15 @@ class Tests extends Component {
});
// global.fs = require("react-native-fs");
// global.path = require("path-browserify");
global.environment = {
// global.environment = {
// Default to the host machine when running on Android

Check failure on line 132 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Expected indentation of 4 tabs but found 5

Check failure on line 132 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Delete `↹`
// realmBaseUrl: Platform.OS === "android" ? "http://10.0.2.2:9090" : undefined,

Check failure on line 133 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Delete `↹`

Check failure on line 133 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Expected indentation of 4 tabs but found 5
...context,
// ...context,

Check failure on line 134 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Delete `↹`

Check failure on line 134 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Expected indentation of 4 tabs but found 5
// reactNative: Platform.OS,

Check failure on line 135 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Expected indentation of 4 tabs but found 5

Check failure on line 135 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Delete `↹`
// android: Platform.OS === "android",

Check failure on line 136 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Delete `↹`

Check failure on line 136 in example/Tests.tsx

View workflow job for this annotation

GitHub Actions / Run lint check

Expected indentation of 4 tabs but found 5
// ios: Platform.OS === "ios",
};
// };
global.environment = JSON.parse(Buffer.from(context.c as string, 'hex').toString('utf-8'));
// Make the tests reinitializable, to allow test running on changes to the "realm" package
// Probing the existance of `getModules` as this only exists in debug mode
// if ("getModules" in require) {
Expand Down
14 changes: 6 additions & 8 deletions example/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3'
services:
bitcoind:
container_name: bitcoin
Expand Down Expand Up @@ -112,12 +111,10 @@ services:
depends_on:
- bitcoind
expose:
- '18080' # REST
- '18081' # DOCPORT
- '18081' # REST
- '9736' # P2P
- '11001' # RPC
ports:
- '18080:18080'
- '18081:18081'
- '9736:9736'
- '11001:11001'
Expand All @@ -135,11 +132,12 @@ services:
- '--dev-bitcoind-poll=2'
- '--dev-fast-gossip'
- '--grpc-port=11001'
- '--log-file=-' # log to stdo
- '--bitcoin-rpcport=18443'
- '--plugin=/opt/c-lightning-rest/plugin.js'
- '--rest-port=18080'
- '--rest-protocol=http'
- '--rest-docport=18081'
- '--clnrest-port=18081'
- '--clnrest-protocol=http'
- '--clnrest-host=0.0.0.0'
- '--developer'

eclair:
container_name: eclair
Expand Down
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"reinstall": "cd ../lib/ && yarn install && yarn build && cd ../example/ && yarn add ../lib && yarn rn-setup",
"clean": "rm -rf node_modules ios/Pods ios/Podfile.lock ios/build && yarn install && cd ios && pod deintegrate && pod install && cd ../",
"rn-setup": "node rn-setup.js",
"test:mocha": "mocha-remote --context lndmacaroon=$(xxd -ps -u -c 1000 docker/lnd/admin.macaroon) clmacaroon=$(xxd -ps -u -c 1000 docker/clightning/access.macaroon)",
"test:mocha:ios": "mocha-remote --context lndmacaroon=$(xxd -ps -u -c 1000 docker/lnd/admin.macaroon) clmacaroon=$(xxd -ps -u -c 1000 docker/clightning/access.macaroon) -- concurrently --kill-others-on-fail npm:m npm:runner-ios",
"test:mocha:android": "mocha-remote --context lndmacaroon=$(xxd -ps -u -c 1000 docker/lnd/admin.macaroon) clmacaroon=$(xxd -ps -u -c 1000 docker/clightning/access.macaroon) -- concurrently --kill-others-on-fail npm:m npm:runner-android",
"test:mocha": "mocha-remote --context --context c=$(node ./tests/context.js)",
"test:mocha:ios": "mocha-remote --context --context c=$(node ./tests/context.js) -- concurrently --kill-others-on-fail npm:m npm:runner-ios",
"test:mocha:android": "mocha-remote --context --context c=$(node ./tests/context.js) -- concurrently --kill-others-on-fail npm:m npm:runner-android",
"m": "react-native start --reset-cache",
"runner-ios": "react-native run-ios --simulator='iPhone 14' --no-packager",
"runner-android": "react-native run-android --no-packager"
Expand Down
40 changes: 21 additions & 19 deletions example/tests/clightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import {
const bitcoinURL = 'http://polaruser:polarpass@localhost:9091';
const clConfig = {
host: 'localhost',
port: 18080,
macaroon: global.environment.clmacaroon,
port: 18081,
rune: global.environment.lcrune,
};

describe.skip('Clightning', function () {
Expand All @@ -51,7 +51,7 @@ describe.skip('Clightning', function () {
balance = await rpc.getBalance();
}

const { address: lndAddress } = await cl.newAddr();
const { bech32: lndAddress } = await cl.newAddr();
await rpc.sendToAddress(lndAddress, '1');
await rpc.generateToAddress(1, await rpc.getNewAddress());

Expand Down Expand Up @@ -107,6 +107,18 @@ describe.skip('Clightning', function () {
// - make a few payments

await ldk.stop();

if (!skipRemoteBackups) {
const backupRes = await ldk.backupSetup({
network: ENetworks.regtest,
seed: profile.getAccount().seed,
details: backupServerDetails,
});
if (backupRes.isErr()) {
throw backupRes.error;
}
}

const lmStart = await lm.start({
...profile.getStartParams(),
// getBestBlock: getBestBlock,
Expand All @@ -124,17 +136,6 @@ describe.skip('Clightning', function () {
throw lmStart.error;
}

if (!skipRemoteBackups) {
const backupRes = await ldk.backupSetup({
network: ENetworks.regtest,
seed: profile.getAccount().seed,
details: backupServerDetails,
});
if (backupRes.isErr()) {
throw backupRes.error;
}
}

const nodeId = await ldk.nodeId();
if (nodeId.isErr()) {
throw nodeId.error;
Expand All @@ -155,8 +156,9 @@ describe.skip('Clightning', function () {
// wait for peer to be connected
let n = 0;
while (true) {
const peers = await cl.listPeers();
if (peers.some((p) => p.id === nodeId.value)) {
const { peers } = await cl.listPeers();
const peer = peers.find((p) => p.id === nodeId.value);
if (peer && peer.connected) {
break;
}
await sleep(1000);
Expand All @@ -166,10 +168,10 @@ describe.skip('Clightning', function () {
}

// open a channel
await cl.openChannel({
await cl.fundchannel({
id: nodeId.value,
satoshis: '100000',
// private: true,
amount: '100000',
announce: false,
});
await rpc.generateToAddress(10, await rpc.getNewAddress());
await waitForElectrum({ cl: true });
Expand Down
18 changes: 18 additions & 0 deletions example/tests/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const run = require('child_process').execSync;

// read lnd macaroon
const lndmacaroon = fs
.readFileSync('docker/lnd/admin.macaroon')
.toString('hex')
.toUpperCase();

// run command to read clightnng rune
const clightning = run(
'cd docker; docker compose exec --user clightning clightning lightning-cli createrune --regtest',
);
const lcrune = JSON.parse(clightning).rune;

const context = { lndmacaroon, lcrune };
const encoded = Buffer.from(JSON.stringify(context)).toString('hex');
console.log(encoded);
49 changes: 23 additions & 26 deletions example/tests/eclair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe('Eclair', function () {
balance = await rpc.getBalance();
}

// const ecAddress = await ec.getnewaddress();
// await rpc.sendToAddress(ecAddress, '1');
await rpc.generateToAddress(1, await rpc.getNewAddress());
const ecAddress = await ec.getnewaddress();
await rpc.sendToAddress(ecAddress, '1');
await rpc.generateToAddress(6, await rpc.getNewAddress());

const storageRes = await lm.setBaseStoragePath(
`${RNFS.DocumentDirectoryPath}/ldk/`,
Expand Down Expand Up @@ -101,23 +101,6 @@ describe('Eclair', function () {
// - make a few payments

await ldk.stop();
const lmStart = await lm.start({
...profile.getStartParams(),
getFees: () => {
return Promise.resolve({
onChainSweep: 30,
maxAllowedNonAnchorChannelRemoteFee: Math.max(25, 30 * 10),
minAllowedAnchorChannelRemoteFee: 5,
minAllowedNonAnchorChannelRemoteFee: Math.max(5 - 1, 0),
anchorChannelFee: 10,
nonAnchorChannelFee: 20,
channelCloseMinimum: 5,
});
},
});
if (lmStart.isErr()) {
throw lmStart.error;
}

if (!skipRemoteBackups) {
const backupRes = await ldk.backupSetup({
Expand All @@ -130,6 +113,13 @@ describe('Eclair', function () {
}
}

const lmStart = await lm.start({
...profile.getStartParams(),
});
if (lmStart.isErr()) {
throw lmStart.error;
}

const nodeId = await ldk.nodeId();
if (nodeId.isErr()) {
throw nodeId.error;
Expand Down Expand Up @@ -160,12 +150,19 @@ describe('Eclair', function () {
}

// open a channel
await ec.open({
const open = await ec.open({
nodeId: nodeId.value,
fundingSatoshis: 100000,
fundingSatoshis: 10000000,
fundingFeeBudgetSatoshis: 2000,
fundingFeerateSatByte: 1,
announceChannel: false,
// channelType: 'anchor_outputs_zero_fee_htlc_tx',
});

if (open.includes('error')) {
throw open;
}

await rpc.generateToAddress(10, await rpc.getNewAddress());
await waitForElectrum({ ec: true });

Expand All @@ -191,7 +188,7 @@ describe('Eclair', function () {
}
}

// CL -> LDK, 0 sats
// Eclair -> LDK, 0 sats
const inv1 = await lm.createAndStorePaymentRequest({
amountSats: 0,
description: 'trololo',
Expand All @@ -205,7 +202,7 @@ describe('Eclair', function () {
amountMsat: 10000 * 1000, // milli-sats
});

// CL -> LDK, 1000 sats
// Eclair -> LDK, 1000 sats
const inv2 = await lm.createAndStorePaymentRequest({
amountSats: 1000,
description: 'ololo',
Expand All @@ -218,7 +215,7 @@ describe('Eclair', function () {

// FIXME LDK -> ECLAIR doesn't work

// // LDK -> CL, 0 sats
// LDK -> Eclair, 0 sats
// const { serialized: invoice3 } = await ec.createinvoice({
// // amount: 0,
// // label: 'payment3' + new Date(),
Expand All @@ -233,7 +230,7 @@ describe('Eclair', function () {
// throw pay3.error;
// }

// LDK -> CL, 444 sats
// // LDK -> Eclair, 444 sats
// const { serialized: invoice4 } = await ec.createinvoice({
// amountMsat: 444000, // milisats
// // label: 'payment4' + new Date(),
Expand Down
37 changes: 19 additions & 18 deletions example/tests/utils/clightning-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import queryString from 'query-string';

export default class ClightningRPC {
destroyed: boolean = false;
macaroon: string;
rune: string;
host: string;
port: number;
headers: {};

constructor(opts: any = {}) {
if (!opts.macaroon) {
throw new Error('macaroon is required');
if (!opts.rune) {
throw new Error('rune is required');
}
if (!opts.host) {
throw new Error('host is required');
Expand All @@ -18,46 +18,47 @@ export default class ClightningRPC {
throw new Error('port is required');
}

this.macaroon = opts.macaroon;
this.rune = opts.rune;
this.host = opts.host;
this.port = opts.port;

this.headers = {
'Accept': 'application/json',
'Content-type': 'application/json',
macaroon: Buffer.from(this.macaroon, 'hex').toString('base64'),
'Rune': this.rune,
};
}

getInfo(): Promise<any> {
return this._get({ path: '/v1/getinfo' });
getInfo(body?: any): Promise<any> {
return this._post({ path: '/v1/getinfo', body });
}

pay(body): Promise<any> {
pay(body?: any): Promise<any> {
return this._post({ path: '/v1/pay', body });
}

genInvoice(body): Promise<any> {
genInvoice(body?: any): Promise<any> {
return this._post({ path: '/v1/invoice/genInvoice', body });
}

newAddr(): Promise<any> {
return this._get({ path: '/v1/newaddr' });
newAddr(body?: any): Promise<any> {
return this._post({ path: '/v1/newaddr', body });
}

listPeers(): Promise<any> {
return this._get({ path: '/v1/peer/listPeers' });
listPeers(body?: any): Promise<any> {
return this._post({ path: '/v1/listpeers', body });
}

openChannel(body): Promise<any> {
return this._post({ path: '/v1/channel/openChannel', body });
fundchannel(body?: any): Promise<any> {
return this._post({ path: '/v1/fundchannel', body });
}

destroy(): void {
this.destroyed = true;
}

async _post({ path, body }): Promise<any> {
const jsonBody = JSON.stringify(body);
async _post({ path, body }: {path: string; body?: object}): Promise<any> {
const jsonBody = JSON.stringify(body || {});
return await this._request({ method: 'POST', path, body: jsonBody });
}

Expand All @@ -72,7 +73,7 @@ export default class ClightningRPC {
return await this._request({ method: 'GET', path: pathWithParams });
}

async _request(opts): Promise<any> {
async _request(opts: any): Promise<any> {
const { path, body, method } = opts;
const fullPath = 'http://' + this.host + ':' + this.port + path;

Expand Down
4 changes: 4 additions & 0 deletions example/tests/utils/eclair-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export default class EclairRPC {
return this._post({ path: '/peers' });
}

onchainbalance(): Promise<any> {
return this._post({ path: '/onchainbalance' });
}

open(body): Promise<any> {
console.info('openChannel', body);
return this._post({ path: '/open', body });
Expand Down

0 comments on commit ccbb8db

Please sign in to comment.