forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
477 lines (425 loc) · 13.6 KB
/
index.spec.ts
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import * as httpMock from '../../../test/http-mock';
import { EXTERNAL_HOST_ERROR } from '../../constants/error-messages';
import { logger } from '../../logger';
import type { HostRule } from '../../types';
import * as memCache from '../cache/memory';
import * as hostRules from '../host-rules';
import {
getMergeConfidenceLevel,
initConfig,
initMergeConfidence,
isActiveConfidenceLevel,
parseSupportedDatasourceString,
resetConfig,
satisfiesConfidenceLevel,
} from '.';
describe('util/merge-confidence/index', () => {
const apiBaseUrl = 'https://www.baseurl.com/';
const defaultApiBaseUrl = 'https://developer.mend.io/';
describe('isActiveConfidenceLevel()', () => {
it('returns false if null', () => {
expect(isActiveConfidenceLevel(null as never)).toBeFalse();
});
it('returns false if low', () => {
expect(isActiveConfidenceLevel('low')).toBeFalse();
});
it('returns false if nonsense', () => {
expect(isActiveConfidenceLevel('nonsense')).toBeFalse();
});
it('returns true if valid value (high)', () => {
expect(isActiveConfidenceLevel('high')).toBeTrue();
});
});
describe('satisfiesConfidenceLevel()', () => {
it('returns false if less', () => {
expect(satisfiesConfidenceLevel('low', 'high')).toBeFalse();
});
it('returns true if equal', () => {
expect(satisfiesConfidenceLevel('high', 'high')).toBeTrue();
});
it('returns true if more', () => {
expect(satisfiesConfidenceLevel('very high', 'high')).toBeTrue();
});
});
describe('API calling functions', () => {
const hostRule: HostRule = {
hostType: 'merge-confidence',
token: 'some-token',
};
beforeEach(() => {
process.env.RENOVATE_X_MERGE_CONFIDENCE_API_BASE_URL = apiBaseUrl;
hostRules.add(hostRule);
initConfig();
memCache.reset();
});
afterEach(() => {
hostRules.clear();
resetConfig();
});
describe('getMergeConfidenceLevel()', () => {
it('returns neutral if undefined updateType', async () => {
expect(
await getMergeConfidenceLevel(
'npm',
'renovate',
'25.0.0',
'25.0.0',
undefined as never,
),
).toBe('neutral');
});
it('returns neutral if irrelevant updateType', async () => {
expect(
await getMergeConfidenceLevel(
'npm',
'renovate',
'24.1.0',
'25.0.0',
'bump',
),
).toBe('neutral');
});
it('returns high if pinning', async () => {
expect(
await getMergeConfidenceLevel(
'npm',
'renovate',
'25.0.1',
'25.0.1',
'pin',
),
).toBe('high');
});
it('returns undefined if no token', async () => {
resetConfig();
hostRules.clear();
expect(
await getMergeConfidenceLevel(
'npm',
'renovate',
'24.2.0',
'25.0.0',
'major',
),
).toBeUndefined();
});
it('returns undefined if datasource is unsupported', async () => {
expect(
await getMergeConfidenceLevel(
'not-npm',
'renovate',
'24.2.0',
'25.0.0',
'major',
),
).toBeUndefined();
});
it('returns valid confidence level', async () => {
const datasource = 'npm';
const depName = 'renovate';
const currentVersion = '24.3.0';
const newVersion = '25.0.0';
httpMock
.scope(apiBaseUrl)
.get(
`/api/mc/json/${datasource}/${depName}/${currentVersion}/${newVersion}`,
)
.reply(200, { confidence: 'high' });
expect(
await getMergeConfidenceLevel(
datasource,
depName,
currentVersion,
newVersion,
'major',
),
).toBe('high');
});
it('escapes a package name containing a forward slash', async () => {
const datasource = 'npm';
const packageName = '@jest/global';
const escapedPackageName = '@jest%2fglobal';
const currentVersion = '24.3.0';
const newVersion = '25.0.0';
httpMock
.scope(apiBaseUrl)
.get(
`/api/mc/json/${datasource}/${escapedPackageName}/${currentVersion}/${newVersion}`,
)
.reply(200, { confidence: 'high' });
expect(
await getMergeConfidenceLevel(
datasource,
packageName,
currentVersion,
newVersion,
'major',
),
).toBe('high');
});
it('returns neutral on invalid merge confidence response from api', async () => {
const datasource = 'npm';
const depName = 'renovate';
const currentVersion = '25.0.0';
const newVersion = '25.1.0';
httpMock
.scope(apiBaseUrl)
.get(
`/api/mc/json/${datasource}/${depName}/${currentVersion}/${newVersion}`,
)
.reply(200, { invalid: 'invalid' });
expect(
await getMergeConfidenceLevel(
datasource,
depName,
currentVersion,
newVersion,
'minor',
),
).toBe('neutral');
});
it('returns neutral on non 403/5xx error from API', async () => {
const datasource = 'npm';
const depName = 'renovate';
const currentVersion = '25.0.0';
const newVersion = '25.4.0';
httpMock
.scope(apiBaseUrl)
.get(
`/api/mc/json/${datasource}/${depName}/${currentVersion}/${newVersion}`,
)
.reply(400);
expect(
await getMergeConfidenceLevel(
datasource,
depName,
currentVersion,
newVersion,
'minor',
),
).toBe('neutral');
expect(logger.warn).toHaveBeenCalledWith(
expect.anything(),
'error fetching merge confidence data',
);
});
it('throws on 403-Forbidden response from API', async () => {
const datasource = 'npm';
const packageName = 'renovate';
const currentVersion = '25.0.0';
const newVersion = '25.4.0';
httpMock
.scope(apiBaseUrl)
.get(
`/api/mc/json/${datasource}/${packageName}/${currentVersion}/${newVersion}`,
)
.reply(403);
await expect(
getMergeConfidenceLevel(
datasource,
packageName,
currentVersion,
newVersion,
'minor',
),
).rejects.toThrow(EXTERNAL_HOST_ERROR);
expect(logger.error).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API token rejected - aborting run',
);
});
it('throws on server error responses', async () => {
const datasource = 'npm';
const packageName = 'renovate';
const currentVersion = '25.0.0';
const newVersion = '25.4.0';
httpMock
.scope(apiBaseUrl)
.get(
`/api/mc/json/${datasource}/${packageName}/${currentVersion}/${newVersion}`,
)
.reply(503);
await expect(
getMergeConfidenceLevel(
datasource,
packageName,
currentVersion,
newVersion,
'minor',
),
).rejects.toThrow(EXTERNAL_HOST_ERROR);
expect(logger.error).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API failure: 5xx - aborting run',
);
});
it('returns high if pinning digest', async () => {
expect(
await getMergeConfidenceLevel(
'npm',
'renovate',
'25.0.1',
'25.0.1',
'pinDigest',
),
).toBe('high');
});
});
describe('initMergeConfidence()', () => {
beforeEach(() => {
resetConfig();
});
it('using default base url if none is set', async () => {
delete process.env.RENOVATE_X_MERGE_CONFIDENCE_API_BASE_URL;
httpMock
.scope(defaultApiBaseUrl)
.get(`/api/mc/availability`)
.reply(200);
await expect(initMergeConfidence()).toResolve();
expect(logger.trace).toHaveBeenCalledWith(
'using default merge confidence API base URL',
);
expect(logger.debug).toHaveBeenCalledWith(
{
supportedDatasources: [
'go',
'maven',
'npm',
'nuget',
'packagist',
'pypi',
'rubygems',
],
},
'merge confidence API - successfully authenticated',
);
});
it('warns and then resolves if base url is invalid', async () => {
process.env.RENOVATE_X_MERGE_CONFIDENCE_API_BASE_URL =
'invalid-url.com';
httpMock
.scope(defaultApiBaseUrl)
.get(`/api/mc/availability`)
.reply(200);
await expect(initMergeConfidence()).toResolve();
expect(logger.warn).toHaveBeenCalledWith(
expect.anything(),
'invalid merge confidence API base URL found in environment variables - using default value instead',
);
expect(logger.debug).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API - successfully authenticated',
);
});
it('uses a custom base url containing path', async () => {
const renovateApi = 'https://domain.com/proxy/renovate-api';
process.env.RENOVATE_X_MERGE_CONFIDENCE_API_BASE_URL = renovateApi;
httpMock.scope(renovateApi).get(`/api/mc/availability`).reply(200);
await expect(initMergeConfidence()).toResolve();
expect(logger.trace).toHaveBeenCalledWith(
expect.anything(),
'using merge confidence API base found in environment variables',
);
expect(logger.debug).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API - successfully authenticated',
);
});
it('resolves if no token', async () => {
hostRules.clear();
await expect(initMergeConfidence()).toResolve();
expect(logger.trace).toHaveBeenCalledWith(
'merge confidence API usage is disabled',
);
});
it('resolves when token is valid', async () => {
httpMock.scope(apiBaseUrl).get(`/api/mc/availability`).reply(200);
await expect(initMergeConfidence()).toResolve();
expect(logger.debug).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API - successfully authenticated',
);
});
it('throws on 403-Forbidden from mc API', async () => {
httpMock.scope(apiBaseUrl).get(`/api/mc/availability`).reply(403);
await expect(initMergeConfidence()).rejects.toThrow(
EXTERNAL_HOST_ERROR,
);
expect(logger.error).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API token rejected - aborting run',
);
});
it('throws on 5xx host errors from mc API', async () => {
httpMock.scope(apiBaseUrl).get(`/api/mc/availability`).reply(503);
await expect(initMergeConfidence()).rejects.toThrow(
EXTERNAL_HOST_ERROR,
);
expect(logger.error).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API failure: 5xx - aborting run',
);
});
it('throws on ECONNRESET', async () => {
httpMock
.scope(apiBaseUrl)
.get(`/api/mc/availability`)
.replyWithError({ code: 'ECONNRESET' });
await expect(initMergeConfidence()).rejects.toThrow(
EXTERNAL_HOST_ERROR,
);
expect(logger.error).toHaveBeenCalledWith(
expect.anything(),
'merge confidence API request failed - aborting run',
);
});
describe('parseSupportedDatasourceList()', () => {
type ParseSupportedDatasourceTestCase = {
name: string;
datasourceListString: string | undefined;
expected: string[] | undefined;
};
afterEach(() => {
delete process.env.RENOVATE_X_MERGE_CONFIDENCE_SUPPORTED_DATASOURCES;
});
it.each([
{
name: 'it should do nothing when the input is undefined',
datasourceListString: undefined,
expected: undefined,
},
{
name: 'it should successfully parse the given datasource list',
datasourceListString: `["go","npm"]`,
expected: ['go', 'npm'],
},
{
name: 'it should gracefully handle invalid json',
datasourceListString: `{`,
expected: undefined,
},
{
name: 'it should discard non-array JSON input',
datasourceListString: `{}`,
expected: undefined,
},
{
name: 'it should discard non-string array JSON input',
datasourceListString: `[1,2]`,
expected: undefined,
},
])(
`$name`,
({
datasourceListString,
expected,
}: ParseSupportedDatasourceTestCase) => {
process.env.RENOVATE_X_MERGE_CONFIDENCE_SUPPORTED_DATASOURCES =
datasourceListString;
expect(parseSupportedDatasourceString()).toStrictEqual(expected);
},
);
});
});
});
});