package epics import ( "context" "fmt" "reflect" "testing" "edge-infra.dev/pkg/lib/logging" "github.com/google/go-github/v47/github" "edge-infra.dev/pkg/f8n/devinfra/jack/constants" "edge-infra.dev/pkg/f8n/devinfra/jack/plugin" ) var c = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}} var hp = &plugin.HandlerParams{ Ctx: context.Background(), Client: c, Org: "some-org", Repo: "some-repo", Log: *logging.NewLogger(), } var l *github.Label const pcBody = "### parents\n\n*No Issues added yet*\n\n### children\n\n*No Issues added yet*\n" func TestLabeled(t *testing.T) { c.GithubIssues = &plugin.MockIssueService{ Labels: []string{}, Body: "Issue Body", } // Test if the new label is a not parent label l = &github.Label{Name: github.String(string(constants.Task))} ie := &github.IssuesEvent{ Action: github.String("labeled"), Label: l, } handleIssue(*hp, *ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}, Body: "Issue Body"}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestLabeled -> Test if the new label is a not parent label.") } // Test if the event sender is a bot ie = &github.IssuesEvent{ Action: github.String("labeled"), Sender: &github.User{Type: github.String("Bot")}, } handleIssue(*hp, *ie) if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestLabeled -> Test if the event sender is a bot.") } // Test parent label addition (normal behavior) c.GithubIssues = &plugin.MockIssueService{ Body: "Issue Body", } l = &github.Label{Name: github.String(string(constants.Epic))} ie = &github.IssuesEvent{ Action: github.String("labeled"), Label: l, Issue: &github.Issue{ Labels: []*github.Label{}, }, } handleIssue(*hp, *ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Body: fmt.Sprintf("\n%s%s%s", constants.Preamble, pcBody, constants.Postamble)}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestLabeled -> Test parent label addition.") } } func TestSwap(t *testing.T) { // Test swapParentLabels c.GithubIssues = &plugin.MockIssueService{ Body: "Issue Body", } l = &github.Label{Name: github.String(string(constants.Feature))} ie := &github.IssuesEvent{ Action: github.String("labeled"), Label: l, Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Epic))}, {Name: github.String(string(constants.Feature))}}, }, } handleIssue(*hp, *ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Feature)}, Body: fmt.Sprintf("\n%s%s%s", constants.Preamble, pcBody, constants.Postamble)}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: Test swapParentLabels.") } } func TestRemoved(t *testing.T) { c.GithubIssues = &plugin.MockIssueService{Labels: []string{}} // Test if the label being removed is a not parent label. l = &github.Label{Name: github.String(string(constants.Task))} ie := &github.IssuesEvent{ Action: github.String("unlabeled"), Label: l, } handleIssue(*hp, *ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestRemoved -> Test if the label being removed is a not parent label.") } // Test if an active parent label exists. l = &github.Label{Name: github.String(string(constants.Epic))} ie = &github.IssuesEvent{ Action: github.String("unlabeled"), Label: l, Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, }, } handleIssue(*hp, *ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestRemoved -> Test if an active parent label exists.") } } func TestEpicLabelRemoval(t *testing.T) { // Test handleEpicLabelRemoval c.GithubIssues = &plugin.MockIssueService{ Body: "pcBody", Labels: []string{string(constants.Epic)}, } l = &github.Label{Name: github.String(string(constants.Epic))} ie := &github.IssuesEvent{ Action: github.String("unlabeled"), Label: l, Issue: &github.Issue{ Labels: []*github.Label{}, }, } handleIssue(*hp, *ie) testBody := "### parents\n\n*No Issues added yet*\n\n### children\n\n\n" ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("%s%s%s", constants.Preamble, testBody, constants.Postamble), Comments: 1, Labels: []string{}, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: Test handleEpicLabelRemoval") } } func TestEdited(t *testing.T) { c.GithubIssues = &plugin.MockIssueService{} // Test if the event sender is a bot ie := &github.IssuesEvent{ Action: github.String("edited"), Sender: &github.User{Type: github.String("Bot")}, } handleIssue(*hp, *ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestEdited -> Test if the event sender is a bot") } } func TestParseJackBlock(t *testing.T) { c.GithubIssues = &plugin.MockIssueService{} testEndBody := "\n\n___\n\nUser Comments" // Test if there are no changes ie := &github.IssuesEvent{ Action: github.String("edited"), Changes: &github.EditChange{Body: &github.EditBody{From: github.String(fmt.Sprintf("User Comments%s%s%s", constants.Preamble, pcBody, testEndBody))}}, Issue: &github.Issue{ Body: github.String(fmt.Sprintf("User Comments%s%s%s", constants.Preamble, pcBody, testEndBody)), }, } handleIssue(*hp, *ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestParseJackBlock -> Test if there are no changes.") } // Test if issue body is left empty ie = &github.IssuesEvent{ Action: github.String("edited"), Changes: &github.EditChange{}, Issue: &github.Issue{}, Repo: &github.Repository{ Name: github.String("name"), }, } handleIssue(*hp, *ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: constants.Preamble + constants.Postamble, Comments: 1}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestParseJackBlock -> Test if issue body is left empty.") } // Test if ENDJACKBOT is deleted c.GithubIssues = &plugin.MockIssueService{} ie = &github.IssuesEvent{ Action: github.String("edited"), Changes: &github.EditChange{Body: &github.EditBody{From: github.String(fmt.Sprintf("User Comments%s%s%s", constants.Preamble, pcBody, testEndBody))}}, Issue: &github.Issue{ Body: github.String(fmt.Sprintf("User Comments%s%s\n\n___\nUser Comments", constants.Preamble, pcBody)), }, Repo: &github.Repository{ Name: github.String("name"), }, } handleIssue(*hp, *ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("User Comments%s%s\n\n___\n\n", constants.Preamble, pcBody), Comments: 1, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestParseJackBlock -> Test if ENDJACKBOT is deleted") } // Test if Jack's body has been damaged c.GithubIssues = &plugin.MockIssueService{} ie = &github.IssuesEvent{ Action: github.String("edited"), Changes: &github.EditChange{Body: &github.EditBody{From: github.String("Jack has been damaged")}}, Issue: &github.Issue{ Body: github.String("Invalid"), }, Repo: &github.Repository{ Name: github.String("name"), }, } handleIssue(*hp, *ie) }