Skip to content

Commit

Permalink
chore: cleanup test
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate committed Oct 1, 2024
1 parent f3bbde7 commit adf9f15
Showing 1 changed file with 60 additions and 64 deletions.
124 changes: 60 additions & 64 deletions packages/fuels/test/features/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { JsonAbi } from '@fuel-ts/abi-coder';
import type { Account } from '@fuel-ts/account';
import { Contract } from '@fuel-ts/program';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
Expand Down Expand Up @@ -58,6 +60,20 @@ describe('deploy', { timeout: 180000 }, () => {
expect(firstFuelsContents.fooBar).toMatch(/0x/);
});

/**
* Executes the target contract and returns the values of the functions for proxy deploys.
*/
async function executeTargetContract(contractId: string, abi: JsonAbi, wallet: Account) {
const targetContract = new Contract(contractId, abi, wallet);

const { value: getCountValue } = await targetContract.functions.get_value().get();

const res = await targetContract.functions.test_function().call();
const { value: testFunctionValue } = await res.waitForResult();

return { getCountValue, testFunctionValue };
}

it('should run `deploy` command [using proxy + re-deploy]', async () => {
using launched = await launchTestNode({
nodeOptions: {
Expand Down Expand Up @@ -92,38 +108,26 @@ describe('deploy', { timeout: 180000 }, () => {
expect(firstFuelsContents.fooBar).toMatch(/0x/);
expect(firstFuelsContents.upgradable).toMatch(/0x/);

/**
* a) Add helper
* For interacting with deployed contract
*/
async function executeTargetContract() {
const upgradableContractId = firstFuelsContents.upgradable;
const upgradableAbi = JSON.parse(
readFileSync(
join(paths.upgradableContractPath, 'out', 'debug', 'upgradable-abi.json'),
'utf-8'
)
);

const targetContract = new Contract(upgradableContractId, upgradableAbi, wallet);

const { value: getCountValue } = await targetContract.functions.get_value().get();
expect(getCountValue).toBe(10);

const res = await targetContract.functions.test_function().call();
const { value } = await res.waitForResult();

return value;
}
const contractId = firstFuelsContents.upgradable;
const abi = JSON.parse(
readFileSync(
join(paths.upgradableContractPath, 'out', 'debug', 'upgradable-abi.json'),
'utf-8'
)
);

/**
* b) Interact with target contract
* Calling `test_function` should return `true` for the first execution.
* a) Interacting with the target contract
* Calling `test_function` should return `true` and `get_value`
* should return `10` for the first execution.
*/
expect(await executeTargetContract()).toBe(true); // TRUE
expect(await executeTargetContract(contractId, abi, wallet)).toStrictEqual({
getCountValue: 10,
testFunctionValue: true,
});

/**
* c) Modify `main.sw` method before second deploy
* b) Modify `main.sw` method before second deploy
* This will make the method return `false` instead of `true`.
*/
const mainPath = join(paths.upgradableContractPath, 'src', 'main.sw');
Expand All @@ -144,10 +148,14 @@ describe('deploy', { timeout: 180000 }, () => {
expect(firstFuelsContents.upgradable).toEqual(secondFuelsContents.upgradable);

/**
* d) Interact with target contract
* Now, calling `test_function` should return `false` instead.
* c) Interact with target contract
* Now, calling `test_function` should return `false` instead,
* but `get_value` should still return `10`.
*/
expect(await executeTargetContract()).toBe(false); // FALSE
expect(await executeTargetContract(contractId, abi, wallet)).toStrictEqual({
getCountValue: 10,
testFunctionValue: false,
});
});

it('should run `deploy` command [using proxy and chunking + re-deploy]', async () => {
Expand Down Expand Up @@ -184,42 +192,26 @@ describe('deploy', { timeout: 180000 }, () => {
expect(firstFuelsContents.fooBar).toMatch(/0x/);
expect(firstFuelsContents.upgradable).toMatch(/0x/);

/**
* a) Add helper
* For interacting with deployed contract
*/
async function executeTargetContract() {
const upgradableChunkedContractId = firstFuelsContents.upgradableChunked;
const upgradableChunkedAbi = JSON.parse(
readFileSync(
join(paths.upgradableChunkedContractPath, 'out', 'debug', 'upgradable-chunked-abi.json'),
'utf-8'
)
);

const targetContract = new Contract(
upgradableChunkedContractId,
upgradableChunkedAbi,
wallet
);

const { value: getCountValue } = await targetContract.functions.get_value().get();
expect(getCountValue).toBe(10);

const res = await targetContract.functions.test_function().call();
const { value } = await res.waitForResult();

return value;
}
const contractId = firstFuelsContents.upgradableChunked;
const abi = JSON.parse(
readFileSync(
join(paths.upgradableChunkedContractPath, 'out', 'debug', 'upgradable-chunked-abi.json'),
'utf-8'
)
);

/**
* b) Interact with target contract
* Calling `test_function` should return `true` for the first execution.
* a) Interacting with the target contract
* Calling `test_function` should return `true` and `get_value`
* should return `10` for the first execution.
*/
expect(await executeTargetContract()).toBe(true); // TRUE
expect(await executeTargetContract(contractId, abi, wallet)).toStrictEqual({
getCountValue: 10,
testFunctionValue: true,
});

/**
* c) Modify `main.sw` method before second deploy
* b) Modify `main.sw` method before second deploy
* This will make the method return `false` instead of `true`.
*/
const mainPath = join(paths.upgradableChunkedContractPath, 'src', 'main.sw');
Expand All @@ -240,9 +232,13 @@ describe('deploy', { timeout: 180000 }, () => {
expect(firstFuelsContents.upgradableChunked).toEqual(secondFuelsContents.upgradableChunked);

/**
* d) Interact with target contract
* Now, calling `test_function` should return `false` instead.
* c) Interact with target contract
* Now, calling `test_function` should return `false` instead,
* but `get_value` should still return `10`.
*/
expect(await executeTargetContract()).toBe(false); // FALSE
expect(await executeTargetContract(contractId, abi, wallet)).toStrictEqual({
getCountValue: 10,
testFunctionValue: false,
});
});
});

0 comments on commit adf9f15

Please sign in to comment.