Skip to content

Commit

Permalink
feat: test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-haynes committed Jun 18, 2024
1 parent d7edfea commit ae7ef28
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 79 deletions.
2 changes: 1 addition & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"compile": "tsc -p tsconfig.json",
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest test"
"test": "jest"
},
"keywords": [],
"author": "",
Expand Down
24 changes: 12 additions & 12 deletions packages/accounts/test/account.access_key.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { beforeAll, beforeEach, expect, test } from '@jest/globals';
import { KeyPair } from '@near-js/crypto';

import testUtils from './test-utils';
import { createAccount, deployContract, generateUniqueString, networkId, setUpTestConnection } from './test-utils';

let nearjs;
let workingAccount;
let contractId;
let contract;

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
nearjs = await setUpTestConnection();
});

beforeEach(async () => {
try {

contractId = testUtils.generateUniqueString('test');
workingAccount = await testUtils.createAccount(nearjs);
contract = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractId);
contractId = generateUniqueString('test');
workingAccount = await createAccount(nearjs);
contract = await deployContract(nearjs.accountCreator.masterAccount, contractId);
} catch (e) {
console.error(e);
}
Expand All @@ -28,8 +28,8 @@ test('make function call using access key', async() => {
await workingAccount.addKey(keyPair.getPublicKey(), contractId, '', '2000000000000000000000000');

// Override in the key store the workingAccount key to the given access key.
await nearjs.connection.signer.keyStore.setKey(testUtils.networkId, workingAccount.accountId, keyPair);
const setCallValue = testUtils.generateUniqueString('setCallPrefix');
await nearjs.connection.signer.keyStore.setKey(networkId, workingAccount.accountId, keyPair);
const setCallValue = generateUniqueString('setCallPrefix');
await contract.setValue({ args: { value: setCallValue } });
expect(await contract.getValue()).toEqual(setCallValue);
});
Expand All @@ -40,28 +40,28 @@ test('remove access key no longer works', async() => {
await nearjs.accountCreator.masterAccount.addKey(publicKey, contractId, '', 400000);
await nearjs.accountCreator.masterAccount.deleteKey(publicKey);
// Override in the key store the workingAccount key to the given access key.
await nearjs.connection.signer.keyStore.setKey(testUtils.networkId, nearjs.accountCreator.masterAccount.accountId, keyPair);
await nearjs.connection.signer.keyStore.setKey(networkId, nearjs.accountCreator.masterAccount.accountId, keyPair);
let failed = true;
try {
await contract.setValue({ args: { value: 'test' } });
failed = false;
} catch (e) {
expect(e.message).toEqual(`Can not sign transactions for account ${nearjs.accountCreator.masterAccount.accountId} on network ${testUtils.networkId}, no matching key pair exists for this account`);
expect(e.message).toEqual(`Can not sign transactions for account ${nearjs.accountCreator.masterAccount.accountId} on network ${networkId}, no matching key pair exists for this account`);
expect(e.type).toEqual('KeyNotFound');
}

if (!failed) {
throw new Error('should throw an error');
}

nearjs = await testUtils.setUpTestConnection();
nearjs = await setUpTestConnection();
});

test('view account details after adding access keys', async() => {
const keyPair = KeyPair.fromRandom('ed25519');
await nearjs.accountCreator.masterAccount.addKey(keyPair.getPublicKey(), contractId, '', 1000000000);

const contract2 = await testUtils.deployContract(nearjs.accountCreator.masterAccount, testUtils.generateUniqueString('test_contract2'));
const contract2 = await deployContract(nearjs.accountCreator.masterAccount, generateUniqueString('test_contract2'));
const keyPair2 = KeyPair.fromRandom('ed25519');
await nearjs.accountCreator.masterAccount.addKey(keyPair2.getPublicKey(), contract2.contractId, '', 2000000000);

Expand Down Expand Up @@ -96,7 +96,7 @@ test('loading account after adding a full key', async() => {

test('load invalid key pair', async() => {
// Override in the key store with invalid key pair
await nearjs.connection.signer.keyStore.setKey(testUtils.networkId, nearjs.accountCreator.masterAccount.accountId, '');
await nearjs.connection.signer.keyStore.setKey(networkId, nearjs.accountCreator.masterAccount.accountId, '');
let failed = true;
try {
await contract.setValue({ args: { value: 'test' } });
Expand Down
49 changes: 24 additions & 25 deletions packages/accounts/test/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import { BlockResult, TypedError } from '@near-js/types';
import * as fs from 'fs';

import { Account, Contract } from '../src';
import testUtils from './test-utils';
import { createAccount, generateUniqueString, HELLO_WASM_PATH, HELLO_WASM_BALANCE, networkId, setUpTestConnection } from './test-utils';

let nearjs;
let workingAccount;

const { HELLO_WASM_PATH, HELLO_WASM_BALANCE } = testUtils;

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
workingAccount = await testUtils.createAccount(nearjs);
nearjs = await setUpTestConnection();
workingAccount = await createAccount(nearjs);
});

afterAll(async () => {
Expand All @@ -27,40 +26,40 @@ test('view pre-defined account works and returns correct name', async () => {
});

test('create account and then view account returns the created account', async () => {
const newAccountName = testUtils.generateUniqueString('test');
const newAccountName = generateUniqueString('test');
const newAccountPublicKey = '9AhWenZ3JddamBoyMqnTbp7yVbRuvqAv3zwfrWgfVRJE';
const { amount } = await workingAccount.state();
const newAmount = BigInt(amount) / BigInt(10);
const newAmount = BigInt(amount) / 10n;
await nearjs.accountCreator.masterAccount.createAccount(newAccountName, newAccountPublicKey, newAmount);
const newAccount = new Account(nearjs.connection, newAccountName);
const state = await newAccount.state();
expect(state.amount).toEqual(newAmount.toString());
});

test('send money', async() => {
const sender = await testUtils.createAccount(nearjs);
const receiver = await testUtils.createAccount(nearjs);
const sender = await createAccount(nearjs);
const receiver = await createAccount(nearjs);
const { amount: receiverAmount } = await receiver.state();
await sender.sendMoney(receiver.accountId, BigInt(10000));
await sender.sendMoney(receiver.accountId, 10000n);
const state = await receiver.state();
expect(state.amount).toEqual((BigInt(receiverAmount) + BigInt(10000)).toString());
expect(state.amount).toEqual((BigInt(receiverAmount) + 10000n).toString());
});

test('send money through signAndSendTransaction', async() => {
const sender = await testUtils.createAccount(nearjs);
const receiver = await testUtils.createAccount(nearjs);
const sender = await createAccount(nearjs);
const receiver = await createAccount(nearjs);
const { amount: receiverAmount } = await receiver.state();
await sender.signAndSendTransaction({
receiverId: receiver.accountId,
actions: [actionCreators.transfer(BigInt(10000))],
actions: [actionCreators.transfer(10000n)],
});
const state = await receiver.state();
expect(state.amount).toEqual((BigInt(receiverAmount) + BigInt(10000)).toString());
expect(state.amount).toEqual((BigInt(receiverAmount) + 10000n).toString());
});

test('delete account', async() => {
const sender = await testUtils.createAccount(nearjs);
const receiver = await testUtils.createAccount(nearjs);
const sender = await createAccount(nearjs);
const receiver = await createAccount(nearjs);
await sender.deleteAccount(receiver.accountId);
// @ts-expect-error test input
const reloaded = new Account(sender.connection, sender);
Expand All @@ -79,7 +78,7 @@ test('multiple parallel transactions', async () => {
});

test('findAccessKey returns the same access key when fetched simultaneously', async() => {
const account = await testUtils.createAccount(nearjs);
const account = await createAccount(nearjs);

const [key1, key2] = await Promise.all([
// @ts-expect-error test input
Expand Down Expand Up @@ -119,11 +118,11 @@ describe('errors', () => {

describe('with deploy contract', () => {
let logs;
const contractId = testUtils.generateUniqueString('test_contract');
const contractId = generateUniqueString('test_contract');
let contract;

beforeAll(async () => {
const newPublicKey = await nearjs.connection.signer.createKey(contractId, testUtils.networkId);
const newPublicKey = await nearjs.connection.signer.createKey(contractId, networkId);
const data = fs.readFileSync(HELLO_WASM_PATH);
await nearjs.accountCreator.masterAccount.createAndDeployContract(contractId, newPublicKey, data, HELLO_WASM_BALANCE);
// @ts-expect-error test input
Expand Down Expand Up @@ -172,7 +171,7 @@ describe('with deploy contract', () => {
});
expect(result).toEqual('hello trex');

const setCallValue = testUtils.generateUniqueString('setCallPrefix');
const setCallValue = generateUniqueString('setCallPrefix');
const result2 = await workingAccount.functionCall({
contractId,
methodName: 'setValue',
Expand All @@ -186,7 +185,7 @@ describe('with deploy contract', () => {
});

test('view contract state', async() => {
const setCallValue = testUtils.generateUniqueString('setCallPrefix');
const setCallValue = generateUniqueString('setCallPrefix');
await workingAccount.functionCall({
contractId,
methodName: 'setValue',
Expand All @@ -212,14 +211,14 @@ describe('with deploy contract', () => {
const result = await contract.hello({ name: 'trex' });
expect(result).toEqual('hello trex');

const setCallValue = testUtils.generateUniqueString('setCallPrefix');
const setCallValue = generateUniqueString('setCallPrefix');
const result2 = await contract.setValue({ args: { value: setCallValue } });
expect(result2).toEqual(setCallValue);
expect(await contract.getValue()).toEqual(setCallValue);
});

test('view function calls by block Id and finality', async() => {
const setCallValue1 = testUtils.generateUniqueString('setCallPrefix');
const setCallValue1 = generateUniqueString('setCallPrefix');
const result1 = await contract.setValue({ args: { value: setCallValue1 } });
expect(result1).toEqual(setCallValue1);
expect(await contract.getValue()).toEqual(setCallValue1);
Expand Down Expand Up @@ -251,7 +250,7 @@ describe('with deploy contract', () => {
blockQuery: { blockId: blockIndex1 },
})).toEqual(setCallValue1);

const setCallValue2 = testUtils.generateUniqueString('setCallPrefix');
const setCallValue2 = generateUniqueString('setCallPrefix');
const result2 = await contract.setValue({ args: { value: setCallValue2 } });
expect(result2).toEqual(setCallValue2);
expect(await contract.getValue()).toEqual(setCallValue2);
Expand Down Expand Up @@ -298,7 +297,7 @@ describe('with deploy contract', () => {
});

test('make function calls via contract with gas', async() => {
const setCallValue = testUtils.generateUniqueString('setCallPrefix');
const setCallValue = generateUniqueString('setCallPrefix');
const result2 = await contract.setValue({
args: { value: setCallValue },
gas: 1000000 * 1000000
Expand Down
19 changes: 9 additions & 10 deletions packages/accounts/test/account_multisig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fs from 'fs';
import semver from 'semver';

import { Account2FA, MULTISIG_DEPOSIT, MULTISIG_GAS, MultisigStateStatus } from '../src';
import testUtils from './test-utils';
import { createAccount, setUpTestConnection } from './test-utils';

const { functionCall, transfer } = actionCreators;

Expand All @@ -18,7 +18,6 @@ const getAccount2FA = async (account, keyMapping = ({ public_key: publicKey }) =
// modifiers to functions replaces contract helper (CH)
const { accountId } = account;
const keys = await account.getAccessKeys();
// @ts-expect-error test input
const account2fa: any = new Account2FA(nearjs.connection, accountId, {
// skip this (not using CH)
getCode: () => {},
Expand Down Expand Up @@ -51,7 +50,7 @@ const getAccount2FA = async (account, keyMapping = ({ public_key: publicKey }) =
};

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
nearjs = await setUpTestConnection();
const nodeStatus = await nearjs.connection.provider.status();
startFromVersion = (version) => semver.gte(nodeStatus.version.version, version);
console.log(startFromVersion);
Expand All @@ -60,7 +59,7 @@ beforeAll(async () => {
describe('deployMultisig key rotations', () => {

test('full access key if recovery method is "ledger" or "phrase", limited access key if "phone"', async () => {
const account = await testUtils.createAccount(nearjs);
const account = await createAccount(nearjs);
await account.addKey(KeyPair.fromRandom('ed25519').getPublicKey());
await account.addKey(KeyPair.fromRandom('ed25519').getPublicKey());
const keys = await account.getAccessKeys();
Expand All @@ -81,7 +80,7 @@ describe('deployMultisig key rotations', () => {
describe('account2fa transactions', () => {

test('add app key before deployMultisig', async() => {
let account = await testUtils.createAccount(nearjs);
let account = await createAccount(nearjs);
const appPublicKey = KeyPair.fromRandom('ed25519').getPublicKey();
const appAccountId = 'foobar';
const appMethodNames = ['some_app_stuff','some_more_app_stuff'];
Expand All @@ -97,7 +96,7 @@ describe('account2fa transactions', () => {
});

test('add app key', async() => {
let account = await testUtils.createAccount(nearjs);
let account = await createAccount(nearjs);
account = await getAccount2FA(account);
const appPublicKey = KeyPair.fromRandom('ed25519').getPublicKey();
const appAccountId = 'foobar';
Expand All @@ -113,8 +112,8 @@ describe('account2fa transactions', () => {
});

test('send money', async() => {
let sender = await testUtils.createAccount(nearjs);
let receiver = await testUtils.createAccount(nearjs);
let sender = await createAccount(nearjs);
let receiver = await createAccount(nearjs);
sender = await getAccount2FA(sender);
receiver = await getAccount2FA(receiver);
const { amount: receiverAmount } = await receiver.state();
Expand All @@ -124,8 +123,8 @@ describe('account2fa transactions', () => {
});

test('send money through signAndSendTransaction', async() => {
let sender = await testUtils.createAccount(nearjs);
let receiver = await testUtils.createAccount(nearjs);
let sender = await createAccount(nearjs);
let receiver = await createAccount(nearjs);
sender = await getAccount2FA(sender);
receiver = await getAccount2FA(receiver);
const { amount: receiverAmount } = await receiver.state();
Expand Down
12 changes: 6 additions & 6 deletions packages/accounts/test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, beforeAll, describe, expect, jest, test } from '@jest/global
import { PositionalArgsError } from '@near-js/types';

import { Contract, Account } from '../src';
import testUtils from './test-utils';
import { deployContractGuestBook, generateUniqueString, setUpTestConnection } from './test-utils';

const account = Object.setPrototypeOf({
getConnection() {
Expand Down Expand Up @@ -116,8 +116,8 @@ describe('local view execution', () => {
jest.setTimeout(60000);

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
contract = await testUtils.deployContractGuestBook(nearjs.accountCreator.masterAccount, testUtils.generateUniqueString('guestbook'));
nearjs = await setUpTestConnection();
contract = await deployContractGuestBook(nearjs.accountCreator.masterAccount, generateUniqueString('guestbook'));

await contract.add_message({ text: 'first message' });
await contract.add_message({ text: 'second message' });
Expand Down Expand Up @@ -184,9 +184,9 @@ describe('contract without account', () => {
jest.setTimeout(60000);

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
const contractId = testUtils.generateUniqueString('guestbook');
await testUtils.deployContractGuestBook(nearjs.accountCreator.masterAccount, contractId);
nearjs = await setUpTestConnection();
const contractId = generateUniqueString('guestbook');
await deployContractGuestBook(nearjs.accountCreator.masterAccount, contractId);

// @ts-expect-error test input
contract = new Contract(nearjs.connection, contractId, {
Expand Down
1 change: 1 addition & 0 deletions packages/accounts/test/lve_storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('Local View Execution - Storage', () => {
test('load empty cached data', async () => {
const storage = new Storage();

// @ts-expect-error test input
const data = storage.load({});

expect(data).toBe(undefined);
Expand Down
16 changes: 8 additions & 8 deletions packages/accounts/test/promise.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { afterEach, beforeAll, beforeEach, describe, expect, test } from '@jest/globals';
import testUtils from './test-utils';
import { deployContract, generateUniqueString, setUpTestConnection } from './test-utils';

let nearjs;

const CONTRACT_CALL_GAS = BigInt(300000000000000);

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
nearjs = await setUpTestConnection();
});

describe('with promises', () => {
let contract, contract1, contract2;
let oldLog;
let logs;
const contractName = testUtils.generateUniqueString('cnt');
const contractName1 = testUtils.generateUniqueString('cnt');
const contractName2 = testUtils.generateUniqueString('cnt');
const contractName = generateUniqueString('cnt');
const contractName1 = generateUniqueString('cnt');
const contractName2 = generateUniqueString('cnt');

beforeAll(async () => {
contract = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractName);
contract1 = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractName1);
contract2 = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractName2);
contract = await deployContract(nearjs.accountCreator.masterAccount, contractName);
contract1 = await deployContract(nearjs.accountCreator.masterAccount, contractName1);
contract2 = await deployContract(nearjs.accountCreator.masterAccount, contractName2);
});

beforeEach(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path';
import { Account, AccountMultisig, Contract, Connection, LocalAccountCreator } from '../src';
import Config from './config';

const networkId = 'unittest';
export const networkId = 'unittest';

export const HELLO_WASM_PATH = process.env.HELLO_WASM_PATH || 'node_modules/near-hello/dist/main.wasm';
export const HELLO_WASM_BALANCE = BigInt('10000000000000000000000000');
Expand Down
Loading

0 comments on commit ae7ef28

Please sign in to comment.