-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathupdateAuthority.test.ts
More file actions
260 lines (226 loc) · 8.47 KB
/
updateAuthority.test.ts
File metadata and controls
260 lines (226 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import type { Address, TransactionSigner } from 'gill';
import { AuthorityType, TOKEN_2022_PROGRAM_ADDRESS } from 'gill/programs/token';
import { getUpdateAuthorityInstructions } from '../updateAuthority';
import {
createMockSigner,
generateMockAddress,
TEST_AUTHORITY,
} from '../../__tests__/test-utils';
describe('getUpdateAuthorityInstructions', () => {
let mockMint: Address;
let mockCurrentAuthority: TransactionSigner<string>;
let mockNewAuthority: Address;
beforeEach(() => {
mockMint = generateMockAddress() as Address;
mockCurrentAuthority = createMockSigner();
mockNewAuthority = generateMockAddress() as Address;
});
describe('when role is "Metadata"', () => {
it('should return metadata update authority instruction', () => {
const result = getUpdateAuthorityInstructions({
mint: mockMint,
role: 'Metadata',
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(1);
const instruction = result[0];
expect(instruction).toBeDefined();
expect(instruction.programAddress).toBe(TOKEN_2022_PROGRAM_ADDRESS);
expect(instruction.accounts).toBeDefined();
expect(instruction.data).toBeDefined();
});
it('should handle different mint addresses for metadata', () => {
const customMint = TEST_AUTHORITY as Address;
const result = getUpdateAuthorityInstructions({
mint: customMint,
role: 'Metadata',
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(result).toHaveLength(1);
expect(result[0].programAddress).toBe(TOKEN_2022_PROGRAM_ADDRESS);
});
});
describe('when role is an AuthorityType', () => {
const authorityTypes = [
AuthorityType.MintTokens,
AuthorityType.FreezeAccount,
AuthorityType.AccountOwner,
AuthorityType.CloseAccount,
AuthorityType.TransferFeeConfig,
AuthorityType.WithheldWithdraw,
AuthorityType.CloseMint,
AuthorityType.InterestRate,
AuthorityType.PermanentDelegate,
AuthorityType.ConfidentialTransferMint,
AuthorityType.TransferHookProgramId,
AuthorityType.ConfidentialTransferFeeConfig,
AuthorityType.MetadataPointer,
AuthorityType.GroupPointer,
AuthorityType.GroupMemberPointer,
AuthorityType.ScaledUiAmount,
AuthorityType.Pause,
];
authorityTypes.forEach(authorityType => {
it(`should return set authority instruction for ${AuthorityType[authorityType]}`, () => {
const result = getUpdateAuthorityInstructions({
mint: mockMint,
role: authorityType,
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(1);
const instruction = result[0];
expect(instruction).toBeDefined();
expect(instruction.programAddress).toBe(TOKEN_2022_PROGRAM_ADDRESS);
expect(instruction.accounts).toBeDefined();
expect(instruction.data).toBeDefined();
});
});
it('should return set authority instruction for MintTokens authority', () => {
const result = getUpdateAuthorityInstructions({
mint: mockMint,
role: AuthorityType.MintTokens,
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(1);
expect(result[0].programAddress).toBe(TOKEN_2022_PROGRAM_ADDRESS);
});
});
describe('edge cases and validation', () => {
it('should handle different new authority addresses', () => {
const customNewAuthority = TEST_AUTHORITY as Address;
const result = getUpdateAuthorityInstructions({
mint: mockMint,
role: AuthorityType.MintTokens,
currentAuthority: mockCurrentAuthority,
newAuthority: customNewAuthority,
});
expect(result).toHaveLength(1);
expect(result[0]).toBeDefined();
});
it('should handle different current authority signers', () => {
const customCurrentAuthority = createMockSigner(TEST_AUTHORITY);
const result = getUpdateAuthorityInstructions({
mint: mockMint,
role: AuthorityType.FreezeAccount,
currentAuthority: customCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(result).toHaveLength(1);
expect(result[0]).toBeDefined();
});
it('should produce different instructions for metadata vs authority types', () => {
const metadataResult = getUpdateAuthorityInstructions({
mint: mockMint,
role: 'Metadata',
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
const authorityResult = getUpdateAuthorityInstructions({
mint: mockMint,
role: AuthorityType.MintTokens,
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(metadataResult).toHaveLength(1);
expect(authorityResult).toHaveLength(1);
// Instructions should be different (different data/accounts structure)
expect(metadataResult[0].data).not.toEqual(authorityResult[0].data);
});
});
describe('function signature and types', () => {
it('should accept all required parameters', () => {
expect(() => {
getUpdateAuthorityInstructions({
mint: mockMint,
role: 'Metadata',
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
}).not.toThrow();
});
it('should accept AuthorityType enum values', () => {
expect(() => {
getUpdateAuthorityInstructions({
mint: mockMint,
role: AuthorityType.AccountOwner,
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
}).not.toThrow();
});
it('should return array of instructions with correct structure', () => {
const result = getUpdateAuthorityInstructions({
mint: mockMint,
role: AuthorityType.MintTokens,
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBeGreaterThan(0);
result.forEach(instruction => {
expect(typeof instruction).toBe('object');
expect(instruction).toHaveProperty('programAddress');
expect(instruction).toHaveProperty('accounts');
expect(instruction).toHaveProperty('data');
expect(typeof instruction.programAddress).toBe('string');
expect(Array.isArray(instruction.accounts)).toBe(true);
expect(instruction.data instanceof Uint8Array).toBe(true);
});
});
});
describe('consistent behavior', () => {
it('should always return exactly one instruction', () => {
// Test metadata role
const metadataResult = getUpdateAuthorityInstructions({
mint: mockMint,
role: 'Metadata',
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(metadataResult).toHaveLength(1);
// Test various authority types
const authorityTypes = [
AuthorityType.MintTokens,
AuthorityType.FreezeAccount,
AuthorityType.AccountOwner,
AuthorityType.CloseAccount,
];
authorityTypes.forEach(authorityType => {
const result = getUpdateAuthorityInstructions({
mint: mockMint,
role: authorityType,
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(result).toHaveLength(1);
});
});
it('should use TOKEN_2022_PROGRAM_ADDRESS for all instructions', () => {
// Test metadata
const metadataResult = getUpdateAuthorityInstructions({
mint: mockMint,
role: 'Metadata',
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(metadataResult[0].programAddress).toBe(TOKEN_2022_PROGRAM_ADDRESS);
// Test authority type
const authorityResult = getUpdateAuthorityInstructions({
mint: mockMint,
role: AuthorityType.MintTokens,
currentAuthority: mockCurrentAuthority,
newAuthority: mockNewAuthority,
});
expect(authorityResult[0].programAddress).toBe(
TOKEN_2022_PROGRAM_ADDRESS
);
});
});
});