|
1 |
| -import { SolanaJsonRpcIntegerOverflowError } from '../rpc-integer-overflow-error'; |
| 1 | +import { SOLANA_ERROR__RPC_INTEGER_OVERFLOW, SolanaError } from '@solana/errors'; |
2 | 2 |
|
3 |
| -describe('SolanaJsonRpcIntegerOverflowError', () => { |
4 |
| - it('features an informative error message', () => { |
5 |
| - expect(new SolanaJsonRpcIntegerOverflowError('someMethod', [2 /* third argument */], 1n)).toMatchInlineSnapshot( |
6 |
| - `[SolanaJsonRpcIntegerOverflowError: The 3rd argument to the \`someMethod\` RPC method was \`1\`. This number is unsafe for use with the Solana JSON-RPC because it exceeds \`Number.MAX_SAFE_INTEGER\`.]`, |
7 |
| - ); |
| 3 | +import { createSolanaJsonRpcIntegerOverflowError } from '../rpc-integer-overflow-error'; |
| 4 | + |
| 5 | +describe('createSolanaJsonRpcIntegerOverflowError()', () => { |
| 6 | + it('creates a `SolanaError`', () => { |
| 7 | + const error = createSolanaJsonRpcIntegerOverflowError('someMethod', [2 /* third argument */], 1n); |
| 8 | + expect(error).toBeInstanceOf(SolanaError); |
| 9 | + }); |
| 10 | + it('creates a `SolanaError` with the code `SOLANA_ERROR__RPC_INTEGER_OVERFLOW`', () => { |
| 11 | + const error = createSolanaJsonRpcIntegerOverflowError('someMethod', [2 /* third argument */], 1n); |
| 12 | + expect(error).toHaveProperty('context.__code', SOLANA_ERROR__RPC_INTEGER_OVERFLOW); |
8 | 13 | });
|
9 |
| - it('includes the full path to the value in the error message', () => { |
10 |
| - expect( |
11 |
| - new SolanaJsonRpcIntegerOverflowError('someMethod', [0 /* first argument */, 'foo', 'bar'], 1n), |
12 |
| - ).toMatchInlineSnapshot( |
13 |
| - `[SolanaJsonRpcIntegerOverflowError: The 1st argument to the \`someMethod\` RPC method at path \`foo.bar\` was \`1\`. This number is unsafe for use with the Solana JSON-RPC because it exceeds \`Number.MAX_SAFE_INTEGER\`.]`, |
| 14 | + it('creates a `SolanaError` with the correct context for a path-less violation', () => { |
| 15 | + const error = createSolanaJsonRpcIntegerOverflowError('someMethod', [2 /* third argument */], 1n); |
| 16 | + expect(error).toEqual( |
| 17 | + new SolanaError(SOLANA_ERROR__RPC_INTEGER_OVERFLOW, { |
| 18 | + argumentLabel: '3rd', |
| 19 | + keyPath: [2], |
| 20 | + methodName: 'someMethod', |
| 21 | + optionalPathLabel: '', |
| 22 | + value: 1n, |
| 23 | + }), |
14 | 24 | );
|
15 | 25 | });
|
16 |
| - it('exposes the method name, key path, and the value that overflowed', () => { |
17 |
| - expect(new SolanaJsonRpcIntegerOverflowError('someMethod', [0, 'foo', 'bar'], 1n)).toMatchObject({ |
18 |
| - keyPath: [0, 'foo', 'bar'], |
19 |
| - methodName: 'someMethod', |
20 |
| - value: 1n, |
21 |
| - }); |
| 26 | + it('creates a `SolanaError` with the correct context for a violation with a deep path', () => { |
| 27 | + const error = createSolanaJsonRpcIntegerOverflowError('someMethod', [0 /* first argument */, 'foo', 'bar'], 1n); |
| 28 | + expect(error).toHaveProperty('context.optionalPathLabel', ' at path `foo.bar`'); |
| 29 | + expect(error).toHaveProperty('context.path', 'foo.bar'); |
| 30 | + }); |
| 31 | + it('omits the error factory function itself from the stack trace', () => { |
| 32 | + const error = createSolanaJsonRpcIntegerOverflowError('someMethod', [0 /* first argument */, 'foo', 'bar'], 1n); |
| 33 | + expect(error.stack).not.toMatch(/createSolanaJsonRpcIntegerOverflowError/); |
| 34 | + }); |
| 35 | + it.each( |
| 36 | + Object.entries({ |
| 37 | + ...(() => { |
| 38 | + const out: Record<number, string> = {}; |
| 39 | + Array.from({ length: 100 }).forEach((_, ii) => { |
| 40 | + const lastDigit = ii % 10; |
| 41 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 42 | + if (lastDigit === 0) { |
| 43 | + out[ii] = `${ii + 1}st`; |
| 44 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 45 | + } else if (lastDigit === 1) { |
| 46 | + out[ii] = `${ii + 1}nd`; |
| 47 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 48 | + } else if (lastDigit === 2) { |
| 49 | + out[ii] = `${ii + 1}rd`; |
| 50 | + } else { |
| 51 | + out[ii] = `${ii + 1}th`; |
| 52 | + } |
| 53 | + }); |
| 54 | + return out; |
| 55 | + })(), |
| 56 | + 10: '11th', |
| 57 | + 11: '12th', |
| 58 | + 12: '13th', |
| 59 | + }), |
| 60 | + )('computes the correct ordinal when crafting the argument label', (index, expectedLabel) => { |
| 61 | + const error = createSolanaJsonRpcIntegerOverflowError('someMethod', [parseInt(index, 10)], 1n); |
| 62 | + expect(error).toHaveProperty('context.argumentLabel', expectedLabel); |
22 | 63 | });
|
23 | 64 | });
|
0 commit comments