Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add script for network testing #3564

Merged
merged 23 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
67e4525
add new package
Torres-ssf Jan 8, 2025
e7f562d
add sway projects
Torres-ssf Jan 8, 2025
b8b623e
add .example.env file
Torres-ssf Jan 8, 2025
e502c40
add test file
Torres-ssf Jan 8, 2025
5a936be
ignore test file
Torres-ssf Jan 8, 2025
a633bb3
remove recently added package
Torres-ssf Jan 8, 2025
25b837a
add network test file to fuel gauge tests
Torres-ssf Jan 8, 2025
b698708
add new script for network tests
Torres-ssf Jan 8, 2025
253f3d4
add environment variables
Torres-ssf Jan 8, 2025
3d5d6c2
rollback change
Torres-ssf Jan 8, 2025
2aad179
add changeset
Torres-ssf Jan 8, 2025
79d8b29
Merge branch 'master' into st/chore/add-network-tests
Torres-ssf Jan 8, 2025
d229ff8
remove test group
Torres-ssf Jan 8, 2025
efafb58
remove bail
Torres-ssf Jan 8, 2025
a5cd7a6
revert changes at pnpm lock file
Torres-ssf Jan 8, 2025
881d911
add network test group flag to test validation
Torres-ssf Jan 8, 2025
a2129a9
rename describe
Torres-ssf Jan 8, 2025
4e8133b
Merge branch 'master' into st/chore/add-network-tests
Torres-ssf Jan 8, 2025
1a56c5f
Merge branch 'master' into st/chore/add-network-tests
Torres-ssf Jan 8, 2025
72dbba6
Merge branch 'master' into st/chore/add-network-tests
Torres-ssf Jan 9, 2025
1ac5f6a
add running network test instructions to CONTRIBUTING.md
Torres-ssf Jan 9, 2025
39e493e
Merge branch 'master' of github.com:FuelLabs/fuels-ts into st/chore/a…
petertonysmith94 Jan 10, 2025
609313b
Merge branch 'master' into st/chore/add-network-tests
petertonysmith94 Jan 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/stupid-jokes-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: add script for network testing
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DEVNET_WALLET_PVT_KEY=
TESTNET_WALLET_PVT_KEY=
PUBLISHED_NPM_TAG=
PUBLISHED_NPM_TAG=
NETWORK_TEST_URL=
arboleya marked this conversation as resolved.
Show resolved Hide resolved
NETWORK_TEST_PVT_KEY=
34 changes: 34 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,40 @@ We'd follow the same approach as explained in the [Patching old releases](#patch
- have the versions of packages on `master` match the `latest` released package versions,
- have the released functionality on `master` as well

# Network Testing

The network test suite is designed to run locally against a specified network for validation purposes.

You can find the test suite at: `packages/fuel-gauge/src/network.test.ts`.

### Setup Instructions

Before running the tests, you need to configure the `.env` file:

1. Copy the `.env.example` file:

```sh
cp .env.example .env
```

2. Set the values for the following environment variables in the `.env` file:

```env
NETWORK_TEST_URL=https://testnet.fuel.network/v1/graphql
NETWORK_TEST_PVT_KEY=0x...
```

- `NETWORK_TEST_URL`: The URL of which network the test should run (e.g., Fuel Testnet endpoint).
- `NETWORK_TEST_PVT_KEY`: Your private key for the network.

### Running the Test Suite

Once the environment is set up, run the network tests using the following command:

```sh
pnpm test:network
```

# FAQ

### Why is the prefix `fuels` and not `fuel`?
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:validate": "./scripts/tests-validate.sh",
"test:browser:filter": "vitest --run --coverage false --config vitest.browser.config.mts",
"test:e2e": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --e2e)",
"test:network": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --network)",
"clinic:flame": "clinic flame --autocannon [ / ] -- node packages/${npm_config_package_name}/dist/profile.js",
"clinic:bubble": "clinic bubbleprof --autocannon [ -c 5 -a 500 / ] -- node packages/${npm_config_package_name}/dist/profile.js",
"clinic:doctor": "clinic doctor --autocannon [ / ] -- node packages/${npm_config_package_name}/dist/profile.js",
Expand Down
151 changes: 151 additions & 0 deletions packages/fuel-gauge/src/network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import type { BaseWalletUnlocked, ContractTransferParams, ReceiptTransfer } from 'fuels';
import { Provider, ReceiptType, Wallet } from 'fuels';

import {
CoverageContract,
CoverageContractFactory,
PredicateWithConfigurable,
ScriptMainArgs,
StorageTestContract,
StorageTestContractFactory,
} from '../test/typegen';

