-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.test.ts
199 lines (168 loc) · 7.25 KB
/
index.test.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
/// <reference lib="dom" />
import assert from 'node:assert/strict';
import {test} from 'vitest';
import stripIndent from 'strip-indent';
import {getAllUrls, getTests} from './collector.js';
import * as pageDetect from './index.js';
(globalThis as any).document = {title: ''};
(globalThis as any).location = new URL('https://github.com/');
const allUrls = getAllUrls();
for (const [detectName, detect] of Object.entries(pageDetect)) {
if (typeof detect !== 'function') {
continue;
}
const validURLs = getTests(detectName);
if (validURLs[0] === 'combinedTestOnly' || String(detect).startsWith('() =>')) {
continue;
}
test(detectName + ' has tests', () => {
assert.ok(
Array.isArray(validURLs),
`The function \`${detectName}\` doesn’t have any tests. Set them via \`collect.set()\``,
);
});
if (!Array.isArray(validURLs)) {
continue;
}
for (const url of validURLs) {
test(`${detectName} ${url.replace('https://github.com', '')}`, () => {
assert.ok(
detect(new URL(url)),
stripIndent(`
Is this URL \`${detectName}\`?
${url.replace('https://github.com', '')}
• Yes? The \`${detectName}\` detection is wrong and should be fixed.
• No? Remove it from its \`collect.set()\` array.
`),
);
});
}
// Skip negatives for this one, it's too long
if (detectName === 'isRepo') {
continue;
}
for (const url of allUrls) {
if (!validURLs.includes(url)) {
test(`${detectName} NO ${url}`, () => {
assert.equal(
detect(new URL(url)),
false,
stripIndent(`
Is this URL \`${detectName}\`?
${url.replace('https://github.com', '')}
• Yes? Add it to the \`collect.set()\` array.
• No? The \`${detectName}\` detection is wrong and should be fixed.
`),
);
});
}
}
}
test('is404', () => {
document.title = 'Page not found · GitHub';
assert.ok(pageDetect.is404());
document.title = 'File not found · GitHub';
assert.ok(pageDetect.is404());
document.title = 'examples/404: Page not found examples';
assert.equal(pageDetect.is404(), false);
document.title = 'Dashboard';
assert.equal(pageDetect.is404(), false);
document.title = 'Page not found · Issue #266 · sintaxi/surge · GitHub';
assert.equal(pageDetect.is404(), false);
});
test('is500', () => {
document.title = 'Server Error · GitHub';
assert.ok(pageDetect.is500());
document.title = 'Unicorn! · GitHub';
assert.ok(pageDetect.is500());
document.title = 'examples/500: Server Error examples';
assert.equal(pageDetect.is500(), false);
document.title = 'sindresorhus/unicorn: You can’t tell what doesn’t exist';
assert.equal(pageDetect.is500(), false);
document.title = 'Dashboard';
assert.equal(pageDetect.is500(), false);
document.title = 'Server Error · Issue #266 · sintaxi/surge · GitHub';
assert.equal(pageDetect.is500(), false);
});
test('isPRCommit404', () => {
document.title = 'Commit range not found · Pull Request #3227 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/3227/commits/32c8a88360a85739f151566eae0225d530ce6a15';
assert.ok(pageDetect.isPRCommit404());
document.title = 'Experiment with `@primer/octicons-react` icons by FloEdelmann · Pull Request #3227 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/3227/commits/edbdcdd5559a2a8da78abdc7cb0814155713974c';
assert.equal(pageDetect.isPRCommit404(), false);
document.title = 'Commit range not found by SomeContributor · Pull Request #999999 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/999999/commits/32c8a88360a85739f151566eae0225d530ce6a15';
assert.equal(pageDetect.isPRCommit404(), false);
});
test('isPRFile404', () => {
document.title = 'Commit range not found · Pull Request #789 · sindresorhus/eslint-plugin-unicorn';
location.href = 'https://github.com/sindresorhus/eslint-plugin-unicorn/pull/789/files/a58b37845f1b2660221de019e4ae6c736feedc26..eed168224d7994652b1d1ff69a5c8cebee223faf';
assert.ok(pageDetect.isPRFile404());
document.title = 'Add `align-repository-header` feature by fregante · Pull Request #3313 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/3313/files/a14fb2c94eae3ca83a3a97688a171fcc3405524f..fbeeba9825f12b5ded9cd4bb04d5df4b0cf2f2a8';
assert.equal(pageDetect.isPRFile404(), false);
});
test('isRepoFile404', () => {
document.title = 'File not found';
location.href = 'https://github.com/fregante/GhostText/tree/3cacd7df71b097dc525d99c7aa2f54d31b02fcc8/chrome/scripts/InputArea';
assert.ok(pageDetect.isRepoFile404());
document.title = 'File not found';
location.href = 'https://github.com/refined-github/refined-github/blob/some-non-existent-ref/source/features/bugs-tab.tsx';
assert.ok(pageDetect.isRepoFile404());
});
const {getRepositoryInfo} = pageDetect.utils;
test('getRepositoryInfo', () => {
const inputTypes = [
getRepositoryInfo, // Full URL
(url: string) => getRepositoryInfo(new URL(url).pathname), // Pathname only
(url: string) => getRepositoryInfo(new URL(url)), // URL object
];
for (const getRepositoryInfoAdapter of inputTypes) {
assert.equal(getRepositoryInfoAdapter('https://github.com'), undefined);
assert.equal(getRepositoryInfoAdapter('https://gist.github.com/'), undefined);
assert.equal(getRepositoryInfoAdapter('https://github.com/settings/developers'), undefined);
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: '',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: '',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'blame/master/package.json',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'commit/57bf4',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'compare/test-branch',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'tree/master/distribution',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'tree/master/distribution',
});
}
});