-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest-all.mjs
More file actions
385 lines (327 loc) · 13 KB
/
Copy pathtest-all.mjs
File metadata and controls
385 lines (327 loc) · 13 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env node
/**
* test-all.mjs — Comprehensive test suite for career-copilot
*
* Run before merging any PR or pushing changes.
* Tests: syntax, scripts, dashboard, data contract, personal data, paths.
*
* Usage:
* node test-all.mjs # Run all tests
* node test-all.mjs --quick # Skip dashboard build (faster)
*/
import { execSync } from 'child_process';
import { readFileSync, existsSync, readdirSync } from 'fs';
import { join, dirname } from 'path';
import { tmpdir } from 'os';
import { fileURLToPath } from 'url';
import { evaluateScriptExit } from './lib/script-exit.mjs';
import { PERSONAL_DATA_SCAN_EXTENSIONS, shouldScanForPersonalData } from './lib/test-file-scope.mjs';
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = __dirname;
const QUICK = process.argv.includes('--quick');
const SCRIPT_TIMEOUT = parseInt(process.env.CAREER_COPILOT_TIMEOUT_MS, 10) || 30000;
let passed = 0;
let failed = 0;
let warnings = 0;
function pass(msg) { console.log(` ✅ ${msg}`); passed++; }
function fail(msg) { console.log(` ❌ ${msg}`); failed++; }
function warn(msg) { console.log(` ⚠️ ${msg}`); warnings++; }
function run(cmd, opts = {}) {
try {
return execSync(cmd, { cwd: ROOT, encoding: 'utf-8', timeout: SCRIPT_TIMEOUT, ...opts }).trim();
} catch (e) {
return null;
}
}
function fileExists(path) { return existsSync(join(ROOT, path)); }
function readFile(path) { return readFileSync(join(ROOT, path), 'utf-8'); }
console.log('\n🧪 career-copilot test suite\n');
// ── 1. SYNTAX CHECKS ────────────────────────────────────────────
console.log('1. Syntax checks');
const mjsFiles = readdirSync(ROOT).filter(f => f.endsWith('.mjs'));
// Also check lib/ and lib/adapters/ subdirectories
const libDir = join(ROOT, 'lib');
if (existsSync(libDir)) {
for (const f of readdirSync(libDir).filter(f => f.endsWith('.mjs'))) {
mjsFiles.push(`lib/${f}`);
}
const adaptersDir = join(libDir, 'adapters');
if (existsSync(adaptersDir)) {
for (const f of readdirSync(adaptersDir).filter(f => f.endsWith('.mjs'))) {
mjsFiles.push(`lib/adapters/${f}`);
}
}
}
for (const f of mjsFiles) {
const result = run(`node --check ${f}`);
if (result !== null) {
pass(`${f} syntax OK`);
} else {
fail(`${f} has syntax errors`);
}
}
// ── 2. SCRIPT EXECUTION ─────────────────────────────────────────
console.log('\n2. Script execution (graceful on empty data)');
const scripts = [
{ name: 'cv-sync-check.mjs', allowFail: true }, // exits 1 without user data, 0 in configured workspaces
{ name: 'doctor.mjs', allowFail: true }, // fails until user-layer setup files exist
{ name: 'analyze-patterns.mjs --summary', allowFail: true },
{ name: 'analytics.mjs --json', expectExit: 0 },
{ name: 'verify-pipeline.mjs', expectExit: 0 },
{ name: 'normalize-statuses.mjs', expectExit: 0 },
{ name: 'dedup-tracker.mjs', expectExit: 0 },
{ name: 'merge-tracker.mjs', expectExit: 0 },
{ name: 'check-liveness.mjs --help', expectExit: 0 },
{ name: 'generate-pdf.mjs --help', expectExit: 0 },
{ name: 'import-cv.mjs --help', expectExit: 0 },
{ name: 'update-system.mjs check', expectExit: 0 },
];
for (const { name, expectExit, allowFail } of scripts) {
let actualExit = 0;
try {
execSync(`node ${name} 2>&1`, { cwd: ROOT, encoding: 'utf-8', timeout: SCRIPT_TIMEOUT }).trim();
} catch (e) {
actualExit = e.status ?? 1;
}
const result = evaluateScriptExit({ name, expectExit, allowFail }, actualExit);
if (result.level === 'pass') {
pass(result.message);
} else if (result.level === 'warn') {
warn(result.message);
} else {
fail(result.message);
}
}
// ── 2B. UNIT TESTS ─────────────────────────────────────────────
console.log('\n2B. Unit tests');
const unitResult = run('node --test tests/*.test.mjs');
if (unitResult !== null) {
pass('Unit tests pass');
} else {
fail('Unit tests failed');
}
// ── 3. DASHBOARD BUILD ──────────────────────────────────────────
if (!QUICK) {
console.log('\n3. Dashboard build');
const goAvailable = run('which go 2>/dev/null');
if (!goAvailable) {
warn('Go not installed — skipping dashboard build');
} else {
const goBuild = run(`cd dashboard && go build -o ${join(tmpdir(), 'career-dashboard-test')} . 2>&1`);
if (goBuild !== null) {
pass('Dashboard compiles');
} else {
fail('Dashboard build failed');
}
}
} else {
console.log('\n3. Dashboard build (skipped --quick)');
}
// ── 4. DATA CONTRACT ────────────────────────────────────────────
console.log('\n4. Data contract validation');
// Check system files exist
const systemFiles = [
'INSTRUCTIONS.md', '.github/copilot-instructions.md', 'VERSION', 'DATA_CONTRACT.md',
'modes/_shared.md', 'modes/_profile.template.md',
'modes/evaluate.md', 'modes/pdf.md', 'modes/scan.md',
'templates/states.yml', 'templates/cv-template.html',
];
for (const f of systemFiles) {
if (fileExists(f)) {
pass(`System file exists: ${f}`);
} else {
fail(`Missing system file: ${f}`);
}
}
// Check user files are NOT tracked (gitignored)
const userFiles = [
'config/profile.yml', 'modes/_profile.md', 'portals.yml', 'feeds.yml',
];
for (const f of userFiles) {
const tracked = run(`git ls-files ${f}`);
if (tracked === '') {
pass(`User file gitignored: ${f}`);
} else if (tracked === null) {
pass(`User file gitignored: ${f}`);
} else {
fail(`User file IS tracked (should be gitignored): ${f}`);
}
}
// ── 5. PERSONAL DATA LEAK CHECK ─────────────────────────────────
console.log('\n5. Personal data leak check');
// Generic patterns to detect personal data leaks.
// Add your own name, email, phone, or personal domain patterns here
// to ensure they don't leak into system files.
const leakPatterns = [
// Example patterns — replace with your own personal identifiers:
// 'Your Name', 'yourname.com', 'your@email.com', 'your-phone-number',
// '/Users/youruser/',
];
// Also check for common PII patterns (generic)
const piiRegexPatterns = [
{ pattern: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/, label: 'email address' },
{ pattern: /\b(?:\+?\d{1,3}[-. ]?)?(?:\(\d{3}\)|\d{3})[-. ]\d{3}[-. ]\d{4}\b/, label: 'phone number' },
];
let leakFound = false;
// Check explicit string and PII regex patterns in system files only.
const systemMdFiles = run(
`find . -type f \\( ${PERSONAL_DATA_SCAN_EXTENSIONS.map(e => `-name "*.${e}"`).join(' -o ')} \\)`
);
if (systemMdFiles) {
for (const filePath of systemMdFiles.split('\n').filter(Boolean)) {
const cleanPath = filePath.replace('./', '');
if (!shouldScanForPersonalData(cleanPath)) continue;
try {
const content = readFileSync(join(ROOT, cleanPath), 'utf-8');
for (const pattern of leakPatterns) {
if (content.includes(pattern)) {
warn(`Possible personal data in ${cleanPath}: "${pattern}"`);
leakFound = true;
}
}
for (const { pattern: regex, label } of piiRegexPatterns) {
const match = content.match(regex);
if (match) {
// Skip if it looks like an example/placeholder
if (match[0].includes('example') || match[0].includes('placeholder') ||
match[0].includes('jane') || match[0].includes('your') ||
match[0].includes('users.noreply.github.com') ||
/^[-+(). 0]+$/.test(match[0])) continue;
warn(`Possible ${label} in ${cleanPath}: "${match[0]}"`);
leakFound = true;
}
}
} catch {
// File read error, skip
}
}
}
if (!leakFound) {
pass('No personal data leaks outside allowed files');
}
// ── 6. ABSOLUTE PATH CHECK ──────────────────────────────────────
console.log('\n6. Absolute path check');
const absPathResult = run(
`grep -rn "/Users/" --include="*.mjs" --include="*.sh" --include="*.md" --include="*.go" --include="*.yml" . 2>/dev/null | grep -v node_modules | grep -v ".git/" | grep -v README.md | grep -v LICENSE | grep -v go.sum | grep -v copilot-instructions.md | grep -v INSTRUCTIONS.md | grep -v test-all.mjs | grep -v PULL_REQUEST_TEMPLATE | grep -v ci.yml`
);
if (!absPathResult) {
pass('No absolute paths in code files');
} else {
for (const line of absPathResult.split('\n').filter(Boolean)) {
fail(`Absolute path: ${line.slice(0, 100)}`);
}
}
// ── 7. MODE FILE INTEGRITY ──────────────────────────────────────
console.log('\n7. Mode file integrity');
const expectedModes = [
'_shared.md', '_profile.template.md', 'evaluate.md', 'pdf.md', 'scan.md',
'batch.md', 'apply.md', 'auto-pipeline.md', 'contact.md', 'deep.md',
'compare.md', 'pipeline.md', 'project.md', 'tracker.md', 'training.md',
'interview-prep.md', 'patterns.md', 'auto-apply.md',
];
for (const mode of expectedModes) {
if (fileExists(`modes/${mode}`)) {
pass(`Mode exists: ${mode}`);
} else {
fail(`Missing mode: ${mode}`);
}
}
// Check _shared.md references _profile.md
const shared = readFile('modes/_shared.md');
if (shared.includes('_profile.md')) {
pass('_shared.md references _profile.md');
} else {
fail('_shared.md does NOT reference _profile.md');
}
// ── 8. INSTRUCTIONS INTEGRITY ───────────────────────────────────
console.log('\n8. Instructions integrity');
const instructions = readFile('INSTRUCTIONS.md');
const requiredSections = [
'Data Contract', 'Update Check', 'Ethical Use',
'Offer Verification', 'Canonical Application States', 'TSV Format',
'First Run', 'Onboarding',
];
for (const section of requiredSections) {
if (instructions.includes(section)) {
pass(`Instructions has section: ${section}`);
} else {
fail(`Instructions missing section: ${section}`);
}
}
// Verify INSTRUCTIONS.md is tool-agnostic (no API call syntax)
const toolCallPatterns = [
/view\(path=/,
/web_search\(/,
/web_fetch\(url=/,
/browser_navigate\(/,
/browser_snapshot\(/,
/task\(agent_type=/,
];
for (const pat of toolCallPatterns) {
if (pat.test(instructions)) {
fail(`INSTRUCTIONS.md contains tool-specific syntax: ${pat.source}`);
} else {
pass(`INSTRUCTIONS.md clean of: ${pat.source}`);
}
}
// ── 9. MULTI-CLI ENTRY POINTS ───────────────────────────────────
console.log('\n9. Multi-CLI entry points');
const entryPoints = [
'.github/copilot-instructions.md',
'CLAUDE.md',
'.cursorrules',
'.windsurfrules',
'GEMINI.md',
'AGENTS.md',
];
for (const ep of entryPoints) {
if (fileExists(ep)) {
const content = readFile(ep);
if (content.includes('INSTRUCTIONS.md')) {
pass(`${ep} references INSTRUCTIONS.md`);
} else {
fail(`${ep} does not reference INSTRUCTIONS.md`);
}
} else {
fail(`Entry point missing: ${ep}`);
}
}
// Verify mode files are tool-agnostic
const modeToolPatterns = [/view\(path=/, /web_search\(/, /web_fetch\(url=/, /browser_navigate\(/, /browser_snapshot\(/, /task\(agent_type=/];
const modeFiles = readdirSync(join(ROOT, 'modes')).filter(f => f.endsWith('.md') && !f.startsWith('_profile'));
let modeClean = true;
for (const mf of modeFiles) {
const content = readFile(`modes/${mf}`);
for (const pat of modeToolPatterns) {
if (pat.test(content)) {
fail(`modes/${mf} contains tool-specific syntax: ${pat.source}`);
modeClean = false;
}
}
}
if (modeClean) pass('All mode files are tool-agnostic');
// ── 10. VERSION FILE ────────────────────────────────────────────
console.log('\n10. Version file');
if (fileExists('VERSION')) {
const version = readFile('VERSION').trim();
if (/^\d+\.\d+\.\d+$/.test(version)) {
pass(`VERSION is valid semver: ${version}`);
} else {
fail(`VERSION is not valid semver: "${version}"`);
}
} else {
fail('VERSION file missing');
}
// ── SUMMARY ─────────────────────────────────────────────────────
console.log('\n' + '='.repeat(50));
console.log(`📊 Results: ${passed} passed, ${failed} failed, ${warnings} warnings`);
if (failed > 0) {
console.log('🔴 TESTS FAILED — do NOT push/merge until fixed\n');
process.exit(1);
} else if (warnings > 0) {
console.log('🟡 Tests passed with warnings — review before pushing\n');
process.exit(0);
} else {
console.log('🟢 All tests passed — safe to push/merge\n');
process.exit(0);
}