/**
* @group network
*/
describe('network', () => {
const timeout = 1000 * 50; // 50 seconds

let contractId1: string;
let contractId2: string;
let provider: Provider;
let wallet: BaseWalletUnlocked;
let baseAssetId: string;

beforeAll(async () => {
if (!process.env.NETWORK_TEST_URL || !process.env.NETWORK_TEST_PVT_KEY) {
throw new Error('Please provide NETWORK_TEST_URL and NETWORK_TEST_PVT_KEY in the .env file');
}

provider = new Provider(process.env.NETWORK_TEST_URL);
wallet = Wallet.fromPrivateKey(process.env.NETWORK_TEST_PVT_KEY, provider);
baseAssetId = await provider.getBaseAssetId();
});

it('should deploy contracts just fine', { timeout }, async () => {
const { waitForResult } = await StorageTestContractFactory.deploy(wallet);
const { contract: contract1, transactionResult: transactionResult1 } = await waitForResult();

const { waitForResult: waitForResult2 } = await CoverageContractFactory.deploy(wallet);
const { contract: contract2, transactionResult: transactionResult2 } = await waitForResult2();

expect(transactionResult1.isStatusSuccess).toBeTruthy();
expect(transactionResult2.isStatusSuccess).toBeTruthy();

contractId1 = contract1.id.toB256();
contractId2 = contract2.id.toB256();
});

it('should transfer to contracts just fine', { timeout }, async () => {
const contractTransferParams: ContractTransferParams[] = [
{
contractId: contractId1,
amount: 1,
assetId: baseAssetId,
},
{
contractId: contractId2,
amount: 3,
assetId: baseAssetId,
},
{
contractId: contractId1,
amount: 2,
assetId: baseAssetId,
},
];

const submit = await wallet.batchTransferToContracts(contractTransferParams);
const { receipts } = await submit.waitForResult();

const transferReceipts = receipts.filter(
({ type }) => type === ReceiptType.Transfer
) as ReceiptTransfer[];

expect(transferReceipts.length).toBe(contractTransferParams.length);

contractTransferParams.forEach(({ amount, contractId, assetId = baseAssetId }) => {
const foundReceipt = transferReceipts.find(
(r) => r.amount.eq(amount) && r.to === contractId && r.assetId === assetId
);

expect(foundReceipt).toBeDefined();
});
});

it('should execute contract call just fine', { timeout }, async () => {
const contract = new StorageTestContract(contractId1, wallet);

const call = await contract.functions.initialize_counter(1).call();
const { transactionResult } = await call.waitForResult();

expect(transactionResult.isStatusSuccess).toBeTruthy();
});

it('should execute contract multi-call just fine', { timeout }, async () => {
const contract1 = new StorageTestContract(contractId1, wallet);
const contract2 = new CoverageContract(contractId2, wallet);

const call = await contract1
.multiCall([
contract1.functions.increment_counter(2),
contract1.functions.counter(),
contract2.functions.echo_bool(true),
])
.call();

const { transactionResult } = await call.waitForResult();

expect(transactionResult.isStatusSuccess).toBeTruthy();
});

it('should deploy contract as blob and execute it just fine', { timeout }, async () => {
const factory = new StorageTestContractFactory(wallet);
const { waitForResult } = await factory.deployAsBlobTx();
const { contract } = await waitForResult();

const call = await contract.functions.initialize_counter(1).call();
const { transactionResult } = await call.waitForResult();

expect(transactionResult.isStatusSuccess).toBeTruthy();
});

it('should deploy script as blob and execute it just fine', { timeout }, async () => {
const script = new ScriptMainArgs(wallet);
const { waitForResult } = await script.deploy(wallet);
const scriptAsBlob = await waitForResult();

const call = await scriptAsBlob.functions.main(100).call();

const { transactionResult } = await call.waitForResult();
expect(transactionResult.isStatusSuccess).toBeTruthy();
});

it('should deploy predicate as blob and use it just fine', { timeout }, async () => {
const predicate = new PredicateWithConfigurable({
provider,
configurableConstants: { FEE: 10, ADDRESS: wallet.address.toB256() },
data: [10, wallet.address.toB256()],
});
const { waitForResult } = await predicate.deploy(wallet);
const predicateAsBlob = await waitForResult();

const transfer = await wallet.transfer(predicateAsBlob.address, 800, baseAssetId);
await transfer.waitForResult();

const transfer2 = await predicateAsBlob.transfer(wallet.address, 100, baseAssetId);
const { isStatusSuccess } = await transfer2.waitForResult();

expect(isStatusSuccess).toBeTruthy();
});
});
2 changes: 2 additions & 0 deletions scripts/tests-find.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ elif [[ $* == *--e2e* ]]; then
grep -lE "\*\s+@group\s+e2e" $FILES
elif [[ $* == *--integration* ]]; then
grep -lE "\*\s+@group\s+integration" $FILES
elif [[ $* == *--network* ]]; then
grep -lE "\*\s+@group\s+network" $FILES
fi
4 changes: 2 additions & 2 deletions scripts/tests-validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ ROOT=$(cd "$(dirname "$0")/.."; pwd)
# ignore files in apps/create-fuels-counter-guide/test/
FILES=$(find $ROOT/{apps,packages,internal} -name '*.test.ts' | grep -v "apps/create-fuels-counter-guide/test/")

INVALID_FILES=$(grep -LE "@group\s+(node|browser|e2e|integration)" $FILES)
INVALID_FILES=$(grep -LE "@group\s+(node|browser|e2e|integration|network)" $FILES)

if [ ! -z "$INVALID_FILES" ]; then
echo -e "Test files don't contain a test environment configuration:"
echo -e $INVALID_FILES
exit 1
fi
fi
Loading