summaryrefslogtreecommitdiff
path: root/source/github-helpers/index.test.ts
blob: 04f8a08c4d2e861db3ca47898093b75d5e2afd00 (plain) (blame)
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
import {test, assert} from 'vitest';

import {
	getConversationNumber,
	parseTag,
	compareNames,
	getLatestVersionTag,
	shouldFeatureRun,
} from './index.js';

test('getConversationNumber', () => {
	const pairs = new Map<string, number | undefined>([
		[
			'https://github.com',
			undefined,
		],
		[
			'https://gist.github.com/',
			undefined,
		],
		[
			'https://github.com/settings/developers',
			undefined,
		],
		[
			'https://github.com/refined-github/refined-github',
			undefined,
		],
		[
			'https://github.com/refined-github/refined-github/',
			undefined,
		],
		[
			'https://github.com/refined-github/refined-github/blame/main/package.json',
			undefined,
		],
		[
			'https://github.com/refined-github/refined-github/commit/57bf4',
			undefined,
		],
		[
			'https://github.com/refined-github/refined-github/compare/test-branch?quick_pull=0',
			undefined,
		],
		[
			'https://github.com/refined-github/refined-github/tree/main/distribution',
			undefined,
		],
		[
			'https://github.com/refined-github/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85',
			148,
		],
		[
			'https://github.com/refined-github/refined-github/pull/148/commits/00196',
			148,
		],
		[
			'https://github.com/refined-github/refined-github/pull/148/commits',
			148,
		],
		[
			'https://github.com/refined-github/refined-github/pull/148',
			148,
		],
		[
			'https://github.com/refined-github/refined-github/issues/146',
			146,
		],
		[
			'https://github.com/refined-github/refined-github/issues',
			undefined,
		],
	]);
	for (const [url, result] of pairs) {
		location.href = url;
		assert.equal(result, getConversationNumber());
	}
});

test('parseTag', () => {
	assert.deepEqual(parseTag(''), {namespace: '', version: ''});
	assert.deepEqual(parseTag('1.2.3'), {namespace: '', version: '1.2.3'});
	assert.deepEqual(parseTag('@1.2.3'), {namespace: '', version: '1.2.3'});
	assert.deepEqual(parseTag('hi@1.2.3'), {namespace: 'hi', version: '1.2.3'});
	assert.deepEqual(parseTag('hi/you@1.2.3'), {namespace: 'hi/you', version: '1.2.3'});
	assert.deepEqual(parseTag('@hi/you@1.2.3'), {namespace: '@hi/you', version: '1.2.3'});
});

test('compareNames', () => {
	assert.isTrue(compareNames('johndoe', 'John Doe'));
	assert.isTrue(compareNames('john-doe', 'John Doe'));
	assert.isTrue(compareNames('john-wdoe', 'John W. Doe'));
	assert.isTrue(compareNames('john-doe-jr', 'John Doe Jr.'));
	assert.isTrue(compareNames('nicolo', 'Nicolò'));
	assert.isFalse(compareNames('dotconnor', 'Connor Love'));
	assert.isFalse(compareNames('fregante ', 'Federico Brigante'));
});

test('getLatestVersionTag', () => {
	assert.equal(getLatestVersionTag([
		'0.0.0',
		'v1.1',
		'r2.0',
		'3.0',
	]), '3.0', 'Tags should be sorted by version');

	assert.equal(getLatestVersionTag([
		'v2.1-0',
		'v2.0',
		'r1.5.5',
		'r1.0',
		'v1.0-1',
	]), 'v2.0', 'Prereleases should be ignored');

	assert.equal(getLatestVersionTag([
		'lol v0.0.0',
		'2.0',
		'2020-10-10',
		'v1.0-1',
	]), 'lol v0.0.0', 'Non-version tags should short-circuit the sorting and return the first tag');
});

test('shouldFeatureRun', () => {
	const yes = (): boolean => true;
	const no = (): boolean => false;
	const yesYes = [yes, yes];
	const yesNo = [yes, no];
	const noNo = [no, no];

	assert.isTrue(shouldFeatureRun({}), 'A lack of conditions should mean "run everywhere"');

	assert.isFalse(shouldFeatureRun({
		asLongAs: yesNo,
	}), 'Every `asLongAs` should be true to run');

	assert.isFalse(shouldFeatureRun({
		asLongAs: yesNo,
		include: [yes],
	}), 'Every `asLongAs` should be true to run, regardless of `include`');

	assert.isFalse(shouldFeatureRun({
		include: noNo,
	}), 'At least one `include` should be true to run');

	assert.isTrue(shouldFeatureRun({
		include: yesNo,
	}), 'If one `include` is true, then it should run');

	assert.isFalse(shouldFeatureRun({
		exclude: yesNo,
	}), 'If any `exclude` is true, then it should not run');

	assert.isFalse(shouldFeatureRun({
		include: [yes],
		exclude: yesNo,
	}), 'If any `exclude` is true, then it should not run, regardless of `include`');

	assert.isFalse(shouldFeatureRun({
		asLongAs: [yes],
		exclude: yesNo,
	}), 'If any `exclude` is true, then it should not run, regardless of `asLongAs`');

	assert.isFalse(shouldFeatureRun({
		asLongAs: [yes],
		include: yesYes,
		exclude: yesNo,
	}), 'If any `exclude` is true, then it should not run, regardless of `asLongAs` and `include`');
});