Skip to content

Commit b688d4a

Browse files
Merge pull request #14 from ChainSafe/wyatt/init-blackbox-tests
Init Black Box Tests
2 parents 3bbd289 + c44103b commit b688d4a

17 files changed

+3706
-116
lines changed

.eslintignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
lib
12
dist
23
jest.config.js
34
webpack.config.js
45
.eslintrc.js
56
cypress
6-
cypress.config.js
7+
cypress.config.js
8+
test/black_box

.github/workflows/test.yml

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ jobs:
3333
path: '/tmp/chainlink-plugin-${{ matrix.node }}.js.tar.gz'
3434
- uses: actions/cache/save@v3
3535
with:
36-
path: |
37-
~/.cache/Cypress
38-
node_modules
39-
key: cypress-${{ runner.os }}-${{ hashFiles('yarn-lock.json') }}
36+
path: |
37+
~/.cache/Cypress
38+
key: cypress-${{ runner.os }}-${{ hashFiles('yarn.lock') }}
4039
build-web:
4140
name: Build Webpack
4241
needs: build
@@ -95,19 +94,35 @@ jobs:
9594
if: matrix.browser == 'firefox'
9695
- uses: actions/download-artifact@v3
9796
with:
98-
name: chainlink-plugin-16.js.tar.gz
99-
path: /tmp
97+
name: chainlink-plugin-16.js.tar.gz
98+
path: /tmp
10099
- run: tar -xf /tmp/chainlink-plugin-16.js.tar.gz -C ./
101100
- uses: actions/cache@v2
102101
with:
103-
path: |
104-
~/.cache/Cypress
105-
node_modules
106-
key: cypress-${{ runner.os }}-${{ hashFiles('yarn-lock.json') }}
102+
path: |
103+
~/.cache/Cypress
104+
key: cypress-${{ runner.os }}-${{ hashFiles('yarn.lock') }}
107105
- run: corepack enable
108106
- name: Cypress run
109107
uses: cypress-io/github-action@v5
110108
with:
111109
install: false
112110
command: 'yarn run test:e2e:${{ matrix.browser }}'
113111
cache-key: node-v16-on-${{ matrix.browser }}-hash-${{ hashFiles('yarn.lock') }}
112+
black-box:
113+
name: Black Box Tests
114+
needs: build
115+
runs-on: ubuntu-latest
116+
strategy:
117+
matrix:
118+
node:
119+
- 16
120+
steps:
121+
- uses: actions/download-artifact@v3
122+
with:
123+
name: chainlink-plugin-${{ matrix.node }}.js.tar.gz
124+
path: /tmp
125+
- run: tar -xf /tmp/chainlink-plugin-${{ matrix.node }}.js.tar.gz -C ./
126+
- run: |
127+
yarn test:black-box
128+
shell: bash

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ tsconfig.tsbuildinfo
3636
package-lock.json
3737

3838
tmp/
39+
.npmrc

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ REVISION
3131

3232
# project specific paths
3333
coverage/
34+
lib/
3435
dist/
3536
tmp/
3637
browsertest.build/

cypress.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const config = {
88
return require('./cypress/plugins/index.js')(on, config);
99
},
1010
specPattern: 'test/e2e/**/**/*.test.ts',
11-
excludeSpecPattern: ['**/contract_defaults_extra.test.ts'],
11+
excludeSpecPattern: ['test/black_box/**'],
1212
},
1313
};
1414

cypress/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const webpack = require('webpack');
22

