forked from solana-foundation/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdlCard.spec.tsx
More file actions
284 lines (238 loc) · 10.1 KB
/
IdlCard.spec.tsx
File metadata and controls
284 lines (238 loc) · 10.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* eslint-disable no-restricted-syntax -- test assertions use RegExp for pattern matching */
import type { Idl } from '@coral-xyz/anchor';
import * as anchorModule from '@entities/idl';
import { Keypair, PublicKey } from '@solana/web3.js';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { vi } from 'vitest';
import { GENESIS_HASHES } from '@/app/entities/chain-id';
import * as programMetadataIdlModule from '@/app/entities/program-metadata';
import { ClusterProvider } from '@/app/providers/cluster';
import { Cluster, clusterSlug } from '@/app/utils/cluster';
import { IdlCard } from '../IdlCard';
vi.mock('next/navigation', () => ({
usePathname: vi.fn(),
useRouter: vi.fn(),
useSearchParams: vi.fn(),
}));
vi.mock('@solana/kit', () => ({
address: vi.fn((addr: string) => addr),
createSolanaRpc: vi.fn(() => ({
getEpochInfo: vi.fn(() => ({
send: vi.fn().mockResolvedValue({
absoluteSlot: 0n,
blockHeight: 0n,
epoch: 0n,
slotIndex: 0n,
slotsInEpoch: 432000n,
}),
})),
getEpochSchedule: vi.fn(() => ({
send: vi.fn().mockResolvedValue({
firstNormalEpoch: 0n,
firstNormalSlot: 0n,
leaderScheduleSlotOffset: 0n,
slotsPerEpoch: 432000n,
warmup: false,
}),
})),
getFirstAvailableBlock: vi.fn(() => ({
send: vi.fn().mockResolvedValue(0n),
})),
getGenesisHash: vi.fn(() => ({
send: vi.fn().mockResolvedValue(GENESIS_HASHES.MAINNET),
})),
})),
}));
const DEFAULT_ADDRESS = PublicKey.default.toBase58();
function createMockAnchorIdl(address = DEFAULT_ADDRESS): Idl {
return {
accounts: [],
address,
constants: [],
errors: [],
events: [],
instructions: [],
metadata: {
name: 'anchor_program',
spec: '0.1.0',
version: '0.1.0',
},
types: [],
};
}
function createMockProgramMetadataIdl() {
return {
kind: 'rootNode' as const,
name: 'metadata_program',
program: {
accounts: [],
definedTypes: [],
errors: [],
instructions: [],
pdas: [],
},
standard: 'codama',
version: '1.2.11',
};
}
describe('IdlCard', () => {
const programId = DEFAULT_ADDRESS;
beforeEach(() => {
vi.clearAllMocks();
(useSearchParams as ReturnType<typeof vi.fn>).mockReturnValue({
get: () => clusterSlug(Cluster.MainnetBeta),
has: (_query?: string) => false,
toString: () => '',
});
(usePathname as ReturnType<typeof vi.fn>).mockReturnValue('/');
(useRouter as ReturnType<typeof vi.fn>).mockReturnValue({
push: vi.fn(),
refresh: vi.fn(),
replace: vi.fn(),
});
});
test('should render IdlCard with PMP IDL when programMetadataIdl exists', async () => {
vi.spyOn(anchorModule, 'useAnchorProgram').mockReturnValue({
idl: null,
program: null,
});
vi.spyOn(programMetadataIdlModule, 'useProgramMetadataIdl').mockReturnValue({
programMetadataIdl: createMockProgramMetadataIdl(),
});
render(
<ClusterProvider>
<IdlCard programId={programId} />
</ClusterProvider>,
);
await waitFor(() => {
expect(screen.getByText('Program Metadata')).toBeInTheDocument();
});
expect(screen.getByText(/Program Metadata IDL/)).toBeInTheDocument();
expect(screen.queryByText(/Anchor IDL/)).not.toBeInTheDocument();
const windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null);
const downloadButton = screen.getByRole('button', { name: 'Download' });
fireEvent.click(downloadButton);
const generateSdkButton = screen.getByRole('button', { name: 'Generate SDK' });
fireEvent.click(generateSdkButton);
expect(screen.getByText('Leaving Solana Explorer')).toBeInTheDocument();
expect(screen.getByText('You are now leaving Explorer and going to Castaway.')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Continue' }));
expect(windowOpenSpy).toHaveBeenCalledTimes(1);
const [openedUrl, target, features] = windowOpenSpy.mock.calls[0]!;
const castawayUrl = new URL(openedUrl as string);
expect(castawayUrl.origin).toBe('https://www.castaway.lol');
expect(castawayUrl.pathname).toBe('/');
expect(castawayUrl.searchParams.get('program')).toBe(programId);
expect(castawayUrl.searchParams.get('idlSource')).toBe('program-metadata');
expect(castawayUrl.searchParams.get('network')).toBe('mainnet-beta');
expect(target).toBe('_blank');
expect(features).toBe('noopener,noreferrer');
windowOpenSpy.mockRestore();
});
test('should render IdlCard with Anchor IDL when anchorIdl exists', async () => {
vi.spyOn(anchorModule, 'useAnchorProgram').mockReturnValue({
idl: createMockAnchorIdl(),
program: null,
});
vi.spyOn(programMetadataIdlModule, 'useProgramMetadataIdl').mockReturnValue({
programMetadataIdl: null,
});
render(
<ClusterProvider>
<IdlCard programId={programId} />
</ClusterProvider>,
);
await waitFor(() => {
expect(screen.getByText('Anchor')).toBeInTheDocument();
});
expect(screen.getByText(/Anchor IDL/)).toBeInTheDocument();
expect(screen.queryByText(/Program Metadata IDL/)).not.toBeInTheDocument();
const windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null);
const downloadButton = screen.getByRole('button', { name: 'Download' });
fireEvent.click(downloadButton);
const generateSdkButton = screen.getByRole('button', { name: 'Generate SDK' });
fireEvent.click(generateSdkButton);
expect(screen.getByText('Leaving Solana Explorer')).toBeInTheDocument();
expect(screen.getByText('You are now leaving Explorer and going to Castaway.')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Continue' }));
expect(windowOpenSpy).toHaveBeenCalledTimes(1);
const [openedUrl, target, features] = windowOpenSpy.mock.calls[0]!;
const castawayUrl = new URL(openedUrl as string);
expect(castawayUrl.origin).toBe('https://www.castaway.lol');
expect(castawayUrl.pathname).toBe('/');
expect(castawayUrl.searchParams.get('program')).toBe(programId);
expect(castawayUrl.searchParams.get('idlSource')).toBe('anchor');
expect(castawayUrl.searchParams.get('network')).toBe('mainnet-beta');
expect(target).toBe('_blank');
expect(features).toBe('noopener,noreferrer');
windowOpenSpy.mockRestore();
});
test('should render IdlCard tabs when both IDLs exist', async () => {
vi.spyOn(anchorModule, 'useAnchorProgram').mockReturnValue({
idl: createMockAnchorIdl(),
program: null,
});
vi.spyOn(programMetadataIdlModule, 'useProgramMetadataIdl').mockReturnValue({
programMetadataIdl: createMockProgramMetadataIdl(),
});
render(
<ClusterProvider>
<IdlCard programId={programId} />
</ClusterProvider>,
);
await waitFor(() => {
expect(screen.getByText(/Program Metadata IDL/)).toBeInTheDocument();
});
const button = screen.getByRole('button', { name: 'Anchor' });
fireEvent.click(button);
expect(screen.getByText(/Anchor IDL/)).toBeInTheDocument();
});
test('should render BaseWarningCard when Anchor IDL address mismatches programId', async () => {
vi.spyOn(anchorModule, 'useAnchorProgram').mockReturnValue({
idl: createMockAnchorIdl(Keypair.generate().publicKey.toBase58()), // imitate malicious IDL
program: null,
});
vi.spyOn(programMetadataIdlModule, 'useProgramMetadataIdl').mockReturnValue({
programMetadataIdl: createMockAnchorIdl(), // but use normal one for PMP program
});
render(
<ClusterProvider>
<IdlCard programId={programId} />
</ClusterProvider>,
);
// PMP tab is active first
await waitFor(() => {
expect(screen.getByRole('button', { name: 'Anchor' })).toBeInTheDocument();
});
// Switch to Anchor tab to trigger the mismatch
fireEvent.click(screen.getByRole('button', { name: 'Anchor' }));
expect(screen.getByText('IDL Program ID Mismatch')).toBeInTheDocument();
expect(screen.getByText(/does not match the program being viewed/)).toBeInTheDocument();
expect(screen.queryByText(/Anchor IDL/)).not.toBeInTheDocument();
// Switch back to PMP tab - should render IDL normally
fireEvent.click(screen.getByRole('button', { name: 'Program Metadata' }));
expect(screen.queryByText('IDL Program ID Mismatch')).not.toBeInTheDocument();
expect(screen.getByText('0.30.1 Program Metadata IDL')).toBeInTheDocument();
});
test('should not render IdlCard when both IDLs are null', async () => {
vi.spyOn(anchorModule, 'useAnchorProgram').mockReturnValue({
idl: null,
program: null,
});
vi.spyOn(programMetadataIdlModule, 'useProgramMetadataIdl').mockReturnValue({
programMetadataIdl: null,
});
render(
<ClusterProvider>
<IdlCard programId={programId} />
</ClusterProvider>,
);
await waitFor(() => {
expect(screen.queryByText(/Anchor/)).not.toBeInTheDocument();
});
await waitFor(() => {
expect(screen.queryByText(/Program Metadata IDL/)).not.toBeInTheDocument();
});
});
});