From adf9f15d1460e31677cfacec3e5c075a1478d236 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 1 Oct 2024 12:27:16 +0100 Subject: [PATCH] chore: cleanup test --- packages/fuels/test/features/deploy.test.ts | 124 ++++++++++---------- 1 file changed, 60 insertions(+), 64 deletions(-) diff --git a/packages/fuels/test/features/deploy.test.ts b/packages/fuels/test/features/deploy.test.ts index e0bf8419963..55a2e7f9f00 100644 --- a/packages/fuels/test/features/deploy.test.ts +++ b/packages/fuels/test/features/deploy.test.ts @@ -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'; @@ -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: { @@ -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'); @@ -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 () => { @@ -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'); @@ -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, + }); }); });