|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { calculateScore } from "../../src/reporter/score.js"; |
| 3 | +import type { Finding, ScanTarget } from "../../src/types.js"; |
| 4 | +import type { ScanResult } from "../../src/scanner/index.js"; |
| 5 | + |
| 6 | +function makeScanResult(findings: Finding[]): ScanResult { |
| 7 | + const target: ScanTarget = { |
| 8 | + path: "/test", |
| 9 | + files: [{ path: "test.json", type: "settings-json", content: "{}" }], |
| 10 | + }; |
| 11 | + return { target, findings }; |
| 12 | +} |
| 13 | + |
| 14 | +function makeFinding(overrides: Partial<Finding> = {}): Finding { |
| 15 | + return { |
| 16 | + id: "test-finding", |
| 17 | + severity: "medium", |
| 18 | + category: "permissions", |
| 19 | + title: "Test finding", |
| 20 | + description: "Test description", |
| 21 | + file: "test.json", |
| 22 | + ...overrides, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +describe("calculateScore", () => { |
| 27 | + it("returns Grade A for no findings", () => { |
| 28 | + const result = makeScanResult([]); |
| 29 | + const report = calculateScore(result); |
| 30 | + expect(report.score.grade).toBe("A"); |
| 31 | + expect(report.score.numericScore).toBe(100); |
| 32 | + }); |
| 33 | + |
| 34 | + it("deducts 25 points per critical finding", () => { |
| 35 | + const result = makeScanResult([ |
| 36 | + makeFinding({ id: "c1", severity: "critical" }), |
| 37 | + ]); |
| 38 | + const report = calculateScore(result); |
| 39 | + expect(report.score.numericScore).toBe(75); |
| 40 | + expect(report.score.grade).toBe("B"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("deducts 15 points per high finding", () => { |
| 44 | + const result = makeScanResult([ |
| 45 | + makeFinding({ id: "h1", severity: "high" }), |
| 46 | + ]); |
| 47 | + const report = calculateScore(result); |
| 48 | + expect(report.score.numericScore).toBe(85); |
| 49 | + expect(report.score.grade).toBe("B"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("deducts 5 points per medium finding", () => { |
| 53 | + const result = makeScanResult([ |
| 54 | + makeFinding({ id: "m1", severity: "medium" }), |
| 55 | + makeFinding({ id: "m2", severity: "medium" }), |
| 56 | + ]); |
| 57 | + const report = calculateScore(result); |
| 58 | + expect(report.score.numericScore).toBe(90); |
| 59 | + }); |
| 60 | + |
| 61 | + it("floors score at 0", () => { |
| 62 | + const result = makeScanResult([ |
| 63 | + makeFinding({ id: "c1", severity: "critical" }), |
| 64 | + makeFinding({ id: "c2", severity: "critical" }), |
| 65 | + makeFinding({ id: "c3", severity: "critical" }), |
| 66 | + makeFinding({ id: "c4", severity: "critical" }), |
| 67 | + makeFinding({ id: "c5", severity: "critical" }), |
| 68 | + ]); |
| 69 | + const report = calculateScore(result); |
| 70 | + expect(report.score.numericScore).toBe(0); |
| 71 | + expect(report.score.grade).toBe("F"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("does not deduct for info findings", () => { |
| 75 | + const result = makeScanResult([ |
| 76 | + makeFinding({ id: "i1", severity: "info" }), |
| 77 | + makeFinding({ id: "i2", severity: "info" }), |
| 78 | + ]); |
| 79 | + const report = calculateScore(result); |
| 80 | + expect(report.score.numericScore).toBe(100); |
| 81 | + }); |
| 82 | + |
| 83 | + it("correctly counts findings by severity in summary", () => { |
| 84 | + const result = makeScanResult([ |
| 85 | + makeFinding({ id: "c1", severity: "critical" }), |
| 86 | + makeFinding({ id: "h1", severity: "high" }), |
| 87 | + makeFinding({ id: "h2", severity: "high" }), |
| 88 | + makeFinding({ id: "m1", severity: "medium" }), |
| 89 | + makeFinding({ id: "l1", severity: "low" }), |
| 90 | + makeFinding({ id: "i1", severity: "info" }), |
| 91 | + ]); |
| 92 | + const report = calculateScore(result); |
| 93 | + expect(report.summary.critical).toBe(1); |
| 94 | + expect(report.summary.high).toBe(2); |
| 95 | + expect(report.summary.medium).toBe(1); |
| 96 | + expect(report.summary.low).toBe(1); |
| 97 | + expect(report.summary.info).toBe(1); |
| 98 | + expect(report.summary.totalFindings).toBe(6); |
| 99 | + }); |
| 100 | + |
| 101 | + it("counts auto-fixable findings", () => { |
| 102 | + const result = makeScanResult([ |
| 103 | + makeFinding({ id: "f1", fix: { description: "fix", before: "a", after: "b", auto: true } }), |
| 104 | + makeFinding({ id: "f2", fix: { description: "fix", before: "a", after: "b", auto: false } }), |
| 105 | + makeFinding({ id: "f3" }), |
| 106 | + ]); |
| 107 | + const report = calculateScore(result); |
| 108 | + expect(report.summary.autoFixable).toBe(1); |
| 109 | + }); |
| 110 | + |
| 111 | + it("maps categories to score breakdown correctly", () => { |
| 112 | + const result = makeScanResult([ |
| 113 | + makeFinding({ id: "s1", severity: "critical", category: "secrets" }), |
| 114 | + makeFinding({ id: "m1", severity: "high", category: "mcp" }), |
| 115 | + makeFinding({ id: "a1", severity: "medium", category: "agents" }), |
| 116 | + ]); |
| 117 | + const report = calculateScore(result); |
| 118 | + expect(report.score.breakdown.secrets).toBe(75); // 100 - 25 |
| 119 | + expect(report.score.breakdown.mcp).toBe(85); // 100 - 15 |
| 120 | + expect(report.score.breakdown.agents).toBe(95); // 100 - 5 |
| 121 | + expect(report.score.breakdown.permissions).toBe(100); // untouched |
| 122 | + expect(report.score.breakdown.hooks).toBe(100); // untouched |
| 123 | + }); |
| 124 | + |
| 125 | + it("grades correctly at boundaries", () => { |
| 126 | + // A: >= 90 |
| 127 | + expect(calculateScore(makeScanResult([ |
| 128 | + makeFinding({ id: "m1", severity: "medium" }), |
| 129 | + makeFinding({ id: "m2", severity: "medium" }), |
| 130 | + ])).score.grade).toBe("A"); |
| 131 | + |
| 132 | + // B: 75-89 |
| 133 | + expect(calculateScore(makeScanResult([ |
| 134 | + makeFinding({ id: "c1", severity: "critical" }), |
| 135 | + ])).score.grade).toBe("B"); |
| 136 | + |
| 137 | + // C: 60-74 |
| 138 | + expect(calculateScore(makeScanResult([ |
| 139 | + makeFinding({ id: "c1", severity: "critical" }), |
| 140 | + makeFinding({ id: "h1", severity: "high" }), |
| 141 | + ])).score.grade).toBe("C"); |
| 142 | + |
| 143 | + // D: 40-59 |
| 144 | + expect(calculateScore(makeScanResult([ |
| 145 | + makeFinding({ id: "c1", severity: "critical" }), |
| 146 | + makeFinding({ id: "c2", severity: "critical" }), |
| 147 | + ])).score.grade).toBe("D"); |
| 148 | + |
| 149 | + // F: < 40 |
| 150 | + expect(calculateScore(makeScanResult([ |
| 151 | + makeFinding({ id: "c1", severity: "critical" }), |
| 152 | + makeFinding({ id: "c2", severity: "critical" }), |
| 153 | + makeFinding({ id: "c3", severity: "critical" }), |
| 154 | + ])).score.grade).toBe("F"); |
| 155 | + }); |
| 156 | +}); |
0 commit comments