Skip to content

Commit

Permalink
more unit tests! #39
Browse files Browse the repository at this point in the history
  • Loading branch information
enceladus committed Sep 30, 2022
1 parent d522d7b commit 08a0509
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 14 deletions.
17 changes: 4 additions & 13 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Testing AlgonautJS


## setup
- install [`taskfile`](https://taskfile.dev/installation/)
- `brew install go-task/tap/go-task`
Expand Down Expand Up @@ -49,19 +48,11 @@
- [`./init-node-ts-esm`](`./init-node-ts-esm`)


## unit tests (TODO)
tests for algonautjs features include:
- `account-recover.js`
- `algo-bal.js`
- `algo-xfr.js`
- `asset-xfr.js`
- `asset-create.js`
- `app-call.js`
- `app-create.js`
- ...
## unit tests

TBD: use jest/mocha/chai for this
Unit tests for algonaut are in `algonaut.test.ts`. Run them with:

`npm run test`

## notes
- ...
- don't mock code you don't own! APIs change all the time! :)
104 changes: 103 additions & 1 deletion tests/algonaut.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {describe, expect, test, beforeAll, beforeEach, afterEach} from '@jest/globals';

import Algonaut from '../src/index';
import { utils } from '../src/index';

import { accountAppID, bricksID, txnCallApp, txnCloseOutApp, txnCreateAsset, txnDeleteApp, txnOptInApp, txnOptInAsset, txnPayment, txnSendAsset } from './mocks/txns';

import { AlgonautConfig, AlgonautWallet } from '../src/AlgonautTypes';

