chore: replace jest with mocha

This commit is contained in:
Justin Starry
2021-02-06 10:59:00 +08:00
committed by Justin Starry
parent 612958ece0
commit c675c67c26
41 changed files with 4635 additions and 6665 deletions

View File

@@ -1,6 +1,8 @@
// @flow
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import {expect} from 'chai';
import {
Account,
@@ -10,15 +12,9 @@ import {
PublicKey,
} from '../src';
import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account';
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch';
import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash';
import {url} from './url';
import {mockConfirmTransaction} from './mockrpc/confirm-transaction';
if (!mockRpcEnabled) {
// Testing max commitment level takes around 20s to complete
jest.setTimeout(30000);
}
import {MOCK_PORT, url} from './url';
import {helpers, mockRpcResponse, mockServer} from './mocks/rpc-http';
import {stubRpcWebSocket, restoreRpcWebSocket} from './mocks/rpc-websockets';
const expectedData = (authorizedPubkey: PublicKey): [string, string] => {
const expectedData = Buffer.alloc(NONCE_ACCOUNT_LENGTH);
@@ -31,257 +27,154 @@ const expectedData = (authorizedPubkey: PublicKey): [string, string] => {
return [expectedData.toString('base64'), 'base64'];
};
test('create and query nonce account', async () => {
const from = new Account();
const nonceAccount = new Account();
const connection = new Connection(url, 'singleGossip');
describe('Nonce', () => {
let connection: Connection;
beforeEach(() => {
connection = new Connection(url);
});
mockRpc.push([
url,
{
if (!process.env.TEST_LIVE) {
beforeEach(() => {
mockServer.start(MOCK_PORT);
stubRpcWebSocket(connection);
});
afterEach(() => {
mockServer.stop();
restoreRpcWebSocket(connection);
});
}
it('create and query nonce account', async () => {
const from = new Account();
const nonceAccount = new Account();
await mockRpcResponse({
method: 'getMinimumBalanceForRentExemption',
params: [NONCE_ACCOUNT_LENGTH, {commitment: 'singleGossip'}],
},
{
error: null,
result: 50,
},
]);
params: [NONCE_ACCOUNT_LENGTH],
value: 50,
});
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
NONCE_ACCOUNT_LENGTH,
);
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
NONCE_ACCOUNT_LENGTH,
);
mockRpc.push([
url,
{
method: 'requestAirdrop',
params: [from.publicKey.toBase58(), minimumAmount * 2],
},
{
error: null,
result:
'1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
await helpers.airdrop({
connection,
address: from.publicKey,
amount: minimumAmount * 2,
});
const signature = await connection.requestAirdrop(
from.publicKey,
minimumAmount * 2,
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'singleGossip');
const transaction = new Transaction().add(
SystemProgram.createNonceAccount({
fromPubkey: from.publicKey,
noncePubkey: nonceAccount.publicKey,
authorizedPubkey: from.publicKey,
lamports: minimumAmount,
}),
);
mockRpc.push([
url,
{
method: 'getBalance',
params: [from.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
result: {
context: {
slot: 11,
},
value: minimumAmount * 2,
},
},
]);
await helpers.processTransaction({
connection,
transaction,
signers: [from, nonceAccount],
commitment: 'singleGossip',
});
const balance = await connection.getBalance(from.publicKey);
expect(balance).toBe(minimumAmount * 2);
mockGetRecentBlockhash('max');
mockRpc.push([
url,
{
method: 'sendTransaction',
},
{
error: null,
result:
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
const transaction = new Transaction().add(
SystemProgram.createNonceAccount({
fromPubkey: from.publicKey,
noncePubkey: nonceAccount.publicKey,
authorizedPubkey: from.publicKey,
lamports: minimumAmount,
}),
);
const nonceSignature = await connection.sendTransaction(
transaction,
[from, nonceAccount],
{
skipPreflight: true,
},
);
mockConfirmTransaction(nonceSignature);
await connection.confirmTransaction(nonceSignature, 'singleGossip');
mockRpc.push([
url,
{
await mockRpcResponse({
method: 'getAccountInfo',
params: [
nonceAccount.publicKey.toBase58(),
{encoding: 'base64', commitment: 'singleGossip'},
],
},
{
error: null,
result: {
context: {
slot: 11,
},
value: {
owner: '11111111111111111111111111111111',
lamports: minimumAmount,
data: expectedData(from.publicKey),
executable: false,
},
value: {
owner: '11111111111111111111111111111111',
lamports: minimumAmount,
data: expectedData(from.publicKey),
executable: false,
},
},
]);
//
const nonceAccountData = await connection.getNonce(nonceAccount.publicKey);
if (nonceAccountData === null) {
expect(nonceAccountData).not.toBeNull();
return;
}
expect(nonceAccountData.authorizedPubkey).toEqual(from.publicKey);
expect(bs58.decode(nonceAccountData.nonce).length).toBeGreaterThan(30);
});
withContext: true,
});
test('create and query nonce account with seed', async () => {
const from = new Account();
const seed = 'seed';
const noncePubkey = await PublicKey.createWithSeed(
from.publicKey,
seed,
SystemProgram.programId,
);
const connection = new Connection(url, 'singleGossip');
mockRpc.push([
url,
{
method: 'getMinimumBalanceForRentExemption',
params: [NONCE_ACCOUNT_LENGTH, {commitment: 'singleGossip'}],
},
{
error: null,
result: 50,
},
]);
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
NONCE_ACCOUNT_LENGTH,
);
mockRpc.push([
url,
{
method: 'requestAirdrop',
params: [from.publicKey.toBase58(), minimumAmount * 2],
},
{
error: null,
result:
'1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
const signature = await connection.requestAirdrop(
from.publicKey,
minimumAmount * 2,
);
mockConfirmTransaction(signature);
await connection.confirmTransaction(signature, 'singleGossip');
mockRpc.push([
url,
{
method: 'getBalance',
params: [from.publicKey.toBase58(), {commitment: 'singleGossip'}],
},
{
error: null,
result: {
context: {
slot: 11,
},
value: minimumAmount * 2,
},
},
]);
const balance = await connection.getBalance(from.publicKey);
expect(balance).toBe(minimumAmount * 2);
mockGetRecentBlockhash('max');
mockRpc.push([
url,
{
method: 'sendTransaction',
},
{
error: null,
result:
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
const transaction = new Transaction().add(
SystemProgram.createNonceAccount({
fromPubkey: from.publicKey,
noncePubkey: noncePubkey,
basePubkey: from.publicKey,
seed,
authorizedPubkey: from.publicKey,
lamports: minimumAmount,
}),
);
const nonceSignature = await connection.sendTransaction(transaction, [from], {
skipPreflight: true,
const nonceAccountData = await connection.getNonce(
nonceAccount.publicKey,
'singleGossip',
);
if (nonceAccountData === null) {
expect(nonceAccountData).not.to.be.null;
return;
}
expect(nonceAccountData.authorizedPubkey).to.eql(from.publicKey);
expect(bs58.decode(nonceAccountData.nonce).length).to.be.greaterThan(30);
});
mockConfirmTransaction(nonceSignature);
await connection.confirmTransaction(nonceSignature, 'singleGossip');
mockRpc.push([
url,
{
it('create and query nonce account with seed', async () => {
const from = new Account();
const seed = 'seed';
const noncePubkey = await PublicKey.createWithSeed(
from.publicKey,
seed,
SystemProgram.programId,
);
await mockRpcResponse({
method: 'getMinimumBalanceForRentExemption',
params: [NONCE_ACCOUNT_LENGTH],
value: 50,
});
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
NONCE_ACCOUNT_LENGTH,
);
await helpers.airdrop({
connection,
address: from.publicKey,
amount: minimumAmount * 2,
});
const transaction = new Transaction().add(
SystemProgram.createNonceAccount({
fromPubkey: from.publicKey,
noncePubkey: noncePubkey,
basePubkey: from.publicKey,
seed,
authorizedPubkey: from.publicKey,
lamports: minimumAmount,
}),
);
await helpers.processTransaction({
connection,
transaction,
signers: [from],
commitment: 'singleGossip',
});
await mockRpcResponse({
method: 'getAccountInfo',
params: [
noncePubkey.toBase58(),
{encoding: 'base64', commitment: 'singleGossip'},
],
},
{
error: null,
result: {
context: {
slot: 11,
},
value: {
owner: '11111111111111111111111111111111',
lamports: minimumAmount,
data: expectedData(from.publicKey),
executable: false,
},
value: {
owner: '11111111111111111111111111111111',
lamports: minimumAmount,
data: expectedData(from.publicKey),
executable: false,
},
},
]);
//
const nonceAccountData = await connection.getNonce(noncePubkey);
if (nonceAccountData === null) {
expect(nonceAccountData).not.toBeNull();
return;
}
expect(nonceAccountData.authorizedPubkey).toEqual(from.publicKey);
expect(bs58.decode(nonceAccountData.nonce).length).toBeGreaterThan(30);
withContext: true,
});
const nonceAccountData = await connection.getNonce(
noncePubkey,
'singleGossip',
);
if (nonceAccountData === null) {
expect(nonceAccountData).not.to.be.null;
return;
}
expect(nonceAccountData.authorizedPubkey).to.eql(from.publicKey);
expect(bs58.decode(nonceAccountData.nonce).length).to.be.greaterThan(30);
});
});