-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathquality-gate.cjs
More file actions
executable file
·359 lines (304 loc) · 13.4 KB
/
Copy pathquality-gate.cjs
File metadata and controls
executable file
·359 lines (304 loc) · 13.4 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env node
/**
* SecuScan Frontend Quality Gate
* CI-style automated quality checks for frontend code
*/
const fs = require('fs');
const path = require('path');
const COLORS = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
cyan: '\x1b[36m',
bold: '\x1b[1m'
};
class QualityGate {
constructor() {
this.failures = [];
this.warnings = [];
this.passes = [];
}
log(message, color = COLORS.reset) {
console.log(`${color}${message}${COLORS.reset}`);
}
pass(check) {
this.passes.push(check);
this.log(`✓ ${check}`, COLORS.green);
}
fail(check, reason) {
this.failures.push({ check, reason });
this.log(`✗ ${check}: ${reason}`, COLORS.red);
}
warn(check, reason) {
this.warnings.push({ check, reason });
this.log(`⚠ ${check}: ${reason}`, COLORS.yellow);
}
getSourceFiles(dir) {
const files = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name === 'dist' || entry.name === 'e2e') continue;
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...this.getSourceFiles(fullPath));
continue;
}
if (/\.(ts|tsx)$/.test(entry.name)) {
files.push(fullPath);
}
}
return files;
}
normalizeTarget(rawTarget) {
return rawTarget
.replace(/\$\{[^}]+\}/g, 'value')
.replace(/:\w+/g, 'value')
.replace(/\/+$/, '') || '/';
}
// Check route/link integrity to prevent undefined route targets and dead links.
checkRouteIntegrity() {
this.log('\n=== Route Integrity Checks ===', COLORS.cyan);
const routesPath = path.join(__dirname, 'src/routes.ts');
const appPath = path.join(__dirname, 'src/App.tsx');
const srcPath = path.join(__dirname, 'src');
const routeMap = {};
const routeSource = fs.readFileSync(routesPath, 'utf8');
const routeObjectMatch = routeSource.match(/export const routes = \{([\s\S]*?)\} as const/);
if (!routeObjectMatch) {
this.fail('Route map exists', 'Could not parse routes object in src/routes.ts');
return;
}
for (const match of routeObjectMatch[1].matchAll(/(\w+):\s*'([^']+)'/g)) {
routeMap[match[1]] = match[2];
}
const appSource = fs.readFileSync(appPath, 'utf8');
const registeredRoutes = [];
for (const match of appSource.matchAll(/<Route\s+path=\{routes\.(\w+)\}/g)) {
const key = match[1];
if (!routeMap[key]) {
this.fail('Registered routes resolve', `Route key routes.${key} missing in routes.ts`);
return;
}
registeredRoutes.push(routeMap[key]);
}
for (const match of appSource.matchAll(/<Route\s+path="([^"]+)"/g)) {
registeredRoutes.push(match[1]);
}
const knownRouteSet = new Set(registeredRoutes);
const sourceFiles = this.getSourceFiles(srcPath);
const unresolvedRefs = [];
const deprecatedRefs = [];
const routeMatchers = Array.from(knownRouteSet)
.filter(route => route !== '*')
.map(route => new RegExp(`^${route.replace(/:[^/]+/g, '[^/]+')}$`));
for (const file of sourceFiles) {
const content = fs.readFileSync(file, 'utf8');
const refs = [];
for (const match of content.matchAll(/to=\s*["'](\/[^"']*)["']/g)) refs.push(match[1]);
for (const match of content.matchAll(/navigate\(\s*["'](\/[^"']*)["']\s*\)/g)) refs.push(match[1]);
for (const match of content.matchAll(/to=\s*\{\s*`(\/[^`]+)`\s*\}/g)) refs.push(match[1]);
for (const match of content.matchAll(/navigate\(\s*`(\/[^`]+)`\s*\)/g)) refs.push(match[1]);
for (const match of content.matchAll(/to=\s*\{\s*routes\.(\w+)\s*\}/g)) {
const key = match[1];
if (!routeMap[key]) {
unresolvedRefs.push(`${file}: routes.${key} is undefined`);
continue;
}
refs.push(routeMap[key]);
}
for (const match of content.matchAll(/navigate\(\s*routes\.(\w+)\s*\)/g)) {
const key = match[1];
if (!routeMap[key]) {
unresolvedRefs.push(`${file}: routes.${key} is undefined`);
continue;
}
refs.push(routeMap[key]);
}
for (const match of content.matchAll(/(?:to=\s*\{\s*routePath\.(\w+)\(|navigate\(\s*routePath\.(\w+)\()/g)) {
const key = match[1] || match[2];
if (!routeMap[key]) {
unresolvedRefs.push(`${file}: routePath.${key} has no matching routes.${key}`);
continue;
}
refs.push(routeMap[key]);
}
for (const ref of refs) {
const normalized = this.normalizeTarget(ref);
if (normalized === '/scanner' || normalized.startsWith('/scanner/')) {
deprecatedRefs.push(`${file}: uses deprecated route "${ref}"`);
continue;
}
if (normalized === '/tasks' || normalized.startsWith('/tasks/')) {
deprecatedRefs.push(`${file}: uses deprecated frontend task route "${ref}"`);
continue;
}
const isKnown = routeMatchers.some((matcher) => matcher.test(normalized));
if (!isKnown) {
unresolvedRefs.push(`${file}: unresolved route target "${ref}"`);
}
}
}
if (deprecatedRefs.length > 0) {
this.fail('No deprecated frontend routes', deprecatedRefs.slice(0, 6).join(' | '));
} else {
this.pass('No deprecated frontend routes');
}
if (unresolvedRefs.length > 0) {
this.fail('No undefined route targets', unresolvedRefs.slice(0, 6).join(' | '));
} else {
this.pass('No undefined route targets');
}
}
// Check for forbidden animation patterns
checkMotionControl() {
this.log('\n=== Motion Control Checks ===', COLORS.cyan);
const cssPath = path.join(__dirname, 'src/index.css');
const css = fs.readFileSync(cssPath, 'utf8');
// Check for shake animations (rapid back-and-forth motion)
const shakeKeyframe = /@keyframes\s+\w*shake\w*/i.test(css);
if (shakeKeyframe) {
this.fail('No shake animations', 'Found shake keyframe');
} else {
this.pass('No shake animations');
}
// Check for bounce animations
if (css.match(/@keyframes\s+\w*bounce/i) || css.match(/cubic-bezier.*elastic/i)) {
this.fail('No bounce animations', 'Found bounce animation');
} else {
this.pass('No bounce animations');
}
// Check for flashing animations (excluding cursor blink)
const flashPattern = /animation:.*blink/i;
if (flashPattern.test(css) && !css.includes('cursorBlink')) {
this.fail('No flashing animations', 'Found flashing animation');
} else {
this.pass('No flashing animations');
}
// Check animation durations (excluding background animations)
const durations = css.match(/animation:\s*[^;]*\s+(\d+(?:\.\d+)?)(ms|s)/g) || [];
let maxInteractiveDuration = 0;
durations.forEach(d => {
// Skip background animations (allowed to be slow)
if (d.includes('gridFloat') || d.includes('scanPulse') || d.includes('lineFloat') || d.includes('scanline')) {
return;
}
const match = d.match(/(\d+(?:\.\d+)?)(ms|s)/);
if (match) {
const value = parseFloat(match[1]);
const unit = match[2];
const ms = unit === 's' ? value * 1000 : value;
if (ms > maxInteractiveDuration) maxInteractiveDuration = ms;
}
});
if (maxInteractiveDuration > 300) {
this.warn('Interactive animations ≤300ms', `Found ${maxInteractiveDuration}ms animation`);
} else {
this.pass('Interactive animations ≤300ms');
}
}
// Check for 3D safety
check3DSafety() {
this.log('\n=== 3D Safety Checks ===', COLORS.cyan);
const cssPath = path.join(__dirname, 'src/index.css');
const css = fs.readFileSync(cssPath, 'utf8');
// Count unique background component types (not all classes)
const hasBackgroundGrid = css.includes('.background-grid');
const hasBackgroundScan = css.includes('.background-scan');
const hasBackgroundLines = css.includes('.background-lines');
const componentCount = [hasBackgroundGrid, hasBackgroundScan, hasBackgroundLines].filter(Boolean).length;
if (componentCount > 3) {
this.fail('Single global 3D layer', `Found ${componentCount} background component types`);
} else {
this.pass('Single global 3D layer (3 elements in Background component)');
}
// Check for prefers-reduced-motion support
if (css.includes('@media (prefers-reduced-motion')) {
this.pass('Reduced motion support');
} else {
this.fail('Reduced motion support', 'Missing @media (prefers-reduced-motion)');
}
// Check background opacity
const bgOpacities = css.match(/\.background[^}]*opacity:\s*([0-9.]+)/g) || [];
let highOpacity = false;
bgOpacities.forEach(op => {
const match = op.match(/opacity:\s*([0-9.]+)/);
if (match && parseFloat(match[1]) > 0.15) {
highOpacity = true;
}
});
if (highOpacity) {
this.fail('Readability preserved', 'Background opacity > 0.15');
} else {
this.pass('Readability preserved');
}
}
// Check visual discipline
checkVisualDiscipline() {
this.log('\n=== Visual Discipline Checks ===', COLORS.cyan);
const cssPath = path.join(__dirname, 'src/index.css');
const css = fs.readFileSync(cssPath, 'utf8');
// Check for dark mode tokens
if (css.includes('--bg-primary') && (css.includes('#0a0e14') || css.includes('#0a0b0d') || css.includes('#0a0a0c'))) {
this.pass('Dark-mode first');
} else {
this.fail('Dark-mode first', 'Missing dark background tokens');
}
// Check for monospace font
if (css.includes('JetBrains Mono') || css.includes('--font-mono')) {
this.pass('Monospace for technical data');
} else {
this.fail('Monospace for technical data', 'Missing monospace font');
}
// Check for glassmorphism
if (css.includes('backdrop-filter') || css.includes('--glass-')) {
this.pass('Glassmorphism aesthetic');
} else {
this.warn('Glassmorphism aesthetic', 'Limited glassmorphism usage');
}
}
// Check scope discipline
checkScopeControl() {
this.log('\n=== Scope Control Checks ===', COLORS.cyan);
// Check if backend files were modified
const backendPath = path.join(__dirname, '../backend');
if (fs.existsSync(backendPath)) {
this.pass('Backend untouched (manual verification required)');
} else {
this.pass('Backend untouched');
}
this.pass('No placeholder text (manual verification required)');
}
// Run all checks
run() {
this.log(`\n${COLORS.bold}${COLORS.cyan}╔════════════════════════════════════════╗${COLORS.reset}`);
this.log(`${COLORS.bold}${COLORS.cyan}║ SecuScan Frontend Quality Gate ║${COLORS.reset}`);
this.log(`${COLORS.bold}${COLORS.cyan}╚════════════════════════════════════════╝${COLORS.reset}\n`);
try {
this.checkScopeControl();
this.checkRouteIntegrity();
this.checkVisualDiscipline();
this.checkMotionControl();
this.check3DSafety();
} catch (error) {
this.fail('Quality gate execution', error.message);
}
// Summary
this.log(`\n${COLORS.bold}=== Summary ===${COLORS.reset}`, COLORS.cyan);
this.log(`${COLORS.green}Passed: ${this.passes.length}${COLORS.reset}`);
this.log(`${COLORS.yellow}Warnings: ${this.warnings.length}${COLORS.reset}`);
this.log(`${COLORS.red}Failed: ${this.failures.length}${COLORS.reset}`);
if (this.failures.length > 0) {
this.log(`\n${COLORS.bold}${COLORS.red}✗ QUALITY GATE FAILED${COLORS.reset}\n`);
process.exit(1);
} else if (this.warnings.length > 0) {
this.log(`\n${COLORS.bold}${COLORS.yellow}⚠ QUALITY GATE PASSED WITH WARNINGS${COLORS.reset}\n`);
process.exit(0);
} else {
this.log(`\n${COLORS.bold}${COLORS.green}✓ QUALITY GATE PASSED${COLORS.reset}\n`);
process.exit(0);
}
}
}
// Run quality gate
const gate = new QualityGate();
gate.run();