33
module.exports = {
4+
mode: 'development',
45
resolve: {
56
extensions: ['.ts', '.js'],
67
},

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
{
22
"name": "@chainsafe/web3.js-chainlink-plugin",
3-
"version": "0.1.0-alpha.0",
3+
"version": "0.1.0",
44
"description": "A Web3.js 4.x Plugin for Interacting With Chainlink Smart Contracts",
5-
"main": "dist/index.js",
5+
"main": "lib/index.js",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/ChainSafe/web3.js-chainlink-plugin.git"
99
},
1010
"author": "ChainSafe Systems",
1111
"license": "LGPL-3.0",
1212
"private": false,
13+
"files": [
14+
"lib/**/*"
15+
],
1316
"scripts": {
1417
"build": "tsc --build",
15-
"build:check": "node -e \"require('./dist')\"",
18+
"build:check": "node -e \"require('./lib')\"",
1619
"build:web": "npx webpack",
17-
"clean": "rimraf dist",
20+
"clean": "rimraf lib",
1821
"format": "prettier --write '**/*'",
1922
"lint": "eslint --ext .js,.ts .",
2023
"lint:fix": "eslint --fix --ext .js,.ts .",
21-
"prebuild": "rimraf dist",
24+
"post-black-box": "./scripts/black_box_test_helpers.sh stop",
25+
"pre-black-box": "./scripts/black_box_test_helpers.sh startBackgroundAndPublish",
26+
"prebuild": "rimraf lib",
2227
"prepare": "husky install",
2328
"test": "jest --config=./test/unit/jest.config.js",
29+
"test:black-box": "yarn pre-black-box && ./scripts/black_box_test_helpers.sh runTests",
2430
"test:ci": "jest --coverage=true --coverage-reporters=json --verbose",
2531
"test:coverage": "jest --config=./test/unit/jest.config.js --coverage=true --coverage-reporters=text",
2632
"test:e2e:chrome": "npx cypress run --headless --browser chrome",
@@ -55,6 +61,7 @@
5561
"jest": "^28.1.3",
5662
"jest-extended": "^3.0.1",
5763
"lint-staged": "^13.0.3",
64+
"npm-auth-to-token": "^1.0.0",
5865
"prettier": "^2.7.1",
5966
"prettier-plugin-solidity": "^1.0.0-beta.24",
6067
"process": "^0.11.10",

scripts/black_box_test_helpers.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
3+
ORIGARGS=("$@")
4+
5+
helpFunction() {
6+
echo "Usage: $0 [start|stop|startBackgroundAndPublish|runTests] [background]"
7+
exit 1 # Exit script after printing help
8+
}
9+
10+
start() {
11+
. scripts/env.sh
12+
if [[ ${ORIGARGS[1]} == "background" ]]; then
13+
startBackground
14+
else
15+
echo "Starting verdaccio..."
16+
docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio
17+
fi
18+
}
19+
20+
startBackground() {
21+
echo "Starting verdaccio in background..."
22+
docker run -d --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio
23+
}
24+
25+
stop() {
26+
echo "Stopping verdaccio ..."
27+
docker ps -q --filter ancestor="verdaccio/verdaccio" | xargs -r docker stop
28+
}
29+
30+
createVerdaccioNPMUser() {
31+
curl -XPUT \
32+
-H "Content-type: application/json" \
33+
-d '{ "name": "test", "password": "test" }' \
34+
'http://localhost:4873/-/user/org.couchdb.user:test'
35+
}
36+
37+
loginNPMUser() {
38+
npx npm-auth-to-token \
39+
-u test \
40+
-p test \
41+
42+
-r http://localhost:4873
43+
}
44+
45+
yarnPublish() {
46+
yarn publish \
47+
--new-version 9.9.9 \
48+
--no-git-tag-version \
49+
--tag blackbox \
50+
--registry http://localhost:4873
51+
}
52+
53+
publish() {
54+
echo "Publishing to verdaccio ..."
55+
56+
npx wait-port -t 60000 4873
57+
58+
createVerdaccioNPMUser
59+
loginNPMUser
60+
yarn build
61+
yarnPublish
62+
}
63+
64+
startBackgroundAndPublish() {
65+
startBackground && publish
66+
}
67+
68+
runTests() {
69+
cd test/black_box
70+
yarn --update-checksums
71+
yarn install
72+
yarn test
73+
}
74+
75+
case $1 in
76+
start) start ;;
77+
stop) stop ;;
78+
startBackgroundAndPublish) startBackgroundAndPublish ;;
79+
runTests) runTests ;;
80+
*) helpFunction ;; # Print helpFunction in case parameter is non-existent
81+
esac

test/black_box/.yarnrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry "http://localhost:4873"

test/black_box/jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

test/black_box/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "web3.js_chainlink_plugin_black_box_tests",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"author": "ChainSafe Systems",
6+
"license": "LGPL-3.0",
7+
"scripts": {
8+
"test": "jest"
9+
},
10+
"dependencies": {
11+
"@chainsafe/web3.js-chainlink-plugin": "^9.9.9"
12+
},
13+
"devDependencies": {
14+
"jest": "^28.1.3",
15+
"ts-jest": "^28.0.7",
16+
"web3": "^4.0.1-alpha.5",
17+
"web3-core": "^4.0.1-alpha.5",
18+
"web3-eth": "^4.0.1-alpha.5"
19+
}
20+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Web3 from 'web3';
2+
import { Web3Context } from 'web3-core';
3+
import Web3Eth from 'web3-eth';
4+
5+
// https://github.com/ChainSafe/web3.js-plugin-chainlink/issues/15
6+
// @ts-ignore
7+
import { ChainlinkPlugin, MainnetPriceFeeds } from '@chainsafe/web3.js-chainlink-plugin';
8+
9+
describe('ChainlinkPlugin Tests', () => {
10+
it('should register ChainlinkPlugin plugin on Web3Context instance', () => {
11+
const web3Context = new Web3Context('http://127.0.0.1:8545');
12+
web3Context.registerPlugin(new ChainlinkPlugin());
13+
expect(web3Context.chainlink).toBeDefined();
14+
});
15+
16+
it('should register ChainlinkPlugin plugin on Web3Eth instance', () => {
17+
const web3Eth = new Web3Eth('http://127.0.0.1:8545');
18+
web3Eth.registerPlugin(new ChainlinkPlugin());
19+
expect(web3Eth.chainlink).toBeDefined();
20+
});
21+
22+
describe('ChainlinkPlugin method tests', () => {
23+
let web3Context: Web3;
24+
let requestManagerSendSpy: jest.SpyInstance;
25+
26+
beforeAll(() => {
27+
web3Context = new Web3('https://rpc.ankr.com/eth');
28+
web3Context.registerPlugin(new ChainlinkPlugin());
29+
requestManagerSendSpy = jest.spyOn(web3Context.chainlink.requestManager, 'send');
30+
});
31+
32+
it('should call ChainlinkPlugin.getPrice with expected RPC object', async () => {
33+
const result = await web3Context.chainlink.getPrice(MainnetPriceFeeds.LinkEth);
34+
expect(Object.keys(result as object)).toEqual(
35+
expect.arrayContaining([
36+
'roundId',
37+
'answer',
38+
'startedAt',
39+
'updatedAt',
40+
'answeredInRound',
41+
]),
42+
);
43+
expect(requestManagerSendSpy).toHaveBeenCalledWith({
44+
method: 'eth_call',
45+
params: [{ data: '0xfeaf968c', to: MainnetPriceFeeds.LinkEth }, 'latest'],
46+
});
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)