forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoctokit.provider.spec.ts
More file actions
69 lines (59 loc) · 2.22 KB
/
Copy pathoctokit.provider.spec.ts
File metadata and controls
69 lines (59 loc) · 2.22 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
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AppConfig } from '../config/configuration';
import {
GITHUB_MAX_RETRIES,
createGithubOctokit,
makeLimitHandler,
} from './octokit.provider';
function mockConfigService(apiToken: string): ConfigService<AppConfig, true> {
return {
get: () => ({ apiToken }),
} as unknown as ConfigService<AppConfig, true>;
}
describe('makeLimitHandler', () => {
let warnSpy: jest.SpyInstance;
beforeEach(() => {
warnSpy = jest.spyOn(Logger.prototype, 'warn').mockImplementation();
});
afterEach(() => {
warnSpy.mockRestore();
});
const options = {
method: 'GET',
url: 'https://api.github.com/repos/a/b/issues',
};
it.each(['primary', 'secondary'] as const)(
'retries while under the ceiling for a %s rate limit',
(kind) => {
const handler = makeLimitHandler(kind);
const result = handler(5, options, {}, 0);
expect(result).toBe(true);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining(kind));
},
);
it('stops retrying once GITHUB_MAX_RETRIES is reached, so a persistent outage cannot retry forever', () => {
const handler = makeLimitHandler('primary');
const result = handler(5, options, {}, GITHUB_MAX_RETRIES);
expect(result).toBe(false);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('giving up'));
});
it('logs the method/url/retryAfter so the interruption is traceable', () => {
const handler = makeLimitHandler('secondary');
handler(12, options, {}, 1);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('GET https://api.github.com/repos/a/b/issues'),
);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('12s'));
});
});
describe('createGithubOctokit', () => {
it('constructs an authenticated Octokit instance when a token is configured', () => {
const octokit = createGithubOctokit(mockConfigService('test-token'));
expect(octokit).toBeDefined();
expect(typeof octokit.request).toBe('function');
});
it('constructs an unauthenticated Octokit instance when no token is configured', () => {
expect(() => createGithubOctokit(mockConfigService(''))).not.toThrow();
});
});