@@ -21,6 +21,25 @@ describe('securityIssueHoverProvider', () => {
2121 token = new vscode . CancellationTokenSource ( )
2222 } )
2323
24+ function buildCommandLink ( command : string , args : any [ ] , label : string , tooltip : string ) : string {
25+ return `[$(${ command . includes ( 'comment' ) ? 'comment' : 'error' } ) ${ label } ](command:${ command } ?${ encodeURIComponent ( JSON . stringify ( args ) ) } '${ tooltip } ')`
26+ }
27+
28+ function buildExpectedContent ( issue : any , fileName : string , description : string , severity ?: string ) : string {
29+ const severityBadge = severity ? `  } .svg)` : ' '
30+ const commands = [
31+ buildCommandLink ( 'aws.amazonq.explainIssue' , [ issue , fileName ] , 'Explain' , 'Explain with Amazon Q' ) ,
32+ buildCommandLink ( 'aws.amazonq.generateFix' , [ issue , fileName ] , 'Fix' , 'Fix with Amazon Q' ) ,
33+ buildCommandLink ( 'aws.amazonq.security.ignore' , [ issue , fileName , 'hover' ] , 'Ignore' , 'Ignore Issue' ) ,
34+ buildCommandLink ( 'aws.amazonq.security.ignoreAll' , [ issue , 'hover' ] , 'Ignore All' , 'Ignore Similar Issues' ) ,
35+ ]
36+ return `## title${ severityBadge } \n${ description } \n\n${ commands . join ( '\n | ' ) } \n`
37+ }
38+
39+ function setupIssues ( issues : any [ ] ) : void {
40+ securityIssueProvider . issues = [ { filePath : mockDocument . fileName , issues } ]
41+ }
42+
2443 it ( 'should return hover for each issue for the current position' , ( ) => {
2544 const issues = [
2645 createCodeScanIssue ( { findingId : 'finding-1' , detectorId : 'language/detector-1' , ruleId : 'Rule-123' } ) ,
@@ -32,49 +51,17 @@ describe('securityIssueHoverProvider', () => {
3251 } ) ,
3352 ]
3453
35- securityIssueProvider . issues = [
36- {
37- filePath : mockDocument . fileName ,
38- issues,
39- } ,
40- ]
41-
54+ setupIssues ( issues )
4255 const actual = securityIssueHoverProvider . provideHover ( mockDocument , new vscode . Position ( 0 , 0 ) , token . token )
4356
4457 assert . strictEqual ( actual . contents . length , 2 )
4558 assert . strictEqual (
4659 ( actual . contents [ 0 ] as vscode . MarkdownString ) . value ,
47- '## title \n' +
48- 'fix\n\n' +
49- `[$(comment) Explain](command:aws.amazonq.explainIssue?${ encodeURIComponent (
50- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName ] )
51- ) } 'Explain with Amazon Q')\n` +
52- ` | [$(comment) Fix](command:aws.amazonq.generateFix?${ encodeURIComponent (
53- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName ] )
54- ) } 'Fix with Amazon Q')\n` +
55- ` | [$(error) Ignore](command:aws.amazonq.security.ignore?${ encodeURIComponent (
56- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName , 'hover' ] )
57- ) } 'Ignore Issue')\n` +
58- ` | [$(error) Ignore All](command:aws.amazonq.security.ignoreAll?${ encodeURIComponent (
59- JSON . stringify ( [ issues [ 0 ] , 'hover' ] )
60- ) } 'Ignore Similar Issues')\n`
60+ buildExpectedContent ( issues [ 0 ] , mockDocument . fileName , 'fix' , 'High' )
6161 )
6262 assert . strictEqual (
6363 ( actual . contents [ 1 ] as vscode . MarkdownString ) . value ,
64- '## title \n' +
65- 'recommendationText\n\n' +
66- `[$(comment) Explain](command:aws.amazonq.explainIssue?${ encodeURIComponent (
67- JSON . stringify ( [ issues [ 1 ] , mockDocument . fileName ] )
68- ) } 'Explain with Amazon Q')\n` +
69- ` | [$(comment) Fix](command:aws.amazonq.generateFix?${ encodeURIComponent (
70- JSON . stringify ( [ issues [ 1 ] , mockDocument . fileName ] )
71- ) } 'Fix with Amazon Q')\n` +
72- ` | [$(error) Ignore](command:aws.amazonq.security.ignore?${ encodeURIComponent (
73- JSON . stringify ( [ issues [ 1 ] , mockDocument . fileName , 'hover' ] )
74- ) } 'Ignore Issue')\n` +
75- ` | [$(error) Ignore All](command:aws.amazonq.security.ignoreAll?${ encodeURIComponent (
76- JSON . stringify ( [ issues [ 1 ] , 'hover' ] )
77- ) } 'Ignore Similar Issues')\n`
64+ buildExpectedContent ( issues [ 1 ] , mockDocument . fileName , 'recommendationText' , 'High' )
7865 )
7966 assertTelemetry ( 'codewhisperer_codeScanIssueHover' , [
8067 { findingId : 'finding-1' , detectorId : 'language/detector-1' , ruleId : 'Rule-123' , includesFix : true } ,
@@ -83,58 +70,28 @@ describe('securityIssueHoverProvider', () => {
8370 } )
8471
8572 it ( 'should return empty contents if there is no issue on the current position' , ( ) => {
86- securityIssueProvider . issues = [
87- {
88- filePath : mockDocument . fileName ,
89- issues : [ createCodeScanIssue ( ) ] ,
90- } ,
91- ]
92-
73+ setupIssues ( [ createCodeScanIssue ( ) ] )
9374 const actual = securityIssueHoverProvider . provideHover ( mockDocument , new vscode . Position ( 2 , 0 ) , token . token )
9475 assert . strictEqual ( actual . contents . length , 0 )
9576 } )
9677
9778 it ( 'should skip issues not in the current file' , ( ) => {
9879 securityIssueProvider . issues = [
99- {
100- filePath : 'some/path' ,
101- issues : [ createCodeScanIssue ( ) ] ,
102- } ,
103- {
104- filePath : mockDocument . fileName ,
105- issues : [ createCodeScanIssue ( ) ] ,
106- } ,
80+ { filePath : 'some/path' , issues : [ createCodeScanIssue ( ) ] } ,
81+ { filePath : mockDocument . fileName , issues : [ createCodeScanIssue ( ) ] } ,
10782 ]
10883 const actual = securityIssueHoverProvider . provideHover ( mockDocument , new vscode . Position ( 0 , 0 ) , token . token )
10984 assert . strictEqual ( actual . contents . length , 1 )
11085 } )
11186
11287 it ( 'should not show severity badge if undefined' , ( ) => {
11388 const issues = [ createCodeScanIssue ( { severity : undefined , suggestedFixes : [ ] } ) ]
114- securityIssueProvider . issues = [
115- {
116- filePath : mockDocument . fileName ,
117- issues,
118- } ,
119- ]
89+ setupIssues ( issues )
12090 const actual = securityIssueHoverProvider . provideHover ( mockDocument , new vscode . Position ( 0 , 0 ) , token . token )
12191 assert . strictEqual ( actual . contents . length , 1 )
12292 assert . strictEqual (
12393 ( actual . contents [ 0 ] as vscode . MarkdownString ) . value ,
124- '## title \n' +
125- 'recommendationText\n\n' +
126- `[$(comment) Explain](command:aws.amazonq.explainIssue?${ encodeURIComponent (
127- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName ] )
128- ) } 'Explain with Amazon Q')\n` +
129- ` | [$(comment) Fix](command:aws.amazonq.generateFix?${ encodeURIComponent (
130- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName ] )
131- ) } 'Fix with Amazon Q')\n` +
132- ` | [$(error) Ignore](command:aws.amazonq.security.ignore?${ encodeURIComponent (
133- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName , 'hover' ] )
134- ) } 'Ignore Issue')\n` +
135- ` | [$(error) Ignore All](command:aws.amazonq.security.ignoreAll?${ encodeURIComponent (
136- JSON . stringify ( [ issues [ 0 ] , 'hover' ] )
137- ) } 'Ignore Similar Issues')\n`
94+ buildExpectedContent ( issues [ 0 ] , mockDocument . fileName , 'recommendationText' )
13895 )
13996 } )
14097
@@ -149,41 +106,17 @@ describe('securityIssueHoverProvider', () => {
149106 ] ,
150107 } ) ,
151108 ]
152- securityIssueProvider . issues = [
153- {
154- filePath : mockDocument . fileName ,
155- issues,
156- } ,
157- ]
109+ setupIssues ( issues )
158110 const actual = securityIssueHoverProvider . provideHover ( mockDocument , new vscode . Position ( 0 , 0 ) , token . token )
159111 assert . strictEqual ( actual . contents . length , 1 )
160112 assert . strictEqual (
161113 ( actual . contents [ 0 ] as vscode . MarkdownString ) . value ,
162- '## title \n' +
163- 'fix\n\n' +
164- `[$(comment) Explain](command:aws.amazonq.explainIssue?${ encodeURIComponent (
165- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName ] )
166- ) } 'Explain with Amazon Q')\n` +
167- ` | [$(comment) Fix](command:aws.amazonq.generateFix?${ encodeURIComponent (
168- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName ] )
169- ) } 'Fix with Amazon Q')\n` +
170- ` | [$(error) Ignore](command:aws.amazonq.security.ignore?${ encodeURIComponent (
171- JSON . stringify ( [ issues [ 0 ] , mockDocument . fileName , 'hover' ] )
172- ) } 'Ignore Issue')\n` +
173- ` | [$(error) Ignore All](command:aws.amazonq.security.ignoreAll?${ encodeURIComponent (
174- JSON . stringify ( [ issues [ 0 ] , 'hover' ] )
175- ) } 'Ignore Similar Issues')\n`
114+ buildExpectedContent ( issues [ 0 ] , mockDocument . fileName , 'fix' , 'High' )
176115 )
177116 } )
178117
179118 it ( 'should not show issues that are not visible' , ( ) => {
180- const issues = [ createCodeScanIssue ( { visible : false } ) ]
181- securityIssueProvider . issues = [
182- {
183- filePath : mockDocument . fileName ,
184- issues,
185- } ,
186- ]
119+ setupIssues ( [ createCodeScanIssue ( { visible : false } ) ] )
187120 const actual = securityIssueHoverProvider . provideHover ( mockDocument , new vscode . Position ( 0 , 0 ) , token . token )
188121 assert . strictEqual ( actual . contents . length , 0 )
189122 } )
0 commit comments