|
| 1 | +import { readFileSync, unlinkSync } from 'node:fs'; |
| 2 | +import { exportScorecardResultsToJson } from '../formatters/json-formatter.js'; |
| 3 | +import type { ScorecardProblem } from '../types.js'; |
| 4 | +import type { ScorecardJsonOutput } from '../formatters/json-formatter.js'; |
| 5 | + |
| 6 | +const createMockSource = (absoluteRef: string) => ({ |
| 7 | + absoluteRef, |
| 8 | + getAst: () => ({}), |
| 9 | + getRootAst: () => ({}), |
| 10 | + getLineColLocation: () => ({ line: 1, col: 1 }), |
| 11 | +}); |
| 12 | + |
| 13 | +describe('exportScorecardResultsToJson', () => { |
| 14 | + const testOutputPath = './test-scorecard-output.json'; |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + try { |
| 18 | + unlinkSync(testOutputPath); |
| 19 | + } catch { |
| 20 | + // File might not exist |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + it('should export empty results when no problems', () => { |
| 25 | + exportScorecardResultsToJson([], testOutputPath); |
| 26 | + |
| 27 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 28 | + |
| 29 | + expect(output).toEqual({}); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should group problems by scorecard level', () => { |
| 33 | + const problems: ScorecardProblem[] = [ |
| 34 | + { |
| 35 | + message: 'Error in Gold level', |
| 36 | + ruleId: 'test-rule-1', |
| 37 | + severity: 'error', |
| 38 | + suggest: [], |
| 39 | + location: [ |
| 40 | + { |
| 41 | + source: createMockSource('/test/file.yaml') as any, |
| 42 | + pointer: '#/paths/~1test/get', |
| 43 | + reportOnKey: false, |
| 44 | + }, |
| 45 | + ], |
| 46 | + scorecardLevel: 'Gold', |
| 47 | + }, |
| 48 | + { |
| 49 | + message: 'Warning in Gold level', |
| 50 | + ruleId: 'test-rule-2', |
| 51 | + severity: 'warn', |
| 52 | + suggest: [], |
| 53 | + location: [ |
| 54 | + { |
| 55 | + source: createMockSource('/test/file.yaml') as any, |
| 56 | + pointer: '#/info', |
| 57 | + reportOnKey: false, |
| 58 | + }, |
| 59 | + ], |
| 60 | + scorecardLevel: 'Gold', |
| 61 | + }, |
| 62 | + { |
| 63 | + message: 'Error in Silver level', |
| 64 | + ruleId: 'test-rule-3', |
| 65 | + severity: 'error', |
| 66 | + suggest: [], |
| 67 | + location: [], |
| 68 | + scorecardLevel: 'Silver', |
| 69 | + }, |
| 70 | + ]; |
| 71 | + |
| 72 | + exportScorecardResultsToJson(problems, testOutputPath); |
| 73 | + |
| 74 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 75 | + |
| 76 | + expect(Object.keys(output)).toEqual(['Gold', 'Silver']); |
| 77 | + expect(output.Gold.summary).toEqual({ errors: 1, warnings: 1 }); |
| 78 | + expect(output.Gold.problems).toHaveLength(2); |
| 79 | + expect(output.Silver.summary).toEqual({ errors: 1, warnings: 0 }); |
| 80 | + expect(output.Silver.problems).toHaveLength(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should include rule URLs for non-namespaced rules', () => { |
| 84 | + const problems: ScorecardProblem[] = [ |
| 85 | + { |
| 86 | + message: 'Test error', |
| 87 | + ruleId: 'operation-summary', |
| 88 | + severity: 'error', |
| 89 | + suggest: [], |
| 90 | + location: [], |
| 91 | + scorecardLevel: 'Gold', |
| 92 | + }, |
| 93 | + ]; |
| 94 | + |
| 95 | + exportScorecardResultsToJson(problems, testOutputPath); |
| 96 | + |
| 97 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 98 | + |
| 99 | + expect(output.Gold.problems[0].ruleUrl).toBe( |
| 100 | + 'https://redocly.com/docs/cli/rules/oas/operation-summary.md' |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not include rule URLs for namespaced rules', () => { |
| 105 | + const problems: ScorecardProblem[] = [ |
| 106 | + { |
| 107 | + message: 'Test error', |
| 108 | + ruleId: 'custom/my-rule', |
| 109 | + severity: 'error', |
| 110 | + suggest: [], |
| 111 | + location: [], |
| 112 | + scorecardLevel: 'Gold', |
| 113 | + }, |
| 114 | + ]; |
| 115 | + |
| 116 | + exportScorecardResultsToJson(problems, testOutputPath); |
| 117 | + |
| 118 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 119 | + |
| 120 | + expect(output.Gold.problems[0].ruleUrl).toBeUndefined(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should format location with file path and range', () => { |
| 124 | + const problems: ScorecardProblem[] = [ |
| 125 | + { |
| 126 | + message: 'Test error', |
| 127 | + ruleId: 'test-rule', |
| 128 | + severity: 'error', |
| 129 | + suggest: [], |
| 130 | + location: [ |
| 131 | + { |
| 132 | + source: createMockSource('/test/file.yaml') as any, |
| 133 | + pointer: '#/paths/~1test/get', |
| 134 | + reportOnKey: false, |
| 135 | + }, |
| 136 | + ], |
| 137 | + scorecardLevel: 'Gold', |
| 138 | + }, |
| 139 | + ]; |
| 140 | + |
| 141 | + exportScorecardResultsToJson(problems, testOutputPath); |
| 142 | + |
| 143 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 144 | + |
| 145 | + expect(output.Gold.problems[0].location).toHaveLength(1); |
| 146 | + expect(output.Gold.problems[0].location[0].file).toBe('/test/file.yaml'); |
| 147 | + expect(output.Gold.problems[0].location[0].pointer).toBe('#/paths/~1test/get'); |
| 148 | + expect(output.Gold.problems[0].location[0].range).toContain('Line'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should handle problems with Unknown level', () => { |
| 152 | + const problems: ScorecardProblem[] = [ |
| 153 | + { |
| 154 | + message: 'Error without level', |
| 155 | + ruleId: 'test-rule', |
| 156 | + severity: 'error', |
| 157 | + suggest: [], |
| 158 | + location: [], |
| 159 | + scorecardLevel: undefined, |
| 160 | + }, |
| 161 | + ]; |
| 162 | + |
| 163 | + exportScorecardResultsToJson(problems, testOutputPath); |
| 164 | + |
| 165 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 166 | + |
| 167 | + expect(Object.keys(output)).toEqual(['Unknown']); |
| 168 | + expect(output.Unknown.problems).toHaveLength(1); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should strip ANSI codes from messages', () => { |
| 172 | + const problems: ScorecardProblem[] = [ |
| 173 | + { |
| 174 | + message: '\u001b[31mError message with color\u001b[0m', |
| 175 | + ruleId: 'test-rule', |
| 176 | + severity: 'error', |
| 177 | + suggest: [], |
| 178 | + location: [], |
| 179 | + scorecardLevel: 'Gold', |
| 180 | + }, |
| 181 | + ]; |
| 182 | + |
| 183 | + exportScorecardResultsToJson(problems, testOutputPath); |
| 184 | + |
| 185 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 186 | + |
| 187 | + expect(output.Gold.problems[0].message).toBe('Error message with color'); |
| 188 | + expect(output.Gold.problems[0].message).not.toContain('\u001b'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should count errors and warnings correctly', () => { |
| 192 | + const problems: ScorecardProblem[] = [ |
| 193 | + { |
| 194 | + message: 'Error 1', |
| 195 | + ruleId: 'rule-1', |
| 196 | + severity: 'error', |
| 197 | + suggest: [], |
| 198 | + location: [], |
| 199 | + scorecardLevel: 'Gold', |
| 200 | + }, |
| 201 | + { |
| 202 | + message: 'Error 2', |
| 203 | + ruleId: 'rule-2', |
| 204 | + severity: 'error', |
| 205 | + suggest: [], |
| 206 | + location: [], |
| 207 | + scorecardLevel: 'Gold', |
| 208 | + }, |
| 209 | + { |
| 210 | + message: 'Warning 1', |
| 211 | + ruleId: 'rule-3', |
| 212 | + severity: 'warn', |
| 213 | + suggest: [], |
| 214 | + location: [], |
| 215 | + scorecardLevel: 'Gold', |
| 216 | + }, |
| 217 | + { |
| 218 | + message: 'Warning 2', |
| 219 | + ruleId: 'rule-4', |
| 220 | + severity: 'warn', |
| 221 | + suggest: [], |
| 222 | + location: [], |
| 223 | + scorecardLevel: 'Gold', |
| 224 | + }, |
| 225 | + { |
| 226 | + message: 'Warning 3', |
| 227 | + ruleId: 'rule-5', |
| 228 | + severity: 'warn', |
| 229 | + suggest: [], |
| 230 | + location: [], |
| 231 | + scorecardLevel: 'Gold', |
| 232 | + }, |
| 233 | + ]; |
| 234 | + |
| 235 | + exportScorecardResultsToJson(problems, testOutputPath); |
| 236 | + |
| 237 | + const output = JSON.parse(readFileSync(testOutputPath, 'utf-8')) as ScorecardJsonOutput; |
| 238 | + |
| 239 | + expect(output.Gold.summary.errors).toBe(2); |
| 240 | + expect(output.Gold.summary.warnings).toBe(3); |
| 241 | + }); |
| 242 | +}); |
0 commit comments