From 8125fff1819ebb3cfe5c7688869046dc389cd696 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 12:09:40 +0100 Subject: [PATCH 1/4] chore: added error code expectations --- packages/abi-coder/test/Interface.test.ts | 45 +++++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/packages/abi-coder/test/Interface.test.ts b/packages/abi-coder/test/Interface.test.ts index 0980014ee0f..fd6b72f6f2b 100644 --- a/packages/abi-coder/test/Interface.test.ts +++ b/packages/abi-coder/test/Interface.test.ts @@ -1,4 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { FuelError } from '@fuel-ts/errors'; +import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; import type { BN } from '@fuel-ts/math'; import { concat } from '@fuel-ts/utils'; @@ -636,121 +638,156 @@ describe('Abi interface', () => { fn: exhaustiveExamplesInterface.functions.u_8, title: '[u8] - negative', value: -1, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u8.'), }, { fn: exhaustiveExamplesInterface.functions.u_8, title: '[u8] - over max', value: U8_MAX + 1, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u8, too many bytes.'), }, { fn: exhaustiveExamplesInterface.functions.u_16, title: '[u16] - negative', value: -1, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u16.'), }, { fn: exhaustiveExamplesInterface.functions.u_16, title: '[u16] - over max', value: U32_MAX + 1, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u16, too many bytes.'), }, { fn: exhaustiveExamplesInterface.functions.u_32, title: '[u32] - negative', value: -1, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u32.'), }, { fn: exhaustiveExamplesInterface.functions.u_32, title: '[u32] - over max', value: U32_MAX + 1, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u32, too many bytes.'), }, { fn: exhaustiveExamplesInterface.functions.u_64, title: '[u64] - negative', value: -1, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u64.'), }, { fn: exhaustiveExamplesInterface.functions.u_64, title: '[u64] - over max', value: U64_MAX.add(1), + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid u64.'), }, { fn: exhaustiveExamplesInterface.functions.b_256, title: '[b256] - too short', value: B256_DECODED.slice(0, B256_DECODED.length - 1), + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid b256.'), }, { fn: exhaustiveExamplesInterface.functions.b_256, title: '[b256] - too long', value: `${B256_DECODED}0`, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid b256.'), }, { fn: exhaustiveExamplesInterface.functions.b_256, title: '[b256] - not hex', value: `not a hex string`, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid b256.'), }, { fn: exhaustiveExamplesInterface.functions.b_512, title: '[b512] - too short', value: B512_ENCODED.slice(0, B512_ENCODED.length - 1), + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid struct B512.'), }, { fn: exhaustiveExamplesInterface.functions.b_512, title: '[b512] - too long', value: `${B512_DECODED}0`, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid struct B512.'), }, { fn: exhaustiveExamplesInterface.functions.b_256, title: '[b512] - not hex', value: `not a hex string`, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid b256.'), }, { fn: exhaustiveExamplesInterface.functions.boolean, title: '[boolean] - not bool', value: 'not bool', + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Invalid boolean value.'), }, { fn: exhaustiveExamplesInterface.functions.enum_simple, title: '[enum] - not in values', value: "Doesn't exist", + error: new FuelError( + FuelError.CODES.INVALID_DECODE_VALUE, + 'Only one field must be provided.' + ), }, { fn: exhaustiveExamplesInterface.functions.enum_with_builtin_type, title: '[enum] - multiple values selected', value: { a: true, b: 1 }, + error: new FuelError( + FuelError.CODES.INVALID_DECODE_VALUE, + 'Only one field must be provided.' + ), }, { fn: exhaustiveExamplesInterface.functions.struct_simple, title: '[struct] - missing property', value: { a: true }, + error: new FuelError( + FuelError.CODES.ENCODE_ERROR, + 'Invalid struct SimpleStruct. Field "b" not present.' + ), }, { fn: exhaustiveExamplesInterface.functions.struct_with_tuple, title: '[tuple] - extra element', value: { propB1: [true, U64_MAX, 'extra element'] }, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Types/values length mismatch.'), }, { fn: exhaustiveExamplesInterface.functions.struct_with_tuple, title: '[tuple] - missing element', value: { propB1: [true] }, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Types/values length mismatch.'), }, { fn: exhaustiveExamplesInterface.functions.array_simple, title: '[array] - input not array', value: { 0: 'element', 1: 'e', 2: 'e', 3: 'e' }, + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Expected array value.'), }, { fn: exhaustiveExamplesInterface.functions.array_simple, title: '[array] - not enough elements', value: [[1, 2, 3]], + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Types/values length mismatch.'), }, { fn: exhaustiveExamplesInterface.functions.array_simple, title: '[array] - too many elements', value: [[1, 2, 3, 4, 5]], + error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Types/values length mismatch.'), }, - ])('$title', ({ fn, value }) => { - expect(() => - Array.isArray(value) ? fn.encodeArguments(value) : fn.encodeArguments([value]) - ).toThrow(); + ])('$title', async ({ fn, value, error }) => { + const expectedError = error ?? new FuelError(FuelError.CODES.UNKNOWN, 'Unknown error'); + + await expectToThrowFuelError( + () => (Array.isArray(value) ? fn.encodeArguments(value) : fn.encodeArguments([value])), + expectedError + ); }); }); }); From 105a788680d66b3603fe2996474044e39c7a1a15 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 12:12:30 +0100 Subject: [PATCH 2/4] chore: use error --- packages/abi-coder/test/Interface.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/abi-coder/test/Interface.test.ts b/packages/abi-coder/test/Interface.test.ts index fd6b72f6f2b..a7633a07bb4 100644 --- a/packages/abi-coder/test/Interface.test.ts +++ b/packages/abi-coder/test/Interface.test.ts @@ -782,11 +782,9 @@ describe('Abi interface', () => { error: new FuelError(FuelError.CODES.ENCODE_ERROR, 'Types/values length mismatch.'), }, ])('$title', async ({ fn, value, error }) => { - const expectedError = error ?? new FuelError(FuelError.CODES.UNKNOWN, 'Unknown error'); - await expectToThrowFuelError( () => (Array.isArray(value) ? fn.encodeArguments(value) : fn.encodeArguments([value])), - expectedError + error ); }); }); From 66d81afa34eb7a7da3efbd38dd9e3c443924ee0b Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 12:12:48 +0100 Subject: [PATCH 3/4] chore: added changeset --- .changeset/ninety-geese-report.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/ninety-geese-report.md diff --git a/.changeset/ninety-geese-report.md b/.changeset/ninety-geese-report.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/ninety-geese-report.md @@ -0,0 +1,2 @@ +--- +--- From 74db961e1bb9c044e55c85a496d0c623a6393b21 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 12:14:22 +0100 Subject: [PATCH 4/4] chore: changeset --- .changeset/ninety-geese-report.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.changeset/ninety-geese-report.md b/.changeset/ninety-geese-report.md index a845151cc84..c93070b8f69 100644 --- a/.changeset/ninety-geese-report.md +++ b/.changeset/ninety-geese-report.md @@ -1,2 +1,4 @@ --- --- + +chore: added error code expectations to the exhaustive examples