-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathroute.error-resilience.test.ts
More file actions
167 lines (134 loc) · 4 KB
/
Copy pathroute.error-resilience.test.ts
File metadata and controls
167 lines (134 loc) · 4 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
/* ---------------------------
* Mocks
* -------------------------- */
vi.mock('next/server', async () => {
const actual = await vi.importActual<typeof import('next/server')>('next/server');
return {
...actual,
after: vi.fn((cb: () => void) => cb()),
};
});
vi.mock('@/lib/github', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/lib/github')>();
return {
...actual,
getFullDashboardData: vi.fn(),
};
});
vi.mock('@/utils/getClientIp', () => ({
getClientIp: vi.fn(() => '127.0.0.1'),
}));
vi.mock('@/services/github/quota-monitor', () => ({
quotaMonitor: {
isQuotaLow: vi.fn(() => false),
getQuota: vi.fn(() => ({
remaining: 5000,
})),
},
}));
vi.mock('@/services/github/refresh-policy', () => ({
refreshPolicy: {
isRefreshAllowed: vi.fn(() => true),
recordRefresh: vi.fn(),
getRemainingCooldown: vi.fn(() => 0),
},
}));
vi.mock('@/services/github/refresh-rate-limiter', () => ({
refreshRateLimiter: {
checkLimit: vi.fn(() => ({
success: true,
limit: 3,
remaining: 2,
reset: Date.now() + 60000,
})),
},
}));
vi.mock('@/services/github/background-refresh', () => ({
backgroundRefresh: {
isStale: vi.fn(() => false),
triggerRefresh: vi.fn(),
},
}));
vi.mock('@/lib/logger', () => ({
logger: {
warn: vi.fn(),
error: vi.fn(),
info: vi.fn(),
},
}));
vi.mock('@/lib/validations', () => ({
githubParamsSchema: {
safeParse: vi.fn(() => ({
success: true,
data: {
username: 'octocat',
refresh: false,
bypassCache: false,
},
})),
},
coerceQueryParams: vi.fn((params) => params),
}));
import { GET } from './route';
import { getFullDashboardData } from '@/lib/github';
const makeRequest = () => new Request('http://localhost:3000/api/github?username=octocat');
beforeEach(() => {
vi.clearAllMocks();
});
describe('GitHub route error resilience', () => {
it('returns 404 when user is not found', async () => {
vi.mocked(getFullDashboardData).mockRejectedValueOnce({
status: 404,
message: 'User not found',
});
const response = await GET(makeRequest());
expect(response.status).toBe(404);
expect(await response.json()).toEqual({
error: 'User not found',
});
});
});
it('returns 403 when GitHub API rate limit is reached', async () => {
vi.mocked(getFullDashboardData).mockRejectedValueOnce({
status: 403,
message: 'API Rate Limit Exceeded',
});
const response = await GET(makeRequest());
expect(response.status).toBe(403);
expect(await response.json()).toEqual({
error: 'GitHub API rate limit reached. Please configure GITHUB_TOKEN.',
});
});
it('returns 500 for unexpected internal errors', async () => {
vi.mocked(getFullDashboardData).mockRejectedValueOnce(new Error('Unexpected database failure'));
const response = await GET(makeRequest());
expect(response.status).toBe(500);
expect(await response.json()).toEqual({
error: 'An unexpected error occurred. Please try again.',
});
});
it('unwraps nested error causes and returns 404', async () => {
vi.mocked(getFullDashboardData).mockRejectedValueOnce(
new Error('Outer wrapper', {
cause: new Error('User not found'),
})
);
const response = await GET(makeRequest());
expect(response.status).toBe(404);
expect(await response.json()).toEqual({
error: 'User not found',
});
});
it('does not crash when stale cache triggers background refresh', async () => {
const { backgroundRefresh } = await import('@/services/github/background-refresh');
vi.mocked(backgroundRefresh.isStale).mockReturnValueOnce(true);
vi.mocked(getFullDashboardData).mockResolvedValueOnce({
profile: {},
repositories: [],
lastSyncedAt: '2023-01-01T00:00:00.000Z',
} as never);
const response = await GET(makeRequest());
expect(response.status).toBe(200);
expect(backgroundRefresh.triggerRefresh).toHaveBeenCalledWith('octocat');
});