Skip to content

Commit

Permalink
E2E Testing add Prettier and Eslint (frequency-chain#1767)
Browse files Browse the repository at this point in the history
# Goal
The goal of this PR is to get e2e tests under formatting and eslint
control.

Closes frequency-chain#1764
  • Loading branch information
wilwade authored Nov 8, 2023
1 parent 8c8384f commit 8f55a11
Show file tree
Hide file tree
Showing 38 changed files with 4,304 additions and 3,266 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/verify-pr-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ jobs:
- name: Install NPM Modules
run: npm ci
working-directory: e2e
- name: Lint
run: npm run lint
working-directory: e2e
- name: Run E2E Tests
working-directory: e2e
env:
Expand Down
24 changes: 24 additions & 0 deletions e2e/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-env node */
module.exports = {
ignorePatterns: ['dist'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:mocha/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'mocha'],
root: true,
env: {
mocha: true,
node: true,
},
rules: {
'@typescript-eslint/no-explicit-any': 'off', // Don't worry about an any in a test
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
},
};
18 changes: 7 additions & 11 deletions e2e/.load-test.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"exit": true,
"extension": ["ts"],
"parallel": false,
"require": [
"scaffolding/globalHooks.ts",
"scaffolding/rootHooks.ts",
"scaffolding/extrinsicHelpers.ts"
],
"loader": "ts-node/esm",
"spec": ["./load-tests/**/*.test.ts"],
"timeout": 60000
"exit": true,
"extension": ["ts"],
"parallel": false,
"require": ["scaffolding/globalHooks.ts", "scaffolding/rootHooks.ts", "scaffolding/extrinsicHelpers.ts"],
"loader": "ts-node/esm",
"spec": ["./load-tests/**/*.test.ts"],
"timeout": 60000
}
18 changes: 7 additions & 11 deletions e2e/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"exit": true,
"extension": ["ts"],
"parallel": true,
"require": [
"scaffolding/globalHooks.ts",
"scaffolding/rootHooks.ts",
"scaffolding/extrinsicHelpers.ts"
],
"loader": "ts-node/esm",
"spec": ["./{,!(node_modules|load-tests)/**}/*.test.ts"],
"timeout": 20000
"exit": true,
"extension": ["ts"],
"parallel": true,
"require": ["scaffolding/globalHooks.ts", "scaffolding/rootHooks.ts", "scaffolding/extrinsicHelpers.ts"],
"loader": "ts-node/esm",
"spec": ["./{,!(node_modules|load-tests)/**}/*.test.ts"],
"timeout": 20000
}
3 changes: 3 additions & 0 deletions e2e/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# package.json is formatted by package managers, so we ignore it here
package.json
package-lock.json
build/*
dist/*
coverage/*
multimodules/*
/*.d.ts
/*.map
/*.js
**/*.md
9 changes: 8 additions & 1 deletion e2e/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
{}
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"useTabs": false,
"printWidth": 120
}
22 changes: 7 additions & 15 deletions e2e/.relay-chain.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
{
"exit": true,
"extension": [
"ts"
],
"parallel": true,
"require": [
"scaffolding/globalHooks.ts",
"scaffolding/rootHooks.ts",
"scaffolding/extrinsicHelpers.ts"
],
"loader": "ts-node/esm",
"spec": [
"./{,!(node_modules|load-tests)/**}/*.test.ts"
],
"timeout": 600000
"exit": true,
"extension": ["ts"],
"parallel": true,
"require": ["scaffolding/globalHooks.ts", "scaffolding/rootHooks.ts", "scaffolding/extrinsicHelpers.ts"],
"loader": "ts-node/esm",
"spec": ["./{,!(node_modules|load-tests)/**}/*.test.ts"],
"timeout": 600000
}
79 changes: 42 additions & 37 deletions e2e/capacity/replenishment.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "@frequency-chain/api-augment";
import { KeyringPair } from "@polkadot/keyring/types";
import { u16, u64 } from "@polkadot/types"
import assert from "assert";
import { Extrinsic, ExtrinsicHelper } from "../scaffolding/extrinsicHelpers";
import '@frequency-chain/api-augment';
import { KeyringPair } from '@polkadot/keyring/types';
import { u16, u64 } from '@polkadot/types';
import assert from 'assert';
import { Extrinsic, ExtrinsicHelper } from '../scaffolding/extrinsicHelpers';
import {
createKeys,
createMsaAndProvider,
Expand All @@ -16,48 +16,47 @@ import {
assertEvent,
getCapacity,
getNonce,
} from "../scaffolding/helpers";
import { getFundingSource } from "../scaffolding/funding";
import { isTestnet } from "../scaffolding/env";
} from '../scaffolding/helpers';
import { getFundingSource } from '../scaffolding/funding';
import { isTestnet } from '../scaffolding/env';

