|
| 1 | +import { Commitment } from '@solana/rpc-types'; |
| 2 | + |
1 | 3 | import { getParamsPatcherForSolanaLabsRpc } from '../params-patcher';
|
| 4 | +import { OPTIONS_OBJECT_POSITION_BY_METHOD } from '../params-patcher-options-object-position-config'; |
2 | 5 |
|
3 | 6 | describe('getParamsPatcherForSolanaLabsRpc', () => {
|
4 | 7 | describe('given no config', () => {
|
@@ -29,6 +32,186 @@ describe('getParamsPatcherForSolanaLabsRpc', () => {
|
29 | 32 | });
|
30 | 33 | });
|
31 | 34 | });
|
| 35 | + describe('with respect to the default commitment', () => { |
| 36 | + const METHODS_SUBJECT_TO_COMMITMENT_DEFAULTING = [ |
| 37 | + 'accountNotifications', |
| 38 | + 'blockNotifications', |
| 39 | + 'getAccountInfo', |
| 40 | + 'getBalance', |
| 41 | + 'getBlock', |
| 42 | + 'getBlockHeight', |
| 43 | + 'getBlockProduction', |
| 44 | + 'getBlocks', |
| 45 | + 'getBlocksWithLimit', |
| 46 | + 'getConfirmedBlock', |
| 47 | + 'getConfirmedBlocks', |
| 48 | + 'getConfirmedBlocksWithLimit', |
| 49 | + 'getConfirmedSignaturesForAddress2', |
| 50 | + 'getConfirmedTransaction', |
| 51 | + 'getEpochInfo', |
| 52 | + 'getFeeCalculatorForBlockhash', |
| 53 | + 'getFeeForMessage', |
| 54 | + 'getFees', |
| 55 | + 'getInflationGovernor', |
| 56 | + 'getInflationReward', |
| 57 | + 'getLargestAccounts', |
| 58 | + 'getLatestBlockhash', |
| 59 | + 'getLeaderSchedule', |
| 60 | + 'getMinimumBalanceForRentExemption', |
| 61 | + 'getMultipleAccounts', |
| 62 | + 'getProgramAccounts', |
| 63 | + 'getRecentBlockhash', |
| 64 | + 'getSignaturesForAddress', |
| 65 | + 'getSlot', |
| 66 | + 'getSlotLeader', |
| 67 | + 'getStakeActivation', |
| 68 | + 'getStakeMinimumDelegation', |
| 69 | + 'getSupply', |
| 70 | + 'getTokenAccountBalance', |
| 71 | + 'getTokenAccountsByDelegate', |
| 72 | + 'getTokenAccountsByOwner', |
| 73 | + 'getTokenLargestAccounts', |
| 74 | + 'getTokenSupply', |
| 75 | + 'getTransaction', |
| 76 | + 'getTransactionCount', |
| 77 | + 'getVoteAccounts', |
| 78 | + 'isBlockhashValid', |
| 79 | + 'logsNotifications', |
| 80 | + 'programNotifications', |
| 81 | + 'requestAirdrop', |
| 82 | + 'signatureNotifications', |
| 83 | + 'simulateTransaction', |
| 84 | + ]; |
| 85 | + describe.each(['processed', 'confirmed'] as Commitment[])( |
| 86 | + 'with the default commitment set to `%s`', |
| 87 | + defaultCommitment => { |
| 88 | + let patcher: ReturnType<typeof getParamsPatcherForSolanaLabsRpc>; |
| 89 | + beforeEach(() => { |
| 90 | + patcher = getParamsPatcherForSolanaLabsRpc({ defaultCommitment }); |
| 91 | + }); |
| 92 | + it.each(METHODS_SUBJECT_TO_COMMITMENT_DEFAULTING)( |
| 93 | + 'adds a default commitment on calls for `%s`', |
| 94 | + methodName => { |
| 95 | + expect(patcher([], methodName)).toContainEqual({ commitment: defaultCommitment }); |
| 96 | + }, |
| 97 | + ); |
| 98 | + it('adds a default preflight commitment on calls to `sendTransaction`', () => { |
| 99 | + expect(patcher([], 'sendTransaction')).toContainEqual({ preflightCommitment: defaultCommitment }); |
| 100 | + }); |
| 101 | + }, |
| 102 | + ); |
| 103 | + describe('with the default commitment set to `finalized`', () => { |
| 104 | + let patcher: ReturnType<typeof getParamsPatcherForSolanaLabsRpc>; |
| 105 | + beforeEach(() => { |
| 106 | + patcher = getParamsPatcherForSolanaLabsRpc({ defaultCommitment: 'finalized' }); |
| 107 | + }); |
| 108 | + it.each(METHODS_SUBJECT_TO_COMMITMENT_DEFAULTING)('adds no commitment on calls for `%s`', methodName => { |
| 109 | + expect(patcher([], methodName)).not.toContainEqual( |
| 110 | + expect.objectContaining({ commitment: expect.anything() }), |
| 111 | + ); |
| 112 | + }); |
| 113 | + it('adds no preflight commitment on calls to `sendTransaction`', () => { |
| 114 | + expect(patcher([], 'sendTransaction')).not.toContainEqual( |
| 115 | + expect.objectContaining({ preflightCommitment: expect.anything() }), |
| 116 | + ); |
| 117 | + }); |
| 118 | + }); |
| 119 | + describe('with no default commitment set', () => { |
| 120 | + let patcher: ReturnType<typeof getParamsPatcherForSolanaLabsRpc>; |
| 121 | + beforeEach(() => { |
| 122 | + patcher = getParamsPatcherForSolanaLabsRpc(); |
| 123 | + }); |
| 124 | + it.each(METHODS_SUBJECT_TO_COMMITMENT_DEFAULTING)('sets no commitment on calls to `%s`', methodName => { |
| 125 | + expect(patcher([], methodName)).not.toContainEqual( |
| 126 | + expect.objectContaining({ commitment: expect.anything() }), |
| 127 | + ); |
| 128 | + }); |
| 129 | + it('adds no preflight commitment on calls to `sendTransaction`', () => { |
| 130 | + expect(patcher([], 'sendTransaction')).not.toContainEqual( |
| 131 | + expect.objectContaining({ preflightCommitment: expect.anything() }), |
| 132 | + ); |
| 133 | + }); |
| 134 | + }); |
| 135 | + describe.each(['finalized', undefined])( |
| 136 | + 'when the params already specify a commitment of `%s`', |
| 137 | + existingCommitment => { |
| 138 | + describe.each(METHODS_SUBJECT_TO_COMMITMENT_DEFAULTING)('on calls to `%s`', methodName => { |
| 139 | + const optionsObjectPosition = OPTIONS_OBJECT_POSITION_BY_METHOD[methodName]!; |
| 140 | + it('removes the commitment property on calls to `%s` when there are other properties in the config object', () => { |
| 141 | + expect.assertions(1); |
| 142 | + const params = [ |
| 143 | + ...new Array(optionsObjectPosition), |
| 144 | + { commitment: existingCommitment, other: 'property' }, |
| 145 | + ]; |
| 146 | + const patcher = getParamsPatcherForSolanaLabsRpc(); |
| 147 | + expect(patcher(params, methodName)).toStrictEqual([ |
| 148 | + ...new Array(optionsObjectPosition).map(() => expect.anything()), |
| 149 | + { other: 'property' }, |
| 150 | + ]); |
| 151 | + }); |
| 152 | + it('deletes the commitment on calls to `%s` when there are no other properties left and the config object is not the last param', () => { |
| 153 | + expect.assertions(1); |
| 154 | + const params = [ |
| 155 | + ...new Array(optionsObjectPosition), |
| 156 | + { commitment: existingCommitment }, |
| 157 | + 'someParam', |
| 158 | + ]; |
| 159 | + const patcher = getParamsPatcherForSolanaLabsRpc(); |
| 160 | + expect(patcher(params, methodName)).toStrictEqual([ |
| 161 | + ...new Array(optionsObjectPosition).map(() => expect.anything()), |
| 162 | + undefined, |
| 163 | + 'someParam', |
| 164 | + ]); |
| 165 | + }); |
| 166 | + it('truncates the params on calls to `%s` when there are no other properties left and the config object is the last param', () => { |
| 167 | + expect.assertions(1); |
| 168 | + const params = [...new Array(optionsObjectPosition), { commitment: existingCommitment }]; |
| 169 | + const patcher = getParamsPatcherForSolanaLabsRpc(); |
| 170 | + expect(patcher(params, methodName)).toStrictEqual([ |
| 171 | + ...new Array(optionsObjectPosition).map(() => expect.anything()), |
| 172 | + ]); |
| 173 | + }); |
| 174 | + }); |
| 175 | + it('removes the preflight commitment property on calls to `%s` when there are other properties in the config object', () => { |
| 176 | + expect.assertions(1); |
| 177 | + const optionsObjectPosition = OPTIONS_OBJECT_POSITION_BY_METHOD['sendTransaction']!; |
| 178 | + const params = [ |
| 179 | + ...new Array(optionsObjectPosition), |
| 180 | + { other: 'property', preflightCommitment: existingCommitment }, |
| 181 | + ]; |
| 182 | + const patcher = getParamsPatcherForSolanaLabsRpc(); |
| 183 | + expect(patcher(params, 'sendTransaction')).toStrictEqual([ |
| 184 | + ...new Array(optionsObjectPosition).map(() => expect.anything()), |
| 185 | + { other: 'property' }, |
| 186 | + ]); |
| 187 | + }); |
| 188 | + it('deletes the preflight commitment on calls to `%s` when there are no other properties left and the config object is not the last param', () => { |
| 189 | + expect.assertions(1); |
| 190 | + const optionsObjectPosition = OPTIONS_OBJECT_POSITION_BY_METHOD['sendTransaction']!; |
| 191 | + const params = [ |
| 192 | + ...new Array(optionsObjectPosition), |
| 193 | + { preflightCommitment: existingCommitment }, |
| 194 | + 'someParam', |
| 195 | + ]; |
| 196 | + const patcher = getParamsPatcherForSolanaLabsRpc(); |
| 197 | + expect(patcher(params, 'sendTransaction')).toStrictEqual([ |
| 198 | + ...new Array(optionsObjectPosition).map(() => expect.anything()), |
| 199 | + undefined, |
| 200 | + 'someParam', |
| 201 | + ]); |
| 202 | + }); |
| 203 | + it('truncates the params on calls to `%s` when there are no other properties left and the config object is the last param', () => { |
| 204 | + expect.assertions(1); |
| 205 | + const optionsObjectPosition = OPTIONS_OBJECT_POSITION_BY_METHOD['sendTransaction']!; |
| 206 | + const params = [...new Array(optionsObjectPosition), { preflightCommitment: existingCommitment }]; |
| 207 | + const patcher = getParamsPatcherForSolanaLabsRpc(); |
| 208 | + expect(patcher(params, 'sendTransaction')).toStrictEqual([ |
| 209 | + ...new Array(optionsObjectPosition).map(() => expect.anything()), |
| 210 | + ]); |
| 211 | + }); |
| 212 | + }, |
| 213 | + ); |
| 214 | + }); |
32 | 215 | describe('given an integer overflow handler', () => {
|
33 | 216 | let onIntegerOverflow: jest.Mock;
|
34 | 217 | let paramsTransformer: (value: unknown) => unknown;
|
|
0 commit comments