forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.test.ts
More file actions
executable file
·266 lines (217 loc) · 7.39 KB
/
Copy pathfeatures.test.ts
File metadata and controls
executable file
·266 lines (217 loc) · 7.39 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
import fastIgnore from 'fast-ignore';
import {existsSync, readdirSync, readFileSync} from 'node:fs';
import path from 'node:path';
import {regexJoinWithSeparator} from 'regex-join';
import {assert, describe, test} from 'vitest';
import {isFeaturePrivate} from '../source/helpers/feature-utils.js';
import {getFeaturesMeta, getImportedFeatures} from './readme-parser.js';
// Re-run tests when these files change https://github.com/vitest-dev/vitest/discussions/5864
void import.meta.glob([
'../source/features/*.*',
'../source/refined-github.ts',
]);
const isGitIgnored = fastIgnore(readFileSync('.gitignore', 'utf8'));
const noScreenshotExceptions = new Set([
// Only add feature here if it's a shortcut only and/or extremely clear by name or description
'last-update-sort',
'create-release-shortcut',
'profile-hotkey',
'repo-wide-file-finder',
'select-all-notifications-shortcut',
'rerun-workflow',
'selection-in-new-tab',
'click-outside-modal',
'same-page-links',
'github-bugs',
'tab-size',
'monospace-textareas',
'new-tab-links',
'extensible-nav', // No visual or behavior change
'coder-pr-loc-stats', // Fork-only feature, no upstream screenshot
'hide-navigation-hover-highlight', // TODO: Add side-by-side GIF
'hide-inactive-deployments', // TODO: side-by-side PNG
'esc-to-deselect-line', // TODO Add GIF with key overlay
'scrollable-areas', // TODO: Add side-by-side PNG
// CSS-only features without screenshots yet
'reactions-popup',
'clean-checks-list',
'clean-footer',
'clean-notifications',
'mark-private-repos',
'mobile-tabs-pr',
'night-not-found',
'readable-title-change-events',
'sticky-csv-header',
'sticky-file-header',
]);
const entryPoint = 'source/refined-github.ts';
const entryPointSource = readFileSync(entryPoint);
const importedFeatures = getImportedFeatures();
const featuresInReadme = getFeaturesMeta();
// We used to enforce the filetype, but this is no longer possible with new URLs
// https://github.com/refined-github/refined-github/pull/7130
const imageRegex = /\.(?:png|gif)$/;
const rghUploadsRegex = /refined-github[/]refined-github[/]assets[/]/;
const userAttachmentsRegex = /user-attachments[/]assets[/]/;
const screenshotRegex = regexJoinWithSeparator('|', [imageRegex, rghUploadsRegex, userAttachmentsRegex]);
class FeatureFile {
readonly id: FeatureId;
readonly path: string;
readonly name: string;
constructor(name: string) {
this.name = name;
this.id = path.parse(name).name as FeatureId;
this.path = path.join('source/features', name);
}
exists(): boolean {
return existsSync(this.path);
}
contents(): string {
return readFileSync(this.path, 'utf8');
}
get tsx(): FeatureFile {
if (this.name.endsWith('.gql')) {
const id = importedFeatures.find(featureId => this.id.startsWith(featureId));
if (id) {
return new FeatureFile(id + '.tsx');
}
}
return new FeatureFile(this.id + '.tsx');
}
get css(): FeatureFile {
return new FeatureFile(this.id + '.css');
}
get svelte(): FeatureFile {
return new FeatureFile(this.id + '.svelte');
}
}
function validateReadme(featureId: FeatureId): void {
const [featureMeta, duplicate] = featuresInReadme.filter(feature => feature.id === featureId);
assert(featureMeta, 'Should be described in the readme');
assert(
featureMeta.description.length >= 20,
'Should be described better in the readme (at least 20 characters)',
);
assert(
screenshotRegex.test(featureMeta.screenshot!)
|| noScreenshotExceptions.has(featureId),
'Should have a screenshot (png/gif) in the readme, unless really difficult to demonstrate (to be discussed in review)',
);
assert(!duplicate, 'Should be described only once in the readme');
}
function validateCss(file: FeatureFile): void {
const isImportedByEntrypoint = entryPointSource.includes(`import './features/${file.name}';`);
if (!file.tsx.exists()) {
assert(
isImportedByEntrypoint,
`Should be imported by \`${entryPoint}\` or removed if it is not needed`,
);
// `github-bugs` has its own ESLint rule for test URLs
if (file.id !== 'github-bugs') {
assert(/test url/i.test(file.contents()), 'Should have test URLs');
}
if (!isFeaturePrivate(file.name)) {
validateReadme(file.id);
}
return;
}
assert(
file.tsx.contents().includes(`import './${file.name}';`),
`Should be imported by \`${file.tsx.name}\``,
);
assert(
!isImportedByEntrypoint,
`Should only be imported by \`${file.tsx.name}\`, not by \`${entryPoint}\``,
);
assert(!/test url/i.test(file.contents()), 'Only TSX files and *lone* CSS files should have test URLs');
}
function validateGql(file: FeatureFile): void {
assert(
file.tsx.exists(),
'Does not match any existing features. The filename should match the feature that uses it.',
);
assert(
file.tsx.contents().includes(`from './${file.name}';`)
|| file.svelte.exists() && file.svelte.contents().includes(`from './${file.name}';`),
`Should be imported by \`${file.tsx.name}\` or \`${file.svelte.name}\``,
);
}
function validateTsx(file: FeatureFile): void {
assert(
importedFeatures.includes(file.id),
`Should be imported by \`${entryPoint}\``,
);
assert(/test url/i.test(file.contents()), 'Should have test URLs');
if (
/api\.v4|getDefaultBranch|getPrInfo/.test(file.contents())
&& /observe\(|delegate\(/.test(file.contents())
) {
assert(
/requiresToken:\s*true|hasToken/.test(file.contents()),
`${file.id} uses the v4 API, so it should include \`requiresToken: true\`, or if the token is optional, \`hasToken\` anywhere`,
);
}
if (file.contents().includes('.addCssFeature')) {
assert(
file.css.exists(),
`${file.id} uses \`.addCssFeature\`, but ${file.css.name} is missing`,
);
assert(
file.css.contents().includes(`html:not([rgh-OFF-${file.id}])`),
`${file.css.name} should contain a \`html:not([rgh-OFF-${file.id}])\` selector`,
);
}
if (file.contents().includes('deduplicate:')) {
assert(
!file.contents().includes('observe('),
`${file.id} should not use both "deduplicate" and "observe()", the observer already takes care of deduplication`,
);
if (file.contents().includes('delegate(')) {
assert(
!file.contents().includes('(signal: AbortSignal)'),
`${file.id} should not use "deduplicate" and "delegate()" together with an abort signal, or else the event listener might be removed and not restored due to the deduplicator https://github.com/refined-github/refined-github/issues/5871`,
);
}
}
if (!isFeaturePrivate(file.name)) {
validateReadme(file.id);
}
}
function validateSvelte(file: FeatureFile): void {
assert(
file.tsx.exists(),
'Does not match any existing features. The filename should match the feature that uses it.',
);
assert(
file.tsx.contents().includes(`from './${file.name}';`),
`Should be imported by \`${file.tsx.name}\``,
);
}
describe('features', () => {
const featuresDirectoryContents = readdirSync('source/features/');
test.each(featuresDirectoryContents)('%s', (filename: string) => {
if (isGitIgnored(filename)) {
return;
}
const file = new FeatureFile(filename);
if (filename.endsWith('.gql')) {
validateGql(file);
return;
}
if (filename.endsWith('.css')) {
validateCss(file);
return;
}
if (filename.endsWith('.tsx')) {
validateTsx(file);
return;
}
if (filename.endsWith('.svelte')) {
validateSvelte(file);
return;
}
assert.fail(
`The \`/source/features\` folder should only contain .css, .tsx and .gql files. Found \`source/features/${filename}\``,
);
});
});