Skip to content

Commit

Permalink
revert: reinstates the tests using the node on port 3030
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranroneill committed May 29, 2024
1 parent 6640652 commit 2d054e0
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 150 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ The above command will compile the Typescript source code into a `dist/` directo

### 4.1. Useful Commands

| Command | Description |
|-------------------|-------------------------------------------------------------------------------------|
| `yarn build` | Builds the source code into the `dist/` directory. |
| `yarn docs:build` | Builds the documentation into the `.docusaurus/` directory. |
| `yarn docs:serve` | Serves the built documentation from the `.docusaurus/` directory. |
| `yarn docs:start` | Builds and runs the documentation in a development environment with hot reloading. |
| `yarn lint` | Runs the linter on `.js` and `.ts` files. |
| `yarn prettier` | Runs the prettier on `.js` and `.ts` files. |
| `yarn test` | Runs the tests. |
| Command | Description |
|-------------------|------------------------------------------------------------------------------------|
| `yarn build` | Builds the source code into the `dist/` directory. |
| `yarn docs:build` | Builds the documentation into the `.docusaurus/` directory. |
| `yarn docs:serve` | Serves the built documentation from the `.docusaurus/` directory. |
| `yarn docs:start` | Builds and runs the documentation in a development environment with hot reloading. |
| `yarn lint` | Runs the linter on `.js` and `.ts` files. |
| `yarn node:start` | Starts up a NEAR development node and runs it in the background. |
| `yarn node:start` | Stops the NEAR development node that was started in `yarn node:start`. |
| `yarn prettier` | Runs the prettier on `.js` and `.ts` files. |
| `yarn test` | Starts a NEAR development node and runs the tests. |

<sup>[Back to top ^][table-of-contents]</sup>

Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pathsToModuleNameMapper } from 'ts-jest';
import { compilerOptions } from './tsconfig.json';

