-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathindex.test.ts
More file actions
332 lines (291 loc) · 10.2 KB
/
index.test.ts
File metadata and controls
332 lines (291 loc) · 10.2 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
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
/// <reference lib="dom" />
import assert from 'node:assert/strict';
import {test, expect} from 'vitest';
import stripIndent from 'strip-indent';
import {getAllUrls, getTests} from './collector.js';
import * as pageDetect from './index.js';
(globalThis as any).document = {title: '', readyState: 'loading'};
(globalThis as any).location = new URL('https://github.com/');
(globalThis as any).requestAnimationFrame = (callback: FrameRequestCallback) => setTimeout(() => {
callback(Date.now());
}, 0) as unknown as number;
(globalThis as any).cancelAnimationFrame = (id: number) => {
clearTimeout(id);
};
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 \`addTests()\``,
);
});
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 \`addTests()\` 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 \`addTests()\` 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);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection')).toMatchInlineSnapshot(`
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/')).toMatchInlineSnapshot(`
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json')).toMatchInlineSnapshot(`
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "blame/master/package.json",
"pathParts": [
"blame",
"master",
"package.json",
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4')).toMatchInlineSnapshot(`
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "commit/57bf4",
"pathParts": [
"commit",
"57bf4",
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0')).toMatchInlineSnapshot(`
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "compare/test-branch",
"pathParts": [
"compare",
"test-branch",
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution')).toMatchInlineSnapshot(`
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/')).toMatchInlineSnapshot(`
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`);
}
});
test('parseRepoExplorerTitle', () => {
const parse = pageDetect.utils.parseRepoExplorerTitle;
assert.deepEqual(
parse('/eslint/js/tree/2.x', 'eslint/js at 2.x'),
{
nameWithOwner: 'eslint/js',
branch: '2.x',
filePath: '',
},
);
assert.deepEqual(
parse('/eslint/js/tree/2.x', 'js/ at 2.x · eslint/js'),
{
nameWithOwner: 'eslint/js',
branch: '2.x',
filePath: '',
},
);
assert.deepEqual(
parse('/eslint/js/tree/2.x/tools', 'js/tools at 2.x · eslint/js'),
{
nameWithOwner: 'eslint/js',
branch: '2.x',
filePath: 'tools',
},
);
assert.deepEqual(
parse('/eslint/js/tree/2.x/docs/ast', 'js/docs/ast at 2.x · eslint/js'),
{
nameWithOwner: 'eslint/js',
branch: '2.x',
filePath: 'docs/ast',
},
);
assert.deepEqual(
parse('https://github.com/eslint/js', 'only /tree/ URLs are supported'),
undefined,
);
assert.deepEqual(
parse('https://github.com/eslint/js/issues', 'irrelephant'),
undefined,
);
});
test('waitFor - immediately true', async () => {
const detection = () => true;
const result = await pageDetect.utils.waitFor(detection);
assert.equal(result, true);
});
test('waitFor - becomes true', async () => {
let callCount = 0;
const detection = () => {
callCount++;
return callCount >= 3;
};
const result = await pageDetect.utils.waitFor(detection);
assert.equal(result, true);
assert.ok(callCount >= 3);
});
test('waitFor - false when document complete', async () => {
// Save original state
const originalReadyState = Object.getOwnPropertyDescriptor(document, 'readyState');
// Mock document.readyState to be 'complete'
Object.defineProperty(document, 'readyState', {
writable: true,
configurable: true,
value: 'complete',
});
const detection = () => false;
const result = await pageDetect.utils.waitFor(detection);
assert.equal(result, false);
// Restore original state
if (originalReadyState) {
Object.defineProperty(document, 'readyState', originalReadyState);
} else {
// If readyState wasn't a property before, delete it
delete (document as any).readyState;
}
});