|
| 1 | +package issues |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/deepsourcelabs/cli/deepsource/graphqlclient" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPRIssuesList_Do(t *testing.T) { |
| 13 | + respJSON := `{ |
| 14 | + "repository": { |
| 15 | + "pullRequest": { |
| 16 | + "issues": { |
| 17 | + "edges": [ |
| 18 | + { |
| 19 | + "node": { |
| 20 | + "source": "static", |
| 21 | + "path": "cmd/deepsource/main.go", |
| 22 | + "beginLine": 42, |
| 23 | + "endLine": 42, |
| 24 | + "title": "Unchecked error return value", |
| 25 | + "shortcode": "GO-W1007", |
| 26 | + "category": "BUG_RISK", |
| 27 | + "severity": "MAJOR", |
| 28 | + "explanation": "Return value not checked", |
| 29 | + "issue": { |
| 30 | + "analyzer": { |
| 31 | + "name": "Go", |
| 32 | + "shortcode": "go" |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + }, |
| 37 | + { |
| 38 | + "node": { |
| 39 | + "source": "ai", |
| 40 | + "path": "internal/vcs/remotes.go", |
| 41 | + "beginLine": 87, |
| 42 | + "endLine": 91, |
| 43 | + "title": "HTTP request with user-controlled URL", |
| 44 | + "shortcode": "GO-S1010", |
| 45 | + "category": "SECURITY", |
| 46 | + "severity": "MAJOR", |
| 47 | + "explanation": "SSRF risk", |
| 48 | + "issue": null |
| 49 | + } |
| 50 | + } |
| 51 | + ], |
| 52 | + "pageInfo": { |
| 53 | + "hasNextPage": false, |
| 54 | + "endCursor": null |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +}` |
| 60 | + |
| 61 | + mock := graphqlclient.NewMockClient() |
| 62 | + mock.QueryFunc = func(_ context.Context, _ string, _ map[string]any, result any) error { |
| 63 | + return json.Unmarshal([]byte(respJSON), result) |
| 64 | + } |
| 65 | + |
| 66 | + req := NewPRIssuesListRequest(mock, PRIssuesListParams{ |
| 67 | + Owner: "testowner", |
| 68 | + RepoName: "testrepo", |
| 69 | + Provider: "GITHUB", |
| 70 | + PRNumber: 42, |
| 71 | + }) |
| 72 | + |
| 73 | + issues, err := req.Do(context.Background()) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("unexpected error: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + if len(issues) != 2 { |
| 79 | + t.Fatalf("expected 2 issues, got %d", len(issues)) |
| 80 | + } |
| 81 | + |
| 82 | + // First issue: has analyzer |
| 83 | + if issues[0].IssueCode != "GO-W1007" { |
| 84 | + t.Errorf("expected issue code GO-W1007, got %s", issues[0].IssueCode) |
| 85 | + } |
| 86 | + if issues[0].IssueText != "Unchecked error return value" { |
| 87 | + t.Errorf("expected title 'Unchecked error return value', got %s", issues[0].IssueText) |
| 88 | + } |
| 89 | + if issues[0].IssueCategory != "BUG_RISK" { |
| 90 | + t.Errorf("expected category BUG_RISK, got %s", issues[0].IssueCategory) |
| 91 | + } |
| 92 | + if issues[0].Analyzer.Name != "Go" { |
| 93 | + t.Errorf("expected analyzer name 'Go', got %s", issues[0].Analyzer.Name) |
| 94 | + } |
| 95 | + if issues[0].Location.Path != "cmd/deepsource/main.go" { |
| 96 | + t.Errorf("expected path 'cmd/deepsource/main.go', got %s", issues[0].Location.Path) |
| 97 | + } |
| 98 | + |
| 99 | + // Second issue: nil analyzer (AI-detected) |
| 100 | + if issues[1].IssueCode != "GO-S1010" { |
| 101 | + t.Errorf("expected issue code GO-S1010, got %s", issues[1].IssueCode) |
| 102 | + } |
| 103 | + if issues[1].Analyzer.Name != "" { |
| 104 | + t.Errorf("expected empty analyzer name for nil issue, got %s", issues[1].Analyzer.Name) |
| 105 | + } |
| 106 | + if issues[1].IssueSource != "ai" { |
| 107 | + t.Errorf("expected source 'ai', got %s", issues[1].IssueSource) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestPRIssuesList_QueryError(t *testing.T) { |
| 112 | + mock := graphqlclient.NewMockClient() |
| 113 | + mock.QueryFunc = func(_ context.Context, _ string, _ map[string]any, _ any) error { |
| 114 | + return fmt.Errorf("network error") |
| 115 | + } |
| 116 | + |
| 117 | + req := NewPRIssuesListRequest(mock, PRIssuesListParams{ |
| 118 | + Owner: "testowner", |
| 119 | + RepoName: "testrepo", |
| 120 | + Provider: "GITHUB", |
| 121 | + PRNumber: 42, |
| 122 | + }) |
| 123 | + |
| 124 | + _, err := req.Do(context.Background()) |
| 125 | + if err == nil { |
| 126 | + t.Fatal("expected error from query") |
| 127 | + } |
| 128 | +} |
0 commit comments