describe("Capacity Replenishment Testing: ", function () {
let schemaId: u16;
const fundingSource = getFundingSource("capacity-replenishment");
const fundingSource = getFundingSource('capacity-replenishment');

describe('Capacity Replenishment Testing: ', function () {
let schemaId: u16;

async function createAndStakeProvider(name: string, stakingAmount: bigint): Promise<[KeyringPair, u64]> {
const stakeKeys = createKeys(name);
const stakeProviderId = await createMsaAndProvider(fundingSource, stakeKeys, "ReplProv", 50n * DOLLARS);
assert.notEqual(stakeProviderId, 0, "stakeProviderId should not be zero");
const stakeProviderId = await createMsaAndProvider(fundingSource, stakeKeys, 'ReplProv', 50n * DOLLARS);
assert.notEqual(stakeProviderId, 0, 'stakeProviderId should not be zero');
await stakeToProvider(fundingSource, stakeKeys, stakeProviderId, stakingAmount);
return [stakeKeys, stakeProviderId];
}


before(async function () {
// Replenishment requires the epoch length to be shorter than testnet (set in globalHooks)
if (isTestnet()) this.skip();

schemaId = await getOrCreateGraphChangeSchema(fundingSource);
});

describe("Capacity is replenished", function () {
it("after new epoch", async function () {
describe('Capacity is replenished', function () {
it('after new epoch', async function () {
const totalStaked = 3n * DOLLARS;
let expectedCapacity = totalStaked / TokenPerCapacity;
const [stakeKeys, stakeProviderId] = await createAndStakeProvider("ReplFirst", totalStaked);
const payload = JSON.stringify({ changeType: 1, fromId: 1, objectId: 2 })
const expectedCapacity = totalStaked / TokenPerCapacity;
const [stakeKeys, stakeProviderId] = await createAndStakeProvider('ReplFirst', totalStaked);
const payload = JSON.stringify({ changeType: 1, fromId: 1, objectId: 2 });
const call = ExtrinsicHelper.addOnChainMessage(stakeKeys, schemaId, payload);
let nonce = await getNonce(stakeKeys);

// confirm that we start with a full tank
await ExtrinsicHelper.runToBlock(await getNextEpochBlock());
let remainingCapacity = (await getCapacity(stakeProviderId)).remainingCapacity.toBigInt();
assert.equal(expectedCapacity, remainingCapacity, "Our expected capacity from staking is wrong");
assert.equal(expectedCapacity, remainingCapacity, 'Our expected capacity from staking is wrong');

await call.payWithCapacity(nonce++);
remainingCapacity = (await getCapacity(stakeProviderId)).remainingCapacity.toBigInt();
assert(expectedCapacity > remainingCapacity, "Our remaining capacity is much higher than expected.");
assert(expectedCapacity > remainingCapacity, 'Our remaining capacity is much higher than expected.');
const capacityPerCall = expectedCapacity - remainingCapacity;
assert(remainingCapacity > capacityPerCall, "We don't have enough to make a second call");

Expand All @@ -77,15 +76,17 @@ describe("Capacity Replenishment Testing: ", function () {
});

function assert_capacity_call_fails_with_balance_too_low(call: Extrinsic) {
return assert.rejects(
call.payWithCapacity('current'), { name: "RpcError", message: /1010.+account balance too low/ });
return assert.rejects(call.payWithCapacity('current'), {
name: 'RpcError',
message: /1010.+account balance too low/,
});
}

describe("Capacity is not replenished", function () {
it("if out of capacity and last_replenished_at is <= current epoch", async function () {
let [stakeKeys, stakeProviderId] = await createAndStakeProvider("NoSend", 150n * CENTS);
let payload = JSON.stringify({ changeType: 1, fromId: 1, objectId: 2 })
let call = ExtrinsicHelper.addOnChainMessage(stakeKeys, schemaId, payload);
describe('Capacity is not replenished', function () {
it('if out of capacity and last_replenished_at is <= current epoch', async function () {
const [stakeKeys, stakeProviderId] = await createAndStakeProvider('NoSend', 150n * CENTS);
const payload = JSON.stringify({ changeType: 1, fromId: 1, objectId: 2 });
const call = ExtrinsicHelper.addOnChainMessage(stakeKeys, schemaId, payload);

// run until we can't afford to send another message.
await drainCapacity(call, stakeProviderId);
Expand All @@ -95,22 +96,22 @@ describe("Capacity Replenishment Testing: ", function () {
});

describe("Regression test: when user attempts to stake tiny amounts before provider's first message of an epoch,", function () {
it("provider is still replenished and can send a message", async function () {
it('provider is still replenished and can send a message', async function () {
const providerStakeAmt = 3n * DOLLARS;
const userStakeAmt = 100n * CENTS;
const userIncrementAmt = 1n * CENTS;

const [stakeKeys, stakeProviderId] = await createAndStakeProvider("TinyStake", providerStakeAmt);
const [stakeKeys, stakeProviderId] = await createAndStakeProvider('TinyStake', providerStakeAmt);
// new user/msa stakes to provider
const userKeys = createKeys("userKeys");
const userKeys = createKeys('userKeys');
await fundKeypair(fundingSource, userKeys, 5n * DOLLARS);
const { eventMap } = await ExtrinsicHelper.stake(userKeys, stakeProviderId, userStakeAmt).signAndSend();
assertEvent(eventMap, 'system.ExtrinsicSuccess');

const payload = JSON.stringify({ changeType: 1, fromId: 1, objectId: 2 })
const payload = JSON.stringify({ changeType: 1, fromId: 1, objectId: 2 });
const call = ExtrinsicHelper.addOnChainMessage(stakeKeys, schemaId, payload);

let expectedCapacity = (providerStakeAmt + userStakeAmt) / TokenPerCapacity;
const expectedCapacity = (providerStakeAmt + userStakeAmt) / TokenPerCapacity;
const totalCapacity = (await getCapacity(stakeProviderId)).totalCapacityIssued.toBigInt();
assert.equal(expectedCapacity, totalCapacity, `expected ${expectedCapacity} capacity, got ${totalCapacity}`);

Expand All @@ -120,15 +121,19 @@ describe("Capacity Replenishment Testing: ", function () {
await assert_capacity_call_fails_with_balance_too_low(call);

// go to next epoch
let nextEpochBlock = await getNextEpochBlock();
const nextEpochBlock = await getNextEpochBlock();
await ExtrinsicHelper.runToBlock(nextEpochBlock);

let remainingCapacity = (await getCapacity(stakeProviderId)).remainingCapacity.toBigInt();
// double check we still do not have enough to send another message
assert(remainingCapacity < callCapacityCost);

// user stakes tiny additional amount
const { eventMap: hasStaked } = await ExtrinsicHelper.stake(userKeys, stakeProviderId, userIncrementAmt).signAndSend();
const { eventMap: hasStaked } = await ExtrinsicHelper.stake(
userKeys,
stakeProviderId,
userIncrementAmt
).signAndSend();
assertEvent(hasStaked, 'capacity.Staked');

// provider can now send a message
Expand All @@ -137,7 +142,7 @@ describe("Capacity Replenishment Testing: ", function () {

remainingCapacity = (await getCapacity(stakeProviderId)).remainingCapacity.toBigInt();
// show that capacity was replenished and then fee deducted.
let approxExpected = providerStakeAmt + userStakeAmt + userIncrementAmt - callCapacityCost;
const approxExpected = providerStakeAmt + userStakeAmt + userIncrementAmt - callCapacityCost;
assert(remainingCapacity <= approxExpected, `remainingCapacity = ${remainingCapacity.toString()}`);
});
});
Expand All @@ -149,7 +154,7 @@ async function drainCapacity(call, stakeProviderId: u64): Promise<bigint> {
// Figure out the cost per call in Capacity
const { eventMap } = await call.payWithCapacity(nonce++);

const callCapacityCost = eventMap["capacity.CapacityWithdrawn"].data.amount.toBigInt();
const callCapacityCost = eventMap['capacity.CapacityWithdrawn'].data.amount.toBigInt();

// // Run them out of funds, but don't flake just because it landed near an epoch boundary.
await ExtrinsicHelper.runToBlock(await getNextEpochBlock());
Expand Down
Loading

0 comments on commit 8f55a11

Please sign in to comment.