-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathpipeline.test.js
More file actions
763 lines (619 loc) · 25 KB
/
Copy pathpipeline.test.js
File metadata and controls
763 lines (619 loc) · 25 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/**
* Tests for pipeline.js
* Slop detection pipeline orchestrator
*/
const fs = require('fs');
const path = require('path');
const os = require('os');
const {
runPipeline,
runPhase1,
runMultiPassAnalyzers,
buildSummary,
formatHandoffPrompt,
formatCompactPrompt,
CERTAINTY,
THOROUGHNESS
} = require('../lib/patterns/pipeline');
describe('pipeline', () => {
// Test directory setup
let tmpDir;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pipeline-test-'));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
describe('CERTAINTY constants', () => {
it('should have all certainty levels defined', () => {
expect(CERTAINTY.HIGH).toBe('HIGH');
expect(CERTAINTY.MEDIUM).toBe('MEDIUM');
expect(CERTAINTY.LOW).toBe('LOW');
});
});
describe('THOROUGHNESS constants', () => {
it('should have all thoroughness levels defined', () => {
expect(THOROUGHNESS.QUICK).toBe('quick');
expect(THOROUGHNESS.NORMAL).toBe('normal');
expect(THOROUGHNESS.DEEP).toBe('deep');
});
});
describe('runPhase1', () => {
it('should detect console.log statements', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.js'),
'function test() {\n console.log("debug");\n return 1;\n}'
);
const findings = runPhase1(tmpDir, ['app.js'], null);
expect(findings.length).toBeGreaterThan(0);
expect(findings[0].patternName).toBe('console_debugging');
expect(findings[0].certainty).toBe(CERTAINTY.HIGH);
expect(findings[0].phase).toBe(1);
});
it('should detect placeholder text', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.js'),
'const text = "lorem ipsum dolor sit amet";\n'
);
const findings = runPhase1(tmpDir, ['app.js'], null);
const placeholderFindings = findings.filter(f => f.patternName === 'placeholder_text');
expect(placeholderFindings.length).toBeGreaterThan(0);
});
it('should filter by language', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.js'),
'console.log("debug");\n'
);
fs.writeFileSync(
path.join(tmpDir, 'app.py'),
'print("debug")\n'
);
const jsFindings = runPhase1(tmpDir, ['app.js', 'app.py'], 'javascript');
// Should only find JS console.log, not Python print
const jsConsole = jsFindings.filter(f => f.patternName === 'console_debugging');
expect(jsConsole.length).toBe(1);
expect(jsConsole[0].file).toBe('app.js');
});
it('should skip excluded files', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.test.js'),
'console.log("debug in test");\n'
);
const findings = runPhase1(tmpDir, ['app.test.js'], null);
// Test files should be excluded for console_debugging pattern
const consoleFindings = findings.filter(f => f.patternName === 'console_debugging');
expect(consoleFindings.length).toBe(0);
});
it('should include line number and content', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.js'),
'function foo() {\n console.log("test");\n}'
);
const findings = runPhase1(tmpDir, ['app.js'], null);
expect(findings[0].line).toBe(2);
expect(findings[0].content).toContain('console.log');
});
it('should handle empty files gracefully', () => {
fs.writeFileSync(path.join(tmpDir, 'empty.js'), '');
const findings = runPhase1(tmpDir, ['empty.js'], null);
expect(findings.length).toBe(0);
});
it('should handle unreadable files gracefully', () => {
const findings = runPhase1(tmpDir, ['nonexistent.js'], null);
expect(findings.length).toBe(0);
});
});
describe('runMultiPassAnalyzers', () => {
it('should detect excessive JSDoc', () => {
// JSDoc: 15 non-empty lines, Function: 4 code lines = 15/4 = 3.75x (exceeds 3.0 max)
const code = `
/**
* Add two numbers together with detailed documentation
* This function performs addition
* Line 3 with more explanation
* Line 4 describes parameters
* @param {number} a - First number to add
* @param {number} b - Second number to add
* @returns {number} The sum of a and b
* Line 8 with additional context
* Line 9 more details about the function
* Line 10 even more information
* Line 11 some more text here
* Line 12 additional notes
* Line 13 edge cases documented
* Line 14 performance considerations
* Line 15 final closing notes
*/
function add(a, b) {
const sum = a + b;
const validated = sum;
console.log(validated);
return validated;
}`;
fs.writeFileSync(path.join(tmpDir, 'math.js'), code);
const findings = runMultiPassAnalyzers(tmpDir, ['math.js']);
const docRatioFindings = findings.filter(f => f.patternName === 'doc_code_ratio_js');
expect(docRatioFindings.length).toBeGreaterThan(0);
expect(docRatioFindings[0].certainty).toBe(CERTAINTY.MEDIUM);
});
it('should detect excessive inline comments', () => {
// Comments: 8 lines, Code: 4 lines = 8/4 = 2x (matches 2.0 maxCommentRatio threshold)
// Need to exceed the threshold, so making it higher
const code = `
function process(data) {
// This is a comment explaining the function
// Another comment with more details
// Yet another comment about edge cases
// Still more comments about implementation
// Even more explanation about approach
// So much text here describing behavior
// Really explaining everything in detail
// One more comment to push over threshold
// And another for good measure
const result = data.trim();
const processed = result.toLowerCase();
const final = processed.replace(/\\s+/g, ' ');
return final;
}`;
fs.writeFileSync(path.join(tmpDir, 'processor.js'), code);
const findings = runMultiPassAnalyzers(tmpDir, ['processor.js']);
const verbosityFindings = findings.filter(f => f.patternName === 'verbosity_ratio');
expect(verbosityFindings.length).toBeGreaterThan(0);
});
it('should skip non-JS files for JSDoc analysis', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.py'),
'"""\nVery long docstring\n"""\ndef foo():\n pass\n'
);
const findings = runMultiPassAnalyzers(tmpDir, ['app.py']);
const docRatioFindings = findings.filter(f => f.patternName === 'doc_code_ratio_js');
expect(docRatioFindings.length).toBe(0);
});
});
describe('buildSummary', () => {
it('should count findings by severity', () => {
const findings = [
{ severity: 'high', certainty: 'HIGH', phase: 1, autoFix: 'remove', patternName: 'a' },
{ severity: 'medium', certainty: 'HIGH', phase: 1, autoFix: 'flag', patternName: 'b' },
{ severity: 'medium', certainty: 'MEDIUM', phase: 1, autoFix: 'flag', patternName: 'c' },
{ severity: 'low', certainty: 'LOW', phase: 2, autoFix: 'none', patternName: 'd' }
];
const summary = buildSummary(findings);
expect(summary.total).toBe(4);
expect(summary.bySeverity.high).toBe(1);
expect(summary.bySeverity.medium).toBe(2);
expect(summary.bySeverity.low).toBe(1);
});
it('should count findings by certainty', () => {
const findings = [
{ severity: 'high', certainty: 'HIGH', phase: 1, autoFix: 'remove', patternName: 'a' },
{ severity: 'medium', certainty: 'HIGH', phase: 1, autoFix: 'flag', patternName: 'b' },
{ severity: 'medium', certainty: 'MEDIUM', phase: 1, autoFix: 'flag', patternName: 'c' },
{ severity: 'low', certainty: 'LOW', phase: 2, autoFix: 'none', patternName: 'd' }
];
const summary = buildSummary(findings);
expect(summary.byCertainty.HIGH).toBe(2);
expect(summary.byCertainty.MEDIUM).toBe(1);
expect(summary.byCertainty.LOW).toBe(1);
});
it('should count findings by phase', () => {
const findings = [
{ severity: 'high', certainty: 'HIGH', phase: 1, autoFix: 'remove', patternName: 'a' },
{ severity: 'medium', certainty: 'HIGH', phase: 1, autoFix: 'flag', patternName: 'b' },
{ severity: 'low', certainty: 'LOW', phase: 2, autoFix: 'none', patternName: 'c' }
];
const summary = buildSummary(findings);
expect(summary.byPhase[1]).toBe(2);
expect(summary.byPhase[2]).toBe(1);
});
it('should count findings by autoFix strategy', () => {
const findings = [
{ severity: 'high', certainty: 'HIGH', phase: 1, autoFix: 'remove', patternName: 'a' },
{ severity: 'medium', certainty: 'HIGH', phase: 1, autoFix: 'flag', patternName: 'b' },
{ severity: 'medium', certainty: 'MEDIUM', phase: 1, autoFix: 'flag', patternName: 'c' }
];
const summary = buildSummary(findings);
expect(summary.byAutoFix.remove).toBe(1);
expect(summary.byAutoFix.flag).toBe(2);
});
it('should track top patterns', () => {
const findings = [
{ severity: 'high', certainty: 'HIGH', phase: 1, autoFix: 'remove', patternName: 'console_debugging' },
{ severity: 'high', certainty: 'HIGH', phase: 1, autoFix: 'remove', patternName: 'console_debugging' },
{ severity: 'medium', certainty: 'HIGH', phase: 1, autoFix: 'flag', patternName: 'placeholder_text' }
];
const summary = buildSummary(findings);
expect(summary.topPatterns.console_debugging).toBe(2);
expect(summary.topPatterns.placeholder_text).toBe(1);
});
it('should handle empty findings array', () => {
const summary = buildSummary([]);
expect(summary.total).toBe(0);
expect(summary.bySeverity.high).toBe(0);
});
});
describe('formatHandoffPrompt', () => {
it('should return no issues message for empty findings', () => {
const prompt = formatHandoffPrompt([], 'report');
expect(prompt).toContain('No issues detected');
});
it('should include mode in prompt', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', description: 'Test', autoFix: 'remove', severity: 'medium' }
];
const reportPrompt = formatHandoffPrompt(findings, 'report');
const applyPrompt = formatHandoffPrompt(findings, 'apply');
expect(reportPrompt).toContain('Mode: **report**');
expect(applyPrompt).toContain('Mode: **apply**');
});
it('should group findings by certainty', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', description: 'High certainty', autoFix: 'remove', severity: 'high' },
{ file: 'b.js', line: 2, certainty: 'MEDIUM', description: 'Medium certainty', autoFix: 'flag', severity: 'medium' },
{ file: 'c.js', line: 3, certainty: 'LOW', description: 'Low certainty', autoFix: 'flag', severity: 'low' }
];
const prompt = formatHandoffPrompt(findings, 'report');
expect(prompt).toContain('HIGH Certainty');
expect(prompt).toContain('MEDIUM Certainty');
expect(prompt).toContain('LOW Certainty');
});
it('should include action guidance for apply mode', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', description: 'Test', autoFix: 'remove', severity: 'high' }
];
const prompt = formatHandoffPrompt(findings, 'apply');
expect(prompt).toContain('Apply fixes directly');
});
it('should include action summary', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', description: 'Test1', autoFix: 'remove', severity: 'high' },
{ file: 'b.js', line: 2, certainty: 'HIGH', description: 'Test2', autoFix: 'flag', severity: 'medium' }
];
const prompt = formatHandoffPrompt(findings, 'report');
expect(prompt).toContain('Auto-fixable: 1');
expect(prompt).toContain('Needs manual review: 1');
});
it('should group findings by file', () => {
const findings = [
{ file: 'app.js', line: 1, certainty: 'HIGH', description: 'Issue 1', autoFix: 'remove', severity: 'high' },
{ file: 'app.js', line: 5, certainty: 'HIGH', description: 'Issue 2', autoFix: 'remove', severity: 'high' },
{ file: 'utils.js', line: 3, certainty: 'HIGH', description: 'Issue 3', autoFix: 'flag', severity: 'medium' }
];
const prompt = formatHandoffPrompt(findings, 'report');
expect(prompt).toContain('**app.js**');
expect(prompt).toContain('**utils.js**');
expect(prompt).toContain('L1:');
expect(prompt).toContain('L5:');
});
it('should include autoFix tags for fixable issues', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', description: 'Test', autoFix: 'remove', severity: 'high' }
];
const prompt = formatHandoffPrompt(findings, 'report');
expect(prompt).toContain('[remove]');
});
it('should not include autoFix tags for flag/none', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', description: 'Test', autoFix: 'flag', severity: 'high' }
];
const prompt = formatHandoffPrompt(findings, 'report');
expect(prompt).not.toContain('[flag]');
});
it('should use compact format when option is set', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', patternName: 'console_debugging', autoFix: 'remove', severity: 'high' },
{ file: 'b.js', line: 2, certainty: 'MEDIUM', patternName: 'old_todos', autoFix: 'flag', severity: 'medium' }
];
const prompt = formatHandoffPrompt(findings, 'report', { compact: true });
// Compact format uses table structure
expect(prompt).toContain('|File|L|Pattern|Cert|Fix|');
expect(prompt).toContain('|---|---|---|---|---|');
// Should use abbreviated certainty
expect(prompt).toContain('|H|');
expect(prompt).toContain('|M|');
});
});
describe('formatCompactPrompt', () => {
it('should format findings in table structure', () => {
const findings = [
{ file: 'app.js', line: 42, certainty: 'HIGH', patternName: 'console_debugging', autoFix: 'remove' }
];
const prompt = formatCompactPrompt(findings, 'report', 50);
expect(prompt).toContain('|File|L|Pattern|Cert|Fix|');
expect(prompt).toContain('|---|---|---|---|---|');
expect(prompt).toContain('|app.js|42|console_debugging|H|remove|');
});
it('should show certainty counts in header', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', patternName: 'console_debugging', autoFix: 'remove' },
{ file: 'b.js', line: 2, certainty: 'HIGH', patternName: 'debug_import', autoFix: 'remove' },
{ file: 'c.js', line: 3, certainty: 'MEDIUM', patternName: 'old_todos', autoFix: 'flag' },
{ file: 'd.js', line: 4, certainty: 'LOW', patternName: 'magic_numbers', autoFix: 'flag' }
];
const prompt = formatCompactPrompt(findings, 'apply', 50);
expect(prompt).toContain('## Slop: apply|H:2|M:1|L:1');
});
it('should abbreviate certainty levels', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', patternName: 'test', autoFix: 'remove' },
{ file: 'b.js', line: 2, certainty: 'MEDIUM', patternName: 'test', autoFix: 'flag' },
{ file: 'c.js', line: 3, certainty: 'LOW', patternName: 'test', autoFix: 'none' }
];
const prompt = formatCompactPrompt(findings, 'report', 50);
// Should use H, M, L abbreviations in the Cert column
expect(prompt).toMatch(/\|a\.js\|1\|test\|H\|/);
expect(prompt).toMatch(/\|b\.js\|2\|test\|M\|/);
expect(prompt).toMatch(/\|c\.js\|3\|test\|L\|/);
});
it('should show dash for non-fixable patterns', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', patternName: 'test', autoFix: 'flag' },
{ file: 'b.js', line: 2, certainty: 'MEDIUM', patternName: 'test', autoFix: 'none' },
{ file: 'c.js', line: 3, certainty: 'LOW', patternName: 'test', autoFix: null }
];
const prompt = formatCompactPrompt(findings, 'report', 50);
// Non-fixable should show '-' in Fix column
expect(prompt).toContain('|a.js|1|test|H|-|');
expect(prompt).toContain('|b.js|2|test|M|-|');
expect(prompt).toContain('|c.js|3|test|L|-|');
});
it('should truncate findings when exceeding maxFindings', () => {
const findings = [];
for (let i = 1; i <= 10; i++) {
findings.push({
file: `file${i}.js`,
line: i,
certainty: 'HIGH',
patternName: 'console_debugging',
autoFix: 'remove'
});
}
const prompt = formatCompactPrompt(findings, 'report', 5);
// Should only have 5 rows plus truncation message
expect(prompt).toContain('file1.js');
expect(prompt).toContain('file5.js');
expect(prompt).not.toContain('file6.js');
expect(prompt).toContain('+5 more findings (truncated)');
});
it('should include auto-fixable summary', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', patternName: 'console_debugging', autoFix: 'remove' },
{ file: 'b.js', line: 2, certainty: 'HIGH', patternName: 'debug_import', autoFix: 'remove' },
{ file: 'c.js', line: 3, certainty: 'MEDIUM', patternName: 'old_todos', autoFix: 'flag' }
];
const prompt = formatCompactPrompt(findings, 'report', 50);
expect(prompt).toContain('**Auto-fixable: 2**');
expect(prompt).toContain('Manual: 1');
});
it('should handle empty findings', () => {
const prompt = formatCompactPrompt([], 'report', 50);
expect(prompt).toContain('## Slop: report|H:0|M:0|L:0');
expect(prompt).toContain('**Auto-fixable: 0**');
});
it('should not show truncation message when under limit', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', patternName: 'test', autoFix: 'remove' }
];
const prompt = formatCompactPrompt(findings, 'report', 50);
expect(prompt).not.toContain('truncated');
});
it('should include mode in header', () => {
const findings = [
{ file: 'a.js', line: 1, certainty: 'HIGH', patternName: 'test', autoFix: 'remove' }
];
const reportPrompt = formatCompactPrompt(findings, 'report', 50);
const applyPrompt = formatCompactPrompt(findings, 'apply', 50);
expect(reportPrompt).toContain('## Slop: report|');
expect(applyPrompt).toContain('## Slop: apply|');
});
});
describe('runPipeline', () => {
it('should return correct structure', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.js'),
'console.log("test");\n'
);
const result = runPipeline(tmpDir, {
thoroughness: 'quick',
mode: 'report'
});
expect(result).toHaveProperty('findings');
expect(result).toHaveProperty('summary');
expect(result).toHaveProperty('phase3Prompt');
expect(result).toHaveProperty('missingTools');
expect(result).toHaveProperty('metadata');
});
it('should include metadata', () => {
const result = runPipeline(tmpDir, {
thoroughness: 'quick',
mode: 'report'
});
expect(result.metadata.repoPath).toBe(tmpDir);
expect(result.metadata.thoroughness).toBe('quick');
expect(result.metadata.mode).toBe('report');
expect(result.metadata.timestamp).toBeDefined();
});
it('should detect findings in quick mode', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.js'),
'function test() {\n console.log("debug");\n}'
);
const result = runPipeline(tmpDir, {
thoroughness: 'quick',
targetFiles: ['app.js']
});
expect(result.findings.length).toBeGreaterThan(0);
// Quick mode only runs Phase 1
expect(result.findings.every(f => f.phase === 1)).toBe(true);
});
it('should run multi-pass analyzers in normal mode', () => {
const code = `
/**
* Excessive docs
* Line 1
* Line 2
* Line 3
* Line 4
* Line 5
* Line 6
* Line 7
* Line 8
*/
function foo() {
return 1;
}`;
fs.writeFileSync(path.join(tmpDir, 'test.js'), code);
const result = runPipeline(tmpDir, {
thoroughness: 'normal',
targetFiles: ['test.js']
});
// Normal mode includes multi-pass analyzers (may or may not have findings depending on thresholds)
expect(result.findings).toBeDefined();
expect(result.metadata.thoroughness).toBe('normal');
});
it('should track missing tools in deep mode', () => {
const result = runPipeline(tmpDir, {
thoroughness: 'deep',
cliTools: { jscpd: false, madge: false, escomplex: false }
});
expect(result.missingTools).toContain('jscpd');
expect(result.missingTools).toContain('madge');
expect(result.missingTools).toContain('escomplex');
});
it('should use default options', () => {
const result = runPipeline(tmpDir);
expect(result.metadata.thoroughness).toBe('normal');
expect(result.metadata.mode).toBe('report');
});
it('should filter by specific files', () => {
fs.writeFileSync(path.join(tmpDir, 'a.js'), 'console.log("a");\n');
fs.writeFileSync(path.join(tmpDir, 'b.js'), 'console.log("b");\n');
const result = runPipeline(tmpDir, {
thoroughness: 'quick',
targetFiles: ['a.js']
});
const filesWithFindings = [...new Set(result.findings.map(f => f.file))];
expect(filesWithFindings).toContain('a.js');
expect(filesWithFindings).not.toContain('b.js');
});
it('should generate phase3Prompt with findings', () => {
fs.writeFileSync(
path.join(tmpDir, 'app.js'),
'console.log("debug");\n'
);
const result = runPipeline(tmpDir, {
thoroughness: 'quick',
targetFiles: ['app.js'],
mode: 'apply'
});
expect(result.phase3Prompt).toContain('Mode: **apply**');
expect(result.phase3Prompt).toContain('HIGH Certainty');
});
});
describe('mode inheritance', () => {
it('should pass mode to handoff prompt', () => {
fs.writeFileSync(path.join(tmpDir, 'a.js'), 'console.log("test");');
const reportResult = runPipeline(tmpDir, {
thoroughness: 'quick',
targetFiles: ['a.js'],
mode: 'report'
});
const applyResult = runPipeline(tmpDir, {
thoroughness: 'quick',
targetFiles: ['a.js'],
mode: 'apply'
});
expect(reportResult.phase3Prompt).toContain('Mode: **report**');
expect(applyResult.phase3Prompt).toContain('Mode: **apply**');
});
});
describe('certainty tagging', () => {
it('should tag Phase 1 regex matches as HIGH certainty', () => {
fs.writeFileSync(path.join(tmpDir, 'a.js'), 'console.log("test");');
const result = runPipeline(tmpDir, {
thoroughness: 'quick',
targetFiles: ['a.js']
});
const phase1Findings = result.findings.filter(f => f.phase === 1);
expect(phase1Findings.every(f => f.certainty === CERTAINTY.HIGH)).toBe(true);
});
it('should tag multi-pass findings as MEDIUM certainty', () => {
const code = `
/**
* Excessive documentation
* Line 1
* Line 2
* Line 3
* Line 4
* Line 5
* Line 6
* Line 7
* Line 8
* Line 9
* Line 10
* Line 11
* Line 12
*/
function foo() {
return 1;
}`;
fs.writeFileSync(path.join(tmpDir, 'test.js'), code);
const result = runPipeline(tmpDir, {
thoroughness: 'normal',
targetFiles: ['test.js']
});
const docRatioFindings = result.findings.filter(f => f.patternName === 'doc_code_ratio_js');
if (docRatioFindings.length > 0) {
expect(docRatioFindings[0].certainty).toBe(CERTAINTY.MEDIUM);
}
});
});
describe('thoroughness levels', () => {
it('quick mode should only run Phase 1 regex', () => {
fs.writeFileSync(path.join(tmpDir, 'a.js'), 'console.log("test");');
const result = runPipeline(tmpDir, {
thoroughness: 'quick',
targetFiles: ['a.js']
});
// All findings should be phase 1 with HIGH certainty
expect(result.findings.every(f => f.phase === 1)).toBe(true);
expect(result.findings.every(f => f.certainty === CERTAINTY.HIGH)).toBe(true);
});
it('normal mode should include multi-pass analyzers', () => {
// Create file that will trigger multi-pass analysis
const code = `
/**
* Excessive docs
* Line 1
* Line 2
* Line 3
* Line 4
* Line 5
* Line 6
* Line 7
* Line 8
* Line 9
* Line 10
*/
function foo() {
return 1;
}`;
fs.writeFileSync(path.join(tmpDir, 'test.js'), code);
const result = runPipeline(tmpDir, {
thoroughness: 'normal',
targetFiles: ['test.js']
});
// Should run multi-pass analyzers which may produce MEDIUM certainty findings
// The doc_code_ratio analyzer may detect the excessive JSDoc if it meets thresholds
expect(result.findings).toBeDefined();
expect(result.metadata.thoroughness).toBe('normal');
});
it('deep mode should track missing CLI tools', () => {
const result = runPipeline(tmpDir, {
thoroughness: 'deep',
cliTools: { jscpd: false, madge: false, escomplex: false }
});
expect(result.missingTools.length).toBe(3);
});
});
});