const validConfig: AlgonautConfig = {
Expand Down Expand Up @@ -73,10 +78,26 @@ describe('isValidConfig tests', () => {
});

// getConfig
describe('getConfig()', () => {
test('getConfig returns config object', () => {
const algonaut = new Algonaut(validConfig);
expect(algonaut.getConfig()).toEqual(validConfig);
});

test('getConfig returns false if Algonaut is not configured', () => {
expect(Algonaut.prototype.getConfig()).toBeFalsy();
});
});

// setConfig
describe('setConfig()', () => {
test('setConfig tests are covered by constructor tests', () => {
expect(true).toBeTruthy();
});
});

// ======= algonaut core ========
describe('offline sync functions', () => {
describe('Algonaut core: offline sync methods', () => {
var algonaut: Algonaut;

beforeEach(() => {
Expand All @@ -95,6 +116,13 @@ describe('offline sync functions', () => {
expect((algonaut.account as any).sk).toBeDefined();
})

test('utils.createAccount also works', () => {
var wallet = utils.createWallet();
expect(wallet).toBeDefined();
expect(typeof wallet.address).toBe('string');
expect(typeof wallet.mnemonic).toBe('string');
});

// recoverAccount
test('recoverAccount works with a newly created wallet', () => {
let account = algonaut.createWallet();
Expand All @@ -103,16 +131,90 @@ describe('offline sync functions', () => {
expect(recoveredAccount.sk).toBeDefined();
})

test('utils.recoverAccount also works', () => {
const wallet = utils.createWallet();
let recoveredAccount: any = utils.recoverAccount(wallet.mnemonic);
expect(recoveredAccount.addr).toBeDefined();
expect(recoveredAccount.sk).toBeDefined();
})

// decodeBase64UnsignedTransaction
// decodeStateArray
// encodeArguments
// fromBase64
test('fromBase64 decodes base64-encoded text', () => {
expect(algonaut.fromBase64('SGVsbG8gV29ybGQ=')).toBe('Hello World');
})

test('utils.fromBase64 decodes base64-encoded text', () => {
expect(utils.fromBase64('SGVsbG8gV29ybGQ=')).toBe('Hello World');
})
// signBase64Transactions
// signTransactionGroup
// stateArrayToObject
// setAccount
// to8Arr
test('to8Arr returns Uint8Array', () => {
expect(algonaut.to8Arr('test note')).toBeInstanceOf(Uint8Array);
})

test('utils.to8Arr returns Uint8Array', () => {
expect(utils.to8Arr('test note')).toBeInstanceOf(Uint8Array);
})

// txnSummary
describe('txnSummary tests', () => {
test('txnSummary takes in a txn and returns a string', () => {
const summary = utils.txnSummary(txnPayment);
expect(typeof summary).toBe('string');
})

test('identifies payment txn', () => {
const summary = utils.txnSummary(txnPayment);
expect(summary.includes('Send')).toBeTruthy();
})

test('identifies opt in asset txn', () => {
const summary = utils.txnSummary(txnOptInAsset);
expect (summary.includes(`Opt-in to asset ID ${bricksID}`)).toBeTruthy();
})

test('identifies asset xfer', () => {
const summary = utils.txnSummary(txnSendAsset);
expect(summary.includes(`Transfer 1 of asset ID ${bricksID}`)).toBeTruthy();
})

test('identifies create asset', () => {
const summary = utils.txnSummary(txnCreateAsset);
expect(summary.includes(`Create asset Test Asset, symbol TEST`)).toBeTruthy();
})

test('identifies call app', () => {
const summary = utils.txnSummary(txnCallApp);
expect(summary.includes(`Call to application ID ${accountAppID}`)).toBeTruthy();
})

test('identifies opt in app', () => {
const summary = utils.txnSummary(txnOptInApp);
expect(summary.includes(`Opt-in to application ID ${accountAppID}`)).toBeTruthy();
})

test('identifies close out app', () => {
const summary = utils.txnSummary(txnCloseOutApp);
expect(summary.includes(`Close out application ID ${accountAppID}`)).toBeTruthy();
})

test('identifies delete app', () => {
const summary = utils.txnSummary(txnDeleteApp);
expect(summary.includes(`Delete application ID ${accountAppID}`)).toBeTruthy();
})

// test('identifies update app', () => {
// const summary = utils.txnSummary(txnUpdateApp);
// expect(summary.includes(`Update application ID ${accountAppID}`)).toBeTruthy();
// })
})

// getAppEscrowAccount
// valueAsAddr
})
Expand Down
74 changes: 74 additions & 0 deletions tests/mocks/txns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const algosdk = require('algosdk');

const mockParams = {
fee: 0,
firstRound: 24477887,
flatFee: false,
genesisHash: "SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=",
genesisID: "testnet-v1.0",
lastRound: 24478887
}

const account = algosdk.generateAccount();

const bricksID = 106237109;
const accountAppID = 101088323;

module.exports = {
bricksID: bricksID,
accountAppID: accountAppID,
txnPayment: algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: account.addr,
to: algosdk.generateAccount().addr,
amount: 111,
suggestedParams: mockParams
}),
txnOptInAsset: algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: account.addr,
to: account.addr,
suggestedParams: mockParams,
assetIndex: bricksID,
amount: 0
}),
txnSendAsset: algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: account.addr,
to: algosdk.generateAccount().addr,
suggestedParams: mockParams,
assetIndex: bricksID,
amount: 1
}),
txnCreateAsset: algosdk.makeAssetCreateTxnWithSuggestedParams(
account.addr,
new Uint8Array(),
1000,
3,
false,
account.addr,
account.addr,
account.addr,
account.addr,
'TEST',
'Test Asset',
'https://asset.url',
'',
mockParams
),
txnCallApp: algosdk.makeApplicationNoOpTxnFromObject({
from: account.addr,
suggestedParams: mockParams,
appIndex: accountAppID,
// appArgs: [new Uint8Array()]
}),
txnOptInApp: algosdk.makeApplicationOptInTxnFromObject({
from: account.addr,
appIndex: accountAppID,
suggestedParams: mockParams
}),
txnCloseOutApp: algosdk.makeApplicationCloseOutTxnFromObject({
from: account.addr,
suggestedParams: mockParams,
appIndex: accountAppID,
// appArgs: [new Uint8Array()]
}),
txnDeleteApp: algosdk.makeApplicationDeleteTxn(account.addr, mockParams, accountAppID),
}

0 comments on commit 08a0509

Please sign in to comment.