diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3c9f266..963cf44 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 @@ -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 diff --git a/.near-credentials/workspaces/testnet/templateprojectmaster.testnet.json b/.near-credentials/workspaces/testnet/templateprojectmaster.testnet.json new file mode 100644 index 0000000..df1ed09 --- /dev/null +++ b/.near-credentials/workspaces/testnet/templateprojectmaster.testnet.json @@ -0,0 +1 @@ +{"account_id":"","public_key":"","private_key":""} \ No newline at end of file diff --git a/README.md b/README.md index d100de7..0ada006 100644 --- a/README.md +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/__tests__/test-testnet-dev-deploy.ava.js b/__tests__/test-testnet-dev-deploy.ava.js new file mode 100644 index 0000000..c939e89 --- /dev/null +++ b/__tests__/test-testnet-dev-deploy.ava.js @@ -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); +}); diff --git a/package.json b/package.json index 49dd7ab..1314832 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "Apache-2.0", @@ -16,6 +22,7 @@ }, "devDependencies": { "ava": "^4.2.0", - "near-workspaces": "^3.3.0" + "near-workspaces": "^3.3.0", + "near-cli": "^4.0.13" } } diff --git a/scripts/near_cli_deploy.sh b/scripts/near_cli_deploy.sh new file mode 100755 index 0000000..ff0303c --- /dev/null +++ b/scripts/near_cli_deploy.sh @@ -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 \ No newline at end of file