| iomodo | d89df96 | 2025-07-31 12:53:05 +0400 | [diff] [blame^] | 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestProcessMergeWebhook(t *testing.T) { |
| 10 | // Create a sample GitHub webhook payload for a merged PR |
| 11 | webhookPayload := GitHubWebhook{ |
| 12 | Action: "closed", |
| 13 | Number: 123, |
| 14 | PullRequest: &GitHubPullRequest{ |
| 15 | ID: 456, |
| 16 | Number: 123, |
| 17 | State: "closed", |
| 18 | Title: "Add solution for task-123", |
| 19 | Merged: true, |
| 20 | MergedAt: func() *time.Time { |
| 21 | t := time.Now() |
| 22 | return &t |
| 23 | }(), |
| 24 | MergedBy: &User{ |
| 25 | Login: "testuser", |
| 26 | ID: 789, |
| 27 | }, |
| 28 | Head: &PullRequestBranch{ |
| 29 | Ref: "solution/task-123", |
| 30 | SHA: "abc123def456", |
| 31 | }, |
| 32 | Base: &PullRequestBranch{ |
| 33 | Ref: "main", |
| 34 | SHA: "def456ghi789", |
| 35 | }, |
| 36 | }, |
| 37 | Repository: &Repository{ |
| 38 | Name: "test-repo", |
| 39 | FullName: "testuser/test-repo", |
| 40 | }, |
| 41 | Sender: &User{ |
| 42 | Login: "testuser", |
| 43 | ID: 789, |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | // Convert to JSON |
| 48 | payload, err := json.Marshal(webhookPayload) |
| 49 | if err != nil { |
| 50 | t.Fatalf("Failed to marshal webhook payload: %v", err) |
| 51 | } |
| 52 | |
| 53 | // Test signature validation (this will fail without proper secret) |
| 54 | _, err = ProcessMergeWebhook(payload, "sha256=invalid", "test-secret") |
| 55 | if err == nil { |
| 56 | t.Error("Expected signature validation to fail with invalid signature") |
| 57 | } |
| 58 | |
| 59 | // Test with valid signature (you would need to generate this properly in a real test) |
| 60 | // For now, we'll test the parsing and validation logic separately |
| 61 | webhook, err := ParseWebhook(payload) |
| 62 | if err != nil { |
| 63 | t.Fatalf("Failed to parse webhook: %v", err) |
| 64 | } |
| 65 | |
| 66 | if !IsValidMergeEvent(webhook) { |
| 67 | t.Error("Expected webhook to be a valid merge event") |
| 68 | } |
| 69 | |
| 70 | taskID, err := ExtractTaskID(webhook.PullRequest.Head.Ref) |
| 71 | if err != nil { |
| 72 | t.Fatalf("Failed to extract task ID: %v", err) |
| 73 | } |
| 74 | |
| 75 | if taskID != "task-123" { |
| 76 | t.Errorf("Expected task ID 'task-123', got '%s'", taskID) |
| 77 | } |
| 78 | |
| 79 | // Test GetWebhookInfo function |
| 80 | info := GetWebhookInfo(webhook) |
| 81 | if info["action"] != "closed" { |
| 82 | t.Errorf("Expected action 'closed', got '%v'", info["action"]) |
| 83 | } |
| 84 | if info["pr_title"] != "Add solution for task-123" { |
| 85 | t.Errorf("Expected title 'Add solution for task-123', got '%v'", info["pr_title"]) |
| 86 | } |
| 87 | if info["head_ref"] != "solution/task-123" { |
| 88 | t.Errorf("Expected head_ref 'solution/task-123', got '%v'", info["head_ref"]) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestExtractTaskID(t *testing.T) { |
| 93 | tests := []struct { |
| 94 | branchName string |
| 95 | expected string |
| 96 | shouldErr bool |
| 97 | }{ |
| 98 | {"solution/task-123", "task-123", false}, |
| 99 | {"subtasks/task-456", "task-456", false}, |
| 100 | {"feature/new-feature", "", true}, |
| 101 | {"main", "", true}, |
| 102 | {"solution/", "", true}, |
| 103 | {"subtasks/", "", true}, |
| 104 | } |
| 105 | |
| 106 | for _, test := range tests { |
| 107 | taskID, err := ExtractTaskID(test.branchName) |
| 108 | if test.shouldErr { |
| 109 | if err == nil { |
| 110 | t.Errorf("Expected error for branch '%s', but got none", test.branchName) |
| 111 | } |
| 112 | } else { |
| 113 | if err != nil { |
| 114 | t.Errorf("Unexpected error for branch '%s': %v", test.branchName, err) |
| 115 | } |
| 116 | if taskID != test.expected { |
| 117 | t.Errorf("Expected task ID '%s' for branch '%s', got '%s'", test.expected, test.branchName, taskID) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestIsValidMergeEvent(t *testing.T) { |
| 124 | tests := []struct { |
| 125 | name string |
| 126 | webhook *GitHubWebhook |
| 127 | expected bool |
| 128 | }{ |
| 129 | { |
| 130 | name: "valid merge event", |
| 131 | webhook: &GitHubWebhook{ |
| 132 | Action: "closed", |
| 133 | PullRequest: &GitHubPullRequest{ |
| 134 | Merged: true, |
| 135 | Head: &PullRequestBranch{Ref: "solution/task-123"}, |
| 136 | }, |
| 137 | }, |
| 138 | expected: true, |
| 139 | }, |
| 140 | { |
| 141 | name: "not closed", |
| 142 | webhook: &GitHubWebhook{ |
| 143 | Action: "opened", |
| 144 | PullRequest: &GitHubPullRequest{ |
| 145 | Merged: true, |
| 146 | Head: &PullRequestBranch{Ref: "solution/task-123"}, |
| 147 | }, |
| 148 | }, |
| 149 | expected: false, |
| 150 | }, |
| 151 | { |
| 152 | name: "not merged", |
| 153 | webhook: &GitHubWebhook{ |
| 154 | Action: "closed", |
| 155 | PullRequest: &GitHubPullRequest{ |
| 156 | Merged: false, |
| 157 | Head: &PullRequestBranch{Ref: "solution/task-123"}, |
| 158 | }, |
| 159 | }, |
| 160 | expected: false, |
| 161 | }, |
| 162 | { |
| 163 | name: "no pull request", |
| 164 | webhook: &GitHubWebhook{ |
| 165 | Action: "closed", |
| 166 | }, |
| 167 | expected: false, |
| 168 | }, |
| 169 | { |
| 170 | name: "no head branch", |
| 171 | webhook: &GitHubWebhook{ |
| 172 | Action: "closed", |
| 173 | PullRequest: &GitHubPullRequest{ |
| 174 | Merged: true, |
| 175 | }, |
| 176 | }, |
| 177 | expected: false, |
| 178 | }, |
| 179 | } |
| 180 | |
| 181 | for _, test := range tests { |
| 182 | t.Run(test.name, func(t *testing.T) { |
| 183 | result := IsValidMergeEvent(test.webhook) |
| 184 | if result != test.expected { |
| 185 | t.Errorf("Expected %v, got %v", test.expected, result) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | } |