Skip to content

Commit 7ac6a2b

Browse files
committed
Add tests for the estimate and update helper
1 parent 4a5375d commit 7ac6a2b

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import {
2+
Address,
3+
ITransactionMessageWithFeePayer,
4+
Rpc,
5+
SimulateTransactionApi,
6+
TransactionMessage,
7+
} from '@solana/kit';
8+
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
9+
import {
10+
estimateAndUpdateProvisoryComputeUnitLimitFactory,
11+
estimateComputeUnitLimitFactory,
12+
getSetComputeUnitLimitInstruction,
13+
MAX_COMPUTE_UNIT_LIMIT,
14+
PROVISORY_COMPUTE_UNIT_LIMIT,
15+
} from '../src';
16+
17+
const FOREVER_PROMISE = new Promise(() => {
18+
/* never resolve */
19+
});
20+
21+
describe('estimateAndUpdateProvisoryComputeUnitLimitFactory', () => {
22+
let sendSimulateTransactionRequest: Mock;
23+
let mockTransactionMessage: TransactionMessage &
24+
ITransactionMessageWithFeePayer;
25+
let rpc: Rpc<SimulateTransactionApi>;
26+
let simulateTransaction: Mock;
27+
28+
beforeEach(() => {
29+
mockTransactionMessage = {
30+
feePayer: {
31+
address: '7U8VWgTUucttJPt5Bbkt48WknWqRGBfstBt8qqLHnfPT' as Address,
32+
},
33+
instructions: [],
34+
version: 0,
35+
};
36+
sendSimulateTransactionRequest = vi.fn().mockReturnValue(FOREVER_PROMISE);
37+
simulateTransaction = vi.fn().mockReturnValue({
38+
send: sendSimulateTransactionRequest,
39+
});
40+
rpc = { simulateTransaction };
41+
});
42+
43+
it('appends a new SetComputeUnitLimit instruction if missing', async () => {
44+
expect.assertions(1);
45+
sendSimulateTransactionRequest.mockResolvedValue({
46+
value: { unitsConsumed: 42n },
47+
});
48+
const estimateAndUpdate = estimateAndUpdateProvisoryComputeUnitLimitFactory(
49+
estimateComputeUnitLimitFactory({ rpc })
50+
);
51+
52+
const updatedTransactionMessage = await estimateAndUpdate(
53+
mockTransactionMessage
54+
);
55+
56+
expect(updatedTransactionMessage).toEqual({
57+
...mockTransactionMessage,
58+
instructions: [getSetComputeUnitLimitInstruction({ units: 42 })],
59+
});
60+
});
61+
62+
it('updates the SetComputeUnitLimit instruction if set to PROVISORY_COMPUTE_UNIT_LIMIT', async () => {
63+
expect.assertions(1);
64+
sendSimulateTransactionRequest.mockResolvedValue({
65+
value: { unitsConsumed: 42n },
66+
});
67+
const estimateAndUpdate = estimateAndUpdateProvisoryComputeUnitLimitFactory(
68+
estimateComputeUnitLimitFactory({ rpc })
69+
);
70+
71+
const updatedTransactionMessage = await estimateAndUpdate({
72+
...mockTransactionMessage,
73+
instructions: [
74+
getSetComputeUnitLimitInstruction({
75+
units: PROVISORY_COMPUTE_UNIT_LIMIT,
76+
}),
77+
],
78+
});
79+
80+
expect(updatedTransactionMessage).toEqual({
81+
...mockTransactionMessage,
82+
instructions: [getSetComputeUnitLimitInstruction({ units: 42 })],
83+
});
84+
});
85+
86+
it('updates the SetComputeUnitLimit instruction if set to MAX_COMPUTE_UNIT_LIMIT', async () => {
87+
expect.assertions(1);
88+
sendSimulateTransactionRequest.mockResolvedValue({
89+
value: { unitsConsumed: 42n },
90+
});
91+
const estimateAndUpdate = estimateAndUpdateProvisoryComputeUnitLimitFactory(
92+
estimateComputeUnitLimitFactory({ rpc })
93+
);
94+
95+
const updatedTransactionMessage = await estimateAndUpdate({
96+
...mockTransactionMessage,
97+
instructions: [
98+
getSetComputeUnitLimitInstruction({
99+
units: MAX_COMPUTE_UNIT_LIMIT,
100+
}),
101+
],
102+
});
103+
104+
expect(updatedTransactionMessage).toEqual({
105+
...mockTransactionMessage,
106+
instructions: [getSetComputeUnitLimitInstruction({ units: 42 })],
107+
});
108+
});
109+
110+
it('does not update the SetComputeUnitLimit instruction if set to an explicit value', async () => {
111+
expect.assertions(1);
112+
sendSimulateTransactionRequest.mockResolvedValue({
113+
value: { unitsConsumed: 42n },
114+
});
115+
const estimateAndUpdate = estimateAndUpdateProvisoryComputeUnitLimitFactory(
116+
estimateComputeUnitLimitFactory({ rpc })
117+
);
118+
119+
const updatedTransactionMessage = await estimateAndUpdate({
120+
...mockTransactionMessage,
121+
instructions: [getSetComputeUnitLimitInstruction({ units: 123456 })],
122+
});
123+
124+
expect(updatedTransactionMessage).toEqual({
125+
...mockTransactionMessage,
126+
instructions: [getSetComputeUnitLimitInstruction({ units: 123456 })],
127+
});
128+
});
129+
130+
it('can be aborted', () => {
131+
const abortController = new AbortController();
132+
const estimateAndUpdate = estimateAndUpdateProvisoryComputeUnitLimitFactory(
133+
estimateComputeUnitLimitFactory({ rpc })
134+
);
135+
136+
estimateAndUpdate(mockTransactionMessage, {
137+
abortSignal: abortController.signal,
138+
}).catch(() => {});
139+
140+
expect(sendSimulateTransactionRequest).toHaveBeenCalledWith({
141+
abortSignal: expect.objectContaining({ aborted: false }),
142+
});
143+
abortController.abort();
144+
expect(sendSimulateTransactionRequest).toHaveBeenCalledWith({
145+
abortSignal: expect.objectContaining({ aborted: true }),
146+
});
147+
});
148+
});

0 commit comments

Comments
 (0)