const config: Config = {
globalSetup: '<rootDir>/test/globalSetup.ts',
moduleNameMapper: {
...pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/',
Expand Down
2 changes: 1 addition & 1 deletion scripts/start_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function main {
# start the node
near-sandbox --home ./.near run > /dev/null 2>&1 &

sleep 1000
sleep 2s

printf "%b node started \n" "${INFO_PREFIX}"

Expand Down
6 changes: 4 additions & 2 deletions scripts/stop_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SCRIPT_DIR=$(dirname "${0}")
source "${SCRIPT_DIR}"/set_vars.sh


# Public: Gets the saved node PID from .near/node.pid and kills the background process.
# Public: Kills the node's process.
#
# Examples
#
Expand All @@ -17,11 +17,13 @@ function main {

set_vars

printf "%b killing node process \n" "${INFO_PREFIX}"
printf "%b stopping the node \n" "${INFO_PREFIX}"

# stop the node
pkill -f "near-sandbox"

printf "%b stopped node \n" "${INFO_PREFIX}"

exit 0
}

Expand Down
38 changes: 5 additions & 33 deletions src/controllers/Social.get.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Account, connect, Near } from 'near-api-js';
import { NearAccount, Worker } from 'near-workspaces';
import { resolve } from 'node:path';
import { cwd } from 'node:process';
import type { Account } from 'near-api-js';

// constants
import { NETWORK_ID } from '@test/constants';
// credentials
import { account_id as socialContractAccountId } from '@test/credentials/localnet/social.test.near.json';

// controllers
import Social from './Social';
Expand All @@ -13,43 +10,18 @@ import Social from './Social';
import createEphemeralAccount from '@test/helpers/createEphemeralAccount';

describe(`${Social.name}#get`, () => {
let contractAccount: NearAccount;
let near: Near;
let signer: Account;
let worker: Worker;

beforeAll(async () => {
worker = await Worker.init({
network: NETWORK_ID,
});

contractAccount = await worker.rootAccount.devDeploy(
resolve(cwd(), 'test', 'contracts', 'social_db.wasm')
);
await worker.rootAccount.call(contractAccount.accountId, 'new', {});

near = await connect({
networkId: NETWORK_ID,
nodeUrl: worker.provider.connection.url,
});
});

afterAll(async () => {
await worker.tearDown();
});

beforeEach(async () => {
const result = await createEphemeralAccount({
worker,
});
const result = await createEphemeralAccount();

signer = result.account;
});

it('should return an empty object when the contract does not know the account', async () => {
// arrange
const client = new Social({
contractId: contractAccount.accountId,
contractId: socialContractAccountId,
});
// act
const result = await client.get({
Expand Down
38 changes: 5 additions & 33 deletions src/controllers/Social.getVersion.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Account, connect, Near } from 'near-api-js';
import { NearAccount, Worker } from 'near-workspaces';
import { resolve } from 'node:path';
import { cwd } from 'node:process';
import type { Account } from 'near-api-js';

// constants
import { NETWORK_ID } from '@test/constants';
// credentials
import { account_id as socialContractAccountId } from '@test/credentials/localnet/social.test.near.json';

// controllers
import Social from './Social';
Expand All @@ -13,43 +10,18 @@ import Social from './Social';
import createEphemeralAccount from '@test/helpers/createEphemeralAccount';

describe(`${Social.name}#getVersion`, () => {
let contractAccount: NearAccount;
let near: Near;
let signer: Account;
let worker: Worker;

beforeAll(async () => {
worker = await Worker.init({
network: NETWORK_ID,
});

contractAccount = await worker.rootAccount.devDeploy(
resolve(cwd(), 'test', 'contracts', 'social_db.wasm')
);
await worker.rootAccount.call(contractAccount.accountId, 'new', {});

near = await connect({
networkId: NETWORK_ID,
nodeUrl: worker.provider.connection.url,
});
});

afterAll(async () => {
await worker.tearDown();
});

beforeEach(async () => {
const result = await createEphemeralAccount({
worker,
});
const result = await createEphemeralAccount();

signer = result.account;
});

it('should return the version of the social contract', async () => {
// arrange
const client = new Social({
contractId: contractAccount.accountId,
contractId: socialContractAccountId,
});
// act
const version = await client.getVersion({ signer });
Expand Down
35 changes: 5 additions & 30 deletions src/controllers/Social.set.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Account, providers, transactions, utils } from 'near-api-js';
import { randomBytes } from 'node:crypto';
import { NearAccount, Worker } from 'near-workspaces';
import { resolve } from 'node:path';
import { cwd } from 'node:process';

// constants
import { NETWORK_ID } from '@test/constants';
// credentials
import { account_id as socialContractAccountId } from '@test/credentials/localnet/social.test.near.json';

// controllers
import Social from './Social';
Expand All @@ -14,38 +11,16 @@ import Social from './Social';
import accountAccessKey, {
IAccessKeyResponse,
} from '@test/helpers/accountAccessKey';
import createEphemeralAccount from '@test/helpers/createEphemeralAccount';

// utils
import convertNEARToYoctoNEAR from '@app/utils/convertNEARToYoctoNEAR';
import createEphemeralAccount from '@test/helpers/createEphemeralAccount';

describe(`${Social.name}#set`, () => {
let contractAccount: NearAccount;
let keyPair: utils.KeyPairEd25519;
let signer: Account;
let signerAccessKeyResponse: IAccessKeyResponse;
let worker: Worker;

beforeAll(async () => {
worker = await Worker.init({
network: NETWORK_ID,
});

contractAccount = await worker.rootAccount.devDeploy(
resolve(cwd(), 'test', 'contracts', 'social_db.wasm')
);
await worker.rootAccount.call(contractAccount.accountId, 'new', {});
});

afterAll(async () => {
await worker.tearDown();
});

beforeEach(async () => {
const result = await createEphemeralAccount({
initialBalanceInAtomicUnits: convertNEARToYoctoNEAR('100'),
worker,
});
const result = await createEphemeralAccount(convertNEARToYoctoNEAR('100'));

keyPair = result.keyPair;
signer = result.account;
Expand All @@ -54,7 +29,7 @@ describe(`${Social.name}#set`, () => {
it('should set storage and add the data', async () => {
// arrange
const client = new Social({
contractId: contractAccount.accountId,
contractId: socialContractAccountId,
});
const data = {
[signer.accountId]: {
Expand Down
2 changes: 1 addition & 1 deletion test/constants/Config.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const NETWORK_ID = 'sandbox';
export const NETWORK_ID = 'localnet';
export const NODE_URL = 'http://localhost:3030';
75 changes: 75 additions & 0 deletions test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Account, connect, keyStores, utils } from 'near-api-js';
import { resolve } from 'node:path';
import { cwd } from 'node:process';
import { readFile } from 'node:fs/promises';

// constants
import { NETWORK_ID, NODE_URL } from './constants';

// credentials
import { account_id as genesisAccountId } from './credentials/localnet/test.near.json';
import { account_id as socialContractAccountId } from './credentials/localnet/social.test.near.json';

// helpers
import createTestAccount from './helpers/createTestAccount';

// utils
import convertNEARToYoctoNEAR from '../src/utils/convertNEARToYoctoNEAR';

export default async function globalSetup() {
const _functionName = 'globalSetup';
const near = await connect({
networkId: NETWORK_ID,
nodeUrl: NODE_URL,
keyStore: new keyStores.UnencryptedFileSystemKeyStore(
resolve(cwd(), 'test', 'credentials')
),
});
const contract = await readFile(
resolve(cwd(), 'test', 'contracts', 'social_db.wasm')
);
let contractAccountPublicKey: utils.PublicKey;
let contractAccount: Account;
let genesisAccount: Account;

genesisAccount = await near.account(genesisAccountId);
contractAccountPublicKey = await near.connection.signer.getPublicKey(
socialContractAccountId,
NETWORK_ID
);

// create the contract account
contractAccount = await createTestAccount({
creatorAccount: genesisAccount,
initialBalanceInAtomicUnits: BigInt(convertNEARToYoctoNEAR('10')),
newAccountID: socialContractAccountId,
newAccountPublicKey: contractAccountPublicKey,
connection: near,
});

// deploy the account
await contractAccount.deployContract(contract);

try {
// initialize the contract
await genesisAccount.functionCall({
contractId: contractAccount.accountId,
methodName: 'new',
});
// set the contract to live
await contractAccount.functionCall({
contractId: contractAccount.accountId,
methodName: 'set_status',
args: {
status: 'Live',
},
});
} catch (error) {
// if the contract has already been initialized, just ignore
if (error.message.includes('The contract has already been initialized')) {
return;
}

console.error(`${_functionName}:`, JSON.stringify(error));
}
}
Loading

0 comments on commit 2d054e0

Please sign in to comment.