1 package epics
2
3 import (
4 "context"
5 "fmt"
6 "reflect"
7 "testing"
8
9 "edge-infra.dev/pkg/lib/logging"
10
11 "github.com/google/go-github/v47/github"
12
13 "edge-infra.dev/pkg/f8n/devinfra/jack/constants"
14 "edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
15 )
16
17 var c = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
18 var hp = &plugin.HandlerParams{
19 Ctx: context.Background(),
20 Client: c,
21 Org: "some-org",
22 Repo: "some-repo",
23 Log: *logging.NewLogger(),
24 }
25
26 var l *github.Label
27
28 const pcBody = "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n*No Issues added yet*\n<!-- ENDCHILDLIST -->"
29
30 func TestLabeled(t *testing.T) {
31 c.GithubIssues = &plugin.MockIssueService{
32 Labels: []string{},
33 Body: "Issue Body",
34 }
35
36
37 l = &github.Label{Name: github.String(string(constants.Task))}
38 ie := &github.IssuesEvent{
39 Action: github.String("labeled"),
40 Label: l,
41 }
42
43 handleIssue(*hp, *ie)
44
45 ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}, Body: "Issue Body"}}
46 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
47 t.Error("Failed: TestLabeled -> Test if the new label is a not parent label.")
48 }
49
50
51 ie = &github.IssuesEvent{
52 Action: github.String("labeled"),
53 Sender: &github.User{Type: github.String("Bot")},
54 }
55
56 handleIssue(*hp, *ie)
57
58 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
59 t.Error("Failed: TestLabeled -> Test if the event sender is a bot.")
60 }
61
62
63 c.GithubIssues = &plugin.MockIssueService{
64 Body: "Issue Body",
65 }
66 l = &github.Label{Name: github.String(string(constants.Epic))}
67
68 ie = &github.IssuesEvent{
69 Action: github.String("labeled"),
70 Label: l,
71 Issue: &github.Issue{
72 Labels: []*github.Label{},
73 },
74 }
75
76 handleIssue(*hp, *ie)
77
78 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Body: fmt.Sprintf("\n%s%s%s", constants.Preamble, pcBody, constants.Postamble)}}
79 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
80 t.Error("Failed: TestLabeled -> Test parent label addition.")
81 }
82 }
83
84 func TestSwap(t *testing.T) {
85
86 c.GithubIssues = &plugin.MockIssueService{
87 Body: "Issue Body",
88 }
89 l = &github.Label{Name: github.String(string(constants.Feature))}
90
91 ie := &github.IssuesEvent{
92 Action: github.String("labeled"),
93 Label: l,
94 Issue: &github.Issue{
95 Labels: []*github.Label{{Name: github.String(string(constants.Epic))}, {Name: github.String(string(constants.Feature))}},
96 },
97 }
98
99 handleIssue(*hp, *ie)
100
101 ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Feature)}, Body: fmt.Sprintf("\n%s%s%s", constants.Preamble, pcBody, constants.Postamble)}}
102 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
103 t.Error("Failed: Test swapParentLabels.")
104 }
105 }
106
107 func TestRemoved(t *testing.T) {
108 c.GithubIssues = &plugin.MockIssueService{Labels: []string{}}
109
110
111 l = &github.Label{Name: github.String(string(constants.Task))}
112 ie := &github.IssuesEvent{
113 Action: github.String("unlabeled"),
114 Label: l,
115 }
116
117 handleIssue(*hp, *ie)
118
119 ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
120 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
121 t.Error("Failed: TestRemoved -> Test if the label being removed is a not parent label.")
122 }
123
124
125 l = &github.Label{Name: github.String(string(constants.Epic))}
126 ie = &github.IssuesEvent{
127 Action: github.String("unlabeled"),
128 Label: l,
129 Issue: &github.Issue{
130 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
131 },
132 }
133
134 handleIssue(*hp, *ie)
135
136 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
137
138 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
139 t.Error("Failed: TestRemoved -> Test if an active parent label exists.")
140 }
141 }
142
143 func TestEpicLabelRemoval(t *testing.T) {
144
145 c.GithubIssues = &plugin.MockIssueService{
146 Body: "pcBody",
147 Labels: []string{string(constants.Epic)},
148 }
149 l = &github.Label{Name: github.String(string(constants.Epic))}
150
151 ie := &github.IssuesEvent{
152 Action: github.String("unlabeled"),
153 Label: l,
154 Issue: &github.Issue{
155 Labels: []*github.Label{},
156 },
157 }
158
159 handleIssue(*hp, *ie)
160
161 testBody := "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n\n<!-- ENDCHILDLIST -->"
162 ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
163 Body: fmt.Sprintf("%s%s%s", constants.Preamble, testBody, constants.Postamble),
164 Comments: 1,
165 Labels: []string{},
166 }}
167 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
168 t.Error("Failed: Test handleEpicLabelRemoval")
169 }
170 }
171
172 func TestEdited(t *testing.T) {
173 c.GithubIssues = &plugin.MockIssueService{}
174
175
176 ie := &github.IssuesEvent{
177 Action: github.String("edited"),
178 Sender: &github.User{Type: github.String("Bot")},
179 }
180
181 handleIssue(*hp, *ie)
182
183 ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
184 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
185 t.Error("Failed: TestEdited -> Test if the event sender is a bot")
186 }
187 }
188
189 func TestParseJackBlock(t *testing.T) {
190 c.GithubIssues = &plugin.MockIssueService{}
191 testEndBody := "\n<!-- ENDLIST -->\n___\n<!-- ENDJACKBOT -->\nUser Comments"
192
193
194 ie := &github.IssuesEvent{
195 Action: github.String("edited"),
196 Changes: &github.EditChange{Body: &github.EditBody{From: github.String(fmt.Sprintf("User Comments%s%s%s", constants.Preamble, pcBody, testEndBody))}},
197 Issue: &github.Issue{
198 Body: github.String(fmt.Sprintf("User Comments%s%s%s", constants.Preamble, pcBody, testEndBody)),
199 },
200 }
201
202 handleIssue(*hp, *ie)
203
204 ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
205 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
206 t.Error("Failed: TestParseJackBlock -> Test if there are no changes.")
207 }
208
209
210 ie = &github.IssuesEvent{
211 Action: github.String("edited"),
212 Changes: &github.EditChange{},
213 Issue: &github.Issue{},
214 Repo: &github.Repository{
215 Name: github.String("name"),
216 },
217 }
218
219 handleIssue(*hp, *ie)
220
221 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
222 Body: constants.Preamble + constants.Postamble,
223 Comments: 1}}
224 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
225 t.Error("Failed: TestParseJackBlock -> Test if issue body is left empty.")
226 }
227
228
229 c.GithubIssues = &plugin.MockIssueService{}
230
231 ie = &github.IssuesEvent{
232 Action: github.String("edited"),
233 Changes: &github.EditChange{Body: &github.EditBody{From: github.String(fmt.Sprintf("User Comments%s%s%s", constants.Preamble, pcBody, testEndBody))}},
234 Issue: &github.Issue{
235 Body: github.String(fmt.Sprintf("User Comments%s%s\n<!-- ENDLIST -->\n___\nUser Comments", constants.Preamble, pcBody)),
236 },
237 Repo: &github.Repository{
238 Name: github.String("name"),
239 },
240 }
241
242 handleIssue(*hp, *ie)
243
244 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
245 Body: fmt.Sprintf("User Comments%s%s\n<!-- ENDLIST -->\n___\n<!-- ENDJACKBOT -->\n", constants.Preamble, pcBody),
246 Comments: 1,
247 }}
248 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
249 t.Error("Failed: TestParseJackBlock -> Test if ENDJACKBOT is deleted")
250 }
251
252
253 c.GithubIssues = &plugin.MockIssueService{}
254 ie = &github.IssuesEvent{
255 Action: github.String("edited"),
256 Changes: &github.EditChange{Body: &github.EditBody{From: github.String("Jack has been damaged")}},
257 Issue: &github.Issue{
258 Body: github.String("Invalid"),
259 },
260 Repo: &github.Repository{
261 Name: github.String("name"),
262 },
263 }
264
265 handleIssue(*hp, *ie)
266 }
267
View as plain text