@@ -11,6 +11,13 @@ import { registerSarifTools } from '../../../src/tools/sarif-tools';
1111import { sessionDataManager } from '../../../src/lib/session-data-manager' ;
1212import { createProjectTempDir } from '../../../src/utils/temp-dir' ;
1313
14+ // Module-scope mock for cli-executor so the dynamic import in the handler
15+ // always resolves to the same controllable mock (prevents module-cache flakiness).
16+ const mockExecuteCLICommand = vi . fn ( ) ;
17+ vi . mock ( '../../../src/lib/cli-executor' , ( ) => ( {
18+ executeCLICommand : mockExecuteCLICommand ,
19+ } ) ) ;
20+
1421// ---------------------------------------------------------------------------
1522// Test fixtures
1623// ---------------------------------------------------------------------------
@@ -507,20 +514,17 @@ describe('SARIF Tools', () => {
507514
508515 describe ( 'sarif_diff_by_commits' , ( ) => {
509516 it ( 'should classify results as new when their files appear in git diff' , async ( ) => {
510- // Mock executeCLICommand to return a simulated git diff output
511- vi . doMock ( '../../../src/lib/cli-executor' , ( ) => ( {
512- executeCLICommand : vi . fn ( ) . mockResolvedValue ( {
513- success : true ,
514- stdout : [
515- 'diff --git a/src/db.js b/src/db.js' ,
516- '--- a/src/db.js' ,
517- '+++ b/src/db.js' ,
518- '@@ -40,5 +40,5 @@' ,
519- ' some context' ,
520- ] . join ( '\n' ) ,
521- stderr : '' ,
522- } ) ,
523- } ) ) ;
517+ mockExecuteCLICommand . mockResolvedValue ( {
518+ success : true ,
519+ stdout : [
520+ 'diff --git a/src/db.js b/src/db.js' ,
521+ '--- a/src/db.js' ,
522+ '+++ b/src/db.js' ,
523+ '@@ -40,5 +40,5 @@' ,
524+ ' some context' ,
525+ ] . join ( '\n' ) ,
526+ stderr : '' ,
527+ } ) ;
524528
525529 const result = await handlers . sarif_diff_by_commits ( {
526530 sarifPath : testSarifPath ,
@@ -538,18 +542,16 @@ describe('SARIF Tools', () => {
538542 } ) ;
539543
540544 it ( 'should classify all results as pre-existing when diff has no matching files' , async ( ) => {
541- vi . doMock ( '../../../src/lib/cli-executor' , ( ) => ( {
542- executeCLICommand : vi . fn ( ) . mockResolvedValue ( {
543- success : true ,
544- stdout : [
545- 'diff --git a/unrelated.txt b/unrelated.txt' ,
546- '--- a/unrelated.txt' ,
547- '+++ b/unrelated.txt' ,
548- '@@ -1,1 +1,1 @@' ,
549- ] . join ( '\n' ) ,
550- stderr : '' ,
551- } ) ,
552- } ) ) ;
545+ mockExecuteCLICommand . mockResolvedValue ( {
546+ success : true ,
547+ stdout : [
548+ 'diff --git a/unrelated.txt b/unrelated.txt' ,
549+ '--- a/unrelated.txt' ,
550+ '+++ b/unrelated.txt' ,
551+ '@@ -1,1 +1,1 @@' ,
552+ ] . join ( '\n' ) ,
553+ stderr : '' ,
554+ } ) ;
553555
554556 const result = await handlers . sarif_diff_by_commits ( {
555557 sarifPath : testSarifPath ,
@@ -569,14 +571,12 @@ describe('SARIF Tools', () => {
569571 } ) ;
570572
571573 it ( 'should return error when git diff fails' , async ( ) => {
572- vi . doMock ( '../../../src/lib/cli-executor' , ( ) => ( {
573- executeCLICommand : vi . fn ( ) . mockResolvedValue ( {
574- success : false ,
575- stdout : '' ,
576- stderr : 'fatal: bad revision' ,
577- error : 'fatal: bad revision' ,
578- } ) ,
579- } ) ) ;
574+ mockExecuteCLICommand . mockResolvedValue ( {
575+ success : false ,
576+ stdout : '' ,
577+ stderr : 'fatal: bad revision' ,
578+ error : 'fatal: bad revision' ,
579+ } ) ;
580580
581581 const result = await handlers . sarif_diff_by_commits ( {
582582 sarifPath : testSarifPath ,
@@ -586,18 +586,16 @@ describe('SARIF Tools', () => {
586586 } ) ;
587587
588588 it ( 'should support line-level granularity' , async ( ) => {
589- vi . doMock ( '../../../src/lib/cli-executor' , ( ) => ( {
590- executeCLICommand : vi . fn ( ) . mockResolvedValue ( {
591- success : true ,
592- stdout : [
593- 'diff --git a/src/db.js b/src/db.js' ,
594- '--- a/src/db.js' ,
595- '+++ b/src/db.js' ,
596- '@@ -42,1 +42,1 @@' ,
597- ] . join ( '\n' ) ,
598- stderr : '' ,
599- } ) ,
600- } ) ) ;
589+ mockExecuteCLICommand . mockResolvedValue ( {
590+ success : true ,
591+ stdout : [
592+ 'diff --git a/src/db.js b/src/db.js' ,
593+ '--- a/src/db.js' ,
594+ '+++ b/src/db.js' ,
595+ '@@ -42,1 +42,1 @@' ,
596+ ] . join ( '\n' ) ,
597+ stderr : '' ,
598+ } ) ;
601599
602600 const result = await handlers . sarif_diff_by_commits ( {
603601 sarifPath : testSarifPath ,
@@ -612,6 +610,22 @@ describe('SARIF Tools', () => {
612610 expect ( newInDb ) . toHaveLength ( 1 ) ;
613611 expect ( newInDb [ 0 ] . line ) . toBe ( 42 ) ;
614612 } ) ;
613+
614+ it ( 'should return error for refRange starting with a dash' , async ( ) => {
615+ const result = await handlers . sarif_diff_by_commits ( {
616+ sarifPath : testSarifPath ,
617+ refRange : '--option-injection' ,
618+ } ) ;
619+ expect ( result . content [ 0 ] . text ) . toContain ( 'Invalid refRange' ) ;
620+ } ) ;
621+
622+ it ( 'should return error for refRange containing whitespace' , async ( ) => {
623+ const result = await handlers . sarif_diff_by_commits ( {
624+ sarifPath : testSarifPath ,
625+ refRange : 'main HEAD' ,
626+ } ) ;
627+ expect ( result . content [ 0 ] . text ) . toContain ( 'Invalid refRange' ) ;
628+ } ) ;
615629 } ) ;
616630 } ) ;
617631} ) ;
0 commit comments