Skip to content

Commit

Permalink
Merge pull request #17 from near/feat-issue-390/update-template
Browse files Browse the repository at this point in the history
Feat issue 390/update template
  • Loading branch information
ailisp authored May 28, 2024
2 parents 1807de1 + f60e3b2 commit 36fd552
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 12 deletions.
48 changes: 40 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
name: Tests
on: push
on:
pull_request:
push:
branches:
- master
- develop

jobs:
workflows:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
workflows_ubuntu:
runs-on: ubuntu-latest
environment:
name: production
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand All @@ -15,5 +20,32 @@ jobs:
run: yarn
- name: Build contract
run: yarn build
- name: Run tests
run: yarn test
- name: Set master credentials
env:
TESTNET_MASTER_ACCOUNT_WALLET: ${{ secrets.TESTNET_MASTER_ACCOUNT_WALLET }}
run: echo "$TESTNET_MASTER_ACCOUNT_WALLET" > .near-credentials/workspaces/testnet/templateprojectmaster.testnet.json
- name: Run ci tests
run: yarn testci
- name: Run near-cli tests
run: yarn run test:clidevdeploy

workflows_macos:
needs: workflows_ubuntu
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- name: Install modules
run: yarn
- name: Build contract
run: yarn build
- name: Set master credentials
env:
TESTNET_MASTER_ACCOUNT_WALLET: ${{ secrets.TESTNET_MASTER_ACCOUNT_WALLET }}
run: echo "$TESTNET_MASTER_ACCOUNT_WALLET" > .near-credentials/workspaces/testnet/templateprojectmaster.testnet.json
- name: Run ci tests
run: yarn testci
- name: Run near-cli tests
run: yarn run test:clidevdeploy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"account_id":"","public_key":"","private_key":""}
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@ npm i
npm run build
```

# Run tests
# Run tests on local node
```
npm run test
npm run test:template
```

# Run tests on testnet
save
```shell
echo "your testnet master account wallet" >> .near-credentials/workspaces/testnet/'$TESTNET_MASTER_ACCOUNT_ID'.json
```

```shell
TESTNET_MASTER_ACCOUNT_ID='your master account id' npm run test:testnetdeploy
```
current master account run by ci on testnet is [templateprojectmaster.testnet](https://testnet.nearblocks.io/address/templateprojectmaster.testnet)

# in gitlab ci, the testnet master account's secret is reserved on git secrets: https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions

# testnet deploy use near-cli
This test script will create accounts with faucet to deploy and call contract
* run test shell
```shell
npm run test:clidevdeploy
```
58 changes: 58 additions & 0 deletions __tests__/test-testnet-dev-deploy.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Worker } from 'near-workspaces';
import test from 'ava';
import process from "process";

test.beforeEach(async t => {
// Init the worker and start a Sandbox server
const worker = await Worker.init({network: 'testnet'});

// Prepare sandbox for tests, create accounts, deploy contracts, etc.
const root = worker.rootAccount;
// Deploy the clean-state contract.
const counter = await root.devDeploy('./build/contract.wasm', {initialBalance: "6 N"});

// Test users
const ali = await root.createSubAccount('ali', {initialBalance : "1 N"});
const bob = await root.createSubAccount('bob', {initialBalance : "1 N"});

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, counter, ali, bob };
});

// If the environment is reused, use test.after to replace test.afterEach
test.afterEach(async t => {
const { root, counter, ali, bob } = t.context.accounts;
const masterAcc = process.env.TESTNET_MASTER_ACCOUNT_ID;
await root.delete(masterAcc);
await counter.delete(masterAcc);
await ali.delete(masterAcc);
await bob.delete(masterAcc);
await t.context.worker.tearDown().catch(error => {
console.log('Failed to tear down the worker:', error);
});
});

test('Initial count is 0, Increase works, Decrease works', async t => {
const { counter, ali, bob } = t.context.accounts;
let result = await counter.view('getCount', {});
t.is(result, 0);

await ali.call(counter, 'increase', {});

result = await counter.view('getCount', {});
t.is(result, 1);

await bob.call(counter, 'increase', { n: 4 });
result = await counter.view('getCount', {});
t.is(result, 5);

await ali.call(counter, 'decrease', {});

result = await counter.view('getCount', {});
t.is(result, 4);

await bob.call(counter, 'decrease', { n: 5 });
result = await counter.view('getCount', {});
t.is(result, -1);
});
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"type": "module",
"scripts": {
"build": "near-sdk-js build",
"test": "ava"
"test": "yarn test:template && yarn test:testnetdeploy",
"test:template": "ava ./__tests__/test-template.ava.js",
"test:testnetdeploy": "ava ./__tests__/test-testnet-dev-deploy.ava.js",
"testci": "yarn testci:template && yarn testci:testnetdeploy",
"testci:template": "ava ./__tests__/test-template.ava.js",
"testci:testnetdeploy": "TESTNET_MASTER_ACCOUNT_ID='templateprojectmaster.testnet' ava ./__tests__/test-testnet-dev-deploy.ava.js",
"test:clidevdeploy": "bash scripts/near_cli_deploy.sh"
},
"author": "Near Inc <[email protected]>",
"license": "Apache-2.0",
Expand All @@ -16,6 +22,7 @@
},
"devDependencies": {
"ava": "^4.2.0",
"near-workspaces": "^3.3.0"
"near-workspaces": "^3.3.0",
"near-cli": "^4.0.13"
}
}
20 changes: 20 additions & 0 deletions scripts/near_cli_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# generate random account
uuid=$(uuidgen | tr 'A-Z' 'a-z')
CONTRACT="devcontract-"${uuid:0:10}".testnet"
echo $CONTRACT
USER="devuser-"${uuid:0:10}".testnet"
echo $USER
BENEFICIARY="templateprojectmaster.testnet"

near create-account $CONTRACT --useFaucet
near deploy $CONTRACT ./build/contract.wasm
near create-account $USER --useFaucet
near view $CONTRACT getCount ''
near call $CONTRACT increase '{ "n": 1 }' --accountId $USER
near view $CONTRACT getCount ''
near call $CONTRACT decrease '{ "n": 2 }' --accountId $USER
near view $CONTRACT getCount ''

echo y | near delete $CONTRACT $BENEFICIARY
echo y | near delete $USER $BENEFICIARY

0 comments on commit 36fd552

Please sign in to comment.