forked from JhaSourav07/commitpulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.mouse-interactivity.test.ts
More file actions
115 lines (81 loc) · 3.45 KB
/
Copy pathroute.mouse-interactivity.test.ts
File metadata and controls
115 lines (81 loc) · 3.45 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GET } from './route';
import { fetchPRInsights } from '@/services/github/pr-insights';
import { RateLimiter } from '@/lib/rate-limit';
vi.mock('@/services/github/pr-insights', () => ({
fetchPRInsights: vi.fn(),
}));
describe('pr-insights mouse interactivity contract', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(RateLimiter.prototype, 'checkWithResult').mockResolvedValue({
success: true,
limit: 10,
remaining: 9,
reset: 123456789,
});
});
it('returns 400 when username is missing', async () => {
const request = new Request('http://localhost/api/pr-insights');
const response = await GET(request);
expect(response.status).toBe(400);
const body = await response.json();
expect(body).toEqual({
error: 'Username is required',
});
});
it('calls fetchPRInsights with username from search params', async () => {
vi.mocked(fetchPRInsights).mockResolvedValue([] as never);
const request = new Request('http://localhost/api/pr-insights?username=aanya');
await GET(request);
expect(fetchPRInsights).toHaveBeenCalledWith('aanya', undefined);
});
it('returns fetched data on success', async () => {
const mockData = [{ id: 1, title: 'PR 1' }];
vi.mocked(fetchPRInsights).mockResolvedValue(mockData as never);
const request = new Request('http://localhost/api/pr-insights?username=aanya');
const response = await GET(request);
expect(response.status).toBe(200);
expect(await response.json()).toEqual(mockData);
});
it('rejects requests when the endpoint abuse budget is exhausted', async () => {
vi.spyOn(RateLimiter.prototype, 'checkWithResult').mockResolvedValueOnce({
success: false,
limit: 10,
remaining: 0,
reset: 123456789,
});
const response = await GET(new Request('http://localhost/api/pr-insights?username=octocat'));
expect(response.status).toBe(429);
expect(response.headers.get('x-ratelimit-remaining')).toBe('0');
expect(fetchPRInsights).not.toHaveBeenCalled();
});
it('uses a fixed endpoint bucket that cannot be rotated with usernames', async () => {
vi.mocked(fetchPRInsights).mockResolvedValue({} as never);
const checkSpy = vi.spyOn(RateLimiter.prototype, 'checkWithResult');
await GET(new Request('http://localhost/api/pr-insights?username=octocat'));
await GET(new Request('http://localhost/api/pr-insights?username=torvalds'));
expect(checkSpy).toHaveBeenNthCalledWith(1, 'pr-insights');
expect(checkSpy).toHaveBeenNthCalledWith(2, 'pr-insights');
});
it('returns error message from thrown Error', async () => {
vi.mocked(fetchPRInsights).mockRejectedValue(new Error('Service failed'));
const request = new Request('http://localhost/api/pr-insights?username=aanya');
const response = await GET(request);
expect(response.status).toBe(500);
const body = await response.json();
expect(body).toEqual({
error: 'Service failed',
});
});
it('returns fallback error message for unknown thrown values', async () => {
vi.mocked(fetchPRInsights).mockRejectedValue('unknown');
const request = new Request('http://localhost/api/pr-insights?username=aanya');
const response = await GET(request);
expect(response.status).toBe(500);
const body = await response.json();
expect(body).toEqual({
error: 'Failed to fetch PR insights',
});
});
});