-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontracts_test.go
More file actions
116 lines (104 loc) · 3.31 KB
/
Copy pathcontracts_test.go
File metadata and controls
116 lines (104 loc) · 3.31 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
package sight
import (
"context"
"testing"
)
func TestToContractResult(t *testing.T) {
t.Parallel()
result := &Result{
Findings: []Finding{
{
Concern: "security",
Severity: SeverityHigh,
File: "main.go",
Line: 12,
Message: "issue",
Fix: "fix",
Confidence: 0.9,
},
},
Comments: []InlineComment{{Path: "main.go", StartLine: 12, Body: "comment"}},
Stats: Stats{
FilesReviewed: 1,
FindingsTotal: 1,
BySeverity: map[Severity]int{SeverityHigh: 1},
ByConcern: map[string]int{"security": 1},
TokensUsed: 42,
},
Report: "report",
FailOn: SeverityMedium,
ConfidenceBreakdown: &ConfidenceBreakdown{
High: []Finding{{Concern: "security", Severity: SeverityHigh, File: "main.go", Line: 12, Message: "issue", Confidence: 0.9}},
},
}
got := ToContractResult(result)
if got == nil {
t.Fatal("expected non-nil contract result")
}
if got.Report != "report" {
t.Fatalf("Report = %q, want report", got.Report)
}
if len(got.Findings) != 1 || got.Findings[0].Severity != SeverityHigh {
t.Fatalf("unexpected findings conversion: %+v", got.Findings)
}
if got.Stats.TokensUsed != 42 {
t.Fatalf("TokensUsed = %d, want 42", got.Stats.TokensUsed)
}
if got.ConfidenceBreakdown == nil || len(got.ConfidenceBreakdown.High) != 1 {
t.Fatal("expected confidence breakdown to convert")
}
}
func TestToContractResult_FailOnThresholdTakesEffect(t *testing.T) {
t.Parallel()
// A below-critical threshold configured on the sight Result must
// survive conversion: the contract's Failed() honors it only when
// FailOnSet is true, which ToContractResult must arrange via SetFailOn.
result := &Result{
FailOn: SeverityHigh,
Findings: []Finding{
{Severity: SeverityInfo, Message: "note", Confidence: 0.5},
},
}
contract := ToContractResult(result)
if !contract.FailOnSet {
t.Fatal("FailOnSet = false, want true after conversion")
}
if contract.FailOn != SeverityHigh {
t.Fatalf("FailOn = %v, want high", contract.FailOn)
}
if contract.Failed() {
t.Error("info finding must not fail a review with a high threshold")
}
result.Findings = append(result.Findings, Finding{
Severity: SeverityHigh, Message: "real problem", Confidence: 0.5,
})
contract = ToContractResult(result)
if !contract.Failed() {
t.Error("high finding must fail a review with a high threshold")
}
}
func TestToContractResult_FailOnConfiguredViaOptions(t *testing.T) {
t.Parallel()
reviewWith := func(response string) *Result {
t.Helper()
r := NewReviewer(
WithProvider(&fixMockProvider{response: response}),
WithFailOn(SeverityHigh),
WithConcerns("security"),
WithParallel(false),
)
result, err := r.Review(context.Background(), sampleDiff)
if err != nil {
t.Fatalf("Review failed: %v", err)
}
return result
}
infoOnly := reviewWith(`[{"file": "handler.go", "line": 13, "severity": "info", "message": "style nit", "fix": "n/a"}]`)
if got := ToContractResult(infoOnly).Failed(); got {
t.Error("Failed() = true for info-only findings with failOn=high, want false")
}
highFinding := reviewWith(`[{"file": "handler.go", "line": 13, "severity": "high", "message": "SQL injection", "fix": "use params"}]`)
if got := ToContractResult(highFinding).Failed(); !got {
t.Error("Failed() = false for a high finding with failOn=high, want true")
}
}