Skip to content

Commit

Permalink
test: integrate launchTestNode with other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jun 26, 2024
1 parent 2a28b9b commit 7b783e3
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 105 deletions.
1 change: 0 additions & 1 deletion packages/fuel-gauge/src/advanced-logging.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { generateTestWallet } from '@fuel-ts/account/test-utils';
import type { FuelError } from '@fuel-ts/errors';
import type { WalletUnlocked } from 'fuels';
import { Script, bn } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

Expand Down
38 changes: 22 additions & 16 deletions packages/fuel-gauge/src/auth-testing.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import { generateTestWallet } from '@fuel-ts/account/test-utils';
import type { WalletUnlocked } from 'fuels';
import { Provider, getRandomB256, FUEL_NETWORK_URL } from 'fuels';
import { getRandomB256 } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { FuelGaugeProjectsEnum } from '../test/fixtures';
import { AuthTestingContractAbi__factory } from '../test/typegen/contracts';
import AuthTestingAbiHex from '../test/typegen/contracts/AuthTestingContractAbi.hex';

import { launchTestContract } from './utils';

let wallet: WalletUnlocked;
let baseAssetId: string;

/**
* @group node
* @group browser
*/
describe('Auth Testing', () => {
beforeAll(async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
baseAssetId = provider.getBaseAssetId();
wallet = await generateTestWallet(provider, [[1_000_000, baseAssetId]]);
});

it('can get is_caller_external', async () => {
using contractInstance = await launchTestContract(FuelGaugeProjectsEnum.AUTH_TESTING_CONTRACT);
using contractInstance = await launchTestContract({
deployer: AuthTestingContractAbi__factory,
bytecode: AuthTestingAbiHex,
});

const { value } = await contractInstance.functions.is_caller_external().call();

expect(value).toBeTruthy();
});

it('can check_msg_sender [with correct id]', async () => {
using contractInstance = await launchTestContract(FuelGaugeProjectsEnum.AUTH_TESTING_CONTRACT);
using launched = await launchTestNode({
contractsConfigs: [
{ deployer: AuthTestingContractAbi__factory, bytecode: AuthTestingAbiHex },
],
});

const {
contracts: [contractInstance],
wallets: [wallet],
} = launched;

const { value } = await contractInstance.functions
.check_msg_sender({ bits: wallet.address.toB256() })
Expand All @@ -39,7 +42,10 @@ describe('Auth Testing', () => {
});

it('can check_msg_sender [with incorrect id]', async () => {
using contractInstance = await launchTestContract(FuelGaugeProjectsEnum.AUTH_TESTING_CONTRACT);
using contractInstance = await launchTestContract({
deployer: AuthTestingContractAbi__factory,
bytecode: AuthTestingAbiHex,
});

await expect(
contractInstance.functions.check_msg_sender({ bits: getRandomB256() }).call()
Expand Down
32 changes: 18 additions & 14 deletions packages/fuel-gauge/src/bytecode-sway-lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { FUEL_NETWORK_URL, Predicate, Provider, arrayify } from 'fuels';

import { FuelGaugeProjectsEnum, getFuelGaugeForcProject } from '../test/fixtures';
import { defaultPredicateAbi } from '../test/fixtures/abi/predicate';
import { defaultPredicateBytecode } from '../test/fixtures/bytecode/predicate';
import { BytecodeSwayLibAbi__factory } from '../test/typegen/contracts';
import BytecodeSwayLibAbiHex from '../test/typegen/contracts/BytecodeSwayLibAbi.hex';
import CallTestContractAbiHex from '../test/typegen/contracts/CallTestContractAbi.hex';

import { launchTestContract } from './utils';

/**
* @group node
* @group browser
*/
describe('bytecode computations', () => {
test('compute_bytecode_root', async () => {
const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject(
FuelGaugeProjectsEnum.CALL_TEST_CONTRACT
);

using contract = await launchTestContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB);
using contract = await launchTestContract({
deployer: BytecodeSwayLibAbi__factory,
bytecode: BytecodeSwayLibAbiHex,
});

const { logs } = await contract.functions
.compute_bytecode_root(arrayify(bytecodeFromFile))
.compute_bytecode_root(Array.from(arrayify(CallTestContractAbiHex)))
.call();

const bytecodeRoot: string = logs[0];
Expand All @@ -28,18 +30,17 @@ describe('bytecode computations', () => {
});

test('verify_contract_bytecode', async () => {
const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject(
FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB
);

using contract = await launchTestContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB);
using contract = await launchTestContract({
deployer: BytecodeSwayLibAbi__factory,
bytecode: BytecodeSwayLibAbiHex,
});

const { value } = await contract.functions
.verify_contract_bytecode(
{
bits: contract.id.toB256(),
},
Array.from(arrayify(bytecodeFromFile))
Array.from(arrayify(BytecodeSwayLibAbiHex))
)
.call();

Expand All @@ -57,7 +58,10 @@ describe('bytecode computations', () => {

const address = predicate.address;

const contract = await launchTestContract(FuelGaugeProjectsEnum.BYTECODE_SWAY_LIB);
const contract = await launchTestContract({
deployer: BytecodeSwayLibAbi__factory,
bytecode: BytecodeSwayLibAbiHex,
});

const { value } = await contract.functions
.compute_predicate_address(Array.from(arrayify(defaultPredicateBytecode)))
Expand Down
54 changes: 39 additions & 15 deletions packages/fuel-gauge/src/bytes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { bn, Predicate, Wallet, Address, Provider, FUEL_NETWORK_URL } from 'fuel
import type { BN } from 'fuels';

import { FuelGaugeProjectsEnum, getFuelGaugeForcProject } from '../test/fixtures';
import { BytesAbi__factory } from '../test/typegen/contracts';
import BytesAbiHex from '../test/typegen/contracts/BytesAbi.hex';

import { getScript, launchTestContract } from './utils';

Expand Down Expand Up @@ -30,14 +32,11 @@ const setup = async (balance = 500_000) => {
* @group browser
*/
describe('Bytes Tests', () => {
let baseAssetId: string;
beforeAll(async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
baseAssetId = provider.getBaseAssetId();
});

it('should test bytes output', async () => {
using contractInstance = await launchTestContract(FuelGaugeProjectsEnum.BYTES);
using contractInstance = await launchTestContract({
deployer: BytesAbi__factory,
bytecode: BytesAbiHex,
});
const INPUT = 10;

const { value } = await contractInstance.functions.return_bytes(INPUT).call<number[]>();
Expand All @@ -46,7 +45,10 @@ describe('Bytes Tests', () => {
});

it('should test bytes output [100 items]', async () => {
using contractInstance = await launchTestContract(FuelGaugeProjectsEnum.BYTES);
using contractInstance = await launchTestContract({
deployer: BytesAbi__factory,
bytecode: BytesAbiHex,
});

const INPUT = 100;

Expand All @@ -56,7 +58,10 @@ describe('Bytes Tests', () => {
});

it('should test bytes input', async () => {
using contractInstance = await launchTestContract(FuelGaugeProjectsEnum.BYTES);
using contractInstance = await launchTestContract({
deployer: BytesAbi__factory,
bytecode: BytesAbiHex,
});

const INPUT = [40, 41, 42];

Expand All @@ -65,6 +70,10 @@ describe('Bytes Tests', () => {
});

it('should test bytes input [nested]', async () => {
using contractInstance = await launchTestContract({
deployer: BytesAbi__factory,
bytecode: BytesAbiHex,
});
const bytes = [40, 41, 42];

const INPUT: Wrapper = {
Expand All @@ -77,6 +86,11 @@ describe('Bytes Tests', () => {
});

it('should test bytes input [predicate-bytes]', async () => {
using launched = await launchTestContract({
deployer: BytesAbi__factory,
bytecode: BytesAbiHex,
});

const wallet = await setup(1_000_000);
const receiver = Wallet.fromAddress(Address.fromRandom(), wallet.provider);
const amountToPredicate = 500_000;
Expand All @@ -101,16 +115,26 @@ describe('Bytes Tests', () => {
});

// setup predicate
const setupTx = await wallet.transfer(predicate.address, amountToPredicate, baseAssetId, {
gasLimit: 10_000,
});
const setupTx = await wallet.transfer(
predicate.address,
amountToPredicate,
launched.provider.getBaseAssetId(),
{
gasLimit: 10_000,
}
);
await setupTx.waitForResult();

const initialReceiverBalance = await receiver.getBalance();

const tx = await predicate.transfer(receiver.address, amountToReceiver, baseAssetId, {
gasLimit: 10_000,
});
const tx = await predicate.transfer(
receiver.address,
amountToReceiver,
launched.provider.getBaseAssetId(),
{
gasLimit: 10_000,
}
);

const { isStatusSuccess } = await tx.waitForResult();

Expand Down
42 changes: 21 additions & 21 deletions packages/fuel-gauge/src/call-test-contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { ASSET_A } from '@fuel-ts/utils/test-utils';
import type { Contract } from 'fuels';
import { BN, bn, toHex } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { CallTestContractAbi__factory } from '../test/typegen/contracts';
import bytecode from '../test/typegen/contracts/CallTestContractAbi.hex';

const setupContract = async () => {
const {
contracts: [contract],
cleanup,
} = await launchTestNode({
contractsConfigs: [{ deployer: CallTestContractAbi__factory, bytecode }],
});
return Object.assign(contract, { [Symbol.dispose]: cleanup });
};
import { launchTestContract } from './utils';

const U64_MAX = bn(2).pow(64).sub(1);

Expand All @@ -24,7 +15,7 @@ const U64_MAX = bn(2).pow(64).sub(1);
*/
describe('CallTestContract', () => {
it.each([0, 1337, U64_MAX.sub(1)])('can call a contract with u64 (%p)', async (num) => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
const { value } = await contract.functions.foo(num).call();
expect(value.toHex()).toEqual(bn(num).add(1).toHex());
});
Expand All @@ -37,14 +28,14 @@ describe('CallTestContract', () => {
[{ a: false, b: U64_MAX.sub(1) }],
[{ a: true, b: U64_MAX.sub(1) }],
])('can call a contract with structs (%p)', async (struct) => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
const { value } = await contract.functions.boo(struct).call();
expect(value.a).toEqual(!struct.a);
expect(value.b.toHex()).toEqual(bn(struct.b).add(1).toHex());
});

it('can call a function with empty arguments', async () => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });

const { value: empty } = await contract.functions.empty().call();
expect(empty.toHex()).toEqual(toHex(63));
Expand All @@ -62,7 +53,7 @@ describe('CallTestContract', () => {
});

it('function with empty return should resolve undefined', async () => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });

// Call method with no params but with no result and no value on config
const { value } = await contract.functions.return_void().call();
Expand Down Expand Up @@ -139,7 +130,10 @@ describe('CallTestContract', () => {
async (method, { values, expected }) => {
// Type cast to Contract because of the dynamic nature of the test
// But the function names are type-constrained to correct Contract's type
using contract = await setupContract();
using contract = await launchTestContract({
deployer: CallTestContractAbi__factory,
bytecode,
});

const { value } = await (contract as Contract).functions[method](...values).call();

Expand All @@ -152,7 +146,7 @@ describe('CallTestContract', () => {
);

it('Forward amount value on contract call', async () => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });
const baseAssetId = contract.provider.getBaseAssetId();
const { value } = await contract.functions
.return_context_amount()
Expand All @@ -164,7 +158,7 @@ describe('CallTestContract', () => {
});

it('Forward asset_id on contract call', async () => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });

const assetId = ASSET_A;
const { value } = await contract.functions
Expand All @@ -177,7 +171,7 @@ describe('CallTestContract', () => {
});

it('Forward asset_id on contract simulate call', async () => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });

const assetId = ASSET_A;
const { value } = await contract.functions
Expand All @@ -190,7 +184,7 @@ describe('CallTestContract', () => {
});

it('can make multiple calls', async () => {
using contract = await setupContract();
using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode });

const num = 1337;
const numC = 10;
Expand Down Expand Up @@ -225,14 +219,20 @@ describe('CallTestContract', () => {
});

it('Calling a simple contract function does only one dry run', async () => {
using contract = await setupContract();
using contract = await launchTestContract({
deployer: CallTestContractAbi__factory,
bytecode,
});
const dryRunSpy = vi.spyOn(contract.provider.operations, 'dryRun');
await contract.functions.no_params().call();
expect(dryRunSpy).toHaveBeenCalledOnce();
});

it('Simulating a simple contract function does two dry runs', async () => {
using contract = await setupContract();
using contract = await launchTestContract({
deployer: CallTestContractAbi__factory,
bytecode,
});
const dryRunSpy = vi.spyOn(contract.provider.operations, 'dryRun');

await contract.functions.no_params().simulate();
Expand Down
Loading

0 comments on commit 7b783e3

Please sign in to comment.