-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathroute.spec.ts
More file actions
175 lines (144 loc) · 7.44 KB
/
Copy pathroute.spec.ts
File metadata and controls
175 lines (144 loc) · 7.44 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
import { SOLANA_ERROR__JSON_RPC__INTERNAL_ERROR, SolanaError } from '@solana/kit';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { IdlVariant } from '@/app/entities/idl/server';
import { Logger } from '@/app/shared/lib/logger';
import { Cluster } from '@/app/utils/cluster';
const PROGRAM_ADDRESS = 'C7QLEmDz81Usvy2sYa4xZSdA8EwEcYvZo8iuYZMaqXmj';
const mocks = vi.hoisted(() => ({
resolveProgramIdls: vi.fn(),
}));
// Resolution lives in `resolveProgramIdls` (its own spec). The route is the transport edge, so we
// mock the resolver and exercise query parsing, response shaping, partial-failure logging, and the
// error-to-HTTP policy (classified with `@solana/idl`'s real `isTransientRpcError`).
vi.mock('@/app/entities/idl/server', async () => {
const actual = await vi.importActual<typeof import('@/app/entities/idl/server')>('@/app/entities/idl/server');
return { ...actual, resolveProgramIdls: mocks.resolveProgramIdls };
});
vi.mock('@solana/kit', async () => {
const actual = await vi.importActual<typeof import('@solana/kit')>('@solana/kit');
// The resolver is mocked and ignores the rpc handle, so a stub is enough.
return { ...actual, createSolanaRpc: vi.fn(() => ({})) };
});
function resolved(overrides: Partial<Record<'anchorIdl' | 'programMetadataIdl' | 'preferredVariant', unknown>> = {}) {
return {
anchorIdl: undefined,
preferredVariant: IdlVariant.ProgramMetadata,
programMetadataIdl: undefined,
...overrides,
};
}
describe('GET /api/idl-latest', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(Logger, 'warn').mockImplementation(() => {});
vi.spyOn(Logger, 'panic').mockImplementation(() => {});
// PMP feature gate on by default; the off case is exercised explicitly below.
vi.stubEnv('NEXT_PUBLIC_PMP_IDL_ENABLED', 'true');
});
afterEach(() => {
vi.unstubAllEnvs();
});
it('should return 400 when required params are missing', async () => {
const { GET } = await importRoute();
const cases = [
createRequest({ cluster: String(Cluster.MainnetBeta) }), // missing programAddress
createRequest({ programAddress: PROGRAM_ADDRESS }), // missing cluster
];
const responses = await Promise.all(cases.map(r => GET(r)));
for (const res of responses) {
expect(res.status).toBe(400);
expect(await res.json()).toEqual({ error: 'Invalid query params' });
}
expect(mocks.resolveProgramIdls).not.toHaveBeenCalled();
});
it('should return 400 for an invalid cluster value', async () => {
const { GET } = await importRoute();
const res = await GET(createRequest({ cluster: '999', programAddress: PROGRAM_ADDRESS }));
expect(res.status).toBe(400);
expect(await res.json()).toEqual({ error: 'Invalid cluster' });
});
it('should return 400 for an invalid program address', async () => {
const { GET } = await importRoute();
const res = await GET(createRequest({ cluster: String(Cluster.MainnetBeta), programAddress: 'not-a-pubkey' }));
expect(res.status).toBe(400);
expect(await res.json()).toEqual({ error: 'Invalid program address' });
});
it('should shape the resolver output into the payload with cache headers', async () => {
mocks.resolveProgramIdls.mockResolvedValueOnce(
resolved({
anchorIdl: { name: 'anchor_idl' },
preferredVariant: IdlVariant.Anchor,
programMetadataIdl: { name: 'pmp' },
}),
);
const { GET } = await importRoute();
const res = await GET(createRequest({ cluster: String(Cluster.MainnetBeta), programAddress: PROGRAM_ADDRESS }));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
idls: { anchor: { name: 'anchor_idl' }, preferred: 'anchor', programMetadata: { name: 'pmp' } },
});
expect(res.headers.get('Cache-Control')).toContain('max-age=');
});
it('should resolve with includePmp=true when the PMP feature flag is on', async () => {
mocks.resolveProgramIdls.mockResolvedValueOnce(resolved({ programMetadataIdl: { name: 'pmp' } }));
const { GET } = await importRoute();
await GET(createRequest({ cluster: String(Cluster.MainnetBeta), programAddress: PROGRAM_ADDRESS }));
expect(mocks.resolveProgramIdls).toHaveBeenCalledWith(
expect.anything(),
PROGRAM_ADDRESS,
expect.objectContaining({ includePmp: true }),
);
});
it('should resolve with includePmp=false when the PMP feature flag is off', async () => {
vi.stubEnv('NEXT_PUBLIC_PMP_IDL_ENABLED', 'false');
mocks.resolveProgramIdls.mockResolvedValueOnce(
resolved({ anchorIdl: { name: 'a' }, preferredVariant: IdlVariant.Anchor }),
);
const { GET } = await importRoute();
await GET(createRequest({ cluster: String(Cluster.MainnetBeta), programAddress: PROGRAM_ADDRESS }));
expect(mocks.resolveProgramIdls).toHaveBeenCalledWith(
expect.anything(),
PROGRAM_ADDRESS,
expect.objectContaining({ includePmp: false }),
);
});
it('should return a retryable 502 (no page) when the resolver keeps throwing a transient RPC error', async () => {
mocks.resolveProgramIdls.mockRejectedValue(
new SolanaError(SOLANA_ERROR__JSON_RPC__INTERNAL_ERROR, { __serverMessage: 'Internal error' }),
);
const { GET } = await importRoute();
const res = await GET(createRequest({ cluster: String(Cluster.MainnetBeta), programAddress: PROGRAM_ADDRESS }));
expect(res.status).toBe(502);
expect(await res.json()).toEqual({ error: 'Upstream RPC error' });
// Transient errors are retried before giving up.
expect(mocks.resolveProgramIdls).toHaveBeenCalledTimes(3);
expect(Logger.warn).toHaveBeenCalled();
expect(Logger.panic).not.toHaveBeenCalled();
});
it('should retry past a premature-close fetch error and succeed', async () => {
mocks.resolveProgramIdls.mockRejectedValueOnce(new Error('Invalid response body ...: Premature close'));
mocks.resolveProgramIdls.mockResolvedValueOnce(
resolved({ anchorIdl: { name: 'a' }, preferredVariant: IdlVariant.Anchor }),
);
const { GET } = await importRoute();
const res = await GET(createRequest({ cluster: String(Cluster.MainnetBeta), programAddress: PROGRAM_ADDRESS }));
expect(res.status).toBe(200);
expect(mocks.resolveProgramIdls).toHaveBeenCalledTimes(2);
expect(Logger.panic).not.toHaveBeenCalled();
});
it('should return 502 and escalate on an unexpected (non-RPC) error', async () => {
mocks.resolveProgramIdls.mockRejectedValueOnce(new Error('boom'));
const { GET } = await importRoute();
const res = await GET(createRequest({ cluster: String(Cluster.MainnetBeta), programAddress: PROGRAM_ADDRESS }));
expect(res.status).toBe(502);
expect(await res.json()).toEqual({ error: 'Failed to resolve IDLs' });
expect(Logger.panic).toHaveBeenCalled();
});
});
function createRequest(params: Record<string, string> = {}) {
const search = new URLSearchParams(params);
return new Request(`http://localhost:3000/api/idl-latest?${search}`);
}
async function importRoute() {
return await import('../route');
}