Skip to content

Commit

Permalink
Merge pull request #1211 from LimeChain/near-workspaces-integration
Browse files Browse the repository at this point in the history
Use near-workspaces for the tests during the CI process
  • Loading branch information
gtsonevv authored Jan 22, 2024
2 parents ae9600b + df0cd87 commit ab4f8d3
Show file tree
Hide file tree
Showing 10 changed files with 865 additions and 346 deletions.
3 changes: 2 additions & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"jest": "26.0.1",
"near-hello": "0.5.1",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
"typescript": "4.9.4",
"near-workspaces": "3.4.0"
},
"files": [
"lib"
Expand Down
18 changes: 13 additions & 5 deletions packages/accounts/test/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = function getConfig(env) {
const { Worker } = require('near-workspaces');
const fs = require('fs');
let worker;
module.exports = async function getConfig(env) {
switch (env) {
case 'production':
case 'mainnet':
Expand Down Expand Up @@ -32,12 +35,17 @@ module.exports = function getConfig(env) {
walletUrl: 'http://localhost:4000/wallet',
};
case 'test':
case 'ci':
case 'ci': {
if (!worker) worker = await Worker.init();
const keyFile = fs.readFileSync(`${worker.rootAccount.manager.config.homeDir}/validator_key.json`);
const keyPair = JSON.parse(keyFile.toString());
return {
networkId: 'shared-test',
nodeUrl: 'https://rpc.ci-testnet.near.org',
masterAccount: 'test.near',
networkId: worker.config.network,
nodeUrl: worker.manager.config.rpcAddr,
masterAccount: worker.rootAccount._accountId,
secretKey: keyPair.secret_key || keyPair.private_key
};
}
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
}
Expand Down
345 changes: 172 additions & 173 deletions packages/accounts/test/providers.test.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions packages/accounts/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ async function loadGuestBookContractCode() {
}
async function setUpTestConnection() {
const keyStore = new InMemoryKeyStore();
const config = Object.assign(require('./config')(process.env.NODE_ENV || 'test'), {
const config = Object.assign(await require('./config')(process.env.NODE_ENV || 'test'), {
networkId,
keyStore
});

if (config.masterAccount) {
// full accessKey on ci-testnet, dedicated rpc for tests.
await keyStore.setKey(networkId, config.masterAccount, KeyPair.fromString('ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw'));
const secretKey = config.secretKey || 'ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw';
await keyStore.setKey(networkId, config.masterAccount, KeyPair.fromString(secretKey));
}

const connection = Connection.fromConfig({
Expand Down
3 changes: 2 additions & 1 deletion packages/near-api-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"rimraf": "3.0.2",
"semver": "7.1.1",
"ts-jest": "26.5.6",
"uglifyify": "5.0.1"
"uglifyify": "5.0.1",
"near-workspaces": "3.4.0"
},
"keywords": [],
"license": "(MIT AND Apache-2.0)",
Expand Down
15 changes: 11 additions & 4 deletions packages/near-api-js/test/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = function getConfig(env) {
const { Worker } = require('near-workspaces');
const fs = require('fs');
let worker;
module.exports = async function getConfig(env) {
switch (env) {
case 'production':
case 'mainnet':
Expand Down Expand Up @@ -33,10 +36,14 @@ module.exports = function getConfig(env) {
};
case 'test':
case 'ci':
if (!worker) worker = await Worker.init();
const keyFile = fs.readFileSync(`${worker.rootAccount.manager.config.homeDir}/validator_key.json`);
const keyPair = JSON.parse(keyFile.toString());
return {
networkId: 'shared-test',
nodeUrl: 'https://rpc.ci-testnet.near.org',
masterAccount: 'test.near',
networkId: worker.config.network,
nodeUrl: worker.manager.config.rpcAddr,
masterAccount: worker.rootAccount._accountId,
secretKey: keyPair.secret_key || keyPair.private_key
};
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
Expand Down
7 changes: 4 additions & 3 deletions packages/near-api-js/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const RANDOM_ACCOUNT_LENGTH = 40;

async function setUpTestConnection() {
const keyStore = new nearApi.keyStores.InMemoryKeyStore();
const config = Object.assign(require('./config')(process.env.NODE_ENV || 'test'), {
networkId: networkId,
const config = Object.assign(await require('./config')(process.env.NODE_ENV || 'test'), {
networkId,
keyStore
});

if (config.masterAccount) {
// full accessKey on ci-testnet, dedicated rpc for tests.
await keyStore.setKey(networkId, config.masterAccount, nearApi.utils.KeyPair.fromString('ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw'));
const secretKey = config.secretKey || 'ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw';
await keyStore.setKey(networkId, config.masterAccount, KeyPair.fromString(secretKey));
}
return nearApi.connect(config);
}
Expand Down
1 change: 1 addition & 0 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"devDependencies": {
"@types/node": "18.11.18",
"jest": "26.0.1",
"near-workspaces": "3.4.0",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
},
Expand Down
Loading

0 comments on commit ab4f8d3

Please sign in to comment.