Skip to content

Commit 399d8e0

Browse files
authored
Meta: Tests all URLs in page-detect against each other (#2512)
1 parent ba795a5 commit 399d8e0

File tree

2 files changed

+293
-387
lines changed

2 files changed

+293
-387
lines changed

source/index.ts

Lines changed: 248 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,117 +5,357 @@ import select from 'select-dom';
55
import reservedNames from 'github-reserved-names/reserved-names.json';
66
import {getUsername, getCleanPathname, getRepoPath, getOwnerAndRepo} from './utils';
77

8+
const skip = 'skip'; // To be used only to skip tests of combined functions, i.e. isPageA() || isPageB()
9+
const domBased = 'skip'; // To be used only to skip tests that are DOM-based rather than URL-based
10+
811
export const is404 = (): boolean => document.title === 'Page not found · GitHub';
12+
export const is404Test = domBased; // They're specified in page-detect.ts
913

1014
export const is500 = (): boolean => document.title === 'Server Error · GitHub' || document.title === 'Unicorn! · GitHub';
15+
export const is500Test = domBased; // They're specified in page-detect.ts
1116

1217
export const isBlame = (): boolean => /^blame\//.test(getRepoPath()!);
18+
export const isBlameTest = [
19+
'https://github.com/sindresorhus/refined-github/blame/master/package.json'
20+
];
1321

1422
export const isCommit = (): boolean => isSingleCommit() || isPRCommit();
23+
export const isCommitTest = [
24+
'https://github.com/sindresorhus/refined-github/commit/5b614b9035f2035b839f48b4db7bd5c3298d526f',
25+
'https://github.com/sindresorhus/refined-github/commit/5b614',
26+
'https://github.com/sindresorhus/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85',
27+
'https://github.com/sindresorhus/refined-github/pull/148/commits/00196'
28+
];
1529

1630
export const isCommitList = (): boolean => /^commits\//.test(getRepoPath()!);
31+
export const isCommitListTest = [
32+
'https://github.com/sindresorhus/refined-github/commits/master?page=2',
33+
'https://github.com/sindresorhus/refined-github/commits/test-branch',
34+
'https://github.com/sindresorhus/refined-github/commits/0.13.0',
35+
'https://github.com/sindresorhus/refined-github/commits/230c2',
36+
'https://github.com/sindresorhus/refined-github/commits/230c2935fc5aea9a681174ddbeba6255ca040d63'
37+
];
1738

1839
export const isCompare = (): boolean => /^compare/.test(getRepoPath()!);
40+
export const isCompareTest = [
41+
'https://github.com/sindresorhus/refined-github/compare',
42+
'https://github.com/sindresorhus/refined-github/compare/',
43+
'https://github.com/sindresorhus/refined-github/compare/master...branch-name',
44+
'https://github.com/sindresorhus/refined-github/compare/master...branch-name?quick_pull=1',
45+
'https://github.com/sindresorhus/refined-github/compare/branch-1...branch-2?quick_pull=1',
46+
'https://github.com/sindresorhus/refined-github/compare/test-branch?quick_pull=1'
47+
];
1948

2049
export const isDashboard = (): boolean => !isGist() && /^$|^(orgs[/][^/]+[/])?dashboard([/]|$)/.test(getCleanPathname());
50+
export const isDashboardTest = [
51+
'https://github.com/',
52+
'https://github.com',
53+
'https://github.com/orgs/test/dashboard',
54+
'https://github.com/dashboard/index/2',
55+
'https://github.com/dashboard',
56+
'https://github.com/orgs/edit/dashboard',
57+
'https://github.big-corp.com/',
58+
'https://not-github.com/',
59+
'https://my-little-hub.com/'
60+
];
2161

2262
export const isEnterprise = (): boolean => location.hostname !== 'github.com' && location.hostname !== 'gist.github.com';
23-
24-
export const isGist = (): boolean => location.hostname.startsWith('gist.') || location.pathname.startsWith('gist/');
63+
export const isEnterpriseTest = [
64+
'https://github.big-corp.com/',
65+
'https://not-github.com/',
66+
'https://my-little-hub.com/',
67+
'https://my-little-hub.com/gist'
68+
];
69+
70+
export const isGist = (): boolean => location.hostname.startsWith('gist.') || location.pathname.split('/', 2)[1] === 'gist';
71+
export const isGistTest = [
72+
'https://gist.github.com',
73+
'http://gist.github.com',
74+
'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064',
75+
'https://my-little-hub.com/gist'
76+
];
2577

2678
export const isGlobalDiscussionList = (): boolean => ['issues', 'pulls'].includes(location.pathname.split('/', 2)[1]);
79+
export const isGlobalDiscussionListTest = [
80+
'https://github.com/issues',
81+
'https://github.com/issues?q=is%3Apr+is%3Aopen',
82+
'https://github.com/issues/assigned',
83+
'https://github.com/issues/mentioned',
84+
'https://github.com/pulls',
85+
'https://github.com/pulls?q=issues',
86+
'https://github.com/pulls/assigned',
87+
'https://github.com/pulls/mentioned',
88+
'https://github.com/pulls/review-requested'
89+
];
2790

2891
export const isGlobalSearchResults = (): boolean => location.pathname === '/search' && new URLSearchParams(location.search).get('q') !== null;
92+
export const isGlobalSearchResultsTest = [
93+
'https://github.com/search?q=refined-github&ref=opensearch'
94+
];
2995

3096
export const isIssue = (): boolean => /^issues\/\d+/.test(getRepoPath()!);
97+
export const isIssueTest = [
98+
'https://github.com/sindresorhus/refined-github/issues/146'
99+
];
31100

32101
export const isDiscussionList = (): boolean => isGlobalDiscussionList() || isRepoDiscussionList();
102+
export const isDiscussionListTest = skip;
33103

34-
export const isLabel = (): boolean => /^labels\/\w+/.test(getRepoPath()!);
35-
36-
export const isLabelList = (): boolean => /^labels\/?(((?=\?).*)|$)/.test(getRepoPath()!);
104+
export const isLabelList = (): boolean => getRepoPath() === 'labels';
105+
export const isLabelListTest = [
106+
'https://github.com/sindresorhus/refined-github/labels'
107+
];
37108

38109
export const isMilestone = (): boolean => /^milestone\/\d+/.test(getRepoPath()!);
110+
export const isMilestoneTest = [
111+
'https://github.com/sindresorhus/refined-github/milestone/12'
112+
];
39113

40114
export const isMilestoneList = (): boolean => getRepoPath() === 'milestones';
115+
export const isMilestoneListTest = [
116+
'https://github.com/sindresorhus/refined-github/milestones'
117+
];
41118

42119
export const isNewIssue = (): boolean => /^issues\/new/.test(getRepoPath()!);
120+
export const isNewIssueTest = [
121+
'https://github.com/sindresorhus/refined-github/issues/new'
122+
];
43123

44124
export const isNewRelease = (): boolean => /^releases\/new/.test(getRepoPath()!);
125+
export const isNewReleaseTest = [
126+
'https://github.com/sindresorhus/refined-github/releases/new'
127+
];
45128

46129
export const isNotifications = (): boolean => /^([^/]+[/][^/]+\/)?notifications/.test(getCleanPathname());
130+
export const isNotificationsTest = [
131+
'https://github.com/notifications',
132+
'https://github.com/notifications/participating',
133+
'https://github.com/sindresorhus/notifications/notifications',
134+
'https://github.com/notifications?all=1'
135+
];
47136

48137
export const isOrganizationProfile = (): boolean => select.exists('.orghead');
138+
export const isOrganizationProfileTest = domBased;
49139

50140
export const isOrganizationDiscussion = (): boolean => /^orgs\/[^/]+\/teams\/[^/]+($|\/discussions)/.test(getCleanPathname());
141+
export const isOrganizationDiscussionTest = [
142+
'https://github.com/orgs/refined-github/teams/core-team/discussions?pinned=1',
143+
'https://github.com/orgs/refined-github/teams/core-team/discussions/1',
144+
'https://github.com/orgs/refined-github/teams/core-team'
145+
];
51146

52147
export const isOwnUserProfile = (): boolean => getCleanPathname() === getUsername();
148+
export const isOwnUserProfileTest = domBased;
53149

54150
// If there's a Report Abuse link, we're not part of the org
55151
export const isOwnOrganizationProfile = (): boolean => isOrganizationProfile() && !select.exists('[href*="contact/report-abuse?report="]');
152+
export const isOwnOrganizationProfileTest = domBased;
56153

57154
export const isProject = (): boolean => /^projects\/\d+/.test(getRepoPath()!);
155+
export const isProjectTest = [
156+
'https://github.com/sindresorhus/refined-github/projects/3'
157+
];
58158

59159
export const isPR = (): boolean => /^pull\/\d+/.test(getRepoPath()!);
160+
export const isPRTest = [
161+
'https://github.com/sindresorhus/refined-github/pull/148',
162+
'https://github.com/sindresorhus/refined-github/pull/148/files',
163+
'https://github.com/sindresorhus/refined-github/pull/148/conflicts',
164+
'https://github.com/sindresorhus/refined-github/pull/148/commits/00196',
165+
'https://github.com/sindresorhus/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85'
166+
];
60167

61168
export const isConflict = (): boolean => /^pull\/\d+\/conflicts/.test(getRepoPath()!);
169+
export const isConflictTest = [
170+
'https://github.com/sindresorhus/refined-github/pull/148/conflicts'
171+
];
62172

63173
export const isPRList = (): boolean => location.pathname === '/pulls' || getRepoPath() === 'pulls';
174+
export const isPRListTest = [
175+
'https://github.com/pulls',
176+
'https://github.com/pulls?q=issues',
177+
'https://github.com/sindresorhus/refined-github/pulls',
178+
'https://github.com/sindresorhus/refined-github/pulls/',
179+
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
180+
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed'
181+
];
64182

65183
export const isPRCommit = (): boolean => /^pull\/\d+\/commits\/[0-9a-f]{5,40}/.test(getRepoPath()!);
184+
export const isPRCommitTest = [
185+
'https://github.com/sindresorhus/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85',
186+
'https://github.com/sindresorhus/refined-github/pull/148/commits/00196'
187+
];
66188

67189
export const isPRConversation = (): boolean => /^pull\/\d+$/.test(getRepoPath()!);
190+
export const isPRConversationTest = [
191+
'https://github.com/sindresorhus/refined-github/pull/148'
192+
];
68193

69194
export const isPRFiles = (): boolean => /^pull\/\d+\/files/.test(getRepoPath()!);
195+
export const isPRFilesTest = [
196+
'https://github.com/sindresorhus/refined-github/pull/148/files'
197+
];
70198

71199
export const isQuickPR = (): boolean => isCompare() && /[?&]quick_pull=1(&|$)/.test(location.search);
72-
73-
export const isReleasesOrTags = (): boolean => /^(releases|tags)/.test(getRepoPath()!);
200+
export const isQuickPRTest = [
201+
'https://github.com/sindresorhus/refined-github/compare/master...branch-name?quick_pull=1',
202+
'https://github.com/sindresorhus/refined-github/compare/branch-1...branch-2?quick_pull=1',
203+
'https://github.com/sindresorhus/refined-github/compare/test-branch?quick_pull=1'
204+
];
205+
206+
export const isReleasesOrTags = (): boolean => {
207+
const parts = (getRepoPath() || '').split('/');
208+
return /^(releases|tags)$/.test(parts[0]) && parts[1] !== 'new';
209+
};
210+
211+
export const isReleasesOrTagsTest = [
212+
'https://github.com/sindresorhus/refined-github/releases',
213+
'https://github.com/sindresorhus/refined-github/tags',
214+
'https://github.com/sindresorhus/refined-github/releases/tag/v1.0.0-beta.4',
215+
'https://github.com/sindresorhus/refined-github/releases/tag/0.2.1'
216+
];
74217

75218
export const isEditingFile = (): boolean => /^edit/.test(getRepoPath()!);
219+
export const isEditingFileTest = [
220+
'https://github.com/sindresorhus/refined-github/edit/master/readme.md',
221+
'https://github.com/sindresorhus/refined-github/edit/ghe-injection/source/background.ts'
222+
];
76223

77224
export const isRepo = (): boolean => /^[^/]+\/[^/]+/.test(getCleanPathname()) &&
78225
!reservedNames.includes(getOwnerAndRepo().ownerName) &&
79226
!isNotifications() &&
80227
!isDashboard() &&
81228
!isGist() &&
82229
!isRepoSearch();
230+
export const isRepoTest = [
231+
'https://github.com/sindresorhus/refined-github/blame/master/package.json',
232+
'https://github.com/sindresorhus/refined-github/issues/146',
233+
'https://github.com/sindresorhus/notifications/',
234+
'https://github.com/sindresorhus/refined-github/pull/148'
235+
];
236+
export const isRepoTestSkipNegatives = true;
83237

84238
export const isRepoDiscussionList = (): boolean =>
85-
/^(issues|pulls)($|\/$|\/(?!\d+|new))/.test(getRepoPath()!) || // `issues/fregante` is a list but `issues/1` isn't
239+
isRepoPRList() ||
240+
isRepoIssueList() ||
86241
/^labels\/.+/.test(getRepoPath()!);
242+
export const isRepoDiscussionListTest = [
243+
'http://github.com/sindresorhus/ava/issues',
244+
'https://github.com/sindresorhus/refined-github/pulls',
245+
'https://github.com/sindresorhus/refined-github/pulls/',
246+
'https://github.com/sindresorhus/refined-github/pulls/fregante',
247+
'https://github.com/sindresorhus/refined-github/issues/fregante',
248+
'https://github.com/sindresorhus/refined-github/labels/Priority%3A%20critical',
249+
'https://github.com/sindresorhus/refined-github/issues?q=is%3Aclosed+sort%3Aupdated-desc',
250+
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
251+
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed',
252+
'https://github.com/sindresorhus/refined-github/issues'
253+
];
254+
255+
export const isRepoPRList = (): boolean => (getRepoPath() || '').startsWith('pulls');
256+
export const isRepoPRListTest = [
257+
'https://github.com/sindresorhus/refined-github/pulls',
258+
'https://github.com/sindresorhus/refined-github/pulls/',
259+
'https://github.com/sindresorhus/refined-github/pulls/fregante',
260+
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
261+
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed'
262+
];
263+
264+
export const isRepoIssueList = (): boolean => {
265+
const parts = (getRepoPath() || '').split('/');
266+
return parts[0] === 'issues' && parts[1] !== 'new' && !/\d/.test(parts[1]); // `issues/fregante` is a list but `issues/1` isn't
267+
};
268+
269+
export const isRepoIssueListTest = [
270+
'http://github.com/sindresorhus/ava/issues',
271+
'https://github.com/sindresorhus/refined-github/issues',
272+
'https://github.com/sindresorhus/refined-github/issues/fregante',
273+
'https://github.com/sindresorhus/refined-github/issues?q=is%3Aclosed+sort%3Aupdated-desc'
274+
];
87275

88276
export const isRepoRoot = (): boolean => /^(tree[/][^/]+)?$/.test(getRepoPath()!);
277+
export const isRepoRootTest = [
278+
// Some tests are here only as "gotchas" for other tests that may misidentify their pages
279+
'https://github.com/sindresorhus/edit',
280+
'https://github.com/sindresorhus/search',
281+
'https://github.com/sindresorhus/refined-github',
282+
'https://github.com/sindresorhus/refined-github/',
283+
'https://github.com/sindresorhus/notifications/',
284+
'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons',
285+
'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons/',
286+
'https://github.com/sindresorhus/refined-github/tree/03fa6b8b4d6e68dea9dc9bee1d197ef5d992fbd6',
287+
'https://github.com/sindresorhus/refined-github/tree/03fa6b8b4d6e68dea9dc9bee1d197ef5d992fbd6/',
288+
'https://github.com/sindresorhus/refined-github/tree/57bf4',
289+
'https://github.com/sindresorhus/refined-github?files=1',
290+
'https://github.com/sindresorhus/refined-github/tree/master?files=1'
291+
];
89292

90293
export const isRepoSearch = (): boolean => location.pathname.slice(1).split('/')[2] === 'search';
294+
export const isRepoSearchTest = [
295+
'https://github.com/sindresorhus/refined-github/search?q=diff',
296+
'https://github.com/sindresorhus/refined-github/search?q=diff&unscoped_q=diff&type=Issues',
297+
'https://github.com/sindresorhus/refined-github/search'
298+
];
91299

92300
export const isRepoSettings = (): boolean => /^settings/.test(getRepoPath()!);
301+
export const isRepoSettingsTest = [
302+
'https://github.com/sindresorhus/refined-github/settings',
303+
'https://github.com/sindresorhus/refined-github/settings/branches'
304+
];
93305

94306
export const isRepoTree = (): boolean => isRepoRoot() || /^tree\//.test(getRepoPath()!);
307+
export const isRepoTreeTest = [
308+
'https://github.com/sindresorhus/refined-github/tree/master/distribution',
309+
'https://github.com/sindresorhus/refined-github/tree/0.13.0/distribution',
310+
'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/distribution'
311+
].concat(isRepoRootTest);
95312

96313
export const isRepoWithAccess = (): boolean => isRepo() && select.exists('.reponav-item[href$="/settings"]');
314+
export const isRepoWithAccessTest = domBased;
97315

98316
export const isSingleCommit = (): boolean => /^commit\/[0-9a-f]{5,40}/.test(getRepoPath()!);
317+
export const isSingleCommitTest = [
318+
'https://github.com/sindresorhus/refined-github/commit/5b614b9035f2035b839f48b4db7bd5c3298d526f',
319+
'https://github.com/sindresorhus/refined-github/commit/5b614'
320+
];
99321

100322
export const isSingleFile = (): boolean => /^blob\//.test(getRepoPath()!);
323+
export const isSingleFileTest = [
324+
'https://github.com/sindresorhus/refined-github/blob/master/.gitattributes',
325+
'https://github.com/sindresorhus/refined-github/blob/fix-narrow-diff/distribution/content.css',
326+
'https://github.com/sindresorhus/refined-github/blob/master/edit.txt'
327+
];
101328

102329
export const isTrending = (): boolean => location.pathname === '/trending' || location.pathname.startsWith('/trending/');
330+
export const isTrendingTest = [
331+
'https://github.com/trending',
332+
'https://github.com/trending/developers',
333+
'https://github.com/trending/unknown'
334+
];
103335

104336
export const isUserProfile = (): boolean => select.exists('.user-profile-nav');
337+
export const isUserProfileTest = domBased;
105338

106339
export const isSingleTagPage = (): boolean => /^(releases\/tag)/.test(getRepoPath()!);
340+
export const isSingleTagPageTest = [
341+
'https://github.com/sindresorhus/refined-github/releases/tag/v1.0.0-beta.4',
342+
'https://github.com/sindresorhus/refined-github/releases/tag/0.2.1'
343+
];
107344

345+
export const hasCommentsTest = skip;
108346
export const hasComments = (): boolean =>
109347
isPR() ||
110348
isIssue() ||
111349
isCommit() ||
112350
isOrganizationDiscussion();
113351

352+
export const hasRichTextEditorTest = skip;
114353
export const hasRichTextEditor = (): boolean =>
115354
hasComments() ||
116355
isNewIssue() ||
117356
isCompare();
118357

358+
export const hasCodeTest = skip;
119359
export const hasCode = (): boolean => // Static code, not the editor
120360
hasComments() ||
121361
isRepoTree() || // Readme files

0 commit comments

Comments
 (0)