Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix old tests, add test runner, & github actions #51

Merged
merged 6 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Node.js CI
on:
push:
branches: ['main']
pull_request:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 20.x
- latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm run fmt
- run: npm ci
- run: npm run lint
- run: npm run test
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/**/*
47 changes: 47 additions & 0 deletions dashwallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@
Wallet.DENOM_SATS = [];
amountsToSats(Wallet.DENOM_AMOUNTS, Wallet.DENOM_SATS);

{
let __LAST_DENOM__ = Wallet.DENOM_SATS.length - 1;
Wallet.__UNIT_TEST_DENOM_INFO__ = {
__DENOMS__: Wallet.DENOM_SATS,
__MIN_DENOM__: Wallet.DENOM_SATS[__LAST_DENOM__],
__STAMP__: 200,
__MIN_STAMPS__: 2,
};
}

/**
* @param {Array<Number>} amounts
* @param {Array<Number>} sats
Expand Down Expand Up @@ -2765,6 +2775,10 @@
* @returns {CoinInfo} - faceValue, stamps, etc
*/
Wallet._parseCoinInfo = function (d, satoshis) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

let MIN_DENOM = d.__MIN_DENOM__;
let STAMP = d.__STAMP__;
let mdash = satoshis / MIN_DENOM;
Expand Down Expand Up @@ -2804,6 +2818,10 @@
* @returns {Array<UtxoCoinInfo>}
*/
Wallet._parseUtxosInfo = function (d, utxos) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

/** @type {Array<UtxoCoinInfo>} */
let utxoInfos = [];

