Skip to content

Commit

Permalink
improved test stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Xalava authored and davhojt committed Jan 25, 2023
1 parent d515479 commit f4b1bb3
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 43 deletions.
2 changes: 1 addition & 1 deletion tests/btc/retrieve-transaction-value.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { expect } = require("chai");
const Client = require('bitcoin-core');
const {retrieveTransactionValue}= require("/jail/student/retrieve-transaction-value.js")
const { DEBUG } = require('/app/lib/helpers')

describe("retrieve transaction value", function() {
let client
let DEBUG=false

beforeEach( async function () {
client = new Client({
Expand Down
6 changes: 3 additions & 3 deletions tests/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ elif test -f "/app/web3/${EXERCISE}".test.js; then

cp "/app/web3/${EXERCISE}.test.js" /jail/test
# Launch a local node
# If the test fails in local environments, try to increase the timeout. It is because hardhat compiles all contracts before running the tests.
timeout 6s npx hardhat node >/dev/null&
sleep 0.2
npx hardhat node >/dev/null&
sleep 0.2 # Short wait for the node to be ready but students should check.
# Launch the tests
npx hardhat test "/jail/test/${EXERCISE}.test.js"
# Failure
else
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

let DEBUG = false
if (process.env.DEBUG) {
console.log("DEBUG mode")
DEBUG = true
}

function displayBrowserLogs (page) {
page
.on('console', message =>
console.log(`🖥️ ${message.type().substr(0, 3).toUpperCase()} ${message.text()}`))
.on('pageerror', ({ message }) =>
console.log("🖥️ ",message))
.on('response', response =>
console.log(`🌍️${response.status()} ${response.url()}`))
.on('requestfailed', request =>
console.log(`🌍️${request.failure().errorText} ${request.url()}`))
}
module.exports = { sleep, DEBUG, displayBrowserLogs }
1 change: 1 addition & 0 deletions tests/web3/check-document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe("Check document", function() {
let provider
before(async function() {
provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");//http://localhost:8545
await provider.ready
})

async function sendHash(entry) {
Expand Down
4 changes: 2 additions & 2 deletions tests/web3/connect-to-metamask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('Connect to MetaMask', function() {
let provider

before(async function() {
this.timeout(100000);
const app = express()
app.use(express.static('/jail/student/'))
app.use(express.static('/app/lib/'))
Expand All @@ -32,7 +31,8 @@ describe('Connect to MetaMask', function() {
page = await browser.newPage();
await page.goto('http://localhost:3001/connect-to-metamask.html');

provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");
provider = new ethers.providers.JsonRpcProvider("http://localhost:8545")
await provider.ready
BLOCKNUMBER = await provider.getBlockNumber()
let netw = await provider.getNetwork()
CHAINID = netw.chainId
Expand Down
11 changes: 6 additions & 5 deletions tests/web3/donation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ const opts = { executablePath: '/usr/bin/google-chrome-stable', args: ['--no-san
// Option with the default puppeteer Chromium browser:
// const puppeteer = require('puppeteer')
// const opts = {args: ['--no-sandbox']}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
const {sleep, DEBUG, displayBrowserLogs} = require('/app/lib/helpers')

describe('Donation tests', function () {
let browser
let page
Expand All @@ -22,6 +19,8 @@ describe('Donation tests', function () {
// this.timeout(100000) // used during development to ensure timely execution

provider = new ethers.providers.JsonRpcProvider("http://localhost:8545")
await provider.ready
if(DEBUG) console.log("Got provider: ", provider)
signer = provider.getSigner()

const app = express()
Expand All @@ -32,6 +31,8 @@ describe('Donation tests', function () {
browser = await puppeteer.launch(opts)
page = await browser.newPage()
await page.goto('http://localhost:3001/donation.html')

if(DEBUG) displayBrowserLogs(page)
})

after(async function () {
Expand Down
8 changes: 6 additions & 2 deletions tests/web3/get-account.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const { expect } = require("chai")
const getAccount = require("/jail/student/get-account.js")

describe("get account", function () {
const getAccount = require("/jail/student/get-account.js")
let provider
before(async function () {
provider = new ethers.providers.JsonRpcProvider("http://localhost:8545")
await provider.ready
})
it("Should get the first account", async function () {
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545")
const accounts = await provider.listAccounts()
let account = await getAccount()
expect(account).to.equal(accounts[0])
Expand Down
34 changes: 18 additions & 16 deletions tests/web3/local-node-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ const express = require('express')
const puppeteer = require('puppeteer-core')
const opts = {executablePath: '/usr/bin/google-chrome-stable', args: ['--no-sandbox']}

const {sleep, DEBUG, displayBrowserLogs} = require('/app/lib/helpers')


describe('Local node info', function() {
let browser
let page
let server
let BLOCKNUMBER
let CHAINID
let signer
let provider
let DEBUG=false

before(async function() {
this.timeout(100000);
Expand All @@ -28,13 +27,11 @@ describe('Local node info', function() {
browser = await puppeteer.launch(opts);
page = await browser.newPage();
await page.goto('http://127.0.0.1:3001/local-node-info.html');
if (DEBUG) displayBrowserLogs(page)

provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");
provider = new ethers.providers.JsonRpcProvider("http://localhost:8545")
await provider.ready
if (DEBUG) console.log(provider)
// BLOCKNUMBER = await provider.getBlockNumber()
let netw = await provider.getNetwork()
CHAINID = netw.chainId
signer = provider.getSigner()
});

after(async function() {
Expand All @@ -44,24 +41,29 @@ describe('Local node info', function() {

it('Should have the correct chainID', async function() {
await page.waitForSelector('#chainId', {visible: true})
sleep(300)
let netw = await provider.getNetwork()
const pageChainId = await page.$eval('#chainId', ci => ci.textContent);
expect(parseInt(pageChainId)).to.be.equal(CHAINID)
expect(parseInt(pageChainId)).to.be.equal(netw.chainId)
});

it('Should have the correct number of blocks', async function() {
await page.waitForSelector('#blockNumber')
sleep(300)
let blockNumber = await provider.getBlockNumber()
const pageBlockNumber = await page.$eval('#blockNumber', ci => ci.innerText);
expect(parseInt(pageBlockNumber)).to.be.equal(blockNumber)
});

// TOCHECK : Temporarily disabled because it getBlockNumber seems unreliable.
// it('Should have the correct number of blocks', async function() {
// await page.waitForSelector('#blockNumber')
// const pageBlockNumber = await page.$eval('#blockNumber', ci => ci.innerText);
// expect(parseInt(pageBlockNumber)).to.be.equal(BLOCKNUMBER)
// });

// it('Should have the correct blocknumber after a transaction', async function() {
// const txHash = await signer.sendTransaction({
// to: '0x7A7a4EdC679bC4E29F74E32E9eEDd256cd435FBb',
// value: ethers.utils.parseEther("0.2"),
// })
// await page.reload({ waitUntil: ["networkidle0", "domcontentloaded"] });
// await page.waitForSelector('#blockNumber')
// sleep(300)
// const pageBlockNumber = await page.$eval('#blockNumber', ci => ci.innerText);
// let currentBlockNumber = await provider.getBlockNumber()
// expect(parseInt(pageBlockNumber)).to.be.equal(currentBlockNumber)
Expand Down
5 changes: 2 additions & 3 deletions tests/web3/random-wallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ describe('Random wallet', function() {
let provider

before(async function() {
this.timeout(100000);

const app = express()
app.use(express.static('/jail/student/'))
app.use(express.static('/app/lib/'))
server = await app.listen(3001);

provider = new ethers.providers.JsonRpcProvider();
provider = new ethers.providers.JsonRpcProvider()
await provider.ready
signer = provider.getSigner()

browser = await puppeteer.launch(opts);
Expand Down
16 changes: 5 additions & 11 deletions tests/web3/send-hash.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
const { expect } = require("chai")

function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
})
}

describe("sendHash", function() {
const sh = require("/jail/student/send-hash.js")
let provider
before(async function() {
provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");//http://localhost:8545
await provider.ready
})
it("Should return the hash of a word", async function() {
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");//http://localhost:8545
await sleep(300)
let txHash = await sh("hello")
let tx = await provider.getTransaction( txHash )
expect(tx.data).to.equal("0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
});
it("Should return the hash of a text", async function() {
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");//http://localhost:8545
let txHash = await sh("Comme je descendais des Fleuves impassibles")
let tx = await provider.getTransaction( txHash )
expect(tx.data).to.equal("0x654ac81d069e3928d9fed854a2f0889463b054e5f51145dbb68a0a93611d4f65");
});
it("Should return the hash of an empty string", async function() {
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");//http://localhost:8545
let txHash = await sh("")
let tx = await provider.getTransaction( txHash )
expect(tx.data).to.equal("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
});
it("Should not change significantly the sender balance", async function() {
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");//http://localhost:8545
const signerAddress = await provider.getSigner().getAddress()
let initialBalance = await provider.getBalance(signerAddress)
let txHash = await sh("")
Expand Down

0 comments on commit f4b1bb3

Please sign in to comment.