Expand All @@ -2825,6 +2843,11 @@
* @returns CoinSelection
*/
Wallet._pairInputsToOutputs = function (d, sendInfo, denomInfos) {
/* jshint maxstatements: 125 */
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

let MIN_STAMPS = d.__MIN_STAMPS__;
let STAMP = d.__STAMP__;
let DENOMS = d.__DENOMS__;
Expand Down Expand Up @@ -3175,6 +3198,10 @@
* @returns {DenomSorter}
*/
Wallet._byLeastChangeLowestValue = function (d, faceValue) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

/** @type {DenomSorter} */
function sorter(a, b) {
let fa = a.faceValue - faceValue;
Expand Down Expand Up @@ -3234,6 +3261,10 @@
* @returns {SendInfo}
*/
Wallet._parseSendInfo = function (d, satoshis) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

let MIN_DENOM = d.__MIN_DENOM__;
let MIN_STAMPS = d.__MIN_STAMPS__;
let STAMP = d.__STAMP__;
Expand Down Expand Up @@ -3286,6 +3317,10 @@
* @return {DenomInfo}
*/
Wallet._denominateCoin = function (d, coinInfo, force = false) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

let DENOMS = d.__DENOMS__;
let STAMP = d.__STAMP__;

Expand Down Expand Up @@ -3344,6 +3379,10 @@
* @return {DenomInfo}
*/
Wallet._denominateSelfPayCoins = function (d, coinInfos, _outputs = []) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

let DENOMS = d.__DENOMS__;
let STAMP = d.__STAMP__;
let MIN_STAMPS = d.__MIN_STAMPS__;
Expand Down Expand Up @@ -3487,6 +3526,10 @@
* @return {DenomInfo}
*/
Wallet._denominateToOutputs = function (d, coinInfos, _outputs = []) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

let SATS_NUM_DIGITS = 8;

let DENOMS = d.__DENOMS__;
Expand Down Expand Up @@ -3659,6 +3702,10 @@
* @return {DenomInfo}
*/
Wallet._denominateCoins = function (d, coinInfos, force = false) {
if (d === null) {
d = Wallet.__UNIT_TEST_DENOM_INFO__;
}

let DENOMS = d.__DENOMS__;
let STAMP = d.__STAMP__;

Expand Down
12 changes: 6 additions & 6 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": [
"*.js",
"bin/**/*.js",
"lib/**/*.js",
"src/**/*.js"
],
"*.js",
"bin/**/*.js",
"lib/**/*.js",
"src/**/*.js"
],
"exclude": ["node_modules"]
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"scripts": {
"bump": "npm version -m \"chore(release): bump to v%s\"",
"fmt": "npm run prettier",
"lint": "npm run tsc && npm run jshint",
"lint": "npm run jshint",
"--------------": "-----------------------------------------",
"jshint": "npx -p [email protected] -- jshint -c ./.jshintrc./*.js",
"jshint": "npx -p [email protected] -- jshint -c ./.jshintrc ./*.js",
"prettier": "npx -p [email protected] -- prettier -w '**/*.{js,md}'",
"reexport-types": "npx -p [email protected] -- reexport",
"tsc": "npx -p [email protected] -- tsc -p ./jsconfig.json",
"-------------": "------------------------------------------",
"prepublish": "npm run reexport-types",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node ./tests/"
},
"repository": {
"type": "git",
Expand Down
13 changes: 7 additions & 6 deletions tests/coin-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

let Wallet = require("../dashwallet.js");

let STAMP = 200;
let MIN_DENOM = 100000;
let DENOM_INFO = null;

function test() {
// 0.00100000
Expand Down Expand Up @@ -81,8 +80,9 @@ function test() {
},
];

console.info(` Sat Val: Face, Stamps, Dust ✅`);
for (let row of table) {
let result = Wallet._parseCoinInfo(MIN_DENOM, STAMP, row.satoshis);
let result = Wallet._parseCoinInfo(DENOM_INFO, row.satoshis);
let unmetExpectations = [];

if (result.faceValue !== row.faceValue) {
Expand Down Expand Up @@ -110,14 +110,15 @@ function test() {
sats = sats.padStart(10, " ");

let faceValue = row.faceValue.toString();
faceValue = faceValue.slice(0, 4);
faceValue = faceValue.padStart(4, " ");
faceValue = faceValue.slice(0, -5);
faceValue = faceValue.padStart(6, " ");

let stamps = row.stamps.toString();
stamps = stamps.padStart(3, " ");
stamps = stamps.padStart(6, " ");

let dust = row.dust.toString();
dust = dust.padStart(3, "0");
dust = dust.padStart(4, " ");
console.info(` - ${sats}: ${faceValue}, ${stamps}, ${dust} ✅`);
}
}
Expand Down
12 changes: 3 additions & 9 deletions tests/denom-force.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

let Wallet = require("../dashwallet.js");

let STAMP = 200;
let MIN_DENOM = 100000;
let DENOM_INFO = null;

// TODO
// - what's the fee to split as-is?
Expand Down Expand Up @@ -177,17 +176,12 @@ function test() {

console.info(` Satoshis Value Fee Stamps | Coins`);
for (let row of table) {
let coinInfo = Wallet._parseCoinInfo(MIN_DENOM, STAMP, row.satoshis);
let coinInfo = Wallet._parseCoinInfo(DENOM_INFO, row.satoshis);

let denomInfo;
let code;
try {
denomInfo = Wallet._denominateCoin(
Wallet.DENOM_SATS,
STAMP,
coinInfo,
row.force,
);
denomInfo = Wallet._denominateCoin(DENOM_INFO, coinInfo, row.force);
} catch (e) {
if (!e.code) {
throw e;
Expand Down
7 changes: 3 additions & 4 deletions tests/denom-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

let Wallet = require("../dashwallet.js");

let STAMP = 200;
let MIN_DENOM = 100000;
let DENOM_INFO = null;

// TODO
// - what's the fee to split as-is?
Expand Down Expand Up @@ -140,7 +139,7 @@ function test() {

console.info(` Satoshis Value Fee Stamps | Coins`);
for (let row of table) {
let coinInfo = Wallet._parseCoinInfo(MIN_DENOM, STAMP, row.satoshis);
let coinInfo = Wallet._parseCoinInfo(DENOM_INFO, row.satoshis);
let v = calcResult(row, coinInfo);
console.info(
`✅ ${v.satoshis} ${v.faceDash} ${v.fee} ${v.stampsPerCoin} | ${v.dashDenomsList}`,
Expand All @@ -149,7 +148,7 @@ function test() {
}

function calcResult(row, coinInfo) {
let result = Wallet._denominateCoin(Wallet.DENOM_SATS, STAMP, coinInfo);
let result = Wallet._denominateCoin(DENOM_INFO, coinInfo);

assertExpectedValues(row, result);

Expand Down
101 changes: 101 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"use strict";

let ChildProcess = require("child_process");
let Fs = require("node:fs/promises");
let Path = require("node:path");

async function main() {
let dirents = await Fs.readdir(__dirname, { withFileTypes: true });

let failures = 0;
let count = 0;
for (let dirent of dirents) {
if (dirent.name === "index.js") {
continue;
}

count += 1;
let direntPath = Path.join(__dirname, dirent.name);
let relPath = Path.relative(".", direntPath);

let success = await handleEach(count, relPath);
if (!success) {
failures += 1;
}
}
if (failures === 0) {
console.info("# PASS");
} else {
console.info("# FAIL");
}

console.info("TAP version 13");

if (failures !== 0) {
process.exit(1);
}
}

async function handleEach(count, relPath) {
let success = await exec("node", [relPath])
.then(function (result) {
console.info(`ok ${count} - ${relPath}`);
return true;
})
.catch(function (err) {
console.info(`not ok ${count} - ${relPath}`);
if (err.code) {
console.info(` # Error: ${err.code}`);
}
if (err.stderr) {
console.info(` # Stderr: ${err.stderr}`);
}
return false;
});

return success;
}

async function exec(exe, args) {
return new Promise(function (resolve, reject) {
let cmd = ChildProcess.spawn(exe, args);

let stdout = [];
let stderr = [];

cmd.stdout.on("data", function (data) {
stdout.push(data.toString("utf8"));
});

cmd.stderr.on("data", function (data) {
stderr.push(data.toString("utf8"));
});

cmd.on("close", function (code) {
let result = {
code: code,
stdout: stdout.join(""),
stderr: stderr.join(""),
};

if (!code) {
resolve(result);
return;
}

let err = new Error(result.stderr);
Object.assign(err, result);
reject(err);
});
});
}

main()
.then(function () {
process.exit(0);
})
.catch(function (err) {
console.error("Fail:");
console.error(err.stack || err);
process.exit(1);
});
Loading
Loading